Itext-formatting-cell-contents
iText-セルの内容のフォーマット
この章では、iTextライブラリを使用してPDFドキュメントを作成し、テーブルを追加し、テーブル内のセルのコンテンツをフォーマットする方法を説明します。
テーブル内のセルのフォーマット
Documentクラスをインスタンス化することにより、空のPDF Document を作成できます。 このクラスをインスタンス化する際、 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オブジェクトの作成
*PdfDocument* クラスは、iTextでPDFDocumentを表すクラスです。 このクラスは、パッケージ *com.itextpdf.kernel.pdf* に属します。 (書き込みモードで)このクラスをインスタンス化するには、クラス *PdfWriter* のオブジェクトをコンストラクターに渡す必要があります。
以下に示すように、上記で作成した PdfWriter オブジェクトをコンストラクターに渡すことで、 PdfDocument クラスをインスタンス化します。
//Creating a PdfDocument
PdfDocument pdfDoc = new PdfDocument(writer);
*PdfDocument* オブジェクトを作成したら、そのクラスが提供する各メソッドを使用して、ページ、フォント、添付ファイル、イベントハンドラーなどのさまざまな要素を追加できます。
ステップ3:Documentオブジェクトを作成する
パッケージ com.itextpdf.layout の Document クラスは、自給自足のPDFを作成する際のルート要素です。 このクラスのコンストラクターの1つは、クラスPdfDocumentのオブジェクトを受け入れます。
以下に示すように、前の手順で作成した PdfDocument クラスのオブジェクトを渡すことにより、 Document クラスをインスタンス化します。
//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:セルの作成
パッケージ com.itextpdf.layout.element の Cell クラスをインスタンス化して、 cell オブジェクトを作成します。 以下に示すように、 Cell クラスの* add()*メソッドを使用して、セルの内容を追加します。
//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:テーブルにセルを追加する
最後に、このセルをテーブルに追加するには、以下に示すように、 Table クラスの* addCell()メソッドを呼び出し、 *cell オブジェクトをパラメーターとしてこのメソッドに渡します。
table.addCell(c1);
ステップ8:文書に表を追加する
以下に示すように、 Document クラスの* add()メソッドを使用して、前の手順で作成した *table オブジェクトを追加します。
//Adding list to the document
document.add(table);
ステップ9:ドキュメントを閉じる
以下に示すように、 Document クラスの* close()*メソッドを使用してドキュメントを閉じます。
//Closing the document
document.close();
例
次のJavaプログラムは、iTextライブラリを使用してテーブル内のセルのコンテンツをフォーマットする方法を示しています。 addingBackground.pdf という名前の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ドキュメントを見つけることができます。