【问题标题】:Copy(or repeat) a page of source pdf into another pdf using java PDFBox使用java PDFBox将一页源pdf复制(或重复)到另一个pdf
【发布时间】: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


【解决方案1】:

我已经回答了我自己的问题。 Tilman Hausherr 为我指明了正确的方向。

public class templateTest {

 public static void main(String[] args) throws IOException { 

   File file = new File("template.pdf");
   PDDocument templatePdf = PDDocument.load(file);
   PDDocument mainDocument = new PDDocument();     

   PDPage myPage = new PDPage();
    mainDocument.addPage(myPage);
   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();

  // Process of imposing a layer begins here
  PDPageTree destinationPages = mainDocument.getDocumentCatalog().getPages();

  LayerUtility layerUtility = new LayerUtility(mainDocument);

  PDFormXObject firstForm = layerUtility.importPageAsForm(templatePDF, 0);

  AffineTransform affineTransform = new AffineTransform();

  PDPage destPage = destinationPages.get(0);

  layerUtility.wrapInSaveRestore(destPage);
  layerUtility.appendFormAsLayer(destPage, firstForm, affineTransform, "external page");

  mainDocument.save("result.pdf");
  mainDocument.close();
 }
}

【讨论】:

  • 这是一个非常有用的例子!感谢您花时间发布它。不幸的是,template.pdf 中包含的带有页眉和页脚的图层似乎覆盖了 ContentStream 中的内容?我有一个 2 页的 template.pdf,两页都有页眉和页脚。我在 importPageAs 表单调用中指定了第一页,但似乎无法在导入时设置 z 层编号以将模板页面放在 contentStream 下方?有任何想法吗?谢谢,马克。
  • @Mark 请自行提出堆栈溢出问题(在某些背景下带有指向此问题的链接)。
  • 我把它整理好了,我认为问题在于首先模板 PDF 文件有两个页面,即使我指定了第一个页面,它也放入了两次,第二个是我没有在 AppendMode.APPEND 中使用 ContentStream。
  • 非常感谢!我一直在努力添加基于单个页面模板(页眉/页脚)的多个页面。这让我到了那里!现在,如果我能理解 AffineTransform 是什么以及为什么它做了它所做的工作以使其工作......但我会想象它就像 pdftk 工具集的 stamp 函数
猜你喜欢
  • 2018-06-23
  • 1970-01-01
  • 2011-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-27
相关资源
最近更新 更多