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

 

(本文完)