如何在每个页面上重复ITextPDF PdfPTable的标题行?

时间:2017-03-13 08:03:15

标签: java itext pdfptable

`table.setHeaderRows(1);

函数setHeaderRows(1);将重复所有页面中的第一行。但是如果我想在所有页面的表格中重复一些列,我该怎么办?

请帮忙! 我正在使用Itextpdf-5.5.10.jar 以下是创建表的功能:

public class RepeatMultipleColumns {
    public static final String DEST = "D://Eample.pdf";

    public static void main(String[] args) throws Exception {
        System.out.println(DEST);

        createPdf(DEST);
    }

    public static void createPdf(String dest)
            throws Exception {

        Document document = new Document(new Rectangle(PageSize.A4), 65, 65, 60, 60);
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();

        for (int i = 0; i < 30; i++) {
            document.add(new Paragraph("Hello World!"));
        }
        document.add(createTableTest(document));

        document.close();
    }

    public static PdfPTable createTableTest(Document document) throws Exception {

        PdfPTable table = new PdfPTable(5);
        table.setWidths(new int[]{1, 1, 1, 2, 1});
        PdfPCell cell;
        cell = new PdfPCell(new Phrase("Repeat Row Head"));
        cell.setRowspan(2);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setFixedHeight(20f);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase("Reapeat Row Head"));
        cell.setColspan(4);
        cell.setFixedHeight(20f);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);

        table.addCell(cell);
        List<String> headLabel = new ArrayList<String>();
        headLabel.add("Repeat Row 1 Head");
        headLabel.add("Repeat Row 2 Head");
        headLabel.add("Repeat Row 3 Head");
        headLabel.add("Repeat Row 4 Head");
        for (int i = 0; i < headLabel.size(); i++) {
            cell = new PdfPCell(new Phrase(headLabel.get(i)));

            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setFixedHeight(20f);
            table.addCell(cell);
        }
        List<String> leftContent = new ArrayList<String>();
        leftContent.add("Repeat Column1");
        leftContent.add("Repeat Column2");
        leftContent.add("Repeat Column3");
        //Body
        List<String> content = new ArrayList<String>();
        content.add("2,001~5,000");
        content.add("30");
        content.add("5,001~10,000");
        content.add("25");

        content.add("10,001~30,000");
        content.add("20");

        content.add("30,001");
        content.add("15");
        // New Record

        content.add("30,001");
        content.add("15");
        content.add("30,001");
        content.add("15");
        content.add("30,001");
        content.add("15");
        content.add("30,001");
        content.add("15");
        content.add("30,001");
        content.add("15");
        content.add("30,001");
        content.add("15");
        content.add("30,001");
        content.add("15");
        content.add("30,001");
        content.add("15");
        content.add("30,001");
        content.add("15");
        content.add("30,001");
        content.add("15");
        content.add("30,001");
        content.add("15");
        content.add("30,001");
        content.add("15");
        content.add("30,001");
        content.add("15");
        content.add("30,001");
        content.add("15");
        content.add("30,001");
        content.add("15");
        content.add("30,001");
        content.add("15");
        content.add("30,001");
        content.add("15");

        // LEFT
        for (int i = 0; i < leftContent.size(); i++) {
            cell = new PdfPCell(new Phrase(leftContent.get(i)));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setFixedHeight(20f);
            //cell.setRowspan(4);
            cell.setRowspan(content.size());
            table.addCell(cell);
        }
        //#TODO BODY
        for (int i = 0; i < content.size(); i++) {
            cell = new PdfPCell(new Phrase(content.get(i)));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setFixedHeight(20f);
            table.addCell(cell);
        }

        table.setHeaderRows(2);
        table.setWidthPercentage(100);
        table.setSpacingBefore(0f);
        table.setSpacingAfter(0f);
        table.setHorizontalAlignment(Element.ALIGN_CENTER);
        return table;
    }
}

结果如下:

Here is the result

我希望这三列(突出显示的列)在所有页面中重复

0 个答案:

没有答案