Add anchor to pdf using itext java

安稳与你 提交于 2019-12-02 17:51:07

问题


I am trying to add anchor(named destinations) to pdf using itext java api. But it's not working.When I click the text , nothing happens.This is what I am doing .

 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);

What am I doing wrong?. Any help


回答1:


Try this. It worked for me. setLocalGoto() and setLocalDestination() will do the magic.

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);


来源:https://stackoverflow.com/questions/23423479/add-anchor-to-pdf-using-itext-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!