itext pdf - 如何在表拆分时组合标题和数据

时间:2014-05-05 22:40:15

标签: itext

以下是我在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();
    }
}

}

1 个答案:

答案 0 :(得分:0)

当你在另一张桌子后面添加一张桌子时,你不会发现任何不同之处:在视觉上,单独的桌子看起来就像是一张桌子。

因此,您不应该使用一个大表来满足您的要求。相反,您应该在每次需要新标头时启动一个新表,并使用setHeaderRows()方法定义标题的一部分行数。分割表时,这些行将自动重复。