iText - 單元格邊框的格式化



本章我們將學習如何使用 iText 庫格式化表格中單元格的邊框。

單元格邊框的格式化

您可以透過例項化Document類來建立一個空的 PDF 文件。例項化此類時,需要將PdfDocument物件作為引數傳遞給其建構函式。

然後,要向文件新增表格,需要例項化Table類並將此物件使用add()方法新增到文件中。

您可以使用Cell類的setBorder()方法新增各種型別的邊框,例如DashedBorder、SolidBorder、DottedBorder、DoubleBorder、RoundDotsBorder等,並使用各種顏色。

以下是格式化表格中單元格邊框的步驟。

步驟 1:建立 PdfWriter 物件

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

透過向其建構函式傳遞一個字串值(表示您需要建立 PDF 的路徑)來例項化 PdfWriter 類,如下所示。

// Creating a PdfWriter 
String dest = "C:/itextExamples/coloredBorders.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.element包中的Cell類來建立一個單元格物件,並使用Cell類的add()方法新增單元格的內容,如下所示。

// Adding cell 1 to the table 
Cell cell1 = new Cell(); // Creating a cell 
cell1.add("Name");       // Adding content to the cell 

步驟 6:格式化單元格的邊框

iText 庫提供了表示邊框的各種類,例如DashedBorder、SolidBorder、DottedBorder、DoubleBorder、RoundDotsBorder等。

這些類的建構函式接受兩個引數:一個表示邊框顏色的color物件和一個表示邊框寬度的整數

選擇其中一種邊框型別,並透過傳遞color物件和表示寬度的整數來例項化相應的邊框,如下所示。

Border b1 = new DashedBorder(Color.RED, 3); 

現在,使用cell類的setBorder()方法設定單元格的邊框。此方法接受Border型別的物件作為引數。

透過將上面建立的Border物件作為引數傳遞給setBorder()方法來設定單元格的邊框,如下所示。

c1.setBorder(b1)

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

table.addCell(c1);

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

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

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

步驟 8:關閉文件

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

// Closing the document 
document.close();

示例

下面的 Java 程式演示瞭如何使用 iText 庫格式化表格中單元格的邊框。它建立一個名為coloredBorders.pdf的 PDF 文件,向其中新增一個表格,格式化其單元格的內容,並將其儲存在C:/itextExamples/路徑中。

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

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

import com.itextpdf.layout.Document; 
import com.itextpdf.layout.border.Border; 
import com.itextpdf.layout.border.DashedBorder; 
import com.itextpdf.layout.border.DottedBorder; 
import com.itextpdf.layout.border.DoubleBorder; 
import com.itextpdf.layout.border.RoundDotsBorder; 
import com.itextpdf.layout.border.SolidBorder; 

import com.itextpdf.layout.element.Cell; 
import com.itextpdf.layout.element.Table; 
import com.itextpdf.layout.property.TextAlignment;  

public class FormatedBorders {      
   public static void main(String args[]) throws Exception {
      // Creating a PdfWriter object   
      String dest = "C:/itextExamples/coloredBorders.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 = {200F, 200F};       
      Table table = new Table(pointColumnWidths); 
   
      // Adding row 1 to the table
      Cell c1 = new Cell();
      
      // Adding the contents of the cell
      c1.add("Name");
   
      // Setting the back ground color of the cell
      c1.setBackgroundColor(Color.DARK_GRAY);    
   
      // Instantiating the Border class 
      Border b1 = new DashedBorder(Color.RED, 3);
   
      // Setting the border of the cell
      c1.setBorder(b1);
      
      // Setting the text alignment       
      c1.setTextAlignment(TextAlignment.CENTER);
   
      // Adding the cell to the table       
      table.addCell(c1);                          
      Cell c2 = new Cell();       
      c2.add("Raju");       
      c1.setBorder(new SolidBorder(Color.RED, 3));       
      c2.setTextAlignment(TextAlignment.CENTER);       
      table.addCell(c2);
   
      // Adding row 2 to the table                
      Cell c3 = new Cell();      
      c3.add("Id"); 
      c3.setBorder(new DottedBorder(Color.DARK_GRAY, 3));       
      c3.setTextAlignment(TextAlignment.CENTER);      
      table.addCell(c3);                          
      
      Cell c4 = new Cell();       
      c4.add("001");       
      c4.setBorder(new DoubleBorder(Color.DARK_GRAY, 3));       
      c4.setTextAlignment(TextAlignment.CENTER);       
      table.addCell(c4);                          
      
      // Adding row 3 to the table       
      Cell c5 = new Cell();       
      c5.add("Designation");       
      c5.setBorder(new RoundDotsBorder(Color.RED, 3));       
      c5.setTextAlignment(TextAlignment.CENTER);       
      table.addCell(c5);                 
      
      Cell c6 = new Cell();       
      c6.add("Programmer");       
      c6.setBorder(new RoundDotsBorder(Color.RED, 3)); 
      c6.setTextAlignment(TextAlignment.CENTER);      
      table.addCell(c6);                              
      
      // Adding Table to document        
      doc.add(table);                  
      
      // Closing the document       
      doc.close();  
      
      System.out.println("Borders added successfully..");     
   } 
} 

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

javac FormatedBorders.java 
java FormatedBorders

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

Borders added successfully

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

Coloured Borders
廣告