【问题标题】:iTextPDF (Version 5) hyperlink not linking to the right placeiTextPDF(版本 5)超链接未链接到正确的位置
【发布时间】:2021-10-29 17:01:27
【问题描述】:

我有一堆我在这个代码点合并的 PDF。在合并 PDF 的开头,我正在生成一个目录部分。现在,目录充当书签,单击它应该会带我到正确的页面。但是,如果我点击链接,它会跳转到页面底部和下一页之间。

我有这个代码:

action = PdfAction.gotoLocalPage("p" + entry.getKey(), false); link = new PdfAnnotation(copydoc, 36, ct.getYLine(), 559, y, action); stamp.addAnnotation(link);

不过,我还在 PDF 中创建了可以正常工作的书签。
Bookmarks example

 Document document = new Document();
    PdfCopy copydoc = new PdfCopy(document, baos);
//some other codes here
 HashMap<String, Object> bookmark = new HashMap<>();
        bookmark.put("Title", toc_value);
        bookmark.put("Action", "GoTo");
        bookmark.put("Page", String.format("%d Fit", entry.getKey()));
 outlines.add(bookmark);
//other codes here
 copydoc.setOutlines(outlines);

请帮我解决这个问题。

【问题讨论】:

标签: java itext pdf-generation tableofcontents


【解决方案1】:

所以,长话短说,我找到了解决问题的方法。为了解释解决方案,让我解释一下之前写的内容。

for (Map.Entry<String, PdfReader> entry : filesToMerge.entrySet()) {
        n = entry.getValue().getNumberOfPages();
        toc.put(pageNo + 1, entry.getKey());
        for (int i = 0; i < n; ) {
            pageNo++;
            page = copy.getImportedPage(entry.getValue(), ++i);
            stamp = copy.createPageStamp(page);
            chunk = new Chunk(String.format("Page %d/ %d", pageNo+1,totalPage));
            if (i == 1)
                chunk.setLocalDestination("p" + pageNo);
            ColumnText.showTextAligned(stamp.getUnderContent(),
                    Element.ALIGN_RIGHT, new Phrase(chunk),
                    250, 10, 0);
            stamp.alterContents();
            copy.addPage(page);
        }
    }

在这里,我在做什么,我正在创建一个包含页码的Chunk,并通过页面底部的ColumnText 对齐块。我的理解是这就是问题所在。因此,当点击目录链接时,它会进入页面底部并且页面不适合窗口。

为了解决这个问题,我在页面顶部的某处添加了一个空白文本填充 Chunck。并将其添加为LocalDestination。这是更新的代码:

 for (Map.Entry<String, PdfReader> entry : filesToMerge.entrySet()) {
        n = entry.getValue().getNumberOfPages();
        toc.put(pageNo + 1, entry.getKey());
        for (int i = 0; i < n; ) {
            pageNo++;
            page = copy.getImportedPage(entry.getValue(), ++i);
            stamp = copy.createPageStamp(page);
            chunkBlank = new Chunk(" ");
            chunk = new Chunk(String.format("Page %d/ %d", pageNo+1,totalPage));
            if (i == 1) {
                chunk.setLocalDestination("p" + pageNo);
                chunkBlank.setLocalDestination( "p"+pageNo );
                }
            ColumnText.showTextAligned( stamp.getOverContent(),Element.ALIGN_RIGHT,new Phrase(chunkBlank),20,1000,0 );
            ColumnText.showTextAligned(stamp.getUnderContent(),
                    Element.ALIGN_RIGHT, new Phrase(chunk),
                    250, 10, 0);
            stamp.alterContents();
            copy.addPage(page);
        }
    }

【讨论】:

    猜你喜欢
    • 2021-10-31
    • 2013-04-19
    • 2017-08-03
    • 2019-11-10
    • 1970-01-01
    • 2023-03-09
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多