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

Java 如何使用pdfbox在pdf中添加超链接,java,pdf,pdf-generation,pdfbox,Java,Pdf,Pdf Generation,Pdfbox,我想在使用PDFBOX创建的PDF中添加一个超链接,这样我单击一些文本示例“单击此处”将重定向到URL。我尝试使用了PDAnnotationLink和PDActionURI,但是如何在contentstream中添加它呢 PDBorderStyleDictionary borderULine = new PDBorderStyleDictionary(); borderULine.setStyle(PDBorderStyleDictionary.STYLE_UNDERLINE); PDAnnot

我想在使用
PDFBOX
创建的PDF中添加一个超链接,这样我单击一些文本示例“单击此处”将重定向到URL。我尝试使用了
PDAnnotationLink
PDActionURI
,但是如何在
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 2.0.19进行了测试:

导入java.awt.Color;
导入java.io.File;
导入org.apache.pdfbox.pdmodel.PDDocument;
导入org.apache.pdfbox.pdmodel.PDPage;
导入org.apache.pdfbox.pdmodel.PDPageContentStream;
导入org.apache.pdfbox.pdmodel.common.PDRectangle;
导入org.apache.pdfbox.pdmodel.font.PDType1Font;
导入org.apache.pdfbox.pdmodel.graphics.color.PDColor;
导入org.apache.pdfbox.pdmodel.graphics.color.PDDeviceRGB;
导入org.apache.pdfbox.pdmodel.interactive.action.PDActionURI;
导入org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink;
导入org.apache.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary;
公共类MainCreateHyerLink
{
公共静态void main(最终字符串[]args)引发异常
{
尝试(最终PDDocument文档=新PDDocument())
{
最终PDPage=新PDPage(新PDPage矩形(250150));
doc.addPage(第页);
try(final PDPageContentStream contentStream=新PDPageContentStream(doc,page))
{
final PDAnnotationLink txtLink=新的PDAnnotationLink();
//边框样式
final PDBorderStyleDictionary linkBorder=新的PDBorderStyleDictionary();
linkBorder.setStyle(PDBorderStyleDictionary.STYLE_下划线);
linkBorder.setWidth(10);
txtLink.setBorderStyle(linkBorder);
//边框颜色
最终颜色=Color.GREEN;
最终浮点[]组件=新浮点[]{color.getRed()/255f,color.getGreen()/255f,color.getBlue()/255f};
setColor(新的PDColor(components,PDDeviceRGB.INSTANCE));
//目标URI
final PDActionURI action=new PDActionURI();
action.setURI(“https://www.helger.com");
txtLink.setAction(动作);
//位置
最终PDRectangle位置=新PDRectangle();
位置.setLowerLeftX(10);
位置设置下限(10);
position.setUpperRightX(200);
位置设置右侧(10+2+10+2);
txtLink.setRectangle(位置);
page.getAnnotations().add(txtLink);
//主页内容
contentStream.beginText();
contentStream.newlineatofset(12,12);
contentStream.setFont(PDType1Font.COURIER_粗体,10);
contentStream.showText(“这是链接到外部世界的”);
contentStream.endText();
}
}
}
}

知道如何将“单击此处”添加到链接中吗?这不是在内容流中添加内容,但批注不是内容流的一部分……因此,如果我没有弄错,为了添加链接,您需要添加批注,使用将定位在内容流所包含文本上的操作?如何将链接添加到现有pdf文件?使用“PDDocument”对象。@NicuVlad您似乎必须加载现有的PDF作为背景。看见
Document document = new Document();

Paragraph paragraph = new Paragraph();
paragraph.addMarkup(
   "This is a link to {link[https://github.com/ralfstuckert/pdfbox-layout]}PDFBox-Layout{link}",
 18f, BaseFont.Helvetica);
document.add(paragraph);

final OutputStream outputStream = new FileOutputStream("link.pdf");
document.save(outputStream);