2014-03-31 44 views
0

我正在生成发票,我已经使用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; 
} 

回答

1

当您使用writeSelectedRows(),你有责任做数学题,例如来计算行的高度。您只能在之后计算行的高度,您已经定义了表格的宽度。就你而言,你可能不希望表的总高度,但你想知道行的高度。请参阅TableHeight示例以了解如何使用getRowHeight()方法。

当然:在阅读文档时,您会发现,向PdfContentByte添加表格并不需要使用writeSelectedRows()更简单。请看看ColumnTable的例子。在本例中,我们将PdfPTable添加到ColumnText对象。我们为该列定义了一个矩形,并且我们继续向新页面添加列,直到完成表格呈现。