gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:54:56 27 4
gpt4 key购买 nike

我想在使用 PDFBOX 创建的 PDF 中添加一个超链接,这样我单击某些文本示例“单击此处”将重定向到 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);

关于java - 如何使用pdfbox在pdf中添加超链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21021502/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com