我想在使用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/

10-13 02:08