【发布时间】:2018-02-26 21:09:40
【问题描述】:
我有一个 template.pdf,其中包含页眉和页脚(一些文本/图像)。我正在生成一个包含其他数据的新 pdf(比如 result.pdf)。我需要在 result.pdf 的每一页上复制/重复 template.pdf。所以基本上 template.pdf 将作为 result.pdf 每一页的页眉和页脚。
问题是 template.pdf 只出现在 result.pdf 的第一页。我的 result.pdf 可以是任意“n”页。
public class templateTest {
public static void main(String[] args) throws IOException {
File file = new File("template.pdf");
PDDocument mainDocument = PDDocument.load(file);
PDPage myPage = mainDocument.getPage(0);
PDPageContentStream contentStream = new PDPageContentStream(mainDocument, myPage, AppendMode.APPEND, true);
contentStream.beginText();
// Some text
// Table 1 (Depending on table 1 size, pdf pages will increase)
contentStream.endText();
contentStream.close();
mainDocument.save("result.pdf");
mainDocument.close();
}
}
【问题讨论】:
-
LayerUtility 可能是您所需要的。
-
@Tilman 你能分享一个LayerUtility的例子或URL吗?
-
在搜索框中输入 LayerUtility 即可。或者尝试源代码下载中的 SuperimposePage 示例。 (抱歉,现在是凌晨 3 点,我睡不着,现在我要回去睡觉了)
-
您是否能够使用 LayerUtility(特别是
importPageAsForm方法)?如果是,请删除此问题。如果不是,请编辑它以解释什么不起作用或为什么 LayerUtility 不适合您。 -
谢谢蒂尔曼,LayerUtility 确实为我工作。我保留了这个问题并发布了答案,以防万一将来有人遇到类似类型的问题,他们可以直接从这里参考。
标签: java pdf-generation pdfbox