如何使用pdfbox在内容中设置超链接

how to set hyperlink in content using pdfbox

在下面的代码中,我想在 "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 时应该重定向

尝试此代码也有效,

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);