【发布时间】:2016-08-08 04:43:26
【问题描述】:
我想通过动态数据生成一个包含 3 列的 PDF。我已经用我的代码试过了,
public class Clazz {
public static final String RESULT = "result.pdf";
private String[] data = {"1","2","3","4","5","6","7","8"};
private void go() throws Exception {
Document doc = new Document();
PdfWriter.getInstance(doc, new FileOutputStream(RESULT));
doc.open();
PdfPTable mainTable = new PdfPTable(3);
PdfPCell cell;
for (int i = 0; i < data.length; i+=2) {
cell = new PdfPCell(new Phrase(data[i]));
PdfPTable table = new PdfPTable(1);
table.addCell(cell);
if (i+1 <= data.length -1) {
cell = new PdfPCell(new Phrase(data[i + 1]));
table.addCell(cell);
} else {
cell = new PdfPCell(new Phrase(""));
table.addCell(cell);
}
mainTable.addCell(table);
}
doc.add(mainTable);
doc.close();
}
}
我想在我的 PDF 中这样打印:
在第一页它会打印 1 2 3 然后在下一页它会打印 4 5 6 然后在下一页它会打印 7 8 和空白单元格。
【问题讨论】:
-
你能澄清一下你正在尝试做什么以及到目前为止你已经尝试过什么吗?
-
我想在我的 pdf 中这样打印:在第一页它将打印 1 2 3 然后在下一页它将打印 4 5 6 然后在下一页它将打印 7 8 和空白细胞。