Java 使用PDFBox动态创建多页文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21995744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:55:26  来源:igfitidea点击:

Create mutli-page document dynamically using PDFBox

javapdfpdfbox

提问by LiquidDrummer

I am attempting to create a PDF report from a Java ResultSet. If the report was only one page, I would have no problem here. The issue comes from the fact that the report could be anywhere from one to ten pages long. Right now, I have this to create a single-page document:

我正在尝试从 Java ResultSet 创建 PDF 报告。如果报告只有一页,我在这里没有问题。问题来自这样一个事实,即报告的长度可能从一到十页不等。现在,我有这个来创建一个单页文档:

PDDocument document = new PDDocument();
PDPage page = new PDPage(PDPage.PAGE_SIZE_LETTER);
document.addPage(page);
PDPageContentStream content = new PDPageContentStream(document,page);

So my question is, how do I create pages dynamically as they are needed. Is there an object-oriented answer staring me in the face and I just cannot see it?

所以我的问题是,如何根据需要动态创建页面。是否有面向对象的答案盯着我看而我看不到它?

采纳答案by LiquidDrummer

As I expected, the answer was staring me right in the face, I just needed someone to point it out for me.

正如我所料,答案就在我的脸上,我只是需要有人为我指出。

PDDocument document = new PDDocument();
PDPage page = new PDPage(PDPage.PAGE_SIZE_LETTER);
document.addPage(page);
PDPageContentStream content = new PDPageContentStream(document,page);

//generate data for first page

content.close();

//if number of results exceeds what can fit on the first page
page = new PDPage(PDPage.PAGE_SIZE_LETTER);
document.addPage(page);
content = new PDPageContentStream(document,page);

//generate data for second page

content.close();

Thanks to @mkl for the answer.

感谢@mkl 的回答。

回答by AVA

To Create Multi Page PDF Document using PDFBox:

使用 PDFBox 创建多页 PDF 文档:

(a) Create new page, new content stream, Move to Top Left, start writing. While writing each word check whether space required is not crossing mediabox width. If crosses, move to next line leftmost and start writing. Continue writing till the last line of the page.

(b) Close the contentStream and add the current page to the document when the writing operation reaches the last line of the current page,

(c) Repeat steps (a) and (b) till the last record/row/line is written.

(a) 创建新页面,新内容流,移至左上角,开始写作。在编写每个单词时,检查所需的空间是否不跨越媒体框宽度。如果交叉,移动到最左边的下一行并开始写作。继续写到页面的最后一行。

(b) 当写入操作到达当前页面的最后一行时,关闭 contentStream 并将当前页面添加到文档中,

(c) 重复步骤 (a) 和 (b),直到写入最后一条记录/行/行。

        PDDocument document = new PDDocument();
        PDFont font = PDType1Font.HELVETICA;

//For Each Page:
        PDPage page = new PDPage(PDPage.PAGE_SIZE_A4);
        PDPageContentStream contentStream = new PDPageContentStream(document, page);
        contentStream.setFont(font, 12);
        contentStream.beginText();
        contentStream.moveTextPositionByAmount(100, 700);
        contentStream.drawString("PDF BOX TEXT CONTENT");
        contentStream.endText();
        contentStream.close();
        document.addPage(page);

//After All Content is written:
        document.save(pdfFile);
        document.close();

Hint:Use Font parameters like size/height and remaining media box height to determine the last line of the page.

提示:使用字体参数(如大小/高度和剩余媒体框高度)来确定页面的最后一行。