【发布时间】:2015-04-02 11:14:18
【问题描述】:
如果页面大小不同,如何将页面从外部 pdf 文档添加到目标 pdf? 这是我想要完成的事情:
我尝试使用 LayerUtility(如本例中的 PDFBox LayerUtility - Importing layers into existing PDF),但一旦我从外部 pdf 导入页面,进程就会挂起:
PDDocument destinationPdfDoc = PDDocument.load(fileInputStream);
PDDocument externalPdf = PDDocument.load(EXTERNAL PDF);
List<PDPage> destinationPages = destinationPdfDoc.getDocumentCatalog().getAllPages();
LayerUtility layerUtility = new LayerUtility(destinationPdfDoc);
// process hangs here
PDXObjectForm firstForm = layerUtility.importPageAsForm(externalPdf, 0);
AffineTransform affineTransform = new AffineTransform();
layerUtility.appendFormAsLayer(destinationPages.get(0), firstForm, affineTransform, "external page");
destinationPdfDoc.save(resultTempFile);
destinationPdfDoc.close();
externalPdf.close();
我做错了什么?
【问题讨论】:
-
如果您的主要关注点是代码中的 hanging process,而不是 如果页面不同,则将页面从外部 pdf doc 添加到目标 pdf尺寸 一般来说,需要更多信息:我运行了您的代码,并且对于我在这里的示例输入文档,它可以完美运行。因此,导致挂起的问题取决于一些额外的因素。可能您的源文件很特殊,应该检查一下。
-
实际上进程并没有挂起,只是根本没有创建文件。如果我取消外部页面导入,并将此页面添加到目标文档,一切正常:文件已创建,外部页面添加为最后一页。
-
这里是如果页面只是作为下一页添加的代码:d.pr/n/11Dv1 - 如果作为表单导入,则不会创建 tmp 文件,没有错误,什么都没有......
-
看来您的文档有些特别之处。你能分享它们吗?此外,请注明您使用的 PDFBox 版本。我正在使用 1.8.8 进行测试。