翻譯|使用教程|編輯:張瑩心|2021-11-15 10:12:58.820|閱讀 541 次
概述:超鏈接是指鏈接到另一個文件或對象的圖標、圖形或文本。它是操作文檔最常用的功能之一。Spire.PDF for Java支持新建PDF 文檔并添加各種超鏈接,包括普通鏈接、超文本鏈接、電子郵件鏈接和文檔鏈接。本文將向您展示如何向現有 PDF 中的特定文本添加超鏈接。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
Spire.PDF for Java 是一款專門對 PDF 文檔進行操作的 Java 類庫。該類庫的主要功能在于幫助開發人員在 Java 應用程序(J2SE和J2EE)中生成 PDF 文檔和操作現有 PDF 文檔,并且運行環境無需安裝 Adobe Acrobat。
超鏈接是指鏈接到另一個文件或對象的圖標、圖形或文本。它是操作文檔最常用的功能之一。Spire.PDF for Java支持新建PDF 文檔并添加各種超鏈接,包括普通鏈接、超文本鏈接、電子郵件鏈接和文檔鏈接。本文將向您展示如何向現有 PDF 中的特定文本添加超鏈接。
可點擊此處下載最新版測試。
為 Java 安裝 Spire.PDF
首先,您需要在 Java 程序中添加 Spire.PDF.jar 文件作為依賴項。JAR 文件可以從此鏈接下載。如果您使用 Maven,則可以通過將以下代碼添加到項目的 pom.xml 文件來輕松導入 JAR 文件。
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>//repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.pdf</artifactId> <version>4.11.1</version> </dependency> </dependencies>
在 PDF 中查找文本并為其添加超鏈接
使用 Spire PDF for Java,您可以在特定的 PDF 頁面中找到所有匹配的文本,并為它們添加超鏈接。以下是要遵循的詳細步驟。
import com.spire.pdf.*; import com.spire.pdf.annotations.*; import com.spire.pdf.general.find.*; import com.spire.pdf.graphics.PdfRGBColor; import java.awt.*; public class SearchTextAndAddHyperlink { public static void main(String[] args) { //Create a PdfDocument instance PdfDocument pdf = new PdfDocument(); //Load a sample PDF document pdf.loadFromFile("C:\\Users\\Test1\\Desktop\\sample.pdf"); //Get the first page PdfPageBase page = pdf.getPages().get(0); // Find all matched strings and return a PdfTextFindCollection oject PdfTextFindCollection collection = page.findText("Spire.PDF for Java", false); //loop through the find collection for(PdfTextFind find : collection.getFinds()) { // Create a PdfUriAnnotation instance to add hyperlinks for the searched text PdfUriAnnotation uri = new PdfUriAnnotation(find.getBounds()); uri.setUri("http://www.e-iceblue.com/Introduce/pdf-for-java.html"); uri.setBorder(new PdfAnnotationBorder(1f)); uri.setColor(new PdfRGBColor(Color.blue)); page.getAnnotationsWidget().add(uri); } //Save the document pdf.saveToFile("output/searchTextAndAddHyperlink.pdf"); } }
整合所有格式API處理套包Spire.office for Java正在慧都網火熱銷售中!立馬1分鐘了解全部咨詢!
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn