
- 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/circleAnnotation.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** 類表示所有批註的超類。
在其派生類中,**PdfCircleAnnotation** 類表示圓形批註。建立此類的物件,如下所示。
// Creating a PdfCircleAnnotation object Rectangle rect = new Rectangle(150, 770, 50, 50); PdfAnnotation annotation = new PdfCircleAnnotation(rect);
步驟 5:設定批註的顏色
使用 **PdfAnnotation** 類的 **setColor()** 方法設定批註的顏色。此方法將表示批註顏色的顏色物件作為引數傳遞。
// Setting color to the annotation annotation.setColor(Color.YELLOW);
步驟 6:設定批註的標題和內容
分別使用 **PdfAnnotation** 類的 **setTitle()** 和 **setContents()** 方法設定批註的標題和內容。
// Setting title to the annotation annotation.setTitle(new PdfString("circle annotation")); // Setting contents of the annotation annotation.setContents(new PdfString("Hi welcome to Tutorialspoint"));
步驟 7:將批註新增到頁面
使用 **PdfDocument** 類的 **addNewPage()** 方法建立一個新的 **PdfPage** 類,並使用 PdfPage 類的 **addAnnotation()** 方法新增上面建立的批註,如下所示。
// Creating a new page PdfPage page = pdf.addNewPage(); // Adding annotation to a page in a PDF page.addAnnotation(ann);
步驟 8:關閉文件
使用 **Document** 類的 **close()** 方法關閉文件,如下所示。
// Closing the document document.close();
示例
以下 Java 程式演示瞭如何使用 iText 庫向 PDF 文件新增圓形批註。它建立一個名為 **circleAnnotation.pdf** 的 PDF 文件,向其中新增圓形批註,並將其儲存在 **C:/itextExamples/** 路徑中。
將此程式碼儲存在名為 **PdfCircleAnnotation.java** 的檔案中。
import com.itextpdf.kernel.color.Color; import com.itextpdf.kernel.geom.Rectangle; import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfPage; import com.itextpdf.kernel.pdf.PdfString; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.kernel.pdf.annot.PdfAnnotation; import com.itextpdf.kernel.pdf.annot.PdfCircleAnnotation; import com.itextpdf.layout.Document; public class CircleAnnotation { public static void main(String args[]) throws Exception { // Creating a PdfDocument object String file = "C:/itextExamples// circleAnnotation.pdf"; PdfDocument pdf = new PdfDocument(new PdfWriter(file)); // Creating a Document object Document doc = new Document(pdf); // Creating a PdfCircleAnnotation object Rectangle rect = new Rectangle(150, 770, 50, 50); PdfAnnotation annotation = new PdfCircleAnnotation(rect); // Setting color to the annotation annotation.setColor(Color.YELLOW); // Setting title to the annotation annotation.setTitle(new PdfString("circle annotation")); // Setting contents of the annotation annotation.setContents(new PdfString("Hi welcome to Tutorialspoint")); // Creating a new page PdfPage page = pdf.addNewPage(); // Adding annotation to a page in a PDF page.addAnnotation(annotation); // Closing the document doc.close(); System.out.println("Annotation added successfully"); } }
使用以下命令從命令提示符編譯並執行儲存的 Java 檔案:
javac PdfCircleAnnotation.java java PdfCircleAnnotation
執行後,上述程式將建立一個顯示以下訊息的 PDF 文件。
Annotation added successfully
如果驗證指定的路徑,您可以找到建立的 PDF 文件,如下所示。
