使用itext以绝对值在多页上生成PdfPTable

时间:2014-03-31 00:28:13

标签: pdf itext

我已生成发票,而且我已经与iText合作了2天。

我的问题是:如果我没有直接在文档中添加它们,而是从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 个答案:

答案 0 :(得分:1)

当您使用writeSelectedRows()时,您有责任进行数学计算,例如计算行的高度。在定义表格宽度后,您只能计算行的高度。在您的情况下,您可能不希望表的总高度,但您想知道行的高度。例如,请参阅TableHeight示例,了解如何使用getRowHeight()方法。

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