无法打开使用 PDFBox 生成的 PDF 中的嵌入式超链接
Cannot open embedded hyperlink in PDF generated using PDFBox
我使用 PDFBox 2.0 版生成了包含可点击 URL 的 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 查看器中打开。 link 是可点击的,没问题。
如果我在 Firefox (41.0.1) 或 IE 11 中单击生成的 PDF,文档将加载到 Adobe PDF 查看器插件中并且 link 不可单击。鼠标悬停显示正确的 URL,但是当我单击 link.
时没有任何反应
这是安全问题吗?我可以在 PDFBox 代码中做些什么来使 link 始终可点击吗?
我可以通过将宽度设置为 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);
我使用 PDFBox 2.0 版生成了包含可点击 URL 的 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 查看器中打开。 link 是可点击的,没问题。
如果我在 Firefox (41.0.1) 或 IE 11 中单击生成的 PDF,文档将加载到 Adobe PDF 查看器插件中并且 link 不可单击。鼠标悬停显示正确的 URL,但是当我单击 link.
时没有任何反应这是安全问题吗?我可以在 PDFBox 代码中做些什么来使 link 始终可点击吗?
我可以通过将宽度设置为 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);