Search code examples
javaitextpdf-generation

I want to create a multiple internal link & external link in existing PDF using Java itext 5. How can i do that?


I want to add internal and external links in existing PDF using Itext5 for my task how can I do that ?


Solution

  • In a comment you wrote

    Rectangle linkLocation = new Rectangle(x, y, x + w, y + h);
    PdfDestination destination = new PdfDestination(PdfDestination.FIT);
    PdfAnnotation link = PdfAnnotation.createLink(stamper.getWriter(),linkLocation, PdfAnnotation.HIGHLIGHT_INVERT, reader.getNumberOfPages(), destination);
    link.setBorder(new PdfBorderArray(0, 0, 0));
    stamper.addAnnotation(link, 2);
    stamper.getOverContent(2).addImage(img);
    

    using this code i'm able to addimg but its redirecting to last page i want it to redirect to brower how to do that?

    In your code you use a PdfDestination as target for the link. That kind of target, though, is only a reference to a location in a PDF file, see the JavaDocs of that class.

    Instead you should use an overload of PdfAnnotation.createLink that accepts a PdfAction, and use a PdfAction you created with a constructor that accepts an URL or a String containing your target URL.