Itext-adding-table

提供:Dev Guides
移動先:案内検索

iText-テーブルの追加

この章では、iTextライブラリを使用してPDFドキュメントを作成し、テーブルを追加する方法を説明します。

PDFへのテーブルの追加

*Document* クラスをインスタンス化することにより、空のPDFドキュメントを作成できます。 このクラスをインスタンス化する際、コンストラクターにパラメーターとして *PdfDocument* オブジェクトを渡す必要があります。 次に、ドキュメントにテーブルを追加するには、 *Table* クラスをインスタンス化し、* add()*メソッドを使用してこのオブジェクトをドキュメントに追加する必要があります。

以下は、テーブルを含むPDFドキュメントを作成する手順です。

ステップ1:PdfWriterオブジェクトの作成

*PdfWriter* クラスは、PDFのDocWriterを表します。 このクラスは、パッケージ *com.itextpdf.kernel.pdf* に属します。 このクラスのコンストラクターは、PDFが作成されるファイルのパスを表す文字列を受け入れます。

以下に示すように、コンストラクターにストリング値(PDFを作成する必要があるパスを表す)を渡すことにより、 PdfWriter クラスをインスタンス化します。

//Creating a PdfWriter
String dest = "C:/itextExamples/addingTable.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.layoutDocument クラスは、自給自足のPDFを作成する際のルート要素です。 このクラスのコンストラクターの1つは、 PdfDocument クラスのオブジェクトを受け入れます。

以下に示すように、前の手順で作成した PdfDocument クラスのオブジェクトを渡すことにより、 Document クラスをインスタンス化します。

//Creating a Document
Document document = new Document(pdfDoc);

ステップ4:テーブルオブジェクトの作成

*Table* クラスは、行と列に並べられたセルで満たされた2次元グリッドを表します。 パッケージ *com.itextpdf.layout.element* に属します。

以下に示すように Table クラスをインスタンス化します。

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

ステップ5:テーブルにセルを追加する

パッケージ com.itextpdf.layout.elementCell クラスをインスタンス化して、 cell オブジェクトを作成します。 このクラスの* add()*メソッドを使用してセルの内容を追加します。

最後に、このセルをテーブルに追加するには、以下に示すように、 Table クラスの* addCell()メソッドを呼び出し、 *cell オブジェクトをパラメーターとしてこのメ​​ソッドに渡します。

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

//Adding cell 2 to the table Cell
cell2 = new Cell();      //Creating a cell
cell2.add("Raju");       //Adding content to the cell
table.addCell(cell2);    //Adding cell to the table

ステップ6:文書に表を追加する

以下に示すように、 Document クラスの* add()メソッドを使用して、前の手順で作成した *table オブジェクトを追加します。

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

ステップ7:ドキュメントを閉じる

以下に示すように、 Document クラスの* close()*メソッドを使用してドキュメントを閉じます。

//Closing the document
document.close();

次のJavaプログラムは、iTextライブラリを使用してPDFドキュメントを作成し、テーブルを追加する方法を示しています。 addingTable.pdf という名前のPDFドキュメントを作成し、それにテーブルを追加して、パス C:/itextExamples/ に保存します

このコードを AddingTable.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 AddingTable {
   public static void main(String args[]) throws Exception {
     //Creating a PdfDocument object
      String dest = "C:/itextExamples/addingTable.pdf";
      PdfWriter writer = new PdfWriter(dest);

     //Creating a PdfDocument object
      PdfDocument pdf = new PdfDocument(writer);

     //Creating a Document object
      Document doc = new Document(pdf);

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

     //Adding cells to the table
      table.addCell(new Cell().add("Name"));
      table.addCell(new Cell().add("Raju"));
      table.addCell(new Cell().add("Id"));
      table.addCell(new Cell().add("1001"));
      table.addCell(new Cell().add("Designation"));
      table.addCell(new Cell().add("Programmer"));

     //Adding Table to document
      doc.add(table);

     //Closing the document
      doc.close();
      System.out.println("Table created successfully..");
   }
}

次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します-

javac AddingTable.java
java AddingTable

実行時に、上記のプログラムはPDFドキュメントを作成し、次のメッセージを表示します。

Table created successfully..

指定したパスを確認すると、以下に示すように、作成されたPDFドキュメントを見つけることができます。

テーブルの追加