我目前正在使用iText 2.1.7将一个.pdf文件附加到另一个.pdf文件 . 我的代码类似于我在网上找到的许多连接示例,添加了为尾随pdf文件添加书签并保留现有书签 .

最近,用户发现在打开连接文件然后关闭之后,系统会提示他们保存文件 . 现在看来,较新版本的Adobe Reader现在检测到.pdf错误并修复它,从而导致保存提示 .

在为此创建测试程序时,我发现它是尾随书签并调整现有书签是问题所在 . 有没有办法确保在查看使用iText将一个.pdf文件连接到另一个.pdf文件时生成的文件后,系统不会提示用户保存?

可以使用“java TestPdfConcat pdf_file_1 pdf_file_2 optional_trailing_bookmark ”运行测试

如果省略了尾随书签,则生成的连接的.pdf不会提示用户在关闭时保存 .

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.*;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class TestPdfConcat {
    public static void main(String[] args) {
        File originalPDF = new File(args[0]);
        File trailingPDF = new File(args[1]);
        String trailingBookmark = null;
        if (args.length > 2)
            trailingBookmark = args[2];

        PdfReader reportReader = null;
        try {
            reportReader = new PdfReader(originalPDF.getPath());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        // Get bookmarks for original report.
        ArrayList master = (ArrayList) SimpleBookmark.getBookmark(reportReader);
        if (master == null)
            master = new ArrayList();
        int reportPages = reportReader.getNumberOfPages();
        PdfReader trailingReader = null;
        try {
            trailingReader = new PdfReader(trailingPDF.getPath());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        trailingReader.consolidateNamedDestinations();
        int trailingPages = trailingReader.getNumberOfPages();
        List bookmarks = SimpleBookmark.getBookmark(trailingReader);
        if (trailingBookmark != null) {
            // Add trailing bookmark.
            System.out.println("Add bookmark, " + trailingBookmark + ", to trailing pdf.");
            if (bookmarks == null)
                bookmarks = new ArrayList();
            // If trailing pdf has existing bookmarks, shift them for the appended pdf.
            if (!bookmarks.isEmpty()) {
                if (reportPages != 0) {
                    SimpleBookmark.shiftPageNumbers(bookmarks, reportPages, null);
                }
            }

            HashMap<String, String> map = new HashMap<String, String>();
            map.put("Title", trailingBookmark);
            map.put("Action", "GoTo");
            map.put("Page", Integer.toString(reportPages + 1));
            bookmarks.add(map);
            // Add to existing bookmarks.
            master.addAll(bookmarks);
        }


        // Append to original document
        System.out.println("Append " + trailingPDF.getName() + " to " + originalPDF.getName());
        Document document = new Document(reportReader.getPageSizeWithRotation(1));
        PdfCopy writer = null;
        try {
            writer = new PdfCopy(document, new FileOutputStream("Concatenated.pdf", true));
        } catch (DocumentException e) {
            throw new RuntimeException(e);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        document.open();

        PdfImportedPage page;
        // Add original report pages.
        System.out.println("Original report has " + reportPages + " page(s).");
        for (int i = 0; i < reportPages;) {
            page = writer.getImportedPage(reportReader, ++i);
            try {
                writer.addPage(page);
            } catch (IOException e) {
                throw new RuntimeException(e);
            } catch (BadPdfFormatException e) {
                throw new RuntimeException(e);
            }
        }
        // Add trailing pdf pages.
        System.out.println("Trailing pdf has " + trailingPages + " page(s).");
        for (int j = 0; j < trailingPages;) {
            page = writer.getImportedPage(trailingReader, ++j);
            try {
                writer.addPage(page);
            } catch (IOException e) {
                throw new RuntimeException(e);
            } catch (BadPdfFormatException e) {
                    throw new RuntimeException(e);
            }
        }

        if (!master.isEmpty()) {
            writer.setOutlines(master);
        }
        document.close();
    }
}