Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
like image 773
Neha Singh Avatar asked Jan 09 '14 13:01

Neha Singh


1 Answers

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);
like image 167
SANN3 Avatar answered Oct 18 '22 04:10

SANN3