内部链接不适用于合并的PDF文件

时间:2016-05-26 19:42:05

标签: java itext

我目前正在使用itext包(itext-2.1.7.jar)修改生成PDF报告的逻辑。首先生成报告,然后 - 目录(TOC)然后将TOC插入到报告文件中,使用PdfStamper及其方法 - replacePage:

PdfStamper stamper = new PdfStamper(new PdfReader(finalFile.getAbsolutePath()),
            new FileOutputStream(reportFile));
stamper.replacePage(new PdfReader(tocFile.getAbsolutePath()), 1, 2); 

但是,我希望TOC也有内部链接指向正确的章节。不幸的是,使用replacePage()方法删除了所有链接和注释,所以我尝试了另一种方式。我使用了一种方法来合并报告文件和TOC文件。它做了诀窍,意味着现在保留了链接,但它们似乎不起作用,用户可以点击它们,但没有任何反应。我已将内部链接放在报告文件中的其他位置,以用于测试目的,它们似乎有效,但它们在放置在实际TOC上时不起作用。 TOC是单独生成的,这是我创建链接的方式(addLeadingDots是一个本地方法,与问题无关):

Chunk anchor = new Chunk(idx + ". " + chapterName + getLeadingDots(chapterName, pageNumber) + " " + pageNumber, MainReport.FONT12);                 
String anchorLocation = idx+chapterName.toUpperCase().replaceAll("\\s","");
anchor.setLocalGoto(anchorLocation);
line = new Paragraph();                 
line.add(anchor);

line.setLeading(6);
table.addCell(line); 

这就是我在报告文件的章节中创建锚点目的地的方法:

Chunk target = new Chunk(title.toUpperCase(), font);        
String anchorDestination = title.toUpperCase().replace(".", "").replaceAll("\\s","");           
target.setLocalDestination(anchorDestination);
Paragraph p = new Paragraph(target);
p.setAlignment(Paragraph.ALIGN_CENTER); 
doc.add(p);

最后,这是应该合并报告文件和TOC的方法:

public static void mergePDFs(String originalFilePath, String fileToInsertPath, String outputFile, int location) {

    PdfReader originalFileReader = null;
    try {
        originalFileReader = new PdfReader(originalFilePath);
    } catch (IOException ex) {
        System.out.println("ITextHelper.addPDFToPDF(): can't read original file: " + ex);
    }
    PdfReader fileToAddReader = null;
    try {
        fileToAddReader = new PdfReader(fileToInsertPath);
    } catch (IOException ex) {
        System.out.println("ITextHelper.addPDFToPDF(): can't read fileToInsert: " + ex);
    }

    if (originalFileReader != null && fileToAddReader != null) {
        int numberOfOriginalPages = originalFileReader.getNumberOfPages();
        Document document = new Document();
        PdfCopy copy = null;
        try {
            copy = new PdfCopy(document, new FileOutputStream(outputFile));
            document.open();

            for (int i = 1; i <= numberOfOriginalPages; i++) {
                if (i == location) {
                    for (int j = 1; j <= fileToAddReader.getNumberOfPages(); j++) {
                        copy.addPage(copy.getImportedPage(fileToAddReader, j));
                    }
                }
                copy.addPage(copy.getImportedPage(originalFileReader, i));
            }
            document.close();
        } catch (DocumentException | FileNotFoundException ex) {
            m_logger.error("ITextHelper.addPDFToPDF(): can't read output location: " + ex);
        } catch (IOException ex) {
            m_logger.error(Level.SEVERE, ex);
        }
    }
}   

所以,主要的问题是,在合并了两个pdf文档以及如何使它们工作之后,链接会发生什么?我对itext没有太多经验,所以任何帮助都表示赞赏。

感谢您的帮助

0 个答案:

没有答案
相关问题