Java 在PDF 中添加超链接

对特定元素添加超链接后,用户可以通过点击被链接的元素来激活这些链接,通常在被链接的元素下带有下划线或者以不同的颜色显示来进行区分。按照使用对象的不同,链接又可以分为:文本超链接,图像超链接,E-mail链接,锚点链接,多媒体文件链接,空链接等多种链接,本篇文章中将介绍在PDF中添加几种不同类型超链接的方法,包括:

  • 普通链接
  • 超文本链接
  • 邮箱链接
  • 文档链接

使用工具:Free Spire.PDF for Java 2.4.4(免费版)

Jar文件导入:

Step1在Java程序中新建一个文件夹可命名为Lib。并将下载包中的jar文件(如下图)复制到新建的文件夹下。

Step2:复制文件后,添加到引用类库:选中这个jar文件,点击鼠标右键,选择“Build Path” – “Add to Build Path”。完成引用。

C# 代码示例

步骤1:创建文档

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

步骤2:初始化坐标及字体

//初始化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);

步骤3:添加简单链接到PDF

//添加简单链接到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("https://www.baidu.com/", underlineFont, PdfBrushes.getBlue(), x, y+2);
y = y + 26;

步骤4:添加超文本链接到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("https://www.baidu.com/");
webLink.setFont(plainFont);
webLink.setBrush(PdfBrushes.getBlue());
webLink.drawTextWebLink(page.getCanvas(), new Point2D.Float(x, y));
y= y + 26;

步骤5:添加邮箱链接到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("mailto:ask@baidu.com");
webLink.setFont(plainFont);
webLink.setBrush(PdfBrushes.getBlue());
webLink.drawTextWebLink(page.getCanvas(), new Point2D.Float(x, y));
y = y + 26;

步骤6:添加文档链接到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);

步骤7:保存文档

doc.saveToFile("超链接.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("https://www.baidu.com/", 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("https://www.baidu.com/");
        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("mailto:ask@baidu.com");
        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("超链接.pdf");
        doc.close();
    }
}
View Code

 

(本文完)

 

转载于:https://www.cnblogs.com/Yesi/p/10790893.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值