【发布时间】:2014-05-05 22:40:15
【问题描述】:
这是我如何在 pdf 上显示表格的示例代码。要求是,当表格要拆分为多页时,本页的最后一行(pdf 上以灰色背景突出显示)和下一页的第一行(白色背景)应合并显示。我们 pdf 中的大多数表格都具有相同的设计,可以在单个表格中显示多个部分。是否可以..?请帮忙!
公共类 PdfTest {
public static void main(String[] args) {
Document document = new Document();
PdfWriter writer = null;
try {
writer = PdfWriter.getInstance(document,
new FileOutputStream("C:/Users/SplitExample.pdf"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
document.open();
document.setPageSize(PageSize.LETTER);
document.setMargins(36, 36, 60, 36);
PdfTest pdfTest = new PdfTest();
pdfTest.createTable(document, writer);
document.close();
}
private void createTable(Document document, PdfWriter writer) {
try {
PdfPTable table = new PdfPTable(5);
float width[] = { 1, 1, 1, 1, 1 };
table.setWidths(width);
table.setTotalWidth(527);
table.setWidthPercentage(90);
document.add(new Paragraph("\n"));
document.add(new Paragraph("Table Header"));
document.add(new Paragraph("\n"));
for(int i=0; i<6; i++) {
table.addCell(buildTabularHeaders("Test", 1));
table.addCell(buildTabularHeaders("Test", 1));
table.addCell(buildTabularHeaders("Test", 1));
table.addCell(buildTabularHeaders("Test", 1));
table.addCell(buildTabularHeaders("Test", 1));
table.addCell(getNameAndAgeCell("Person ", 25));
table.addCell(wrapStringToPdfPCellWithFormatting("Date", 1));
table.addCell(wrapStringToPdfPCellWithFormatting("Male", 1));
table.addCell(wrapStringToPdfPCellWithFormatting("Test", 1));
table.addCell(wrapStringToPdfPCellWithFormatting("Eglish", 1));
table.addCell(buildTabularHeaders("Benefits", 4));
table.addCell(wrapStringToPdfPCellWithFormatting("No", 4));
table.addCell(buildTabularHeaders("Taxes", 4));
table.addCell(wrapStringToPdfPCellWithFormatting("Yes", 4));
table.addCell(buildTabularHeaders("Test", 1));
table.addCell(buildTabularHeaders("Date", 1));
table.addCell(buildTabularHeaders("Citizen", 1));
table.addCell(buildTabularHeaders("Entry", 1));
table.addCell(wrapStringToPdfPCellWithFormatting(null, 1));
table.addCell(wrapStringToPdfPCellWithFormatting(null, 1));
table.addCell(wrapStringToPdfPCellWithFormatting(null, 1));
table.addCell(wrapStringToPdfPCellWithFormatting(null, 1));
table.addCell(buildTabularHeaders("Test", 1));
table.addCell(buildTabularHeaders("Test", 2));
table.addCell(buildTabularHeaders("Test", 1));
table.addCell(wrapStringToPdfPCellWithFormatting("Test", 1));
table.addCell(wrapStringToPdfPCellWithFormatting(null, 2));
table.addCell(wrapStringToPdfPCellWithFormatting(null, 1));
table.addCell(buildTabularHeaders("Test", 1));
table.addCell(buildTabularHeaders("Test", 1));
table.addCell(buildTabularHeaders("Test", 1));
table.addCell(buildTabularHeaders("Test", 1));
table.addCell(wrapStringToPdfPCellWithFormatting(null, 1));
table.addCell(wrapStringToPdfPCellWithFormatting(null, 1));
table.addCell(wrapStringToPdfPCellWithFormatting(null, 1));
table.addCell(wrapStringToPdfPCellWithFormatting(null, 1));
table.addCell(buildTabularHeaders("Test", 1));
table.addCell(buildTabularHeaders("Test", 2));
table.addCell(buildTabularHeaders("Test", 1));
table.addCell(wrapStringToPdfPCellWithFormatting(null, 1));
table.addCell(wrapStringToPdfPCellWithFormatting(null, 2));
table.addCell(wrapStringToPdfPCellWithFormatting(null, 1));
table.addCell(buildTabularHeaders("Test", 1));
table.addCell(buildTabularHeaders("Test", 1));
table.addCell(buildTabularHeaders("Test", 1));
table.addCell(buildTabularHeaders("Test", 1));
table.addCell(wrapStringToPdfPCellWithFormatting(null, 1));
table.addCell(wrapStringToPdfPCellWithFormatting(null, 1));
table.addCell(wrapStringToPdfPCellWithFormatting(null, 1));
table.addCell(wrapStringToPdfPCellWithFormatting(null, 1));
table.addCell(buildTabularHeaders("Test", 1));
table.addCell(buildTabularHeaders("Test", 1));
table.addCell(buildTabularHeaders("Test", 1));
table.addCell(buildTabularHeaders("Test", 1));
table.addCell(wrapStringToPdfPCellWithFormatting(null, 1));
table.addCell(wrapStringToPdfPCellWithFormatting(null, 1));
table.addCell(wrapStringToPdfPCellWithFormatting(null, 1));
table.addCell(wrapStringToPdfPCellWithFormatting(null, 1));
table.addCell(buildTabularHeaders("Test", 1));
table.addCell(buildTabularHeaders("Test", 2));
table.addCell(buildTabularHeaders("Test", 1));
table.addCell(wrapStringToPdfPCellWithFormatting(null, 1));
table.addCell(wrapStringToPdfPCellWithFormatting(null, 2));
table.addCell(wrapStringToPdfPCellWithFormatting(null, 1));
table.addCell(buildTabularHeaders("Test", 1));
table.addCell(buildTabularHeaders("Test", 1));
table.addCell(buildTabularHeaders("Test", 1));
table.addCell(buildTabularHeaders("Test", 1));
table.addCell(wrapStringToPdfPCellWithFormatting(null, 1));
table.addCell(wrapStringToPdfPCellWithFormatting(null, 1));
table.addCell(wrapStringToPdfPCellWithFormatting(null, 1));
table.addCell(wrapStringToPdfPCellWithFormatting(null, 1));
table.addCell(buildTabularHeaders("Test", 1));
table.addCell(buildTabularHeaders("Test", 1));
table.addCell(buildTabularHeaders("Test", 1));
table.addCell(buildTabularHeaders("Test", 1));
table.addCell(wrapStringToPdfPCellWithFormatting(null, 1));
table.addCell(wrapStringToPdfPCellWithFormatting(null, 1));
table.addCell(wrapStringToPdfPCellWithFormatting(null, 1));
table.addCell(wrapStringToPdfPCellWithFormatting(null, 1));
table.addCell(buildTabularHeaders("Test", 1));
table.addCell(buildTabularHeaders("Test", 2));
table.addCell(buildTabularHeaders("Test", 1));
table.addCell(wrapStringToPdfPCellWithFormatting(null, 1));
table.addCell(wrapStringToPdfPCellWithFormatting(null, 2));
table.addCell(wrapStringToPdfPCellWithFormatting(null, 1));
}
//addNewPage(writer, document, 80, table.getTotalHeight());
document.add(table);
} catch (Exception objExp) {
objExp.printStackTrace();
}
}
private PdfPCell getNameAndAgeCell(String name, int rowSpan) throws Exception {
PdfPCell nameAgeCell = new PdfPCell();
nameAgeCell.addElement(new Phrase("Name", FontFactory.getFont("ARIAL", 9)));
nameAgeCell.addElement(new Phrase("Age" + "20", FontFactory.getFont("ARIAL", 9)));
nameAgeCell.setRowspan(rowSpan);
return nameAgeCell;
}
private PdfPCell wrapStringToPdfPCellWithFormatting(String input, int colSpan) {
PdfPCell output = null;
if (input != null) {
output = new PdfPCell(new Phrase(input, FontFactory.getFont("ARIAL", 9)));
output.setPadding(1);
output.setColspan(colSpan);
} else {
output = new PdfPCell(new Phrase(INInterfacesHelper.SPACE_STRING , FontFactory.getFont("ARIAL", 9)));
output.setPadding(1);
output.setColspan(colSpan);
}
//output.setBorder(1);
return output;
}
private PdfPCell buildTabularHeaders(String strHeaderText, int colSpan) {
PdfPCell objHeadCell = new PdfPCell(new Phrase(strHeaderText,FontFactory.getFont(FontFactory.HELVETICA, 9,Font.BOLD)));
//objHeadCell.setHorizontalAlignment(Element.ALIGN_CENTER);
//objHeadCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
objHeadCell.setGrayFill(0.8f);
objHeadCell.setColspan(colSpan);
return objHeadCell;
}
private void addNewPage(PdfWriter writer, Document document, int tableHeaderHeight, float tableHeight) {
if ((writer.getVerticalPosition(true) - tableHeaderHeight - document.bottom()) < tableHeight) {
document.newPage();
}
}
}
【问题讨论】:
标签: itext