iText-セルコンテンツのフォーマット
この章では、iTextライブラリを使用して、PDFドキュメントを作成し、テーブルを追加し、テーブル内のセルの内容をフォーマットする方法を説明します。
テーブル内のセルのフォーマット
空のPDFを作成できます DocumentDocumentクラスをインスタンス化する。このクラスをインスタンス化するときに、合格する必要がありますPdfDocumentコンストラクターへのパラメーターとしてのオブジェクト。次に、ドキュメントにテーブルを追加するには、インスタンス化する必要がありますTable クラスを作成し、を使用してこのオブジェクトをドキュメントに追加します add()方法。次の方法を使用して、テーブル内のセルの内容をフォーマットできます。Cell クラス。
以下は、テーブル内のセルの内容をフォーマットする手順です。
ステップ1:PdfWriterオブジェクトを作成する
ザ・ PdfWriterクラスはPDFのDocWriterを表します。このクラスはパッケージに属していますcom.itextpdf.kernel.pdf。このクラスのコンストラクターは、PDFが作成されるファイルのパスを表す文字列を受け入れます。
以下に示すように、文字列値(PDFを作成する必要があるパスを表す)をコンストラクターに渡すことにより、PdfWriterクラスをインスタンス化します。
// Creating a PdfWriter 
String dest = "C:/itextExamples/addingBackground.pdf"; 
PdfWriter writer = new PdfWriter(dest);このタイプのオブジェクトがPdfDocument(クラス)に渡されると、このドキュメントに追加されたすべての要素が指定されたファイルに書き込まれます。
ステップ2:PdfDocumentオブジェクトを作成する
ザ・ PdfDocumentclassは、iTextでPDFDocumentを表すクラスです。このクラスはパッケージに属していますcom.itextpdf.kernel.pdf。このクラスを(書き込みモードで)インスタンス化するには、クラスのオブジェクトを渡す必要がありますPdfWriter そのコンストラクタに。
インスタンス化する PdfDocument 上記で作成したクラスを渡すことによってクラス PdfWriter 以下に示すように、コンストラクターに対するオブジェクト。
// Creating a PdfDocument  
PdfDocument pdfDoc = new PdfDocument(writer);一度 PdfDocument オブジェクトが作成されると、そのクラスによって提供されるそれぞれのメソッドを使用して、ページ、フォント、添付ファイル、イベントハンドラーなどのさまざまな要素を追加できます。
ステップ3:Documentオブジェクトを作成する
ザ・ Document パッケージのクラス com.itextpdf.layoutは、自給自足のPDFを作成する際のルート要素です。このクラスのコンストラクターの1つは、クラスPdfDocumentのオブジェクトを受け入れます。
インスタンス化する Document クラスのオブジェクトを渡すことによってクラス PdfDocument 以下に示すように、前の手順で作成しました。
// Creating a Document   
Document document = new Document(pdfDoc);ステップ4:テーブルオブジェクトを作成する
ザ・ Tableクラスは、行と列で順序付けられた、セルで満たされた2次元グリッドを表します。パッケージに属していますcom.itextpdf.layout.element。
以下に示すように、Tableクラスをインスタンス化します。
// Creating a table 
float [] pointColumnWidths = {200F, 200F}; 
Table table = new Table(pointColumnWidths);ステップ5:セルを作成する
作成する cell オブジェクトをインスタンス化して Cell パッケージのクラス com.itextpdf.layout.element。を使用してセルの内容を追加しますadd() の方法 Cell 以下に示すように、クラス。
// Adding cell 1 to the table 
Cell cell1 = new Cell();     // Creating a cell 
cell1.add("Name");           // Adding content to the cell       
// Adding cell 2 to the table 
Cell cell2 = new Cell();     // Creating a cell 
cell2.add("Raju");           // Adding content to the cellステップ6:セルに背景を追加する
セルを作成してコンテンツを追加したら、セルをフォーマットできます。たとえば、セルクラスのさまざまなメソッドを使用して、背景を設定したり、セル内のテキストを配置したり、テキストの色を変更したりできます。setBackgroundColor(), setBorder(), setTextAlignment()。
以下に示すように、前の手順で作成したセルに背景色、境界線、およびテキストの配置を設定できます。
c1.setBackgroundColor(Color.DARK_GRAY);    // Setting background color to cell1 
c1.setBorder(Border.NO_BORDER);            // Setting border to cell1 
c1.setTextAlignment(TextAlignment.CENTER); // Setting text alignment to cell1ステップ7:テーブルにセルを追加する
最後に、このセルをテーブルに追加するには、 addCell() の方法 Table クラスと合格 cell 以下に示すように、このメソッドのパラメーターとしてのオブジェクト。
table.addCell(c1);ステップ8:ドキュメントにテーブルを追加する
追加します table 前の手順で作成されたオブジェクト add() の方法 Document 以下に示すクラス。
// Adding list to the document 
document.add(table);ステップ9:ドキュメントを閉じる
を使用してドキュメントを閉じます close() の方法 Document 以下に示すように、クラス。
// Closing the document 
document.close();例
次のJavaプログラムは、iTextライブラリを使用してテーブル内のセルの内容をフォーマットする方法を示しています。名前の付いたPDFドキュメントを作成しますaddingBackground.pdf、テーブルを追加し、セルの内容をフォーマットして、パスに保存します C:/itextExamples/
このコードを名前のファイルに保存します BackgroundToTable.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.element.Cell; 
import com.itextpdf.layout.element.Table; 
import com.itextpdf.layout.property.TextAlignment;  
public class BackgroundToTable {      
   public static void main(String args[]) throws Exception {        
      // Creating a PdfWriter object   
      String dest = "C:/itextExamples/addingBackground.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);
      
      // Populating row 1 and adding it to the table               
      Cell c1 = new Cell();                        // Creating cell 1 
      c1.add("Name");                              // Adding name to cell 1   
      c1.setBackgroundColor(Color.DARK_GRAY);      // Setting background color
      c1.setBorder(Border.NO_BORDER);              // Setting border
      c1.setTextAlignment(TextAlignment.CENTER);   // Setting text alignment      
      table.addCell(c1);                           // Adding cell 1 to the table 
      
      Cell c2 = new 
      Cell();                               
      c2.add("Raju");       
      c2.setBackgroundColor(Color.GRAY);       
      c2.setBorder(Border.NO_BORDER);       
      c2.setTextAlignment(TextAlignment.CENTER);      
      table.addCell(c2);      
      
      // Populating row 2 and adding it to the table               
      Cell c3 = new Cell();       
      c3.add("Id");       
      c3.setBackgroundColor(Color.WHITE);       
      c3.setBorder(Border.NO_BORDER);       
      c3.setTextAlignment(TextAlignment.CENTER);      
      table.addCell(c3);                          
      
      Cell c4 = new Cell();       
      c4.add("001");       
      c4.setBackgroundColor(Color.WHITE);       
      c4.setBorder(Border.NO_BORDER);       
      c4.setTextAlignment(TextAlignment.CENTER);      
      table.addCell(c4);                          
      
      // Populating row 3 and adding it to the table        
      Cell c5 = new Cell();       
      c5.add("Designation");       
      c5.setBackgroundColor(Color.DARK_GRAY);       
      c5.setBorder(Border.NO_BORDER);       
      c5.setTextAlignment(TextAlignment.CENTER);      
      table.addCell(c5);                 
      
      Cell c6 = new Cell(); 
      c6.add("Programmer");       
      c6.setBackgroundColor(Color.GRAY);       
      c6.setBorder(Border.NO_BORDER);       
      c6.setTextAlignment(TextAlignment.CENTER);       
      table.addCell(c6);                              
      
      // Adding Table to document        
      doc.add(table);                  
      
      // Closing the document       
      doc.close();  
      
      System.out.println("Background added successfully..");     
   } 
}次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します-
javac BackgroundToTable.java 
java BackgroundToTable上記のプログラムを実行すると、PDFドキュメントが作成され、次のメッセージが表示されます。
Background added successfully..指定されたパスを確認すると、以下に示すように、作成されたPDFドキュメントを見つけることができます。
