【问题标题】:How to add hyperlink in pdf using pdfbox如何使用pdfbox在pdf中添加超链接
【发布时间】:2014-01-28 01:54:28
【问题描述】:

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

【问题讨论】:

  • 知道如何在链接中添加“单击此处”吗?

标签: java pdf pdf-generation pdfbox


【解决方案1】:

要添加到contentStream,请使用以下代码

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

【讨论】:

  • 这不是向内容流添加内容,而是注释不是内容流的一部分...
  • 所以如果我做对了,为了添加一个链接,您需要添加一个注释,其动作将定位在内容流保存的文本上?跨度>
【解决方案2】:

有一个名为PDFBox-Layout 的库,它使它成为easier to add 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 看来您必须将现有的 PDF 加载为背景。见github issue #69
【解决方案3】:

这是一个完整的示例,使用 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 ();
      }
    }
  }
}

【讨论】:

  • 感谢分享。缺少 2 行: contentStream.close(); doc.save(filePath);
猜你喜欢
  • 2018-12-01
  • 2017-06-20
  • 2013-10-29
  • 1970-01-01
  • 1970-01-01
  • 2015-09-15
  • 2021-02-21
  • 2015-06-23
相关资源
最近更新 更多