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/textAnnotation.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.layoutDocument類是建立自包含 PDF 時的根元素。此類的一個建構函式接受PdfDocument類的物件。

透過將前面步驟中建立的PdfDocument類的物件傳遞給Document類的建構函式來例項化Document類,如下所示。

// Creating a Document   
Document document = new Document(pdfDoc); 

步驟 4:建立 PdfAnnotation 物件

com.itextpdf.kernel.pdf.annotPdfAnnotation類表示所有註釋的超類。

在其派生類中,PdfTextAnnotation類表示文字註釋。建立此類的物件,如下所示。

// Creating PdfAnnotation 
Rectangle rect = new Rectangle(20, 800, 0, 0); 
PdfAnnotation ann = new PdfTextAnnotation(rect);

步驟 5:設定註釋的顏色

使用PdfAnnotation類的setColor()方法設定註釋的顏色。將表示註釋顏色的color物件作為引數傳遞給此方法。

// Setting color to the annotation 
ann.setColor(Color.GREEN); 

步驟 6:設定註釋的標題和內容

分別使用PdfAnnotation類的setTitle()setContents()方法設定註釋的標題和內容,如下所示。

// Setting title to the annotation 
ann.setTitle(new PdfString("Hello"));        

// Setting contents of the annotation 
ann.setContents("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 文件新增文字註釋。它建立一個名為textAnnotation.pdf的 PDF 文件,向其中新增文字註釋,並將其儲存到C:/itextExamples/路徑。

將此程式碼儲存在名為TextAnnotation.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.PdfTextAnnotation; 
import com.itextpdf.layout.Document;  

public class TextAnnotation {    
   public static void main(String args[]) throws Exception {        
      // Creating a PdfWriter       
      String dest = "C:/itextExamples/textAnnotation.pdf";       
      PdfWriter writer = new PdfWriter(dest);               
      
      // Creating a PdfDocument       
      PdfDocument pdf = new PdfDocument(writer);               
      
      // Creating a Document        
      Document document = new Document(pdf);             
      
      // Creating PdfTextAnnotation object
      Rectangle rect = new Rectangle(20, 800, 0, 0);       
      PdfAnnotation ann = new PdfTextAnnotation(rect);              
      
      // Setting color to the annotation
      ann.setColor(Color.GREEN);              
      
      // Setting title to the annotation 
      ann.setTitle(new PdfString("Hello"));              
      
      // Setting contents of the annotation       
      ann.setContents("Hi welcome to Tutorialspoint.");              
      
      // Creating a new page       
      PdfPage page =  pdf.addNewPage();              
      
      // Adding annotation to a page in a PDF
      page.addAnnotation(ann);

      // Closing the document       
      document.close();       
      
      System.out.println("Annotation added successfully");    
   } 
}

使用以下命令從命令提示符編譯並執行儲存的 Java 檔案。

javac TextAnnotation.java 
java TextAnnotation 

執行後,上述程式將建立一個顯示以下訊息的 PDF 文件。

Annotation added successfully 

如果您驗證指定的路徑,您可以找到建立的 PDF 文件,如下所示。

Text Annotation
廣告