我正在生成发票,我已经使用iText 2天了。PdfPTable生成多个页面的绝对值与itext
我的问题是:如果我不直接将它们添加到文档中,而是从PdfContentByte中写入,我怎样才能在多个页面上分割一个PdfPTable。
this是输出。我没有得到的东西挂起:
1:如何使标题行显示在新的页面上? (没有读取第一行)
2:我如何自动化循环(而不是硬代码)的许多记录,所以它拆分了多个页面上的表?
3:Here这就是我的发票应该看起来的样子,在表格末尾添加一个页脚表格,其中包含关于总成本和增值税成本的信息。我如何计算最后一页上表格的总高度并在其末尾添加我的页脚表格?
这是我用它来生成至今PDF的代码:
private void generateTable(Document doc, PdfContentByte cb) throws DocumentException {
TableHeaderFields headerFields = new TableHeaderFields();
PdfPTable invTable = new PdfPTable(7);
invTable.setWidths(new int[] {20, 200, 40, 40, 70, 70, 70});
PdfPCell invCell;
// invTable.getDefaultCell().setBackgroundColor(BaseColor.LIGHT_GRAY);
for (String colTitle : headerFields.getHeaderFields()) {
invCell = new PdfPCell(new Phrase(colTitle, new Font(bfBold, 8)));
invCell.setBackgroundColor(BaseColor.LIGHT_GRAY);
invCell.setHorizontalAlignment(Element.ALIGN_CENTER);
invTable.addCell(invCell);
}
invTable.setHeaderRows(2);
invTable.setTotalWidth(500);
// invTable.getDefaultCell().setBackgroundColor(null);
//Sample Content of the table;
List<InvoiceLine> contentList = InvoiceLine.generateListOfInvLine(70);
int nrCrt = 1;
float totalSum = 0;
float height = 0;
for (InvoiceLine invLine : contentList) {
// System.out.println(invLine);
height = invTable.getTotalHeight();
invCell = new PdfPCell(new Phrase("" + nrCrt++, new Font(bf, 8)));
invTable.addCell(invCell);
invCell = new PdfPCell(new Phrase(invLine.getItem_desc(), new Font(bf, 8)));
invTable.addCell(invCell);
invCell = new PdfPCell(new Phrase("" + invLine.getUm(), new Font(bf, 8)));
invCell.setHorizontalAlignment(Element.ALIGN_CENTER);
invTable.addCell(invCell);
invCell = new PdfPCell(new Phrase("" + invLine.getQty(), new Font(bf, 8)));
invCell.setHorizontalAlignment(Element.ALIGN_CENTER);
invTable.addCell(invCell);
invCell = new PdfPCell(new Phrase("" + invLine.getPrice(), new Font(bf, 8)));
invCell.setHorizontalAlignment(Element.ALIGN_CENTER);
invTable.addCell(invCell);
invCell = new PdfPCell(new Phrase("" + (float) (invLine.getQty() * invLine.getPrice()), new Font(bf, 8)));
invCell.setHorizontalAlignment(Element.ALIGN_CENTER);
invTable.addCell(invCell);
invCell = new PdfPCell(new Phrase("" + (float) (invLine.getQty() * invLine.getPrice() * 0.24), new Font(bf, 8)));
invCell.setHorizontalAlignment(Element.ALIGN_CENTER);
invTable.addCell(invCell);
totalSum += (invLine.getQty() * invLine.getPrice());
}
invTable.writeSelectedRows(0, 40, 50, 630, cb);
// if ((PageSize.A4.getHeight() - height) <= 40) {
// System.out.println("A4 : " + PageSize.A4.getHeight() + " vs " + height);
doc.newPage();
invTable.writeSelectedRows(41, -1, 50, 630, cb);
// } else
System.out.println("WE'RE OK:" + "A4 : " + PageSize.A4.getHeight() + " vs " + height);
PdfPTable footer = generateFooterForTable(totalSum, (float) 0.24);
footer.setTotalWidth(500);
footer.writeSelectedRows(0, -1, 50, 630 - height, cb);
}
private PdfPTable generateFooterForTable(float total, float vatRate) throws DocumentException {
PdfPTable footerTable = new PdfPTable(5);
footerTable.setWidths(new int[] {75, 185, 110, 70, 70,});
PdfPCell invCell = new PdfPCell(new Phrase("Semnatura si\n" + "stampila furnizorului", new Font(bf, 8)));
invCell.setRowspan(2);
footerTable.addCell(invCell);
invCell = new PdfPCell(new Phrase("Numele Delegatului\n" + "Act Delegat" + "Semnatura", new Font(bf, 8)));
invCell.setRowspan(2);
footerTable.addCell(invCell);
invCell = new PdfPCell(new Phrase("TOTAL", new Font(bf, 8)));
invCell.setHorizontalAlignment(Element.ALIGN_CENTER);
footerTable.addCell(invCell);
invCell = new PdfPCell(new Phrase("" + total, new Font(bf, 8)));
invCell.setHorizontalAlignment(Element.ALIGN_CENTER);
footerTable.addCell(invCell);
invCell = new PdfPCell(new Phrase("" + total * vatRate, new Font(bf, 8)));
invCell.setHorizontalAlignment(Element.ALIGN_CENTER);
footerTable.addCell(invCell);
invCell = new PdfPCell(new Phrase("TOTAL GENERAL", new Font(bf, 8)));
invCell.setHorizontalAlignment(Element.ALIGN_CENTER);
footerTable.addCell(invCell);
invCell = new PdfPCell(new Phrase("" + (total + (total * vatRate)), new Font(bfBold, 8)));
invCell.setHorizontalAlignment(Element.ALIGN_CENTER);
invCell.setColspan(2);
footerTable.addCell(invCell);
return footerTable;
}