如何使用pdfbox在pdf中添加超链接

我想在使用创建的PDF中添加超链接PDFBOX,这样我单击一些文本示例,“单击此处”将重定向到URL。我尝试使用PDAnnotationLinkPDActionURI,但是如何添加呢contentstream

PDBorderStyleDictionary borderULine = new PDBorderStyleDictionary();

borderULine.setStyle(PDBorderStyleDictionary.STYLE_UNDERLINE);

PDAnnotationLink txtLink = new PDAnnotationLink();

txtLink.setBorderStyle(borderULine);

txtLink.setColour(colourBlue);

// add an action

PDActionURI action = new PDActionURI();

action.setURI("www.google.com");

txtLink.setAction(action);

contentStream.beginText();

contentStream.moveTextPositionByAmount(400, y-30);

contentStream.drawString(txtLink);----error

contentStream.endText();

回答:

要添加contentStream使用以下代码

    PDRectangle position = new PDRectangle();

position.setLowerLeftX(10);

position.setLowerLeftY(20);

position.setUpperRightX(100);

position.setUpperRightY(10);

txtLink.setRectangle(position);

page.getAnnotations().add(txtLink);

以上是 如何使用pdfbox在pdf中添加超链接 的全部内容, 来源链接: utcz.com/qa/399714.html

回到顶部