简体   繁体   中英

Add anchor to pdf using itext java

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

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

You are not passing anchorTarget in setName() , that's why it's not leading you towards #linktarget

Anchor anchorTarget = new Anchor("This is the target of the link above");
        anchorTarget.setName("linkTarget");

        Paragraph anchorTargetLinkParagraph = new Paragraph();
        anchorTargetLinkParagraph.setAlignment(Element.ALIGN_CENTER);
        anchorTargetLinkParagraph.add(anchorTarget);
        document.add(anchorTargetLinkParagraph);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM