Itext-nested-table
iText-入れ子になったテーブル
この章では、iTextライブラリを使用して、ネストされたテーブルをPDFドキュメントのテーブルに追加する方法を説明します。
PDFにネストされたテーブルを追加する
*Document* クラスをインスタンス化することにより、空のPDFドキュメントを作成できます。 このクラスをインスタンス化する際、 *PdfDocument* オブジェクトをパラメーターとしてコンストラクターに渡す必要があります。 次に、ドキュメントにテーブルを追加するには、 *Table* クラスをインスタンス化し、* add()*メソッドを使用してこのオブジェクトをドキュメントに追加する必要があります。
このテーブルにテーブルを追加するには、別のテーブル(ネストされたテーブル)を作成し、 Cell クラスの* add()*メソッドを使用してセルオブジェクトに渡す必要があります。
以下は、テーブルのセルにテーブルを挿入する手順です。
ステップ1:PdfWriterオブジェクトの作成
*PdfWriter* クラスは、PDFのDocWriterを表します。 このクラスは、パッケージ *com.itextpdf.kernel.pdf* に属します。 このクラスのコンストラクターは、PDFが作成されるファイルのパスを表す文字列を受け入れます。
以下に示すように、文字列値(PDFを作成する必要があるパスを表す)をコンストラクターに渡すことにより、PdfWriterクラスをインスタンス化します。
//Creating a PdfWriter
String dest = "C:/itextExamples/addingNestedTable.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を作成する際のルート要素です。 このクラスのコンストラクターの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 の Cell クラスをインスタンス化して、 cell オブジェクトを作成します。
//Adding cell to the table
Cell contact = new Cell(); //Creating a cell
ステップ6:ネストされたテーブルを作成する
*cell* を作成したら、以下に示すように、ネストされたテーブルを作成し、そのセルにデータを入力します。
//Creating nested table for contact
float [] pointColumnWidths2 = {150f, 150f};
Table nestedTable = new Table(pointColumnWidths2);
//Populating row 1 and adding it to the nested table
Cell nested1 = new Cell();
nested1.add("Phone");
nestedTable.addCell(nested1);
Cell nested2 = new Cell();
nested2.add("9848022338");
nestedTable.addCell(nested2);
//Populating row 2 and adding it to the nested table
Cell nested3 = new Cell();
nested3.add("email");
nestedTable.addCell(nested3);
Cell nested4 = new Cell();
nested4.add("[email protected]");
nestedTable.addCell(nested4);
//Populating row 3 and adding it to the nested table
Cell nested5 = new Cell();
nested5.add("Address");
nestedTable.addCell(nested5);
Cell nested6 = new Cell();
nested6.add("Hyderabad");
nestedTable.addCell(nested6);
ステップ7:入れ子になったテーブルをセルに追加する
次に、 Cell クラスの* add()メソッドを使用して、上記の作成されたネストされたテーブルを親(コンテナ)テーブルのセルに追加します。 そして、以下に示すように、 *Table クラスの* addCell()*メソッドを使用して、このセルを親テーブルに追加します。
contact.add(nestedTable);
table.addCell(contact);
ステップ8:文書に表を追加する
以下に示すように、 Document クラスの* add()メソッドを使用して、前の手順で作成した *table オブジェクトを追加します。
//Adding list to the document
document.add(table);
ステップ9:ドキュメントを閉じる
以下に示すように、 Document クラスの* close()*メソッドを使用してドキュメントを閉じます。
//Closing the document
document.close();
例
次のJavaプログラムは、iTextライブラリを使用してPDF文書内のテーブル(ネストされたテーブル)のセルにテーブルを追加する方法を示しています。 addingNestedTable.pdf という名前のPDFドキュメントを作成し、それにテーブルを追加し、そのセルの1つに別のテーブルを挿入して、パス C:/itextExamples/ に保存します。
このコードを AddNestedTable.java という名前のファイルに保存します。
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.Table;
public class a4AddNestedTablesPdf {
public static void main(String args[]) throws Exception {
//Creating a PdfWriter object
String dest = "C:/itextExamples/addingNestedTable.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 [] pointColumnWidths1 = {150f, 150f};
Table table = new Table(pointColumnWidths1);
//Populating row 1 and adding it to the table
Cell cell1 = new Cell();
cell1.add("Name");
table.addCell(cell1);
Cell cell2 = new Cell();
cell2.add("Raju");
table.addCell(cell2);
//Populating row 2 and adding it to the table
Cell cell3 = new Cell();
cell3.add("Id");
table.addCell(cell3);
Cell cell4 = new Cell();
cell4.add("1001");
table.addCell(cell4);
//Populating row 3 and adding it to the table
Cell cell5 = new Cell();
cell5.add("Designation");
table.addCell(cell5);
Cell cell6 = new Cell();
cell6.add("Programmer");
table.addCell(cell6);
//Creating nested table for contact
float [] pointColumnWidths2 = {150f, 150f};
Table nestedTable = new Table(pointColumnWidths2);
//Populating row 1 and adding it to the nested table
Cell nested1 = new Cell();
nested1.add("Phone");
nestedTable.addCell(nested1);
Cell nested2 = new Cell();
nested2.add("9848022338");
nestedTable.addCell(nested2);
//Populating row 2 and adding it to the nested table
Cell nested3 = new Cell();
nested3.add("email");
nestedTable.addCell(nested3);
Cell nested4 = new Cell();
nested4.add("[email protected]");
nestedTable.addCell(nested4);
//Populating row 3 and adding it to the nested table
Cell nested5 = new Cell();
nested5.add("Address");
nestedTable.addCell(nested5);
Cell nested6 = new Cell();
nested6.add("Hyderabad");
nestedTable.addCell(nested6);
//Adding table to the cell
Cell cell7 = new Cell();
cell7.add("Contact");
table.addCell(cell7);
Cell cell8 = new Cell();
cell8.add(nestedTable);
table.addCell(cell8);
//Adding table to the document
doc.add(table);
//Closing the document
doc.close();
System.out.println("Nested Table Added successfully..");
}
}
次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します-
javac AddNestedTable.java
java AddNestedTable
実行時に、上記のプログラムは、次のメッセージを表示するPDFドキュメントを作成します。
Nested Table Added successfully..
指定したパスを確認すると、以下に示すように、作成されたPDFドキュメントを見つけることができます。