1

How to add an external link in PDF and redirect to the webpage. . . .

example image describe below

enter image description here

On click on Goolge,user should redirect to webpage https://www.google.com

here is my code

private void createPDFiText() {
        int margin = getResources().getDimensionPixelSize(R.dimen._5sdp);
        Document document = new Document(PageSize.A4, margin, margin, margin, margin);

        try {
            PdfWriter.getInstance(document, getOutputStream());
            document.open();

            for (int i = 12; i <= 17; i++) {
                
                Phrase phrase = new Phrase("Open ");
                Phrase phrase1 = new Phrase(" on Click On it.");

                Font anchorFont = new Font(Font.FontFamily.UNDEFINED, 25);
                anchorFont.setColor(BaseColor.BLUE);
                anchorFont.setStyle(Font.FontStyle.UNDERLINE.getValue());

                Anchor anchor = new Anchor("Google", anchorFont);
                anchor.setReference("www.google.com");


                phrase.add(anchor);
                phrase.add(phrase1);
                
                document.add(phrase);
                

            }
            document.close();

        } catch (DocumentException | IOException e) {
            e.printStackTrace();
        }

    }
EAS
  • 366
  • 2
  • 18
  • Is the text "Google" already there and you merely want to make it a link? Or do you want to add the text and make it a link in one go? – mkl Feb 07 '22 at 08:14
  • No," google" text is not there,i want to add text into PDF with a link. – EAS Feb 07 '22 at 10:13

1 Answers1

2

I am referring to this answer. Have a look. Modifying existing pdf file using iText 5.5.13.2 is complicated. But the referred solution is more easier. iText 7 has handier way to modify existing pdf. There are several other ways. Like PdfStamper etc.

From referred answer, add following code to make an anchor.

Phrase phrase = new Phrase("Open ");
Phrase phrase1 = new Phrase(" on Click On it.");

Font anchorFont = new Font(Font.FontFamily.UNDEFINED, 11);
anchorFont.setColor(BaseColor.BLUE);
anchorFont.setStyle(Font.FontStyle.UNDERLINE.getValue());

Anchor anchor = new Anchor("Google", anchorFont);
anchor.setReference("www.google.com");

phrase.add(anchor);
phrase.add(phrase1);
document.add(phrase);

Change the font and colors based on your needs.

Full code:

try {
    PdfReader reader = new PdfReader("test.pdf"); //src pdf path (the pdf I need to modify)

    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("test2.pdf")); // destination pdf path
    document.open();

    PdfContentByte cb = writer.getDirectContent();
    PdfImportedPage page = writer.getImportedPage(reader, 1);

    document.newPage();
    document.setPageSize(reader.getPageSize(1));

    cb.addTemplate(page, 0, 0);

    Phrase phrase = new Phrase("Open ");
    Phrase phrase1 = new Phrase(" on Click On it.");
    Font anchorFont = new Font(Font.FontFamily.UNDEFINED, 11);
    anchorFont.setColor(BaseColor.BLUE);
    anchorFont.setStyle(Font.FontStyle.UNDERLINE.getValue());

    Anchor anchor = new Anchor("Google", anchorFont);
    anchor.setReference("https://www.google.com");

    phrase.add(anchor);
    phrase.add(phrase1);
    document.add(phrase);

    document.close();

    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}
Chinmoy Acharjee
  • 520
  • 5
  • 16
  • link is created but the click is not working on it. – EAS Feb 11 '22 at 05:13
  • In my machine, it is working fine. What problem you are facing. Is it not clickable? – Chinmoy Acharjee Feb 11 '22 at 08:06
  • No, it's not clickable, if I click on it, zooms in and out occured. – EAS Feb 11 '22 at 10:51
  • I've tested your code. First finding is, you are creating absolutely new pdf. You are not modifying any existing Pdf. But that should not be the cause of your problem. Second one is, It creates a new pdf having exactly 6 "Open Google on Click On it" where "Google " is hyperlink. And all of the links are clickable and redirecting to google page. I've tested in both Adobe acrobat and google chrome. For instance, I've used margin value 10. – Chinmoy Acharjee Feb 11 '22 at 11:16
  • what is margin value 10? – EAS Feb 11 '22 at 11:19
  • see my full code in the answer. The margin value you used for creating Document instance. I put 10 as margin. – Chinmoy Acharjee Feb 11 '22 at 11:23
  • Yes, you're right, actually, the problem is in my PDFviewer, not in code,Thanks – EAS Feb 11 '22 at 11:42
  • PDF linking working fine, but when I add in the existing PDF, it will go under the page, did you know why? – EAS Feb 14 '22 at 10:14