我想使用 iText 5 和 Java 将 jTable(tblinvoice)(16 列)数据导出到 A4 大小的 pdf 文档。pdf 可能有多个页面,每个页面都应该有表头和页码。
我有一个 16 列的 jTable。我尝试使用 itext 5,并且能够在 pdf 文档(A4 大小)的一页内获得结果。如果 jTable 有多行(2,3 页),那么我会收到一条 nullPointerException 消息。
`Document dc = new Document(PageSize.A4.rotate()); PdfWriter.getInstance(dc, new FileOutputStream(path + allin)); dc.open();
PdfPTable table = new PdfPTable(17);
table.setHeaderRows(1);
table.setSplitLate(false);
table.setSplitRows(false);
table.setComplete(false);
for (int q = 0; q < 1; q++) { //header setter
table.addCell("No");
table.addCell("Invoice No");
table.addCell("Customer Name");
table.addCell("Sales Date");
table.addCell("Sales Time");
table.addCell("Total Item");
table.addCell("Total Price");
table.addCell("Discount");
table.addCell("Total Payable");
table.addCell("Paid Amount");
table.addCell("Balance");
table.addCell("Payment Type");
table.addCell("Hold Id");
table.addCell("Return Note");
table.addCell("Hold Note");
table.addCell("Invoice Status");
table.addCell("User ID / Name");
}
for (int i = 1; i < tblinvoice.getRowCount(); i++) { //cell
String id = tblinvoice.getValueAt(i, 0).toString();
String cn = tblinvoice.getValueAt(i, 1).toString();
String sd = tblinvoice.getValueAt(i, 2).toString();
String st = tblinvoice.getValueAt(i, 3).toString();
String ti = tblinvoice.getValueAt(i, 4).toString();
String tp = tblinvoice.getValueAt(i, 5).toString();
String dt = tblinvoice.getValueAt(i, 6).toString();
String tl = tblinvoice.getValueAt(i, 7).toString();
String pa = tblinvoice.getValueAt(i, 8).toString();
String bn = tblinvoice.getValueAt(i, 9).toString();
String pt = tblinvoice.getValueAt(i, 10).toString();
String hi = tblinvoice.getValueAt(i, 11).toString();
String rn = tblinvoice.getValueAt(i, 12).toString();
String hn = tblinvoice.getValueAt(i, 13).toString();
String is = tblinvoice.getValueAt(i, 14).toString();
String ui = tblinvoice.getValueAt(i, 15).toString();
table.addCell("" + i);
table.addCell(id);
table.addCell(cn);
table.addCell(sd);
table.addCell(st);
table.addCell(ti);
table.addCell(tp);
table.addCell(dt);
table.addCell(tl);
table.addCell(pa);
table.addCell(bn);
table.addCell(pt);
table.addCell(hi);
table.addCell(rn);
table.addCell(hn);
table.addCell(is);
table.addCell(ui);
}
dc.newPage();
table.setComplete(true);
table.setSplitLate(false);
dc.add(table);
dc.close();`
我希望有多行 pdf 文档(A4 大小)。但我得到了一个 nullPointerException 消息。和pdf保存但无法打开!