Search code examples
javaandroidpdfhyperlinkitext

Create a link in Existing PDF Using iText 5.5.13.2


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

    }

Solution

  • 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();
        }
    }