【问题标题】:Cannot open embedded hyperlink in PDF generated using PDFBox无法在使用 PDFBox 生成的 PDF 中打开嵌入的超链接
【发布时间】:2015-10-07 10:08:40
【问题描述】:

我使用 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 查看器中打开。链接可以点击,没问题。

如果我在 Firefox (41.0.1) 或 IE 11 中单击生成的 PDF,文档将加载到 Adob​​e PDF 查看器插件中,并且无法单击链接。鼠标悬停显示正确的 URL,但当我单击链接时没有任何反应。

这是一个安全问题吗?我可以在 PDFBox 代码中做些什么来使链接始终可点击吗?

【问题讨论】:

  • 在 Acrobat/Reader 中打开文件时会得到什么结果?链接在 Acrobat 的链接工具中的外观如何?
  • 如果我下载 PDF 文件并在 Acrobat Reader 中打开它,鼠标悬停会显示正确的 URL,但是当我单击它时没有任何反应。
  • 更正,Adobe Reader。我无权访问 Adob​​e Acrobat。
  • 在玩了一会儿代码后,我发现如果可点击区域矩形设置为不可见,那么在某些情况下它是不可点击的。 txtLink.setInvisible(false);设置为false后,链接在Adobe Reader中是可点击的。
  • 太好了,问题解决了。在我看来,您在 PDFBox 中发现了一个(恕我直言)讨厌的错误,因为在可见性属性方面,活动区域及其边界矩形是独立的。也就是说,隐藏边框一定不能隐藏活动区域。如果您不想在活动区域​​周围显示边框,解决方法是,如果您可以访问它,将边框的宽度设置为 0。

标签: pdfbox acrobat


【解决方案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);

【讨论】:

  • 这是对这个问题的回答吗?
猜你喜欢
  • 2013-01-03
  • 2016-05-07
  • 2014-01-28
  • 2014-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多