小编典典

使用itext java将锚点添加到pdf

java

我正在尝试使用itext java
api将anchor(命名为destinations)添加到pdf。但是它不起作用。当我单击文本时,什么也没有发生。这就是我在做什么。

 Anchor anchor =
            new Anchor("Jump down to next paragraph");
    anchor.setReference("#linkTarget");
    Paragraph paragraph = new Paragraph();
    paragraph.add(anchor);
    document.add(paragraph);

    Anchor anchorTarget =
            new Anchor("This is the target of the link above");
    anchor.setName("linkTarget");
    Paragraph targetParagraph = new Paragraph();
    targetParagraph.setSpacingBefore(50);

    targetParagraph.add(anchorTarget);
    document.add(targetParagraph);

我究竟做错了什么?。任何帮助


阅读 559

收藏
2020-11-30

共1个答案

小编典典

试试这个。它为我工作。setLocalGoto()setLocalDestination()会做魔术。

Chunk chunk = new Chunk("Contact information");
    chunk.setLocalGoto("contact");  
    document.add(new Paragraph(chunk));
    document.newPage();

    chunk chunk1 = new Chunk("Contact information");
    chunk1.setLocalDestination("contact");
    Chapter chapter = new Chapter(new Paragraph(chunk1),1);    
    chapter.setNumberDepth(0);
    document.add(chapter);
2020-11-30