【发布时间】: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