Itext-adding-image-to-table

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

iText-画像をテーブルに追加する

この章では、iTextライブラリを使用してPDFドキュメントの表に画像を追加する方法を説明します。

テーブルに画像を追加する

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

このテーブルに画像を追加するには、 Cell クラスをインスタンス化し、追加する必要のある画像のオブジェクトを作成し、 add メソッドを使用して画像を cell オブジェクトに追加する必要があります。 Cell クラス。

以下は、テーブルのセルに画像を挿入する手順です。

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

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

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

//Creating a PdfWriter
String dest = "C:/itextExamples/addingImage.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
float [] pointColumnWidths = {200F, 200F};
Table table = new Table(pointColumnWidths);

ステップ5:セルの作成

以下に示すように、パッケージ com.itextpdf.layoutCell クラスをインスタンス化して、 cell オブジェクトを作成します。

//Adding cell to the table
Cell cell = new Cell(); //Creating a cell

ステップ6:イメージを作成する

*image* オブジェクトを作成するには、まず、 *ImageDataFactory* クラスの* create()*メソッドを使用して *ImageData* オブジェクトを作成します。 このメソッドのパラメーターとして、以下に示すように、画像のパスを表す文字列パラメーターを渡します。
//Creating an ImageData object
String imageFile = "C:/itextExamples/javafxLogo.jpg";
ImageData data = ImageDataFactory.create(imageFile);

ここで、 com.itextpdf.layout.element パッケージの Image クラスをインスタンス化します。 インスタンス化中に、上記で作成した ImageData オブジェクトを、以下に示すように、コンストラクターにパラメーターとして渡します。

//Creating an Image object
Image img = new Image(data);

以下に示すように、セルクラスの* add()メソッドを使用して、セルに *image オブジェクトを追加します。

//Adding image to the cell
cell.add(img.setAutoScale(true));

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

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

table.addCell(cell);

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

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

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

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

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

//Closing the document
document.close();

次のJavaプログラムは、iTextライブラリを使用して、PDF文書の表のセルに画像を追加する方法を示しています。 addingImage.pdf という名前のPDFドキュメントを作成し、それにテーブルを追加し、そのセルの1つに画像(javafxLogo.jpg)を挿入し、パス C:/itextExamples/ に保存します。

このコードを AddingImageToTable.java という名前のファイルに保存します。

import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;

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.Image;
import com.itextpdf.layout.element.Table;

public class a3AddingImageToTable {
   public static void main(String args[]) throws Exception {
     //Creating a PdfWriter object
      String dest = "C:/itextExamples/addingImage.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 = {150f, 150f};
      Table table = new Table(pointColumnWidths);

     //Populating row 1 and adding it to the table
      Cell cell1 = new Cell();
      cell1.add("Tutorial ID");
      table.addCell(cell1);

      Cell cell2 = new Cell();
      cell2.add("1");
      table.addCell(cell2);

     //Populating row 2 and adding it to the table
      Cell cell3 = new Cell();
      cell3.add("Tutorial Title");
      table.addCell(cell3);

      Cell cell4 = new Cell();
      cell4.add("JavaFX");
      table.addCell(cell4);

     //Populating row 3 and adding it to the table
      Cell cell5 = new Cell();
      cell5.add("Tutorial Author");
      table.addCell(cell5);

      Cell cell6 = new Cell();
      cell6.add("Krishna Kasyap");
      table.addCell(cell6);

     //Populating row 4 and adding it to the table
      Cell cell7 = new Cell();
      cell7.add("Submission date");
      table.addCell(cell7);

      Cell cell8 = new Cell();
      cell8.add("2016-07-06");
      table.addCell(cell8);

     //Populating row 5 and adding it to the table
      Cell cell9 = new Cell();
      cell9.add("Tutorial Icon");
      table.addCell(cell9);

     //Creating the cell10
      Cell cell10 = new Cell();

     //Creating an ImageData object
      String imageFile = "C:/itextExamples/javafxLogo.jpg";
      ImageData data = ImageDataFactory.create(imageFile);

     //Creating the image
      Image img = new Image(data);

     //Adding image to the cell10
      cell10.add(img.setAutoScale(true));

     //Adding cell110 to the table
      table.addCell(cell10);

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

     //Closing the document
      doc.close();

      System.out.println("Image added to table successfully..");
   }
}

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

javac AddingImageToTable.java
java AddingImageToTable

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

Image added to table successfully..

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

テーブルへの画像の追加