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

使用itext以绝对值在多个页面上生成PdfPTable,pdf,itext,Pdf,Itext,我正在生成一张发票,我已经使用iText两天了 我的问题是:如果我不是直接在文档中添加PdfPTable,而是从PdfContentByte写入,那么如何将PdfPTable拆分为多个页面 是输出。我不懂的是: 1:如何使标题行显示在新页面上?(不读取第一行) 2:我如何能够为许多记录自动执行循环(而不是硬代码),从而将表拆分到多个页面上 3:这是我的发票的外观,在表的末尾,我添加了一个页脚表,其中包含有关总成本和增值税成本的信息。如何计算最后一页表格的总高度,并在其末尾添加页脚表格 这是迄今为

我正在生成一张发票,我已经使用iText两天了

我的问题是:如果我不是直接在文档中添加PdfPTable,而是从PdfContentByte写入,那么如何将PdfPTable拆分为多个页面

是输出。我不懂的是:

1:如何使标题行显示在新页面上?(不读取第一行)

2:我如何能够为许多记录自动执行循环(而不是硬代码),从而将表拆分到多个页面上

3:这是我的发票的外观,在表的末尾,我添加了一个页脚表,其中包含有关总成本和增值税成本的信息。如何计算最后一页表格的总高度,并在其末尾添加页脚表格

这是迄今为止我用来生成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;
}
private void generateTable(Document doc,PdfContentByte cb)抛出DocumentException{
TableHeaderFields headerFields=新的TableHeaderFields();
PdfPTable invTable=新的PdfPTable(7);
可投资的设置宽度(新的int[]{20,200,40,40,70,70,70});
PdfPCell-invCell;
//invTable.getDefaultCell().setBackgroundColor(BaseColor.LIGHT_GRAY);
for(字符串colTitle:headerFields.getHeaderFields()){
invCell=新的PdfPCell(新短语(colTitle,新字体(bfBold,8));
invCell.setBackgroundColor(基色.浅灰色);
invCell.setHorizontalAlignment(元素对齐\中心);
invTable.addCell(invCell);
}
可投资的固定资产(2);
可投资设置总宽度(500);
//invTable.getDefaultCell().setBackgroundColor(空);
//表格的样本内容;
List contentList=InvoiceLine.generateListOfInvLine(70);
int-nrct=1;
浮动总和=0;
浮动高度=0;
for(发票行invLine:contentList){
//系统输出打印项次(invLine);
高度=invTable.getTotalHeight();
invCell=新的PdfPCell(新短语(“+nrCrt++,新字体(bf,8));
invTable.addCell(invCell);
invCell=新的PdfPCell(新短语(invLine.getItem_desc(),新字体(bf,8));
invTable.addCell(invCell);
invCell=newpdfpcell(新短语(“+invLine.getUm(),新字体(bf,8));
invCell.setHorizontalAlignment(元素对齐\中心);
invTable.addCell(invCell);
invCell=newpdfpcell(新短语(“+invLine.getQty(),新字体(bf,8));
invCell.setHorizontalAlignment(元素对齐\中心);
invTable.addCell(invCell);
invCell=newpdfpcell(新短语(“+invLine.getPrice(),新字体(bf,8));
invCell.setHorizontalAlignment(元素对齐\中心);
invTable.addCell(invCell);
invCell=newpdfpcell(新短语(“+(float)(invLine.getQty()*invLine.getPrice()),新字体(bf,8));
invCell.setHorizontalAlignment(元素对齐\中心);
invTable.addCell(invCell);
invCell=newpdfpcell(新短语(“+(float)(invLine.getQty()*invLine.getPrice()*0.24),新字体(bf,8));
invCell.setHorizontalAlignment(元素对齐\中心);
invTable.addCell(invCell);
totalSum+=(invLine.getQty()*invLine.getPrice());
}
invTable.writeSelectedRows(0,40,50630,cb);

//如果((PageSize.A4.getHeight()-height)使用
writeSelectedRows()时
,您负责进行计算,例如计算行的高度。您只能在定义表的宽度后计算行的高度。在您的情况下,您可能不需要表的总高度,但需要知道行的高度。请参阅示例以了解如何使用e> getRowHeight()
方法

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