简体   繁体   中英

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.

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. You would have to calculate before calling drawTable in the main method whether multiple pages are needed and create the pages there. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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