Java 操作PDF中的超連結——新增、更新、刪除超連結
對特定元素新增超連結後,使用者可以通過點選被連結的元素來啟用這些連結,通常在被連結的元素下帶有下劃線或者以不同的顏色顯示來進行區分。按照使用物件的不同,連結又可以分為:文字超連結,影像超連結,E-mail連結,錨點連結,多媒體檔案連結,空連結等多種連結,本篇文章中將介紹在PDF中新增幾種不同型別超連結的方法,以及如何更新和刪除PDF中已有的超連結。文章將從以下三個程式碼實力展示如何來實現超連結的相關操作
-
新增超連結( 普通連結、 超文字連結、 郵箱連結、 文件連結)
-
更新超連結
-
刪除超連結
【匯入jar】
本次程式碼中使用Free Spire.PDF for Java。
可按照如下方法來匯入Spire.Pdf.jar 版本:5.1.0
方法1:將Free Spire.PDF for Java 包下載到本地,解壓,找到lib資料夾下的Spire.Pdf.jar檔案。然後在IDEA中開啟“Project Structure”介面,然後執行如圖步驟來手動匯入本地路徑下的jar檔案:
方法2:通過Maven倉庫下載匯入,如下配置pom.xml:
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.pdf.free</artifactId> <version>5.1.0</version> </dependency> </dependencies>
【程式碼示例】
1. 新增超連結
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("(), 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("); 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(); } }
2. 更新超連結
下面是實現更新超連結的程式碼步驟:
-
建立 PdfDocument類的物件。
-
呼叫 PdfDocument.loadFromFile(String fileName)方法載入PDF文件。
-
通過 PdfDocument.getPages().get(int index)方法獲取指定頁面。
-
通過 PdfPageBase.getAnnotationWidget()方法獲取所有超連結集合。
-
使用 PdfUriAnnotationWidget.setUri(String value)方法設定新的超連結地址。
-
最後,通過 PdfDocument.saveToFile(String filename, FileFormat fileFormat)方法儲存文件到指定路徑。
Java
import com.spire.pdf.*; import com.spire.pdf.annotations.PdfAnnotationCollection; import com.spire.pdf.annotations.PdfUriAnnotationWidget; public class UpdateHyperlink { public static void main(String[] args) throws Exception{ //載入PDF文件 PdfDocument pdf = new PdfDocument(); pdf.loadFromFile("test.pdf"); //獲取PDF中的指定頁面 PdfPageBase page = pdf.getPages().get(0); //獲取超連結,更改連結地址 PdfAnnotationCollection widgetCollection = page.getAnnotationsWidget(); PdfUriAnnotationWidget uri = (PdfUriAnnotationWidget) widgetCollection.get(0); uri.setUri("); //儲存文件 pdf.saveToFile("UpdateHyperlinks.pdf"); pdf.dispose(); } }
3. 刪除超連結
刪除超連結時,可通過 PdfAnnotationCollection.removeAt(int index)方法刪除指定的超連結,或者通過 PdfAnnotationCollection.clear()方法刪除文件中的所有超連結。下面是刪除超連結的程式碼步驟:
-
建立 PdfDocument類的物件。
-
呼叫 PdfDocument.loadFromFile(String fileName)方法載入PDF文件。
-
通過 PdfDocument.getPages().get(int index)方法獲取指定頁面。
-
通過 PdfPageBase.getAnnotationWidget()方法獲取所有超連結集合。
-
通過 PdfAnnotationCollection.removeAt(int index)方法刪除指定超連結。
-
通過 PdfAnnotationCollection.clear()方法刪除所有超連結。
-
最後,通過PdfDocument.saveToFile(String filename, FileFormat fileFormat)方法儲存文件到指定路徑。
Java
import com.spire.pdf.*; import com.spire.pdf.annotations.*; public class RemoveHyperlinks { public static void main(String[] args) { //載入PDF PdfDocument pdf = new PdfDocument(); pdf.loadFromFile("test.pdf"); //獲取PDF中的指定頁面 PdfPageBase page = pdf.getPages().get(0); //獲取所有的PDF 超連結集合 PdfAnnotationCollection widgetCollection = page.getAnnotationsWidget(); widgetCollection.removeAt(1);//刪除第2個超連結 //widgetCollection.clear();//刪除所有超連結 //儲存文件 pdf.saveToFile("RemoveHyperlinks.pdf"); pdf.dispose(); } }
—END—
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31499788/viewspace-2899646/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Java 8 新特性
- java版工程管理系統Spring Cloud+Spring Boot+Mybatis實現工程管理系統原始碼
- Java 版 Spring Cloud+Spring Boot+Mybatis 電子招標採購系統功能清單
- 【Java併發程式設計】Synchronized關鍵字實現原理
- 使用 JavaScript 開發AR(擴增實境)移動應用的預備知識和環境搭建
- php實現pdf轉圖片
- 百問百答第41期:應用效能探針監測原理-Java探針
- Java新增條形碼到PDF表格
- Java隨談(六)## 我們真的理解 Java 裡的整型嗎?
- 使用JavaCV實現讀取視訊資訊及自動擷取封面圖
- BatchOutput PDF for Mac(PDF批量列印工具)
- java程式設計師學歷重要嗎?
- 我常用的兩個翻譯神器!程式設計師必備 | JavaGuide
- 面試官:說說執行 JavaScript 的 V8 引擎做了什麼?
- Java大型工程專案管理系統原始碼,原生APP原始碼
- Java 基礎常見知識點&面試題總結(上),2022 最新版!| JavaGuide
- C#和Java,究竟選哪個方向?我只說事實,你自己分析……
- 利用Github Action實現Tornadofx/JavaFx打包