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/lineAnnotation.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** 類是所有標註的超類。

在其派生類中,**PdfLineAnnotation** 類表示線條標註。建立此類的物件,如下所示。

// Creating PdfAnnotation 
Rectangle rect = new Rectangle(20, 800, 0, 0); 
PdfAnnotation annotation = new PdfLineAnnotation(rect); 

步驟 5:設定標註的顏色

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

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

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

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

// Setting title to the PdfLineAnnotation 
annotation.setTitle(new PdfString("iText"));        

// Setting contents of the PdfLineAnnotation 
annotation.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(annotation); 

步驟 8:關閉文件

使用 **Document** 類的 **close()** 方法關閉文件,如下所示。

// Closing the document 
document.close(); 

示例

以下 Java 程式演示瞭如何使用 iText 庫向 PDF 文件新增線條標註。它建立一個名為 **lineAnnotation.pdf** 的 PDF 文件,向其中新增線條標註,並將其儲存在 **C:/itextExamples/** 路徑中。

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

public class LineAnnotation {
   public static void main(String args[]) throws Exception {             
      // Creating a PdfWriter       
      String dest = "C:/itextExamples/lineAnnotations.pdf";       
      PdfWriter writer = new PdfWriter(dest);               

      // Creating a PdfDocument       
      PdfDocument pdf = new PdfDocument(writer);               

      // Creating a Document        
      Document document = new Document(pdf);              
      
      // Creating a PdfPage       
      PdfPage page = pdf.addNewPage();              

      // creating PdfLineAnnotation object       
      Rectangle rect = new Rectangle(0, 0);
      float[] floatArray  = new float[]{
         20, 790, page.getPageSize().getWidth() - 20, 790
      };
      PdfAnnotation annotation = new PdfLineAnnotation(rect, floatArray);              
      
      // Setting color of the PdfLineAnnotation       
      annotation.setColor(Color.BLUE);              
      
      // Setting title to the PdfLineAnnotation       
      annotation.setTitle(new PdfString("iText"));              
      
      // Setting contents of the PdfLineAnnotation       
      annotation.setContents("Hi welcome to Tutorialspoint");              
      
      // Adding annotation to the page       
      page.addAnnotation(annotation);               
      
      // Closing the document       
      document.close();              
      
      System.out.println("Annotation added successfully");   
   } 
} 

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

javac LineAnnotation.java 
java LineAnnotation 

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

Annotation added successfully

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

Line Annotation
廣告
© . All rights reserved.