對特定元素添加超鏈接后,用戶可以通過點擊被鏈接的元素來激活這些鏈接,通常在被鏈接的元素下帶有下划線或者以不同的顏色顯示來進行區分。按照使用對象的不同,鏈接又可以分為:文本超鏈接,圖像超鏈接,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(); } }
(本文完)