Itext-scaling-image

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

iText-画像のスケーリング

この章では、iTextライブラリを使用してPDFドキュメント内の画像を拡大縮小する方法を説明します。

PDF内の画像のスケーリング

*Document* クラスをインスタンス化することにより、空のPDFドキュメントを作成できます。 このクラスをインスタンス化する際、コンストラクターにパラメーターとして *PdfDocument* オブジェクトを渡す必要があります。

PDFに画像を追加するには、追加する必要がある画像のオブジェクトを作成し、 Document クラスの* add()*メソッドを使用して追加します。 * setAutoScale()*メソッドを使用して画像を拡大縮小できます。

以下は、PDFドキュメントに存在する画像を拡大縮小する手順です。

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

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

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

//Creating a PdfWriter
String dest = "C:/itextExamples/autoScale.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: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);

ステップ5:画像のスケーリング

  • setAutoScale()*メソッドを使用して画像を拡大縮小できます。
//Setting the position of the image to the center of the page
image.setFixedPosition(100, 250);

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

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

//Adding image to the document
document.add(img);

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

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

//Closing the document
document.close();

次のJavaプログラムは、iTextライブラリを使用してPDFドキュメントのドキュメントサイズに合わせて画像を拡大縮小する方法を示しています。 autoScale.pdf という名前のPDF文書を作成し、それに画像を追加し、ページの寸法に合わせてスケーリングし、パス C:/itextExamples/ に保存します。

このコードを SettingAutoScale.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.Image;

public class SettingAutoScale {
   public static void main(String args[]) throws Exception{
     //Creating a PdfWriter
      String dest = "C:/itextExamples/positionOfImage.pdf";
      PdfWriter writer = new PdfWriter(dest);

     //Creating a PdfDocument
      PdfDocument pdfDoc = new PdfDocument(writer);

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

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

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

     //Setting the position of the image to the center of the page
      image.setFixedPosition(100,250);

     //Adding image to the document
      document.add(image);

     //Closing the document
      document.close();
      System.out.println("Image Scaled");
   }
}

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

javac SettingAutoScale.java
java SettingAutoScale

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

Image Scaled

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

オートスケール