Search code examples
pdfboxacrobat

Cannot open embedded hyperlink in PDF generated using PDFBox


I have used PDFBox version 2.0 to generated a PDF containing a clickable URL.

// Create a new annotation and make it invisible
PDAnnotationLink txtLink = new PDAnnotationLink();
txtLink.setInvisible(true);

// Add an action
PDActionURI action = new PDActionURI();
action.setURI(url);
txtLink.setAction(action);

// Create a new rectangle that will be the clickable area
PDRectangle position = new PDRectangle();
position.setLowerLeftX(currentXpos);
position.setLowerLeftY(currentYpos - rectangleHeight);
position.setUpperRightX(currentXpos + rectangleWidth);
position.setUpperRightY(currentYpos);

 // Write the "Link" string in blue 
contentStream.setNonStrokingColor(Color.blue);
contentStream.showText(elm.text());
contentStream.setNonStrokingColor(Color.black);

// Make the rectangle a clickable link and add it to the page
txtLink.setRectangle(position);
page.getAnnotations().add(txtLink);

When I click on the generated PDF in Chrome 45, the document is opened in the Chrome's PDF viewer. The link is clickable, no problem.

If I click on the generated PDF in Firefox (41.0.1) or IE 11, the document is loaded in the Adobe PDF viewer plugin and the link is not clickable. The mouse-over displays the correct URL, but nothing happens when I click the link.

Is this a security issue? Is there anything I can do in the PDFBox code to make the link clickable always?


Solution

  • I was able to hide the border by setting the width to 0:

    // Create a new annotation and make it visible
    PDAnnotationLink txtLink = new PDAnnotationLink();
    txtLink.setInvisible(false);
    
    // Set the border to zero to hide it
    PDBorderStyleDictionary border = new PDBorderStyleDictionary();
    border.setWidth(0);
    
    txtLink.setBorderStyle(border);