简体   繁体   English

如何使用Java将自动增量功能设置为pdf文件?

[英]how to set auto increment feature into a pdf file using java?

I have the Java code to generate a table in a PDF file, but it is not able to generate more than one page even if more fields were needed. 我有Java代码可以在PDF文件中生成表格,但是即使需要更多字段,它也不能生成多个页面。

How would I add auto increment feature for pages? 如何为页面添加自动增量功能?

This the code which I'm refering. 这是我所引用的代码。

public static void main(String[] args) {
    PDDocument doc = new PDDocument();
    PDPage page = new PDPage();
    doc.addPage(page);
    PDPageContentStream contentStream = new PDPageContentStream(doc, page);
    String[][] content = { { "a", "b", "1" }, { "c", "d", "2" },
            { "e", "f", "3" }, { "g", "h", "4" }, { "i", "j", "5" } };
    drawTable(page, contentStream, 700, 100, content);
    contentStream.close();
    doc.save("test.pdf");
}

The problem with this particular piece of code is that you are passing a single page to the drawTable method. 这段特殊的代码的问题在于,您正在将单个页面传递给drawTable方法。 You would have to calculate before calling drawTable in the main method whether multiple pages are needed and create the pages there. 您需要在main方法中调用drawTable之前计算是否需要多个页面,然后在其中创建页面。 This might require some slight modification of the drawTable method to accept say a List of PDPage instead of a single PDPage in the event that more than one page is necessary. 在需要多个页面的情况下,可能需要对drawTable方法进行一些细微修改,以接受PDPage列表而不是单个PDPage。 So the way drawTable is implemented, no there would be no way to automatically handle it, but with a little math is should be doable to accomplish. 因此, drawTable的实现方式是不可能自动处理它的,但是只需一点点数学就可以完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM