【问题标题】:itext pdf - how to combine headers and data when table splitsitext pdf - 表格拆分时如何合并标题和数据
【发布时间】: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


    【解决方案1】:

    当您一个接一个地添加表格时,您不会看到任何区别:从视觉上看,单独的表格看起来就像是一个表格。

    因此,您不应该使用一张大桌子来满足您的要求。相反,您应该在每次需要新表头时创建一个新表,并使用setHeaderRows() 方法来定义表头中有多少行。拆分表格时,这些行将自动重复。

    【讨论】:

    • 感谢您的回复,这只是一个表格,例如,在我们的应用程序中显示多个人的地址信息。每个人的信息都被分隔在一个表格中,但是,由于它的外观设计像多张桌子。是否可以找到当前页面最后一行的位置,如果它从它的标题中分离出来,将它与数据一起显示在下一页中..?
    • 我不明白这个问题。我假设您使用单词 header 来引用 普通行。如果不是,使用setHeaderRows() 应该可以。如果是,请改写问题,不要错误地使用header这个词。
    • 你说得对,我指的是一个普通的行,使用了header这个词。我已经编辑了我的问题。
    • 答案是:普通行无法做到这一点,但您可以通过使用较小的单独表格来解决这个问题,其中灰色背景的行是真正的标题行。
    • 这给了我一些想法...我们已经确定了 pdf 中的大表格并更改了设计以将它们显示为每个页面中的单独表格,对于其他表格,我们还确定了表格的数量可以适合每个页面,如果有更多,则在新页面中显示它们。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-09
    相关资源
    最近更新 更多