簡體   English   中英

iText PDF-將兩個pdf與PdfCopy合並后,PDFActions(PdfAction.gotoLocalPage)丟失/不起作用

[英]iText PDF - After merging two pdfs with PdfCopy, PDFActions (PdfAction.gotoLocalPage) are lost/does't work

我創建了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();

您可以使用Apache PDF合並兩個PDF。

以下是示例代碼段。

    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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM