首页 文章

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

提问于
浏览
0

我正在生成发票,我已经和iText合作了两天了 .

我的问题是:如果我不是直接在文档中添加它们而是从PdfContentByte写入它,我如何在多个页面上拆分PdfPTable .

this是输出 . 我没有得到的东西:

1:如何在新页面上显示 Headers 行? (没有读取第一行)

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 回答

  • 1

    当您使用 writeSelectedRows() 时,您有责任进行数学计算,例如:计算行的高度 . 在您想要表的总高度之后,您只能计算行的高度,但是您想知道行的高度 . 例如,参见TableHeight示例以了解如何使用 getRowHeight() 方法 .

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

相关问题