Java 在PDF中添加超鏈接

對特定元素添加超鏈接後,用戶可以通過點擊被鏈接的元素來激活這些鏈接,通常在被鏈接的元素下帶有下劃線或者以不同的顏色顯示來進行區分。按照使用對象的不同,鏈接又可以分爲:文本超鏈接,圖像超鏈接,E-mail鏈接,錨點鏈接,多媒體文件鏈接,空鏈接等多種鏈接,本篇文章中將介紹在PDF中添加幾種不同類型超鏈接的方法,包括:

  • 普通鏈接
  • 超文本鏈接
  • 郵箱鏈接
  • 文檔鏈接

使用工具:Free Spire.PDF for Java(免費版)
Jar文件導入:
方法1:通過官網下載文件包。下載後,解壓文件,並將lib文件夾下的Spire.Pdf.jar文件導入java程序。
方法2:可通過maven倉庫安裝導入。配置路徑及導入方法可參考教程

Java 代碼示例

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("[email protected]");
        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();
    }
}

鏈接添加結果:
Java 在PDF中添加超鏈接

(本文完)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章