【问题标题】:how to set hyperlink in content using pdfbox如何使用pdfbox在内容中设置超链接
【发布时间】:2018-05-03 03:25:27
【问题描述】:

在下面的代码中,我想在“Google.com”中添加超链接

contentStream.beginText();
contentStream.setNonStrokingColor(0,0,0);
contentStream.setFont(PDType1Font.TIMES_ROMAN, 8);
contentStream.newLineAtOffset(315, 220);
contentStream.showText("Website: google.com");
contentStream.endText();

我想在 google.com 中显示超链接,点击 google.com 时它应该重定向

【问题讨论】:

  • 您还需要在此处添加链接注释。或者显示完整的 url,一些查看者在文本中搜索 URL 并将它们视为链接。
  • 请在源代码下载中的AddAnnotations.java示例链接
  • 哎呀,我的意思是在 AddAnnotations.java 示例中写“看”。
  • PDAnnotationLink txtLink = new PDAnnotationLink(); PDBorderStyleDictionary 下划线 = new PDBorderStyleDictionary(); underline.setStyle(PDBorderStyleDictionary.STYLE_UNDERLINE); txtLink.setBorderStyle(下划线); PDRectangle 位置 = 新 PDRectangle();位置.setLowerLeftX(325);位置.setLowerLeftY(210);位置.setUpperRightX(350);位置.setUpperRightY(220); txtLink.setRectangle(位置); PDActionURI 动作 = 新的 PDActionURI(); action.setURI("Google.com"); txtLink.setAction(action); page.getAnnotations().add(txtLink);
  • 在下面添加代码并在链接不正确时使用它然后设置 PDRectanlge 对齐

标签: java servlets pdfbox


【解决方案1】:

试试这个代码也可以,

contentStream.beginText();
contentStream.setNonStrokingColor(0,0,0);
contentStream.setFont(PDType1Font.TIMES_ROMAN, 8);
contentStream.newLineAtOffset(50,220);
contentStream.showText("Website: google.com");
contentStream.endText();

// create a link annotation
PDAnnotationLink txtLink = new PDAnnotationLink();

// add an underline
PDBorderStyleDictionary underline = new PDBorderStyleDictionary();
underline.setStyle(PDBorderStyleDictionary.STYLE_UNDERLINE);
txtLink.setBorderStyle(underline);

PDRectangle position = new PDRectangle();
position.setLowerLeftX(75);
position.setLowerLeftY(210);
position.setUpperRightX(85);
position.setUpperRightY(220);
txtLink.setRectangle(position);

PDActionURI action = new PDActionURI();
action.setURI("http://www.Google.com");
txtLink.setAction(action);

page.getAnnotations().add(txtLink);

【讨论】:

    猜你喜欢
    • 2014-01-28
    • 2016-11-29
    • 1970-01-01
    • 2018-12-01
    • 1970-01-01
    • 2012-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多