Java 在PDF 中添加超连接

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

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

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

Jar文件导入:web

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

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:添加简单连接到PDFui

//添加简单连接到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:添加超文本连接到PDFspa

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 code

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

 

(本文完)

相关文章
相关标签/搜索