
- iText 教程
- iText - 首頁
- iText 畫布
- iText - 繪製圓弧
- iText - 繪製線條
- iText - 繪製圓形
- iText 有用資源
- iText - 快速指南
- iText - 有用資源
- iText - 討論
iText - 連結註釋
在本章中,我們將瞭解如何使用 iText 庫向 PDF 文件新增連結註釋。
在 PDF 中建立連結註釋
您可以透過例項化 **Document** 類來建立一個空的 PDF 文件。在例項化此類時,您需要將 **PdfDocument** 物件作為引數傳遞給其建構函式。
要在您的 PDF 文件中使用文字註釋,您需要建立一個 PdfTextAnnotation 類的物件,並將其新增到 PdfPage 中。
以下是 PDF 文件中使用文字註釋的步驟。
步驟 1:建立 PdfWriter 物件
**PdfWriter** 類表示 PDF 的 DocWriter。此類屬於 **com.itextpdf.kernel.pdf** 包。此類的建構函式接受一個字串,表示要建立 PDF 檔案的路徑。
透過將字串值(表示您需要在其中建立 PDF 的路徑)傳遞給其建構函式來例項化 **PdfWriter** 類,如下所示。
// Creating a PdfWriter String dest = "C:/itextExamples/linkAnnotation.pdf"; PdfWriter writer = new PdfWriter(dest);
當此型別的物件傳遞給 PdfDocument(類)時,新增到此文件中的每個元素都將寫入指定的檔案。
步驟 2:建立 PdfDocument 物件
**PdfDocument** 類是表示 iText 中 PDF 文件的類。此類屬於 **com.itextpdf.kernel.pdf** 包。要例項化此類(在寫入模式下),您需要將 **PdfWriter** 類的物件傳遞給其建構函式。
透過將 **PdfWriter** 物件傳遞給其建構函式來例項化 PdfDocument 類,如下所示。
// Creating a PdfDocument PdfDocument pdfDoc = new PdfDocument(writer);
建立 PdfDocument 物件後,您可以使用其類提供的相應方法新增各種元素,例如頁面、字型、檔案附件和事件處理程式。
步驟 3:建立 Document 物件
**com.itextpdf.layout** 包的 **Document** 類是建立自包含 PDF 時的根元素。此類的一個建構函式接受 **PdfDocument** 類的物件。
透過將上一步中建立的 PdfDocument 類的物件傳遞給建構函式來例項化 **Document** 類,如下所示。
// Creating a Document Document document = new Document(pdfDoc);
步驟 4:建立 PdfAnnotation 物件
**com.itextpdf.kernel.pdf.annot** 包的 **PdfAnnotation** 類表示所有註釋的超類。
在其派生類中,**PdfLinkAnnotation** 類表示連結註釋。建立此類的物件,如下所示。
// Creating a PdfLinkAnnotation object Rectangle rect = new Rectangle(0, 0); PdfLinkAnnotation annotation = new PdfLinkAnnotation(rect);
步驟 5:設定註釋的操作
使用 **PdfLinkAnnotation** 類的 **setAction()** 方法將操作設定為註釋,如下所示。
// Setting action of the annotation PdfAction action = PdfAction.createURI("http: // www.tutorialspoint.com/"); annotation.setAction(action);
步驟 6:建立連結
透過例項化 **com.itextpdf.layout.element** 包的 **Link** 類來建立連結,如下所示。
// Creating a link Link link = new Link("Click here", annotation);
步驟 7:將連結註釋新增到段落
透過例項化 **Paragraph** 類建立一個新段落,並使用此類的 **add()** 方法新增上一步中建立的連結,如下所示。
// Creating a paragraph Paragraph paragraph = new Paragraph("Hi welcome to Tutorialspoint "); // Adding link to paragraph paragraph.add(link.setUnderline());
步驟 8:將段落新增到文件
使用 **Document** 類的 **add()** 方法將段落新增到文件,如下所示。
// Adding paragraph to document document.add(paragraph);
步驟 9:關閉文件
使用 **Document** 類的 **close()** 方法關閉文件,如下所示。
// Closing the document document.close();
示例
以下 Java 程式演示瞭如何使用 iText 庫向 PDF 文件新增連結註釋。
它建立一個名為 **linkAnnotation.pdf** 的 PDF 文件,向其中新增連結註釋,並將其儲存在 **C:/itextExamples/** 路徑中。
將此程式碼儲存在名為 **LinkAnnotation.java** 的檔案中。
import com.itextpdf.kernel.geom.Rectangle; import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.kernel.pdf.action.PdfAction; import com.itextpdf.kernel.pdf.annot.PdfLinkAnnotation; import com.itextpdf.layout.Document; import com.itextpdf.layout.element.Link; import com.itextpdf.layout.element.Paragraph; public class LinkAnnotation { public static void main(String args[]) throws Exception { // Creating a PdfWriter String dest = "C:/itextExamples/linkAnnotation.pdf"; PdfWriter writer = new PdfWriter(dest); // Creating a PdfDocument PdfDocument pdf = new PdfDocument(writer); // Creating a Document Document document = new Document(pdf); // Creating a PdfLinkAnnotation object Rectangle rect = new Rectangle(0, 0); PdfLinkAnnotation annotation = new PdfLinkAnnotation(rect); // Setting action of the annotation PdfAction action = PdfAction.createURI("http:// www.tutorialspoint.com/"); annotation.setAction(action); // Creating a link Link link = new Link("Click here", annotation); // Creating a paragraph Paragraph paragraph = new Paragraph("Hi welcome to Tutorialspoint "); // Adding link to paragraph paragraph.add(link.setUnderline()); // Adding paragraph to document document.add(paragraph); // Closing the document document.close(); System.out.println("Annotation added successfully"); } }
使用以下命令從命令提示符編譯並執行儲存的 Java 檔案:
javac LinkAnnotation.java java LinkAnnotation
執行後,上述程式將建立一個 PDF 文件,顯示以下訊息。
Annotation added successfully
如果驗證指定的路徑,則可以找到建立的 PDF 文件,如下所示。
