Java 如何在PDF文档中添加超链接

2025-11-23 06:15:49

1、首先,需要将PDF Jar包-Free Spire.PDF for Java下载至本地。解压后,找到lib文件夹下的jar文件。方便下一步手动引入jar。

2、在IDEA中打开“Project Structure”界面,执行如下图步骤,手动将本地路径下的Spire.Pdf.jar文件导入到java应用程序。

Java 如何在PDF文档中添加超链接

3、勾选选项,点击“Apply”,完成引入jar到Java程序。

Java 如何在PDF文档中添加超链接

4、引用完成后,编辑如下代码,在PDF文档中添加不同类型的超链接。

import com.spire.pdf.annotations.*;

import com.spire.pdf.graphics.*;

import com.spire.pdf.*;

import java.awt.*;

import java.awt.font.TextAttribute;

import java.awt.geom.Point2D;

import java.awt.geom.Rectangle2D;

import java.util.HashMap;

public class AddLinksToPdf {

    public static void main(String[] args) throws Exception {

        //创建PDF文档

        PdfDocument doc = new PdfDocument();

        PdfPageBase page = doc.getPages().add();

        //初始化X,Y坐标

        float y = 30;

        float x = 0;

        // 创建一个普通字体

        PdfTrueTypeFont plainFont = new PdfTrueTypeFont(new Font("Arial Unicode MS",Font.PLAIN,13),true);

        //创建一个带下划线的字体

        HashMap<TextAttribute, Object> hm = new HashMap<TextAttribute, Object>();

        hm.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);

        hm.put(TextAttribute.SIZE, 13);

        hm.put(TextAttribute.FAMILY, "Arial");

        Font font = new Font(hm);

        PdfTrueTypeFont underlineFont = new PdfTrueTypeFont(font,true);

        //添加简单链接到PDF

        String label = "简单链接: ";

        PdfStringFormat format = new PdfStringFormat();

        format.setMeasureTrailingSpaces(true);

        page.getCanvas().drawString(label, plainFont, PdfBrushes.getOrange(), 0, y,format);

        x = (float)plainFont.measureString(label,format).getWidth();

        page.getCanvas().drawString("超链接地址", underlineFont, PdfBrushes.getBlue(), x, y+2);

        y = y + 26;

        //添加超文本链接到PDF

        label= "超文本链接: ";

        page.getCanvas().drawString(label, plainFont, PdfBrushes.getOrange(), 0, y, format);

        x = (float)plainFont.measureString(label,format).getWidth();

        PdfTextWebLink webLink = new PdfTextWebLink();

        webLink.setText("超链接文本");

        webLink.setUrl("链接地址");

        webLink.setFont(plainFont);

        webLink.setBrush(PdfBrushes.getBlue());

        webLink.drawTextWebLink(page.getCanvas(), new Point2D.Float(x, y));

        y= y + 26;

        //添加邮箱链接到PDF

        label = "邮箱链接:  ";

        page.getCanvas().drawString(label, plainFont, PdfBrushes.getOrange(), 0, y, format);

        x = (float)plainFont.measureString(label, format).getWidth();

        webLink = new PdfTextWebLink();

        webLink.setText("联系我们");

        webLink.setUrl("邮件地址");

        webLink.setFont(plainFont);

        webLink.setBrush(PdfBrushes.getBlue());

        webLink.drawTextWebLink(page.getCanvas(), new Point2D.Float(x, y));

        y = y + 26;

        //添加文档链接到PDF

        label = "文档链接: ";

        page.getCanvas().drawString(label, plainFont, PdfBrushes.getOrange(), 0, y, format);

        x = (float)plainFont.measureString(label, format).getWidth();

        page.getCanvas().drawString("详情参阅原文件", plainFont, PdfBrushes.getBlue(), x, y, format);

        Rectangle2D rect = new Rectangle2D.Float(x,y+2,60,15);

        PdfFileLinkAnnotation fileLinkAnnotation = new PdfFileLinkAnnotation(rect,"C:\\Users\\Administrator\\Desktop\\测试文件.docx");

        fileLinkAnnotation.setBorder(new PdfAnnotationBorder(0f));

        ((PdfNewPage) ((page instanceof PdfNewPage) ? page : null)).getAnnotations().add(fileLinkAnnotation);

        //保存文档

        doc.saveToFile("result.pdf");

        doc.close();

    }

}

5、执行以上代码,生成PDF文档。可在生成的文档中查看创建的超链接效果。

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢