Neha Singh
Neha Singh

Reputation: 345

How to add hyperlink in pdf using pdfbox

I want to add a hyperlink in PDF created using PDFBOX, such that i click on some text example 'Click here' will redirect to URL. I tried using PDAnnotationLink and PDActionURI, but how to add it in contentstream?

PDBorderStyleDictionary borderULine = new PDBorderStyleDictionary();
borderULine.setStyle(PDBorderStyleDictionary.STYLE_UNDERLINE);
PDAnnotationLink txtLink = new PDAnnotationLink();
txtLink.setBorderStyle(borderULine);
txtLink.setColour(colourBlue);

// add an action
PDActionURI action = new PDActionURI();
action.setURI("www.google.com");
txtLink.setAction(action);

contentStream.beginText();
contentStream.moveTextPositionByAmount(400, y-30);
contentStream.drawString(txtLink);----error
contentStream.endText();

Upvotes: 18

Views: 13410

Answers (4)

Vlado Lovis
Vlado Lovis

Reputation: 13

If anyone has followed the answers on this question and is still strugling to get the links working, make sure to avoid my mistake...
From my testing, the link annotation will not work if the URI String doesn't start with one of those prefixes https://, http:// or www.


This behaviour was observed on PDFBox 3.0.3.
Edit: This behavirour is PDF reader dependent, in my case I was using Firefox 134.0 (64-bit). Thanks to K J for pointing this out!

Upvotes: 0

Philip Helger
Philip Helger

Reputation: 1864

This is a fully working example, tested with PDFBox 2.0.19:

import java.awt.Color;
import java.io.File;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.graphics.color.PDColor;
import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceRGB;
import org.apache.pdfbox.pdmodel.interactive.action.PDActionURI;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary;

public class MainCreateHyerLink
{
  public static void main (final String [] args) throws Exception
  {
    try (final PDDocument doc = new PDDocument ())
    {
      final PDPage page = new PDPage (new PDRectangle (250, 150));
      doc.addPage (page);

      try (final PDPageContentStream contentStream = new PDPageContentStream (doc, page))
      {
        final PDAnnotationLink txtLink = new PDAnnotationLink ();

        // border style
        final PDBorderStyleDictionary linkBorder = new PDBorderStyleDictionary ();
        linkBorder.setStyle (PDBorderStyleDictionary.STYLE_UNDERLINE);
        linkBorder.setWidth (10);
        txtLink.setBorderStyle (linkBorder);

        // Border color
        final Color color = Color.GREEN;
        final float [] components = new float [] { color.getRed () / 255f, color.getGreen () / 255f, color.getBlue () / 255f };
        txtLink.setColor (new PDColor (components, PDDeviceRGB.INSTANCE));

        // Destination URI
        final PDActionURI action = new PDActionURI ();
        action.setURI ("https://www.helger.com");
        txtLink.setAction (action);

        // Position
        final PDRectangle position = new PDRectangle ();
        position.setLowerLeftX (10);
        position.setLowerLeftY (10);
        position.setUpperRightX (200);
        position.setUpperRightY (10 + 2 + 10 + 2);
        txtLink.setRectangle (position);
        page.getAnnotations ().add (txtLink);

        // Main page content
        contentStream.beginText ();
        contentStream.newLineAtOffset (12, 12);
        contentStream.setFont (PDType1Font.COURIER_BOLD, 10);
        contentStream.showText ("This is linked to the outside world");
        contentStream.endText ();
      }
    }
  }
}

Upvotes: 6

Enwired
Enwired

Reputation: 1593

There is a library called PDFBox-Layout which makes it easier to add hyperlinks:

Document document = new Document();

Paragraph paragraph = new Paragraph();
paragraph.addMarkup(
   "This is a link to {link[https://github.com/ralfstuckert/pdfbox-layout]}PDFBox-Layout{link}",
 18f, BaseFont.Helvetica);
document.add(paragraph);

final OutputStream outputStream = new FileOutputStream("link.pdf");
document.save(outputStream);

Upvotes: 4

SANN3
SANN3

Reputation: 10069

To add to contentStream use the following code

    PDRectangle position = new PDRectangle();
    position.setLowerLeftX(10);
    position.setLowerLeftY(20); 
    position.setUpperRightX(100); 
    position.setUpperRightY(10); 
    txtLink.setRectangle(position);
    page.getAnnotations().add(txtLink);

Upvotes: 11

Related Questions