如何使用可装箱pdfbox继续在下一页上绘制具有许多列的表格?

时间:2019-07-15 03:25:15

标签: java pdfbox boxable

由于以下问题,我试图绘制一张跨越多页的表格,但没有成功:

  1. 该表有很多列(一页不能容纳10列)。
  2. 该表有很多行(一页上不能容纳50行)。

我要实现的目标是,一旦表位于另一页上,则标题(应该重复很​​多列)以及第一列。

请在此处查看示例文件。我能够使用Excel创建此文件。 ListExampleLandscape.pdf

我希望获得的最终产品是课程列表,带有:

  1. 三个重复的列:StudentId,Lastname,Firstname
  2. 重复的行:标题(studentId,Lastname,Firstname,...至少30列日期,供学生在每一列/天签名)。

我应该怎么做?

非常感谢您。

我从此页上发现的旧问题中借用了该示例。


        //Initialize Document
        PDDocument doc = new PDDocument();
        PDPage page = new PDPage();

        //Create a landscape page
        page.setMediaBox(new PDRectangle(PDRectangle.A4.getHeight(), PDRectangle.A4.getWidth()));
        PDRectangle mediabox = new PDPage(new PDRectangle(
            PDRectangle.A4.getHeight(), PDRectangle.A4.getWidth()))
                .getMediaBox();
        doc.addPage(page);
        int spaceBetweenTables = 0;
        //Initialize table
        float margin = 40;
        // we want 2 tables so our table width is 50% of page width without left and right margin AND provided space between tables
        float tableWidth = 0.5f * (PDRectangle.A4.getHeight() - (2 * margin)- spaceBetweenTables);
        float yStartNewPage = page.getMediaBox().getHeight() - (2 * margin);
        float yStart = yStartNewPage;
        float bottomMargin = 0;
        float maxY;
        int maxPageIndex;
        float y = 0.0f;

        List<List> data = new ArrayList();
        data.add(new ArrayList<>(
                   Arrays.asList("Column One", "Column Two", "Column Three", "Column Four", "Column Five",
                       "Column Six", "Column Seven", "Column Eight", "Column Nine", "Column Ten")
                   )
            );
                for (int i = 1; i <= 10; i++) {
                    data.add(new ArrayList<>(
                               Arrays.asList("Row Row Row Row Row Row " + i + " Col One", "Row " + i + " Col Two", "Row " + i + " Col Three", "Row " + i + " Col Four", "Row " + i + " Col Five",
                                   "Row " + i + " Col Six", "Row " + i + " Col Seven", "Row " + i + " Col Eight", "Row " + i + " Col Nine", "Row " + i + " Col Ten")));
                    BaseTable dataTableLeft = new BaseTable(500, 500, 40, tableWidth, 40, doc, page, true, true);
                    DataTable t = new DataTable(dataTableLeft, page);
                    t.addListToTable(data, DataTable.HASHEADER);
                    y = dataTableLeft.draw();
                }

                for(int i = 11; i <= 20; i++) {
                    data.add(new ArrayList<>(
                               Arrays.asList("Row Row Row Row Row Row " + i + " Col One", "Row " + i + " Col Two", "Row " + i + " Col Three", "Row " + i + " Col Four", "Row " + i + " Col Five",
                                   "Row " + i + " Col Six", "Row " + i + " Col Seven", "Row " + i + " Col Eight", "Row " + i + " Col Nine", "Row " + i + " Col Ten")));
                    BaseTable dataTableRight = new BaseTable(500, 500, 40, tableWidth, 40 + tableWidth + spaceBetweenTables, doc, page, true, true); 
                    DataTable tRight = new DataTable(dataTableRight, doc.getPage(0));
                    tRight.addListToTable(data, DataTable.HASHEADER);
                    dataTableRight.draw();
                }

        File file = new File("target/ListExampleLandscape.pdf");
        System.out.println("Sample file saved at : " + file.getAbsolutePath());
        Files.createParentDirs(file);
        doc.save(file);
        doc.close();
    }

0 个答案:

没有答案