iText - 向表格新增影像



在本章中,我們將瞭解如何使用 iText 庫將影像新增到 PDF 文件中的表格。

向表格新增影像

您可以透過例項化 **Document** 類來建立一個空的 PDF 文件。在例項化此類時,您需要將 **PdfDocument** 物件作為引數傳遞給它的建構函式。然後,要向文件新增表格,您需要例項化 **Table** 類並將此物件使用 **add()** 方法新增到文件中。

要向此表格新增影像,您需要例項化 **Cell** 類,建立要新增的影像物件,並使用 **Cell** 類的 **add()** 方法將影像新增到 **cell** 物件中。

以下是將影像插入表格單元格的步驟。

步驟 1:建立 PdfWriter 物件

**PdfWriter** 類表示 PDF 的文件寫入器,此類屬於 **com.itextpdf.kernel.pdf** 包。此類的建構函式接受一個字串,表示要建立 PDF 檔案的路徑。

透過將表示您需要建立 PDF 的路徑的字串值作為引數傳遞給其建構函式,例項化 PdfWriter 類,如下所示。

// Creating a PdfWriter 
String dest = "C:/itextExamples/addingImage.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:建立 Table 物件

**Table** 類表示一個由單元格填充的二維網格,按行和列排序。它屬於 **com.itextpdf.layout.element** 包。

如下所示例項化 **Table** 類。

// Creating a table 
float [] pointColumnWidths = {200F, 200F}; 
Table table = new Table(pointColumnWidths); 

步驟 5:建立單元格

透過例項化 **com.itextpdf.layout** 包的 **Cell** 類建立 **cell** 物件,如下所示。

// Adding cell to the table
Cell cell = new Cell();  // Creating a cell 

步驟 6:建立影像

要建立 **image** 物件,首先使用 **ImageDataFactory** 類的 **create()** 方法建立一個 **ImageData** 物件。作為此方法的引數,傳遞一個表示影像路徑的字串引數,如下所示。

// Creating an ImageData object 
String imageFile = "C:/itextExamples/javafxLogo.jpg"; 
ImageData data = ImageDataFactory.create(imageFile); 

現在,例項化 **com.itextpdf.layout.element** 包的 **Image** 類。在例項化時,將上面建立的 **ImageData** 物件作為引數傳遞給它的建構函式,如下所示。

// Creating an Image object 
Image img = new Image(data); 

使用單元格類的 **add()** 方法將 **image** 物件新增到單元格中,如下所示。

// Adding image to the cell  
cell.add(img.setAutoScale(true)); 

步驟 7:將單元格新增到表格

最後,要將此單元格新增到表格中,請呼叫 **Table** 類的 **addCell()** 方法並將 **cell** 物件作為引數傳遞給此方法,如下所示。

table.addCell(cell);

步驟 8:將表格新增到文件

使用 **Document** 類的 **add()** 方法新增前面步驟中建立的 **table** 物件,如下所示。

// Adding list to the document 
document.add(table);

步驟 9:關閉文件

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

// Closing the document 
document.close(); 

示例

以下 Java 程式演示瞭如何使用 iText 庫將影像新增到 PDF 文件的表格單元格中。它建立一個名為 **addingImage.pdf** 的 PDF 文件,向其中新增一個表格,將一個影像 (javafxLogo.jpg) 插入到其單元格之一,並將其儲存到路徑 **C:/itextExamples/** 中。

將此程式碼儲存在名為 **AddingImageToTable.java** 的檔案中。

import com.itextpdf.io.image.ImageData; 
import com.itextpdf.io.image.ImageDataFactory; 

import com.itextpdf.kernel.pdf.PdfDocument; 
import com.itextpdf.kernel.pdf.PdfWriter; 

import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell; 
import com.itextpdf.layout.element.Image; 
import com.itextpdf.layout.element.Table;  

public class a3AddingImageToTable {
   public static void main(String args[]) throws Exception {
      // Creating a PdfWriter object 
      String dest = "C:/itextExamples/addingImage.pdf";
      PdfWriter writer = new PdfWriter(dest);    
      
      // Creating a PdfDocument object   
      PdfDocument pdfDoc = new PdfDocument(writer);
      
      // Creating a Document object
      Document doc = new Document(pdfDoc);
      
      // Creating a table
      float [] pointColumnWidths = {150f, 150f};
      Table table = new Table(pointColumnWidths);
      
      // Populating row 1 and adding it to the table
      Cell cell1 = new Cell();
      cell1.add("Tutorial ID");
      table.addCell(cell1);
      
      Cell cell2 = new Cell();
      cell2.add("1");
      table.addCell(cell2);
      
      // Populating row 2 and adding it to the table
      Cell cell3 = new Cell();
      cell3.add("Tutorial Title");
      table.addCell(cell3);             
      
      Cell cell4 = new Cell(); 
      cell4.add("JavaFX");  
      table.addCell(cell4);
      
      // Populating row 3 and adding it to the table
      Cell cell5 = new Cell();
      cell5.add("Tutorial Author");
      table.addCell(cell5);            
      
      Cell cell6 = new Cell();
      cell6.add("Krishna Kasyap");
      table.addCell(cell6);
      
      // Populating row 4 and adding it to the table
      Cell cell7 = new Cell();
      cell7.add("Submission date");
      table.addCell(cell7);
      
      Cell cell8 = new Cell();
      cell8.add("2016-07-06");
      table.addCell(cell8);              
      
      // Populating row 5 and adding it to the table
      Cell cell9 = new Cell();
      cell9.add("Tutorial Icon");
      table.addCell(cell9);              
      
      // Creating the cell10       
      Cell cell10 = new Cell();              
      
      // Creating an ImageData object       
      String imageFile = "C:/itextExamples/javafxLogo.jpg";       
      ImageData data = ImageDataFactory.create(imageFile);        

      // Creating the image       
      Image img = new Image(data);              

      // Adding image to the cell10       
      cell10.add(img.setAutoScale(true));        

      // Adding cell110 to the table       
      table.addCell(cell10);                         
      
      // Adding Table to document        
      doc.add(table);                  
      
      // Closing the document       
      doc.close();  
      
      System.out.println("Image added to table successfully..");     
   } 
}  

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

javac AddingImageToTable.java 
java AddingImageToTable 

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

Image added to table successfully..

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

Adding Image to Table
廣告

© . All rights reserved.