当前位置: 首页 > 知识库问答 >
问题:

使用JavaPDFBox将源pdf页面复制(或重复)到另一个pdf中

丰佐
2023-03-14

我有一个模板。有页眉和页脚的pdf(一些文本/图像)。我正在生成一个包含其他数据的新pdf(比如result.pdf)。我需要复制/重复模板。结果的每一页上都有pdf。pdf。所以基本上是模板。pdf将作为结果每页的页眉和页脚。pdf。

问题在于模板。pdf仅显示在结果的第1页。pdf。我的结果。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();
 }
}

共有1个答案

郦祺
2023-03-14

我已经回答了我自己的问题。蒂尔曼·豪斯赫尔为我指明了正确的方向。

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();
 }
}
 类似资料:
  • 我使用BIRT报告创建了一个PDF,每个数据页的标题中都包含一个表。现在我使用IText将目录添加到PDF报告中。有没有办法使用IText将标题从数据页复制到TOC页?

  • 我已经编写了执行以下操作的代码: 以特定页面大小(例如8.5英寸x 11英寸)的PDF为例 为此,我使用方法从原始PDF获取当前页面,然后使用方法将原始页面放置到新PDF的当前页面上。 我的新挑战是,在将原始PDF添加到新PDF之前,我需要裁剪它。例如,假设我想在强制使用之前将原始PDF裁剪2英寸。输入PDF仍然是8.5英寸x11英寸,新PDF仍然是17英寸x11英寸,但新PDF中原始PDF的两个

  • 我有一个22*17的PDF文件,我需要它来适应11*8.5的页面内容。 基本上减小了现有的页面大小。我正在使用断章。 我该怎么做?

  • 我制作了一个pdf表单,我正在尝试使用pdfBox填写表单并打印文档。我让它在单页打印作业中运行良好,但我不得不尝试修改多页。基本上这是一个表单,顶部有基本信息和内容列表。好吧,如果内容大于表单的空间,我必须将其设为多页文档。我最终得到了一个具有漂亮页面的文档,然后所有剩余页面都是空白模板。我做错了什么?

  • 我读了很多关于SO的帖子,并搜索了很多解决方案,所有这些似乎都回避了我的特定问题。 是否有一种方法可以使用正确的页面大小将现有的PDF复制到新文档中? 谢谢。 更新 接受了布鲁诺的建议,我仔细检查了剪裁框和媒体框。我正在使用的PDF两者都有。裁剪框数组为 但是这样做会使“文本”向右和顶部移动太多,留下不均匀的边距。 我发现通过改变左下角的坐标,裁剪框,我可以移动文本。事实上,将裁剪框设置为40、4

  • 在java Web应用程序中使用PDFBox。我有一个代表多页绘图或蓝图的PDF文件。还有其他PDF文件表示对父文件的修改。我想将这些页面插入到第1页和第2页之间的父PDF文件中。 我找到了关于合并和添加页面的文章,但不完全是我需要的。 一些指导会很有帮助。 到目前为止我的代码: