【问题标题】:iText PDF - After merging two pdfs with PdfCopy, PDFActions (PdfAction.gotoLocalPage) are lost/does't workiText PDF - 将两个 pdf 与 PdfCopy 合并后,PDFActions (PdfAction.gotoLocalPage) 丢失/不起作用
【发布时间】:2019-01-03 05:56:18
【问题描述】:

我创建了 PDF 文档 file1.pdf 和 file2.pdf(几乎相似的文件具有不同的数据)。每个文件都有自己的目录,带有 PdfAction,单击目录中的元素可导航到相应文档中的相应页面。它工作正常。

我使用以下代码对 TOC 元素设置操作

Chunk chunk = new Chunk("Analysis Report");
chunk.setAction(PdfAction.gotoLocalPage(title, true));

问题是……

在合并这两个文档时,使用 PDFCopy 我在源 PDF 中设置的操作会在新合并的 PDF 中丢失。

下面是我用来合并两个 PDF 文档的代码。


File file1 = new File("file1.pdf");
PdfReader reader1 = new PdfReader(file1.getAbsolutePath());

File file2 = new File("file2.pdf");
PdfReader reader2 = new PdfReader(file2.getAbsolutePath());

File tempDestFile = File.createTempFile("temp", ".pdf");
Document document = TemplateHelper.getDocument(PageSize.A4);
PdfCopy copy = new PdfSmartCopy(document, new FileOutputStream(tempDestFile.getAbsolutePath()));

document.open();

copy.addDocument(reader1);
copy.addDocument(reader2);

document.close();
copy.close();
reader1.close();

【问题讨论】:

  • 我不知道 PdfSmartCopy 如何处理 TOC。您是否尝试使用 PdfCopy?如果它不起作用,您可能需要手动将 TOC 复制到新文档中...

标签: java pdf itext pdf-generation


【解决方案1】:

您可以使用 Apache PDF 合并两个 PDF。

下面是示例代码sn-p。

    PDFMergerUtility ut = new PDFMergerUtility();
    ut.addSource(file1);
    ut.addSource(file2);
    ut.setDestinationFileName("done.pdf");
    ut.mergeDocuments(); 

对 Apache PDF 框使用以下依赖项。

 <!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.11</version>
</dependency>

【讨论】:

  • PDFBox 不是 iText。不过,问题集中在 iText 上。
  • 是的@mkl,我尝试在 itext 中合并两个 PDF,但它不起作用,但我们可以在 PDFBox 中完成。
猜你喜欢
  • 2015-08-17
  • 1970-01-01
  • 2016-04-07
  • 1970-01-01
  • 2018-02-07
  • 2014-10-08
  • 1970-01-01
相关资源
最近更新 更多