使用PDFBox动态创建多页面文档 [英] Create mutli-page document dynamically using PDFBox
问题描述
我正在尝试从Java ResultSet创建PDF报告。如果报告只有一页,我在这里没问题。问题来自报告可能长达一到十页的事实。现在,我有这个来创建一个单页文档:
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:
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?
推荐答案
正如我所料,答案我正好盯着我,我只需要有人为我指出。
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();
感谢@mkl的回答。
这篇关于使用PDFBox动态创建多页面文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!