如何使用PDFBox在PDF中添加超链接

18

我想在使用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();

如何将“点击此处”添加到链接中? - decal
只是一点小提示,如果我们将文本“https://www.google.com”添加到内容流中,它将自动被视为链接,并且在PDF内可点击。可能对某些人有用。 - Alfaz Jikani
3个回答

11

要向 contentStream 添加内容,请使用以下代码

    PDRectangle position = new PDRectangle();
    position.setLowerLeftX(10);
    position.setLowerLeftY(20); 
    position.setUpperRightX(100); 
    position.setUpperRightY(10); 
    txtLink.setRectangle(position);
    page.getAnnotations().add(txtLink);

这不是将某些内容添加到内容流中,但注释不是内容流的一部分... - fabian
所以如果我理解正确的话,为了添加链接,您需要添加一个注释,带有一个动作,该动作将定位于内容流中保存的文本上? - AElMehdi

5
这是一个完全可运行的示例,已使用PDFBox 2.0.19进行了测试:
import java.awt.Color;
import java.io.File;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.graphics.color.PDColor;
import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceRGB;
import org.apache.pdfbox.pdmodel.interactive.action.PDActionURI;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary;

public class MainCreateHyerLink
{
  public static void main (final String [] args) throws Exception
  {
    try (final PDDocument doc = new PDDocument ())
    {
      final PDPage page = new PDPage (new PDRectangle (250, 150));
      doc.addPage (page);

      try (final PDPageContentStream contentStream = new PDPageContentStream (doc, page))
      {
        final PDAnnotationLink txtLink = new PDAnnotationLink ();

        // border style
        final PDBorderStyleDictionary linkBorder = new PDBorderStyleDictionary ();
        linkBorder.setStyle (PDBorderStyleDictionary.STYLE_UNDERLINE);
        linkBorder.setWidth (10);
        txtLink.setBorderStyle (linkBorder);

        // Border color
        final Color color = Color.GREEN;
        final float [] components = new float [] { color.getRed () / 255f, color.getGreen () / 255f, color.getBlue () / 255f };
        txtLink.setColor (new PDColor (components, PDDeviceRGB.INSTANCE));

        // Destination URI
        final PDActionURI action = new PDActionURI ();
        action.setURI ("https://www.helger.com");
        txtLink.setAction (action);

        // Position
        final PDRectangle position = new PDRectangle ();
        position.setLowerLeftX (10);
        position.setLowerLeftY (10);
        position.setUpperRightX (200);
        position.setUpperRightY (10 + 2 + 10 + 2);
        txtLink.setRectangle (position);
        page.getAnnotations ().add (txtLink);

        // Main page content
        contentStream.beginText ();
        contentStream.newLineAtOffset (12, 12);
        contentStream.setFont (PDType1Font.COURIER_BOLD, 10);
        contentStream.showText ("This is linked to the outside world");
        contentStream.endText ();
      }
    }
  }
}

1
感谢分享。缺少两行代码: contentStream.close(); doc.save(filePath); - user10053673
1
PDPageContentStream 实现了 AutoClosable 接口,因此在 try-with-resources 块中自动调用了 contentStream.close(); - ryvantage

4

有一个名为 PDFBox-Layout 的库,它可以使添加超链接更加容易:(Hyperlinks 例子)

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

如何将链接添加到现有的PDF文件中?使用“PDDocument”对象。 - NicuVlad
@NicuVlad 看起来你需要将现有的PDF作为背景加载。请参见[github问题#69](https://github.com/ralfstuckert/pdfbox-layout/issues/69) - luckydonald
这个应该已经在官方图书馆上了吧?它已经有多次更新了... - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接