我正在使用 PdfPTable (iText) 打印一个填充了一些值列表的表格。
问题是,在 PdfPTable 需要显示多页的情况下,它的最后一行打印在第一页的末尾,也打印在第二页的开头。
请在下面找到一个示例:
编辑 :
请在下面找到代码:
protected static PdfPTable addUserList(PdfWriter writer, Document document, List<MyObject> objects) throws Exception {
PdfPTable headerTable = new PdfPTable(4);
headerTable.setWidthPercentage(100);
headerTable.setWidths(new int[] { 4, 7, 5, 3 });
PdfPCell headerCell = PDFUtils.makeDefaultCell(1);
headerCell.setBorderColor(Color.WHITE);
headerCell.setBorder(PdfPCell.RIGHT);
headerCell.setBorderWidth(1f);
Phrase phrase = new Phrase("Column1", Style.OPIFICIO_12_BOLD_WHITE);
headerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
headerCell.setPhrase(phrase);
headerTable.addCell(headerCell);
phrase = new Phrase("Column2", Style.OPIFICIO_12_BOLD_WHITE);
headerCell.setPhrase(phrase);
headerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
headerTable.addCell(headerCell);
phrase = new Phrase("Column3", Style.OPIFICIO_12_BOLD_WHITE);
headerCell.setPhrase(phrase);
headerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
headerTable.addCell(headerCell);
phrase = new Phrase("Column4", Style.OPIFICIO_12_BOLD_WHITE);
Chunk chunk = new Chunk("(1)", Style.OPIFICIO_6_BOLD_WHITE);
chunk.setTextRise(7f);
phrase.add(chunk);
chunk = new Chunk("(XX)", Style.OPIFICIO_8_BOLD_WHITE);
chunk.setTextRise(1f);
phrase.add(chunk);
headerCell.setPhrase(phrase);
headerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
headerCell.setBorder(PdfPCell.NO_BORDER);
headerTable.addCell(headerCell);
PdfPTable userTable = new PdfPTable(4);
userTable.setWidthPercentage(100);
userTable.setWidths(new int[] { 4, 7, 5, 3 });
PdfPCell cell = PDFUtils.makeDefaultCell(1);
cell.setBackgroundColor(null);
cell.setPaddingTop(2f);
cell.setPaddingLeft(6f);
cell.setPaddingRight(6f);
for (MyObject object : objects) {
if (object != null) {
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
if (object.getAttribute1() != null) {
phrase = new Phrase(object.getAttribute1(), Style.FUTURASTD_10_NORMAL_BLACK);
} else {
phrase = new Phrase("", Style.FUTURASTD_10_NORMAL_BLACK);
}
cell.setBorderWidth(1f);
cell.setBorderColor(Color.WHITE);
cell.setBorder(PdfPCell.RIGHT);
cell.setPhrase(phrase);
userTable.addCell(cell);
phrase = new Phrase(object.getAttribute2(), Style.FUTURASTD_10_NORMAL_BLACK);
cell.setBorderWidth(1f);
cell.setBorderColor(Color.WHITE);
cell.setBorder(PdfPCell.RIGHT);
cell.setPhrase(phrase);
userTable.addCell(cell);
phrase = new Phrase(object.getAttribute3(), Style.FUTURASTD_10_NORMAL_BLACK);
cell.setBorderWidth(1f);
cell.setBorderColor(Color.WHITE);
cell.setBorder(PdfPCell.RIGHT);
cell.setPhrase(phrase);
userTable.addCell(cell);
phrase = new Phrase(object.getAttribute4(), Style.FUTURASTD_10_NORMAL_BLACK);
cell.setBorder(PdfPCell.NO_BORDER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell.setPhrase(phrase);
userTable.addCell(cell);
}
}
PdfPTable mainTable = new PdfPTable(1);
mainTable.setWidthPercentage(100);
mainTable.setSplitLate(false);
mainTable.setHeaderRows(1);
PdfPCell cellH = new PdfPCell();
cellH.addElement(headerTable);
cellH.setBorder(Rectangle.NO_BORDER);
cellH.setCellEvent(new PDFUtils.CellBackgroundRedRecap());
mainTable.addCell(cellH);
if (userTable.getRows().size() > 0) {
PdfPCell cellUser = PDFUtils.makeDefaultCell(1);
cellUser.setPaddingTop(7f);
cellUser.setCellEvent(new PDFUtils.CellBackgroundRecap());
cellUser.setBorder(PdfCell.NO_BORDER);
cellUser.addElement(userTable);
mainTable.addCell(cellUser);
}
return mainTable;
}