我使用PDFBox 2.0版生成包含可点击网址的PDF。
// 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);
当我在Chrome 45中点击生成的PDF时,文档会在Chrome的PDF查看器中打开。链接是可点击的,没问题。
如果我在Firefox(41.0.1)或IE 11中单击生成的PDF,则文档将加载到Adobe PDF查看器插件中,并且该链接无法单击。鼠标悬停显示正确的URL,但单击链接时没有任何反应。
这是安全问题吗?我可以在PDFBox代码中做些什么来使链接始终可以点击吗?
答案 0 :(得分:1)
我可以通过将宽度设置为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);