Itext-quick-guide
iText-概要
Portable Document Format(PDF)は、アプリケーションソフトウェア、ハードウェア、およびオペレーティングシステムに依存しない方法でデータを表示するのに役立つファイル形式です。 各PDFファイルには、テキスト、フォント、グラフィックス、および表示に必要なその他の情報を含む、固定レイアウトのフラットドキュメントの説明が含まれています。
次のようなプログラムを介してPDFドキュメントを作成および操作するために利用できるいくつかのライブラリがあります-
- Adobe PDF Library -このライブラリは、C ++ 、. NET、Javaなどの言語でAPIを提供します。 これを使用して、PDFドキュメントからテキストを編集、表示、印刷、抽出できます。
- Formatting Objects Processor -XSL Formatting Objectsおよび出力に依存しないフォーマッタによって駆動されるオープンソース印刷フォーマッタ。 主な出力先はPDFです。
- PDF Box -Apache PDFBoxは、PDFドキュメントの開発と変換をサポートするオープンソースのJavaライブラリです。 このライブラリを使用すると、PDFドキュメントを作成、変換、操作するJavaプログラムを開発できます。
- * Jasperレポート*-これは、Microsoft Excel、RTF、ODT、コンマ区切り値、XMLファイルなどのPDFドキュメントでレポートを生成するJavaレポートツールです。
iTextとは何ですか?
上記のソフトウェアのiTextと同様に、Java PDFライブラリを使用して、PDFドキュメントを作成、変換、操作するJavaプログラムを開発できます。
iTextの機能
以下はiTextライブラリの注目すべき機能です-
- インタラクティブ-iTextは、インタラクティブPDFドキュメントを生成するためのクラス(API)を提供します。 これらを使用して、マップとブックを作成できます。
- ブックマーク、ページ番号などの追加-iTextを使用して、ブックマーク、ページ番号、透かしを追加できます。
- 分割とマージ-iTextを使用して、既存のPDFを複数のPDFに分割し、追加ページを追加/連結することもできます。
- フォームに記入-iTextを使用して、PDFドキュメントにインタラクティブなフォームを記入できます。
- 画像として保存-iTextを使用して、PNGをPNGやJPEGなどの画像ファイルとして保存できます。
- Canvas -iTextライブラリは、円、線などのさまざまな幾何学的図形をPDFドキュメントに描画できるCanvasクラスを提供します。
- Create PDFs -iTextを使用して、Javaプログラムから新しいPDFファイルを作成できます。 画像とフォントも含めることができます。
IText環境
以下の手順に従って、EclipseでiText環境を設定します。
- ステップ1 *-Eclipseをインストールし、以下に示すように新しいプロジェクトを開きます。
ステップ2 *-以下に示すように *iTextSample プロジェクトを作成します。
ステップ3 *-プロジェクトを右クリックして、以下に示すようにMavenプロジェクトに変換します。 これをMavenプロジェクトに変換すると、 *pom.xml が提供され、必要な依存関係を記載する必要があります。 その後、それらの依存関係の jar ファイルがプロジェクトに自動的にダウンロードされます。
ステップ4 *-プロジェクトの *pom.xml で、次のコンテンツ(iTextアプリケーションの依存関係)をコピーして貼り付け、プロジェクトを更新します。
- pom.xmlを使用*
プロジェクトをMavenプロジェクトに変換し、次のコンテンツをその pom.xml に追加します。
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SanthoshExample</groupId>
<artifactId>SanthoshExample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- always needed -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>kernel</artifactId>
<version>7.0.2</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>io</artifactId>
<version>7.0.2</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>layout</artifactId>
<version>7.0.2</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>forms</artifactId>
<version>7.0.2</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>pdfa</artifactId>
<version>7.0.2</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>sign</artifactId>
<version>7.0.2</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>barcodes</artifactId>
<version>7.0.2</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>font-asian</artifactId>
<version>7.0.2</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>hyph</artifactId>
<version>7.0.2</version>
</dependency>
</dependencies>
</project>
最後に、Mavenの依存関係を確認すると、必要なすべての jar ファイルがダウンロードされたことを確認できます。
iText-PDFドキュメントの作成
iTextライブラリを使用してPDFドキュメントを作成する方法を理解しましょう。
空のPDFドキュメントの作成
*Document* クラスをインスタンス化することにより、空のPDFドキュメントを作成できます。 このクラスをインスタンス化する際、コンストラクターにパラメーターとして *PdfDocument* オブジェクトを渡す必要があります。
空のPDFドキュメントを作成する手順は次のとおりです。
ステップ1:PdfWriterオブジェクトの作成
*PdfWriter* クラスは、PDFのDoc Writerを表します。 このクラスは、パッケージ *com.itextpdf.kernel.pdf* に属します。 このクラスのコンストラクターは、PDFが作成されるファイルのパスを表す文字列を受け入れます。
以下に示すように、文字列値(PDFを作成する必要があるパスを表す)をコンストラクターに渡すことにより、PdfWriterクラスをインスタンス化します。
//Creating a PdfWriter
String dest = "C:/itextExamples/sample.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:空のページを追加する
*PdfDocument* クラスの* addNewPage()*メソッドは、PDFドキュメントに空のページを作成するために使用されます。
以下に示すように、前の手順で作成したPDFドキュメントに空のページを追加します。
//Adding an empty page
pdfDoc.addNewPage();
ステップ4:ドキュメントオブジェクトの作成
パッケージ com.itextpdf.layout の Document クラスは、自給自足のPDFを作成する際のルート要素です。 このクラスのコンストラクターの1つは、クラスPdfDocumentのオブジェクトを受け入れます。
以下に示すように、前の手順で作成された PdfDocument クラスのオブジェクトを渡すことにより、 Document クラスをインスタンス化します。
//Creating a Document
Document document = new Document(pdfDoc);
ステップ5:ドキュメントを閉じる
以下に示すように、 Document クラスの* close()*メソッドを使用してドキュメントを閉じます。
//Closing the document
document.close();
例
以下は、PDFドキュメントの作成を示すJavaプログラムです。 sample.pdf という名前のPDFドキュメントを作成し、空のページを追加して、パス C:/itextExamples/ に保存します。
このコードを create_PDF.java という名前のファイルに保存します。
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
public class create_PDF {
public static void main(String args[]) throws Exception {
//Creating a PdfWriter
String dest = "C:/itextExamples/sample.pdf";
PdfWriter writer = new PdfWriter(dest);
//Creating a PdfDocument
PdfDocument pdfDoc = new PdfDocument(writer);
//Adding a new page
pdfDoc.addNewPage();
//Creating a Document
Document document = new Document(pdfDoc);
//Closing the document
document.close();
System.out.println("PDF Created");
}
}
次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します-
javac create_PDF.java
java create_PDF
実行時に、上記のプログラムはPDFドキュメントを作成し、次のメッセージを表示します。
PDF created
指定されたパスを確認すると、以下に示すように作成されたPDFドキュメントを見つけることができます。
これは空のドキュメントであるため、このドキュメントを開こうとすると、次のスクリーンショットに示すようにエラーメッセージが表示されます。
iText-AreaBreakの追加
この章では、iTextライブラリを使用してAreaBreakでPDFドキュメントを作成する方法を説明します。
AreaBreakの作成
*Document* クラスをインスタンス化することにより、空のPDFドキュメントを作成できます。 このクラスをインスタンス化する際、 *PdfDocument* オブジェクトをパラメーターとしてコンストラクターに渡す必要があります。 次に、ドキュメントにエリアブレークを追加するには、 *AreaBreak* クラスをインスタンス化し、* add()*メソッドを使用してこのオブジェクトをドキュメントに*追加*する必要があります。
以下は、AreaBreakで空のPDFドキュメントを作成する手順です。
ステップ1:PdfWriterオブジェクトの作成
*PdfWriter* クラスはPDFのDoc Writerを表し、このクラスはパッケージ *com.itextpdf.kernel.pdf* に属します。 このクラスのコンストラクターは、PDFが作成されるファイルのパスを表す文字列を受け入れます。
以下に示すように、PDFを作成する必要があるパスを表す文字列値をコンストラクターに渡すことにより、PdfWriterクラスをインスタンス化します。
//Creating a PdfWriter
String dest = "C:/itextExamples/addingAreaBreak.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:ドキュメントオブジェクトの作成
パッケージ com.itextpdf.layout の Document クラスは、自給自足のPDFを作成する際のルート要素です。 このクラスのコンストラクターの1つは、クラスPdfDocumentのオブジェクトを受け入れます。
以下に示すように、前の手順で作成した PdfDocument クラスのオブジェクトを渡すことにより、 Document クラスをインスタンス化します。
//Creating a Document
Document document = new Document(pdfDoc);
ステップ4:Area Breakオブジェクトの作成
*AreaBreak* クラスは、パッケージ *com.itextpdf.layout.element* に属します。 このクラスをインスタンス化すると、現在のコンテキスト領域が終了し、同じサイズの新しいコンテキスト領域が作成されます(デフォルトのコンストラクタを使用する場合)。
以下に示すように、 AreaBreak クラスをインスタンス化します。
//Creating an Area Break
AreaBreak aB = new AreaBreak();
ステップ5:AreaBreakを追加する
以下に示すように、Documentクラスの* add()メソッドを使用して、前の手順で作成した *areabreak オブジェクトを追加します。
//Adding area break to the PDF
document.add(aB);
ステップ6:ドキュメントを閉じる
以下に示すように、 Document クラスの* close()*メソッドを使用してドキュメントを閉じます。
//Closing the document
document.close();
例
次のJavaプログラムは、iTextライブラリを使用してAreaBreakでPDFドキュメントを作成する方法を示しています。 addingAreaBreak.pdf という名前のPDFドキュメントを作成し、それに areabreak を追加して、パス C:/itextExamples/ に保存します。
このコードを AddingAreaBreak.java という名前のファイルに保存します。
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.AreaBreak;
public class AddingAreaBreak {
public static void main(String args[]) throws Exception {
//Creating a PdfWriter
String dest = "C:/itextExamples/addingAreaBreak.pdf";
PdfWriter writer = new PdfWriter(dest);
//Creating a PdfDocument
PdfDocument pdf = new PdfDocument(writer);
//Creating a Document by passing PdfDocument object to its constructor
Document document = new Document(pdf);
//Creating an Area Break
AreaBreak aB = new AreaBreak();
//Adding area break to the PDF
document.add(aB);
//Closing the document
document.close();
System.out.println("Pdf created");
}
}
次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します-
javac AddingAreaBreak.java
java AddingAreaBreak
実行時に、上記のプログラムはPDFドキュメントを作成し、次のメッセージを表示します。
Pdf Created
指定したパスを確認すると、以下に示すように、作成されたPDFドキュメントを見つけることができます。
iText-段落の追加
この章では、iTextライブラリを使用してPDFドキュメントを作成し、それに段落を追加する方法を説明します。
段落を作成する
*Document* クラスをインスタンス化することにより、空のPDFドキュメントを作成できます。 このクラスをインスタンス化する際、 *PdfDocument* オブジェクトをパラメーターとしてコンストラクターに渡す必要があります。 次に、ドキュメントに段落を追加するには、 *Paragraph* クラスをインスタンス化し、* add()*メソッドを使用してこのオブジェクトをドキュメントに追加する必要があります。
以下は、段落を含むPDFドキュメントを作成する手順です。
ステップ1:PdfWriterオブジェクトの作成
*PdfWriter* クラスは、PDFのDoc Writerを表します。 このクラスは、パッケージ *com.itextpdf.kernel.pdf* に属します。 このクラスのコンストラクターは、PDFが作成されるファイルのパスを表す文字列を受け入れます。
以下に示すように、文字列値(PDFを作成する必要があるパスを表す)をコンストラクターに渡すことにより、PdfWriterクラスをインスタンス化します。
//Creating a PdfWriter
String dest = "C:/itextExamples/addingParagraph.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:段落オブジェクトの作成
*Paragraph* クラスは、テキストおよびグラフィック情報の自己完結型ブロックを表します。 パッケージ *com.itextpdf.layout.element* に属します。
以下に示すように、テキストコンテンツをそのコンストラクタに文字列として渡すことにより、 Paragraph クラスをインスタンス化します。
String para = "Welcome to finddevguides.";
//Creating an Area Break
Paragraph para = new Paragraph (para);
ステップ5:段落の追加
以下に示すように、 Document クラスの* add()メソッドを使用して、前の手順で作成した *Paragraph オブジェクトを追加します。
//Adding area break to the PDF
document.add(para);
ステップ6:ドキュメントを閉じる
以下に示すように、 Document クラスの* close()*メソッドを使用してドキュメントを閉じます。
//Closing the document
document.close();
例
次のJavaプログラムは、iTextライブラリを使用してPDFドキュメントを作成し、それに段落を追加する方法を示しています。 addingParagraph.pdf という名前のPDFドキュメントを作成し、それに段落を追加して、パス C:/itextExamples/ に保存します。
このコードを AddingParagraph.java という名前のファイルに保存します。
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
public class AddingParagraph {
public static void main(String args[]) throws Exception {
//Creating a PdfWriter
String dest = "C:/itextExamples/addingParagraph.pdf";
PdfWriter writer = new PdfWriter(dest);
//Creating a PdfDocument
PdfDocument pdf = new PdfDocument(writer);
//Creating a Document
Document document = new Document(pdf);
String para1 = "Tutorials Point originated from the idea that there exists
a class of readers who respond better to online content and prefer to learn
new skills at their own pace from the comforts of their drawing rooms.";
String para2 = "The journey commenced with a single tutorial on HTML in 2006
and elated by the response it generated, we worked our way to adding fresh
tutorials to our repository which now proudly flaunts a wealth of tutorials
and allied articles on topics ranging from programming languages to web designing
to academics and much more.";
//Creating Paragraphs
Paragraph paragraph1 = new Paragraph(para1);
Paragraph paragraph2 = new Paragraph(para2);
//Adding paragraphs to document
document.add(paragraph1);
document.add(paragraph2);
//Closing the document
document.close();
System.out.println("Paragraph added");
}
}
次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します-
javac AddingParagraph.java
java AddingParagraph
実行時に、上記のプログラムはPDFドキュメントを作成し、次のメッセージを表示します。
Paragraph added
指定したパスを確認すると、以下に示すように、作成されたPDFドキュメントを見つけることができます。
iText-リストの追加
この章では、iTextライブラリを使用してPDFドキュメントを作成し、リストを追加する方法を説明します。
リストを作成する
*Document* クラスをインスタンス化することにより、空のPDFドキュメントを作成できます。 このクラスをインスタンス化する際、 *PdfDocument* オブジェクトをパラメーターとしてコンストラクターに渡す必要があります。 次に、ドキュメントにリストを追加するには、 *List* クラスをインスタンス化し、* add()*メソッドを使用してこのオブジェクトをドキュメントに追加する必要があります。
PDFドキュメントを作成し、リストを追加する手順は次のとおりです。
ステップ1:PdfWriterオブジェクトの作成
*PdfWriter* クラスは、PDFのDocWriterを表します。 このクラスは、パッケージ *com.itextpdf.kernel.pdf* に属します。 このクラスのコンストラクターは、PDFが作成されるファイルのパスを表す文字列を受け入れます。
以下に示すように、文字列値(PDFを作成する必要があるパスを表す)をコンストラクターに渡すことにより、PdfWriterクラスをインスタンス化します。
//Creating a PdfWriter
String dest = "C:/itextExamples/addingList.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:リストオブジェクトの作成
*List* クラスは、垂直方向に輪郭が描かれた一連のオブジェクトを表します。 パッケージ *com.itextpdf.layout.element* に属します。
以下に示すように List クラスをインスタンス化します。
//Creating a list
List list = new List();
ステップ5:リストに要素を追加する
以下に示すように、ストリング値を渡すことにより、 List クラスの* add()メソッドを使用して、 *list オブジェクトにコンテンツを追加します。
// Add elements to the list
list.add("Java");
list.add("JavaFX");
list.add("Apache Tika");
list.add("OpenCV");
ステップ6:リストをドキュメントに追加する
以下に示すように、 Document クラスの* add()メソッドを使用して、前の手順で作成した *list オブジェクトを追加します。
//Adding list to the document
document.add(list);
ステップ7:ドキュメントを閉じる
以下に示すように、 Document クラスの* close()*メソッドを使用してドキュメントを閉じます。
//Closing the document
document.close();
例
次のJavaプログラムは、iTextライブラリを使用してPDFドキュメントを作成し、リストを追加する方法を示しています。 addingList.pdf という名前のPDFドキュメントを作成し、リストを追加して、パス C:/itextExamples/ に保存します。
このコードを AddingList.java という名前のファイルに保存します。
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.List;
import com.itextpdf.layout.element.Paragraph;
public class AddingList {
public static void main(String args[]) throws Exception {
//Creating a PdfWriter
String dest = "C:/itextExamples/addngList.pdf";
PdfWriter writer = new PdfWriter(dest);
//Creating a PdfDocument
PdfDocument pdf = new PdfDocument(writer);
//Creating a Document
Document document = new Document(pdf);
//Creating a Paragraph
Paragraph paragraph = new Paragraph("Tutorials Point provides the following tutorials");
//Creating a list
List list = new List();
//Add elements to the list
list.add("Java");
list.add("JavaFX");
list.add("Apache Tika");
list.add("OpenCV");
list.add("WebGL");
list.add("Coffee Script");
list.add("Java RMI");
list.add("Apache Pig");
//Adding paragraph to the document
document.add(paragraph);
//Adding list to the document
document.add(list);
//Closing the document
document.close();
System.out.println("List added");
}
}
次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します-
javac AddingList.java
java AddingList
実行時に、上記のプログラムはPDFドキュメントを作成し、次のメッセージを表示します。
List added
指定したパスを確認すると、以下に示すように、作成されたPDFドキュメントを見つけることができます。
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.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 object
float [] pointColumnWidths = {150F, 150F, 150F};
Table table = new Table(pointColumnWidths);
ステップ5:テーブルにセルを追加する
パッケージ com.itextpdf.layout.element の Cell クラスをインスタンス化して、 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ドキュメントを見つけることができます。
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ドキュメントを見つけることができます。
iText-セルの境界線のフォーマット
この章では、iTextライブラリを使用してテーブル内のセルの境界線をフォーマットする方法を説明します。
セルの境界線のフォーマット
*Document* クラスをインスタンス化することにより、空のPDFドキュメントを作成できます。 このクラスをインスタンス化する際、コンストラクターにパラメーターとして *PdfDocument* オブジェクトを渡す必要があります。
次に、ドキュメントにテーブルを追加するには、 Table クラスをインスタンス化し、* add()*メソッドを使用してこのオブジェクトをドキュメントに追加する必要があります。
*DashedBorder、SolidBorder、DottedBorder、DoubleBorder、RoundDotsBorder* などのさまざまなタイプの境界線を追加できます。 *Cell* クラスの* setBorder()*メソッドを使用してさまざまな色で。
以下は、テーブル内のセルの境界線をフォーマットする手順です。
ステップ1:PdfWriterオブジェクトの作成
*PdfWriter* クラスは、PDFのDocWriterを表します。 このクラスは、パッケージ *com.itextpdf.kernel.pdf* に属します。 このクラスのコンストラクターは、PDFが作成されるファイルのパスを表す文字列を受け入れます。
以下に示すように、文字列値(PDFを作成する必要があるパスを表す)をコンストラクターに渡すことにより、PdfWriterクラスをインスタンス化します。
//Creating a PdfWriter
String dest = "C:/itextExamples/coloredBorders.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:セルの作成
パッケージの Cell クラスをインスタンス化してセルオブジェクトを作成します com.itextpdf.layout.element は、以下に示すように、 Cell クラスの* add()*メソッドを使用してセルのコンテンツを追加します。
//Adding cell 1 to the table
Cell cell1 = new Cell();//Creating a cell
cell1.add("Name"); //Adding content to the cell
ステップ6:セルの境界線をフォーマットする
iTextライブラリは、 DashedBorder、SolidBorder、DottedBorder、DoubleBorder、RoundDotsBorder などの境界を表すさまざまなクラスを提供します。
これらのクラスのコンストラクターは、境界線の色を表す color オブジェクトと境界線の幅を表す integer という2つのパラメーターを受け入れます。
以下に示すように、この境界タイプの1つを選択し、 color オブジェクトと幅を表す integer を渡すことにより、それぞれの境界をインスタンス化します。
Border b1 = new DashedBorder(Color.RED, 3);
次に、 cell クラスの* setBorder()メソッドを使用してセルの境界線を設定します。 このメソッドは、 *Border 型のオブジェクトをパラメーターとして受け入れます。
以下に示すように、上記で作成した Border オブジェクトをパラメーターとして* setBorder()*メソッドに渡すことにより、セルの境界線を設定します。
c1.setBorder(b1)
最後に、このセルをテーブルに追加するには、以下に示すように、 Table クラスの* addCell()メソッドを呼び出し、 *cell オブジェクトをパラメーターとしてこのメソッドに渡します。
table.addCell(c1);
ステップ7:文書に表を追加する
以下に示すように、 Document クラスの* add()メソッドを使用して、前の手順で作成した *table オブジェクトを追加します。
//Adding list to the document
document.add(table);
ステップ8:ドキュメントを閉じる
以下に示すように、 Document クラスの* close()*メソッドを使用してドキュメントを閉じます。
//Closing the document
document.close();
例
次のJavaプログラムは、iTextライブラリを使用してテーブル内のセルの境界線をフォーマットする方法を示しています。 coloredBorders.pdf という名前のPDFドキュメントを作成し、テーブルを追加し、そのセルのコンテンツをフォーマットし、パス C:/itextExamples/ に保存します
このコードを FormatedBorders.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.border.DashedBorder;
import com.itextpdf.layout.border.DottedBorder;
import com.itextpdf.layout.border.DoubleBorder;
import com.itextpdf.layout.border.RoundDotsBorder;
import com.itextpdf.layout.border.SolidBorder;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.TextAlignment;
public class FormatedBorders {
public static void main(String args[]) throws Exception {
//Creating a PdfWriter object
String dest = "C:/itextExamples/coloredBorders.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);
//Adding row 1 to the table
Cell c1 = new Cell();
//Adding the contents of the cell
c1.add("Name");
//Setting the back ground color of the cell
c1.setBackgroundColor(Color.DARK_GRAY);
//Instantiating the Border class
Border b1 = new DashedBorder(Color.RED, 3);
//Setting the border of the cell
c1.setBorder(b1);
//Setting the text alignment
c1.setTextAlignment(TextAlignment.CENTER);
//Adding the cell to the table
table.addCell(c1);
Cell c2 = new Cell();
c2.add("Raju");
c1.setBorder(new SolidBorder(Color.RED, 3));
c2.setTextAlignment(TextAlignment.CENTER);
table.addCell(c2);
//Adding row 2 to the table
Cell c3 = new Cell();
c3.add("Id");
c3.setBorder(new DottedBorder(Color.DARK_GRAY, 3));
c3.setTextAlignment(TextAlignment.CENTER);
table.addCell(c3);
Cell c4 = new Cell();
c4.add("001");
c4.setBorder(new DoubleBorder(Color.DARK_GRAY, 3));
c4.setTextAlignment(TextAlignment.CENTER);
table.addCell(c4);
//Adding row 3 to the table
Cell c5 = new Cell();
c5.add("Designation");
c5.setBorder(new RoundDotsBorder(Color.RED, 3));
c5.setTextAlignment(TextAlignment.CENTER);
table.addCell(c5);
Cell c6 = new Cell();
c6.add("Programmer");
c6.setBorder(new RoundDotsBorder(Color.RED, 3));
c6.setTextAlignment(TextAlignment.CENTER);
table.addCell(c6);
//Adding Table to document
doc.add(table);
//Closing the document
doc.close();
System.out.println("Borders added successfully..");
}
}
次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します-
javac FormatedBorders.java
java FormatedBorders
実行時に、上記のプログラムはPDFドキュメントを作成し、次のメッセージを表示します。
Borders added successfully
指定したパスを確認すると、以下に示すように、作成されたPDFドキュメントを見つけることができます。
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.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 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ドキュメントを見つけることができます。
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ドキュメントを見つけることができます。
iText-リストをテーブルに追加する
この章では、iTextライブラリを使用してPDF文書の表にリストを追加する方法を説明します。
PDFのテーブルにリストを追加する
*Document* クラスをインスタンス化することにより、空のPDFドキュメントを作成できます。 このクラスをインスタンス化する際、コンストラクターにパラメーターとして *PdfDocument* オブジェクトを渡す必要があります。 次に、ドキュメントにテーブルを追加するには、 *Table* クラスをインスタンス化し、* add()*メソッドを使用してこのオブジェクトをドキュメントに追加する必要があります。
テーブルに*リスト*を追加するには、 com.itextpdf.layout.element パッケージの List クラスをインスタンス化し、 add メソッドを使用して cell オブジェクトに挿入する必要があります。 Cell クラス。
以下は、テーブルのセルにリストを追加する手順です。
ステップ1:PdfWriterオブジェクトの作成
*PdfWriter* クラスは、PDFのDoc Writerを表します。 このクラスは、パッケージ *com.itextpdf.kernel.pdf* に属します。 このクラスのコンストラクターは、PDFが作成されるファイルのパスを表す文字列を受け入れます。
以下に示すように、文字列値(PDFを作成する必要があるパスを表す)をコンストラクターに渡すことにより、PdfWriterクラスをインスタンス化します。
//Creating a PdfWriter
String dest = "C:/itextExamples/addingObjects.pdf";
PdfWriter writer = new PdfWriter(dest);
このタイプのオブジェクトがPdfDocument(クラス)に渡されると、このドキュメントに追加されたすべての要素が指定されたファイルに書き込まれます。
ステップ2:PdfDocumentオブジェクトの作成
*PdfDocument* クラスは、iTextでPDFドキュメントを表すクラスです。 このクラスは、パッケージc * *om.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 listCell = new Cell();//Creating a cell
ステップ6:リストオブジェクトの作成
セルを作成したら、パッケージ com.itextpdf.layout.element の List クラスをインスタンス化して、 list オブジェクトを作成します。 以下に示すように、 ListItem クラスをインスタンス化してリストアイテムを作成し、 List クラスの* add()*メソッドを使用して作成したアイテムを追加します。
List list = new List();
ListItem item1 = new ListItem("JavaFX");
ListItem item2 = new ListItem("Java");
ListItem item3 = new ListItem("Java Servlets");
list.add(item1);
list.add(item2);
list.add(item3);
ステップ7:テーブルのセルにリストを追加する
次に、Cellクラスの* add()メソッドを使用して、上記の作成済みリストをテーブルのセルに追加します。 そして、以下に示すように、 *Table クラスの* addCell()*メソッドを使用して、このセルをテーブルに追加します。
listCell.add(list);
table.addCell(listCell);
ステップ8:文書に表を追加する
以下に示すように、 Document クラスの* add()メソッドを使用して、前の手順で作成した *table オブジェクトを追加します。
//Adding list to the document
document.add(table);
ステップ9:ドキュメントを閉じる
以下に示すように、 Document クラスの* close()*メソッドを使用してドキュメントを閉じます。
//Closing the document
document.close();
例
次のJavaプログラムは、iTextライブラリを使用してPDFドキュメント内のテーブルのセルにリストを追加する方法を示しています。 addingObjects.pdf という名前のPDFドキュメントを作成し、テーブルを追加し、そのセルの1つにリストを挿入して、パス C:/itextExamples/ に保存します
このコードを AddingListsToTable.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.List;
import com.itextpdf.layout.element.ListItem;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.TextAlignment;
public class AddingListsToTable {
public static void main(String args[]) throws Exception {
//Creating a PdfWriter object
String file = "C:/itextExamples/addingObjects.pdf";
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(file));
//Creating a Document object
Document doc = new Document(pdfDoc);
//Creating a table
float [] pointColumnWidths = {300F, 300F};
Table table = new Table(pointColumnWidths);
//Adding row 1 to the table
Cell c1 = new Cell();
c1.add("Java Related Tutorials");
c1.setTextAlignment(TextAlignment.LEFT);
table.addCell(c1);
List list1 = new List();
ListItem item1 = new ListItem("JavaFX");
ListItem item2 = new ListItem("Java");
ListItem item3 = new ListItem("Java Servlets");
list1.add(item1);
list1.add(item2);
list1.add(item3);
Cell c2 = new Cell();
c2.add(list1);
c2.setTextAlignment(TextAlignment.LEFT);
table.addCell(c2);
//Adding row 2 to the table
Cell c3 = new Cell();
c3.add("No SQL Databases");
c3.setTextAlignment(TextAlignment.LEFT);
table.addCell(c3);
List list2 = new List();
list2.add(new ListItem("HBase"));
list2.add(new ListItem("Neo4j"));
list2.add(new ListItem("MongoDB"));
Cell c4 = new Cell();
c4.add(list2);
c4.setTextAlignment(TextAlignment.LEFT);
table.addCell(c4);
//Adding Table to document
doc.add(table);
//Closing the document
doc.close();
System.out.println("Lists added to table successfully..");
}
}
次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します-
javac AddingListsToTable.java
java AddingListsToTable
実行時に、上記のプログラムはPDFドキュメントを作成し、次のメッセージを表示します。
Lists added to table successfully..
指定したパスを確認すると、以下に示すように、作成されたPDFドキュメントを見つけることができます。
iText-PDFへの画像の追加
この章では、iTextライブラリを使用してPDFドキュメントに画像を追加する方法を説明します。
PDFへの画像の追加
*Document* クラスをインスタンス化することにより、空のPDFドキュメントを作成できます。 このクラスをインスタンス化する際、 *PdfDocument* オブジェクトをパラメーターとしてコンストラクターに渡す必要があります。 PDFに画像を追加するには、追加する必要がある画像のオブジェクトを作成し、 *Document* クラスの* add()*メソッドを使用して追加します。
以下は、PDFドキュメントに画像を追加する手順です。
ステップ1:PdfWriterオブジェクトの作成
*PdfWriter* クラスは、PDFのDocWriterを表します。 このクラスは、パッケージ *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.layout の Document クラスは、自給自足のPDFを作成する際のルート要素です。 このクラスのコンストラクターの1つは、クラスPdfDocumentのオブジェクトを受け入れます。
以下に示すように、前の手順で作成した PdfDocument クラスのオブジェクトを渡すことにより、 Document クラスをインスタンス化します。
//Creating a Document Document
document = new Document(pdfDoc);
ステップ4:Imageオブジェクトの作成
*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:文書に画像を追加する
以下に示すように、 Document クラスの* add()*メソッドを使用して、前の手順で作成した画像オブジェクトを追加します。
//Adding image to the document
document.add(img);
ステップ6:ドキュメントを閉じる
以下に示すように、 Document クラスの* close()*メソッドを使用してドキュメントを閉じます。
//Closing the document
document.close();
例
次のJavaプログラムは、iTextライブラリを使用してPDFドキュメントに画像を追加する方法を示しています。 addingImage.pdf という名前のPDFドキュメントを作成し、それに画像を追加して、パス C:/itextExamples/ に保存します。
このコードを AddingImage.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 AddingImage {
public static void main(String args[]) throws Exception {
//Creating a PdfWriter
String dest = "C:/itextExamples/addingImage.pdf";
PdfWriter writer = new PdfWriter(dest);
//Creating a PdfDocument
PdfDocument pdf = new PdfDocument(writer);
//Creating a Document
Document document = new Document(pdf);
//Creating an ImageData object
String imFile = "C:/itextExamples/logo.jpg";
ImageData data = ImageDataFactory.create(imFile);
//Creating an Image object
Image image = new Image(data);
//Adding image to the document
document.add(image);
//Closing the document
document.close();
System.out.println("Image added");
}
}
次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します-
javac AddingImage.java
java AddingImage
実行時に、上記のプログラムは、次のメッセージを表示するPDFドキュメントを作成します。
Image added
指定したパスを確認すると、以下に示すように、作成されたPDFドキュメントを見つけることができます。
iText-画像の位置を設定する
この章では、iTextライブラリを使用してPDFドキュメント内の画像の位置を設定する方法を説明します。
画像の位置を設定する
*Document* クラスをインスタンス化することにより、空のPDFドキュメントを作成できます。 このクラスをインスタンス化する際、コンストラクターにパラメーターとして *PdfDocument* オブジェクトを渡す必要があります。
PDFに画像を追加するには、追加する必要がある画像のオブジェクトを作成し、 Document クラスの* add()メソッドを使用して追加します。 *Image クラスのメソッド* setFixedPosition()*を使用して、ドキュメント上の目的の位置に画像を挿入できます。
以下は、PDFドキュメント内の画像の位置を設定する手順です。
ステップ1:PdfWriterオブジェクトの作成
*PdfWriter* クラスは、PDFのDocWriterを表します。 このクラスは、パッケージ *com.itextpdf.kernel.pdf* に属します。 このクラスのコンストラクターは、PDFが作成されるファイルのパスを表す文字列を受け入れます。
以下に示すように、文字列値(PDFを作成する必要があるパスを表す)をコンストラクターに渡すことにより、PdfWriterクラスをインスタンス化します。
//Creating a PdfWriter
String dest = "C:/itextExamples/positionOfImage.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: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:画像の位置を設定する
Imageの* setFixedPosition()*メソッドを使用して、PDFドキュメント内の画像の位置を設定できます。 以下に示すように、この方法を使用して、ドキュメント上の座標(100、250)に画像の位置を設定します。
//Setting the position of the image to the center of the page
image.setFixedPosition(100, 250);
ステップ6:文書に画像を追加する
次に、以下に示すように、 Document クラスの* add()*メソッドを使用して、前の手順で作成した画像オブジェクトを追加します。
//Adding image to the document
document.add(img);
ステップ7:ドキュメントを閉じる
以下に示すように、 Document クラスの* close()*メソッドを使用してドキュメントを閉じます。
//Closing the document
document.close();
例
次のJavaプログラムは、iTextライブラリを使用してPDFドキュメントの目的の位置に画像を設定する方法を示しています。 positionOfImage.pdf という名前のPDF文書を作成し、それに画像を追加し、ページの中央近くに設定し、パス C:/itextExamples/ に保存します。
このコードを SettingPosition.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 SettingPosition {
public static void main(String args[]) throws Exception {
//Creating a PdfWriter
String dest = "C:/EXAMPLES/itextExamples/3images/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:/EXAMPLES/itextExamples/3images/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 added");
}
}
次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します。
javac SettingPosition.java
java SettingPosition
実行時に、上記のプログラムはPDFドキュメントを作成し、次のメッセージを表示します。
Image added
指定されたパスを確認すると、以下に示すように作成されたPDFドキュメントを見つけることができます。
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.layout の Document クラスは、自給自足の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ドキュメントを見つけることができます。
iText-画像の回転
この章では、iTextライブラリで使用可能な関数を使用して、PDFドキュメントに画像を挿入する方法と、その画像を回転させる方法を説明します。
PDF内の画像を回転させる
*Document* クラスをインスタンス化することにより、空のPDFドキュメントを作成できます。 このクラスをインスタンス化する際、コンストラクターにパラメーターとして *PdfDocument* オブジェクトを渡す必要があります。
PDFに画像を追加するには、追加する必要がある画像のオブジェクトを作成し、 Document クラスの* add()*メソッドを使用して追加します。 * setRotationAngle()*メソッドを使用して画像を回転できます。
以下は、PDFドキュメント内の画像を回転させる手順です。
ステップ1:PdfWriterオブジェクトの作成
*PdfWriter* クラスは、PDFのDocWriterを表します。 このクラスは、パッケージ *com.itextpdf.kernel.pdf* に属します。 このクラスのコンストラクターは、PDFが作成されるファイルのパスを表す文字列を受け入れます。
以下に示すように、文字列値(PDFを作成する必要があるパスを表す)をコンストラクターに渡すことにより、PdfWriterクラスをインスタンス化します。
//Creating a PdfWriter
String dest = "C:/itextExamples/rotatingImage.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: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:画像の回転
- setRotationAngle()*メソッドを使用して画像を回転できます。 このメソッドには、画像を回転させる回転角度を表す整数を渡す必要があります。
//Rotating the image
image.setRotationAngle(45);
ステップ6:文書に画像を追加する
次に、以下に示すように、 Document クラスの* add()*メソッドを使用して、前の手順で作成した画像オブジェクトを追加します。
//Adding image to the document
document.add(img);
ステップ7:ドキュメントを閉じる
以下に示すように、 Document クラスの* close()*メソッドを使用してドキュメントを閉じます。
//Closing the document
document.close();
例
次のJavaプログラムは、iTextライブラリを使用して、PDFドキュメントで特定の角度だけ画像を回転する方法を示しています。
*rotatingImage.pdf* という名前のPDFドキュメントを作成し、それに画像を追加し、回転させ、パス *C:/itextExamples/* に保存します。
このコードを RotatingImage.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 RotatingImage {
public static void main(String args[]) throws Exception {
//Creating a PdfWriter
String dest = "C:/itextExamples/rotatingImage.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);
//Rotating the image
image.setRotationAngle(45);
//Adding image to the document
document.add(image);
//Closing the document
document.close();
System.out.println("Image rotated");
}
}
次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します-
javac RotatingImage.java
java RotatingImage
実行時に、上記のプログラムは、次のメッセージを表示するPDFドキュメントを作成します。
Image Rotated
指定されたパスを確認すると、以下に示すように作成されたPDFドキュメントを見つけることができます。
iText-テキスト注釈
この章では、iTextライブラリを使用してPDF文書にテキスト注釈を追加する方法を説明します。
PDFでのテキスト注釈の作成
*Document* クラスをインスタンス化することにより、空のPDFドキュメントを作成できます。 このクラスをインスタンス化する際、コンストラクターにパラメーターとして *PdfDocument* オブジェクトを渡す必要があります。
PDFドキュメントでテキスト注釈を使用するには、 PdfTextAnnotation クラスのオブジェクトを作成し、これを PdfPage に追加する必要があります。
PDFドキュメントでテキスト注釈を使用する手順は次のとおりです。
ステップ1:PdfWriterオブジェクトの作成
*PdfWriter* クラスは、PDFのDocWriterを表します。 このクラスは、パッケージ *com.itextpdf.kernel.pdf* に属します。 このクラスのコンストラクターは、PDFが作成されるファイルのパスを表す文字列を受け入れます。
以下に示すように、文字列値(PDFを作成する必要があるパスを表す)をコンストラクターに渡すことにより、PdfWriterクラスをインスタンス化します。
//Creating a PdfWriter
String dest = "C:/itextExamples/textAnnotation.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:PdfAnnotationオブジェクトの作成
パッケージ com.itextpdf.kernel.pdf.annot の PdfAnnotation クラスは、すべての注釈のスーパークラスを表します。
派生クラスの中で、 PdfTextAnnotation クラスはテキスト注釈を表します。 以下に示すように、このクラスのオブジェクトを作成します。
//Creating PdfAnnotation
Rectangle rect = new Rectangle(20, 800, 0, 0);
PdfAnnotation ann = new PdfTextAnnotation(rect);
ステップ5:注釈の色を設定する
*PdfAnnotation* クラスの* setColor()*メソッドを使用して、注釈に色を設定します。 このメソッドには、注釈の色を表す *color* オブジェクトをパラメーターとして渡します。
//Setting color to the annotation
ann.setColor(Color.GREEN);
ステップ6:注釈のタイトルと内容を設定する
以下に示すように、それぞれ PdfAnnotation クラスの* setTitle()および setContents()*メソッドを使用して、注釈のタイトルと内容を設定します。
//Setting title to the annotation
ann.setTitle(new PdfString("Hello"));
//Setting contents of the annotation
ann.setContents("Hi welcome to finddevguides.");
ステップ7:ページに注釈を追加する
以下に示すように、PdfDocumentクラスの* addNewPage()メソッドを使用して新しい *PdfPage クラスを作成し、 PdfPage クラスの* addAnnotation()*メソッドを使用して上記の注釈を追加します。
//Creating a new page PdfPage page =
pdf.addNewPage();
//Adding annotation to a page in a PDF
page.addAnnotation(ann);
ステップ8:ドキュメントを閉じる
以下に示すように、 Document クラスの* close()*メソッドを使用してドキュメントを閉じます。
//Closing the document
document.close();
例
次のJavaプログラムは、iTextライブラリを使用してPDF文書にテキスト注釈を追加する方法を示しています。 textAnnotation.pdf という名前のPDFドキュメントを作成し、それにテキスト注釈を追加し、パス C:/itextExamples/ に保存します
このコードを TextAnnotation.java という名前のファイルに保存します。
import com.itextpdf.kernel.color.Color;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfString;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
import com.itextpdf.kernel.pdf.annot.PdfTextAnnotation;
import com.itextpdf.layout.Document;
public class TextAnnotation {
public static void main(String args[]) throws Exception {
//Creating a PdfWriter
String dest = "C:/itextExamples/textAnnotation.pdf";
PdfWriter writer = new PdfWriter(dest);
//Creating a PdfDocument
PdfDocument pdf = new PdfDocument(writer);
//Creating a Document
Document document = new Document(pdf);
//Creating PdfTextAnnotation object
Rectangle rect = new Rectangle(20, 800, 0, 0);
PdfAnnotation ann = new PdfTextAnnotation(rect);
//Setting color to the annotation
ann.setColor(Color.GREEN);
//Setting title to the annotation
ann.setTitle(new PdfString("Hello"));
//Setting contents of the annotation
ann.setContents("Hi welcome to finddevguides.");
//Creating a new page
PdfPage page = pdf.addNewPage();
//Adding annotation to a page in a PDF
page.addAnnotation(ann);
//Closing the document
document.close();
System.out.println("Annotation added successfully");
}
}
次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します。
javac TextAnnotation.java
java TextAnnotation
実行時に、上記のプログラムは、次のメッセージを表示するPDFドキュメントを作成します。
Annotation added successfully
指定したパスを確認すると、以下に示すように、作成されたPDFドキュメントを見つけることができます。
iText-リンク注釈
この章では、iTextライブラリを使用してPDF文書にリンク注釈を追加する方法を説明します。
PDFでのリンク注釈の作成
*Document* クラスをインスタンス化することにより、空のPDFドキュメントを作成できます。 このクラスをインスタンス化する際、コンストラクターにパラメーターとして *PdfDocument* オブジェクトを渡す必要があります。
PDFドキュメントでテキスト注釈を使用するには、PdfTextAnnotationクラスのオブジェクトを作成し、これをPdfPageに追加する必要があります。
PDFドキュメントでテキスト注釈を使用する手順は次のとおりです。
ステップ1:PdfWriterオブジェクトの作成
*PdfWriter* クラスは、PDFのDocWriterを表します。 このクラスは、パッケージ *com.itextpdf.kernel.pdf* に属します。 このクラスのコンストラクターは、PDFが作成されるファイルのパスを表す文字列を受け入れます。
以下に示すように、コンストラクターにストリング値(PDFを作成する必要があるパスを表す)を渡すことにより、 PdfWriter クラスをインスタンス化します。
//Creating a PdfWriter
String dest = "C:/itextExamples/linkAnnotation.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:PdfAnnotationオブジェクトの作成
パッケージ com.itextpdf.kernel.pdf.annot の PdfAnnotation クラスは、すべての注釈のスーパークラスを表します。
派生クラスの中で、 PdfLinkAnnotation クラスはリンクアノテーションを表します。 以下に示すように、このクラスのオブジェクトを作成します。
//Creating a PdfLinkAnnotation object
Rectangle rect = new Rectangle(0, 0);
PdfLinkAnnotation annotation = new PdfLinkAnnotation(rect);
ステップ5:注釈のアクションを設定する
以下に示すように、 PdfLinkAnnotation クラスの* setAction()*メソッドを使用して、アクションを注釈に設定します。
//Setting action of the annotation
PdfAction action = PdfAction.createURI("http://www.finddevguides.com/");
annotation.setAction(action);
ステップ6:リンクを作成する
以下に示すように、パッケージ com.itextpdf.layout.element の Link クラスをインスタンス化して、リンクを作成します。
//Creating a link
Link link = new Link("Click here", annotation);
ステップ7:リンクアノテーションを段落に追加する
以下に示すように、 Paragraph クラスをインスタンス化して新しい段落を作成し、このクラスの* add()*メソッドを使用して前の手順で作成したリンクを追加します。
//Creating a paragraph
Paragraph paragraph = new Paragraph("Hi welcome to finddevguides ");
//Adding link to paragraph
paragraph.add(link.setUnderline());
ステップ8:文書に段落を追加する
以下に示すように、 Document クラスの* add()*メソッドを使用して、文書に段落を追加します。
//Adding paragraph to document
document.add(paragraph);
ステップ9:ドキュメントを閉じる
以下に示すように、 Document クラスの* close()*メソッドを使用してドキュメントを閉じます。
//Closing the document
document.close();
例
次のJavaプログラムは、iTextライブラリを使用してPDF文書にリンク注釈を追加する方法を示しています。
*linkAnnotation.pdf* という名前のPDF文書を作成し、それにリンク注釈を追加し、パス *C:/itextExamples/* に保存します
このコードを LinkAnnotation.java という名前のファイルに保存します。
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.action.PdfAction;
import com.itextpdf.kernel.pdf.annot.PdfLinkAnnotation;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Link;
import com.itextpdf.layout.element.Paragraph;
public class LinkAnnotation {
public static void main(String args[]) throws Exception {
//Creating a PdfWriter
String dest = "C:/itextExamples/linkAnnotation.pdf";
PdfWriter writer = new
PdfWriter(dest);
//Creating a PdfDocument
PdfDocument pdf = new PdfDocument(writer);
//Creating a Document
Document document = new Document(pdf);
//Creating a PdfLinkAnnotation object
Rectangle rect = new Rectangle(0, 0);
PdfLinkAnnotation annotation = new PdfLinkAnnotation(rect);
//Setting action of the annotation
PdfAction action = PdfAction.createURI("http://www.finddevguides.com/");
annotation.setAction(action);
//Creating a link
Link link = new Link("Click here", annotation);
//Creating a paragraph
Paragraph paragraph = new Paragraph("Hi welcome to finddevguides ");
//Adding link to paragraph
paragraph.add(link.setUnderline());
//Adding paragraph to document
document.add(paragraph);
//Closing the document
document.close();
System.out.println("Annotation added successfully");
}
}
次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します-
javac LinkAnnotation.java
java LinkAnnotation
実行時に、上記のプログラムは、次のメッセージを表示するPDFドキュメントを作成します。
Annotation added successfully
指定したパスを確認すると、以下に示すように、作成されたPDFドキュメントを見つけることができます。
iText-行注釈
この章では、iTextライブラリを使用してPDFドキュメントに行注釈を追加する方法を説明します。
PDFでのラインアノテーションの作成
*Document* クラスをインスタンス化することにより、空のPDFドキュメントを作成できます。 このクラスをインスタンス化する際、 *PdfDocument* オブジェクトをパラメーターとしてコンストラクターに渡す必要があります。
PDFドキュメントでテキスト注釈を使用するには、 PdfTextAnnotation クラスのオブジェクトを作成し、これを PdfPage に追加する必要があります。
PDFドキュメントでテキスト注釈を使用する手順は次のとおりです。
ステップ1:PdfWriterオブジェクトの作成
*PdfWriter* クラスは、PDFのDocWriterを表します。 このクラスは、パッケージ *com.itextpdf.kernel.pdf* に属します。 このクラスのコンストラクターは、PDFが作成されるファイルのパスを表す文字列を受け入れます。
以下に示すように、PDFを作成する必要があるパスを表す文字列値をコンストラクターに渡すことにより、PdfWriterクラスをインスタンス化します。
//Creating a PdfWriter
String dest = "C:/itextExamples/lineAnnotation.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:PdfAnnotationオブジェクトの作成
*com.itextpdf.kernel.pdf.annot* が表すパッケージの *PdfAnnotation* クラスは、すべての注釈のスーパークラスです。
派生クラスの中で、 PdfLineAnnotation クラスは行注釈を表します。 以下に示すように、このクラスのオブジェクトを作成します。
//Creating PdfAnnotation
Rectangle rect = new Rectangle(20, 800, 0, 0);
PdfAnnotation annotation = new PdfLineAnnotation(rect);
ステップ5:注釈の色を設定する
*PdfAnnotation* クラスの* setColor()*メソッドを使用して、注釈に色を設定します。 このメソッドには、注釈の色を表す色オブジェクトをパラメーターとして渡します。
//Setting color to the annotation
annotation.setColor(Color.BLUE);
ステップ6:注釈のタイトルと内容を設定する
以下に示すように、それぞれ PdfAnnotation クラスの* setTitle()および setContents()*メソッドを使用して、注釈のタイトルと内容を設定します。
//Setting title to the PdfLineAnnotation
annotation.setTitle(new PdfString("iText"));
//Setting contents of the PdfLineAnnotation
annotation.setContents("Hi welcome to finddevguides");
ステップ7:ページに注釈を追加する
以下に示すように、 PdfDocument クラスの* addNewPage()メソッドを使用して新しい *PdfPage クラスを作成し、PdfPageクラスのaddAnnotation()メソッドを使用して上記で作成した注釈を追加します。
//Creating a new page
PdfPage page = pdf.addNewPage();
//Adding annotation to a page in a PDF
page.addAnnotation(annotation);
ステップ8:ドキュメントを閉じる
以下に示すように、 Document クラスの* close()*メソッドを使用してドキュメントを閉じます。
//Closing the document
document.close();
例
次のJavaプログラムは、iTextライブラリを使用してPDFドキュメントに行注釈を追加する方法を示しています。 lineAnnotation.pdf という名前のPDFドキュメントを作成し、それにライン注釈を追加して、パス C:/itextExamples/ に保存します。
このコードを LineAnnotation.java という名前のファイルに保存します。
import com.itextpdf.kernel.color.Color;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfString;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
import com.itextpdf.kernel.pdf.annot.PdfLineAnnotation;
import com.itextpdf.layout.Document;
public class LineAnnotation {
public static void main(String args[]) throws Exception {
//Creating a PdfWriter
String dest = "C:/itextExamples/lineAnnotations.pdf";
PdfWriter writer = new PdfWriter(dest);
//Creating a PdfDocument
PdfDocument pdf = new PdfDocument(writer);
//Creating a Document
Document document = new Document(pdf);
//Creating a PdfPage
PdfPage page = pdf.addNewPage();
//creating PdfLineAnnotation object
Rectangle rect = new Rectangle(0, 0);
float[] floatArray = new float[]{
20, 790, page.getPageSize().getWidth() - 20, 790
};
PdfAnnotation annotation = new PdfLineAnnotation(rect, floatArray);
//Setting color of the PdfLineAnnotation
annotation.setColor(Color.BLUE);
//Setting title to the PdfLineAnnotation
annotation.setTitle(new PdfString("iText"));
//Setting contents of the PdfLineAnnotation
annotation.setContents("Hi welcome to finddevguides");
//Adding annotation to the page
page.addAnnotation(annotation);
//Closing the document
document.close();
System.out.println("Annotation added successfully");
}
}
次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します-
javac LineAnnotation.java
java LineAnnotation
実行時に、上記のプログラムは、次のメッセージを表示するPDFドキュメントを作成します。
Annotation added successfully
指定されたパスを確認すると、以下に示すように作成されたPDFドキュメントを見つけることができます。
iText-マークアップ注釈
この章では、iTextライブラリを使用してPDF文書にテキストマークアップ注釈を追加する方法を説明します。
PDFでのマークアップ注釈の作成
*Document* クラスをインスタンス化することにより、空のPDFドキュメントを作成できます。 このクラスをインスタンス化する際、コンストラクターにパラメーターとして *PdfDocument* オブジェクトを渡す必要があります。 PDFドキュメントでテキスト注釈を使用するには、 *PdfTextAnnotation* クラスのオブジェクトを作成し、これを *PdfPage* に追加する必要があります。
PDFドキュメントでテキスト注釈を使用する手順は次のとおりです。
ステップ1:PdfWriterオブジェクトの作成
*PdfWriter* クラスは、PDFのDocWriterを表します。 このクラスは、パッケージ *com.itextpdf.kernel.pdf* に属します。 このクラスのコンストラクターは、PDFが作成されるファイルのパスを表す文字列を受け入れます。
以下に示すように、文字列値(PDFを作成する必要があるパスを表す)をコンストラクターに渡すことにより、PdfWriterクラスをインスタンス化します。
//Creating a PdfWriter
String dest = "C:/itextExamples/markupAnnotation.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:PdfAnnotationオブジェクトの作成
パッケージ com.itextpdf.kernel.pdf.annot の PdfAnnotation クラスは、すべての注釈のスーパークラスを表します。
派生クラスの中で、 PdfTextMarkupAnnotation クラスはテキストマークアップアノテーションを表します。 以下に示すように、このクラスのオブジェクトを作成します。
//Creating a PdfTextMarkupAnnotation object
Rectangle rect = new Rectangle(105, 790, 64, 10);
float[] floatArray = new float[]{169, 790, 105, 790, 169, 800, 105, 800};
PdfAnnotation annotation = PdfTextMarkupAnnotation.createHighLight(rect,floatArray);
ステップ5:注釈の色を設定する
*PdfAnnotation* クラスの* setColor()*メソッドを使用して、注釈に色を設定します。 このメソッドには、注釈の*色*を表す色オブジェクトをパラメーターとして渡します。
//Setting color to the annotation
annotation.setColor(Color.YELLOW);
ステップ6:注釈のタイトルと内容を設定する
*PdfAnnotation* クラスの* setTitle()*および* setContents()*メソッドをそれぞれ使用して、注釈のタイトルと内容を設定します。
//Setting title to the annotation
annotation.setTitle(new PdfString("Hello!"));
//Setting contents to the annotation
annotation.setContents(new PdfString("Hi welcome to finddevguides"));
ステップ7:ページに注釈を追加する
以下に示すように、 PdfDocument クラスの* addNewPage()メソッドを使用して新しい *PdfPage クラスを作成し、PdfPageクラスの* addAnnotation()*メソッドを使用して上記で作成した注釈を追加します。
//Creating a new Pdfpage
PdfPage pdfPage = pdfDoc.addNewPage();
//Adding annotation to a page in a PDF
pdfPage.addAnnotation(annotation);
ステップ8:ドキュメントを閉じる
以下に示すように、 Document クラスの* close()*メソッドを使用してドキュメントを閉じます。
//Closing the document
document.close();
例
次のJavaプログラムは、iTextライブラリを使用してテキストマークアップ注釈をPDFドキュメントに追加する方法を示しています。 markupAnnotation.pdf という名前のPDFドキュメントを作成し、それにテキストマークアップアノテーションを追加し、パス C:/itextExamples/ に保存します。
このコードを MarkupAnnotation.java という名前のファイルに保存します。
import com.itextpdf.kernel.color.Color;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfString;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
import com.itextpdf.kernel.pdf.annot.PdfTextMarkupAnnotation;
import com.itextpdf.layout.Document;
public class MarkupAnnotation {
public static void main(String args[]) throws Exception {
//Creating a PdfDocument object
String file = "C:/itextExamples/markupAnnotation.pdf";
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(file));
//Creating a Document object
Document doc = new Document(pdfDoc);
//Creating a PdfTextMarkupAnnotation object
Rectangle rect = new Rectangle(105, 790, 64, 10);
float[] floatArray = new float[]{169, 790, 105, 790, 169, 800, 105, 800};
PdfAnnotation annotation =
PdfTextMarkupAnnotation.createHighLight(rect,floatArray);
//Setting color to the annotation
annotation.setColor(Color.YELLOW);
//Setting title to the annotation
annotation.setTitle(new PdfString("Hello!"));
//Setting contents to the annotation
annotation.setContents(new PdfString("Hi welcome to finddevguides"));
//Creating a new Pdfpage
PdfPage pdfPage = pdfDoc.addNewPage();
//Adding annotation to a page in a PDF
pdfPage.addAnnotation(annotation);
//Closing the document
doc.close();
System.out.println("Annotation added successfully");
}
}
次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します-
javac MarkupAnnotation.java
java MarkupAnnotation
実行時に、上記のプログラムは、次のメッセージを表示するPDFドキュメントを作成します。
Annotation added successfully
指定されたパスを確認すると、以下に示すように作成されたPDFドキュメントを見つけることができます。
iText-サークル注釈
この章では、iTextライブラリを使用してPDFドキュメントに円注釈を追加する方法を説明します。
PDFでの円注釈の作成
*Document* クラスをインスタンス化することにより、空のPDFドキュメントを作成できます。 このクラスをインスタンス化する際、コンストラクターにパラメーターとして *PdfDocument* オブジェクトを渡す必要があります。
PDFドキュメントでテキスト注釈を使用するには、PdfTextAnnotationクラスのオブジェクトを作成し、これを Pdfpage に追加する必要があります。
PDFドキュメントでテキスト注釈を使用する手順は次のとおりです。
ステップ1:PdfWriterオブジェクトの作成
*PdfWriter* クラスは、PDFのDocWriterを表します。 このクラスは、パッケージ *com.itextpdf.kernel.pdf* に属します。 このクラスのコンストラクターは、PDFが作成されるファイルのパスを表す文字列を受け入れます。
以下に示すように、コンストラクターにストリング値(PDFを作成する必要があるパスを表す)を渡すことにより、 PdfWriter クラスをインスタンス化します。
//Creating a PdfWriter
String dest = "C:/itextExamples/circleAnnotation.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:PdfAnnotationオブジェクトの作成
パッケージ com.itextpdf.kernel.pdf.annot の PdfAnnotation クラスは、すべての注釈のスーパークラスを表します。
派生クラスの中で、 PdfCircleAnnotation クラスは円注釈を表します。 以下に示すように、このクラスのオブジェクトを作成します。
//Creating a PdfCircleAnnotation object Rectangle
rect = new Rectangle(150, 770, 50, 50);
PdfAnnotation annotation = new PdfCircleAnnotation(rect);
ステップ5:注釈の色を設定する
*PdfAnnotation* クラスの* setColor()*メソッドを使用して、注釈に色を設定します。 このメソッドには、注釈の色を表す色オブジェクトをパラメーターとして渡します。
//Setting color to the annotation
annotation.setColor(Color.YELLOW);
ステップ6:注釈のタイトルと内容を設定する
*PdfAnnotation* クラスの* setTitle()*および* setContents()*メソッドをそれぞれ使用して、注釈のタイトルと内容を設定します。
//Setting title to the annotation
annotation.setTitle(new PdfString("circle annotation"));
//Setting contents of the annotation
annotation.setContents(new PdfString("Hi welcome to finddevguides"));
ステップ7:ページに注釈を追加する
以下に示すように、 PdfDocument クラスの* addNewPage()メソッドを使用して新しい *PdfPage クラスを作成し、PdfPageクラスの* addAnnotation()*メソッドを使用して上記で作成した注釈を追加します。
//Creating a new page
PdfPage page = pdf.addNewPage();
//Adding annotation to a page in a PDF
page.addAnnotation(ann);
ステップ8:ドキュメントを閉じる
以下に示すように、 Document クラスの* close()*メソッドを使用してドキュメントを閉じます。
//Closing the document
document.close();
例
次のJavaプログラムは、iTextライブラリを使用してPDFドキュメントに円注釈を追加する方法を示しています。 circleAnnotation.pdf という名前のPDFドキュメントを作成し、それに円注釈を追加し、パス C:/itextExamples/ に保存します
このコードを PdfCircleAnnotation.java という名前のファイルに保存します。
import com.itextpdf.kernel.color.Color;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfString;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
import com.itextpdf.kernel.pdf.annot.PdfCircleAnnotation;
import com.itextpdf.layout.Document;
public class CircleAnnotation {
public static void main(String args[]) throws Exception {
//Creating a PdfDocument object
String file = "C:/itextExamples//circleAnnotation.pdf";
PdfDocument pdf = new PdfDocument(new PdfWriter(file));
//Creating a Document object
Document doc = new Document(pdf);
//Creating a PdfCircleAnnotation object
Rectangle rect = new Rectangle(150, 770, 50, 50);
PdfAnnotation annotation = new PdfCircleAnnotation(rect);
//Setting color to the annotation
annotation.setColor(Color.YELLOW);
//Setting title to the annotation
annotation.setTitle(new PdfString("circle annotation"));
//Setting contents of the annotation
annotation.setContents(new PdfString("Hi welcome to finddevguides"));
//Creating a new page
PdfPage page = pdf.addNewPage();
//Adding annotation to a page in a PDF
page.addAnnotation(annotation);
//Closing the document
doc.close();
System.out.println("Annotation added successfully");
}
}
次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します-
javac PdfCircleAnnotation.java
java PdfCircleAnnotation
実行時に、上記のプログラムは、次のメッセージを表示するPDFドキュメントを作成します。
Annotation added successfully
指定したパスを確認すると、以下に示すように、作成されたPDFドキュメントを見つけることができます。
iText-弧を描く
この章では、iTextライブラリを使用してPDFドキュメントに円弧を描く方法を説明します。
PDFに弧を描く
*Document* クラスをインスタンス化することにより、空のPDFドキュメントを作成できます。 このクラスをインスタンス化する際、コンストラクターにパラメーターとして *PdfDocument* オブジェクトを渡す必要があります。
PdfDocumentに弧を描くには、パッケージ com.itextpdf.kernel.pdf .canvasの PdfCanvas クラスをインスタンス化し、このクラスの* arc()*メソッドを使用して弧を作成します。
以下は、PDFドキュメントに円弧を描く手順です。
ステップ1:PdfWriterオブジェクトの作成
*PdfWriter* クラスは、PDFのDocWriterを表します。 このクラスは、パッケージ *com.itextpdf.kernel.pdf* に属します。 このクラスのコンストラクターは、PDFが作成されるファイルのパスを表す文字列を受け入れます。
以下に示すように、文字列値(PDFを作成する必要があるパスを表す)をコンストラクターに渡すことにより、PdfWriterクラスをインスタンス化します。
//Creating a PdfWriter
String dest = "C:/itextExamples/drawingArc.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:PdfCanvasオブジェクトの作成
*PdfDocument* クラスの* addNewPage()*メソッドを使用して、新しい *PdfPage* クラスを作成します。
以下に示すように、上記で作成した PdfPage オブジェクトをこのクラスのコンストラクターに渡すことにより、パッケージ com.itextpdf.kernel.pdf.canvas の PdfCanvas オブジェクトをインスタンス化します。
//Creating a new page
PdfPage pdfPage = pdfDoc.addNewPage();
//Creating a PdfCanvas object
PdfCanvas canvas = new PdfCanvas(pdfPage);
ステップ5:弧を描く
以下に示すように、 Canvas クラスの* arc()メソッドを使用して円弧を描き、 fill()*メソッドを使用して塗りつぶします。
//Drawing an arc
canvas.arc(50, 50, 300, 545, 0, 360);
//Filling the arc
canvas.fill();
ステップ6:ドキュメントを閉じる
以下に示すように、 Document クラスの* close()*メソッドを使用してドキュメントを閉じます。
//Closing the document
document.close();
例
次のJavaプログラムは、iTextライブラリを使用してPDFドキュメントに円弧を描く方法を示しています。
*drawingArc.pdf* という名前のPDF文書を作成し、その中に弧を描き、パス *C:/itextExamples/* に保存します
このコードを DrawingArc.java という名前のファイルに保存します。
import com.itextpdf.kernel.color.Color;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Document;
public class DrawingArc {
public static void main(String args[]) throws Exception {
//Creating a PdfWriter
String dest = "C:/itextExamples/drawingArc.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 new page
PdfPage pdfPage = pdfDoc.addNewPage();
//Creating a PdfCanvas object
PdfCanvas canvas = new PdfCanvas(pdfPage);
//Drawing an arc
canvas.arc(50, 50, 300, 545, 0, 360);
//Filling the arc
canvas.fill();
//Closing the document
doc.close();
System.out.println("Object drawn on pdf successfully");
}
}
次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します-
javac DrawingArc.java
java DrawingArc
実行時に、上記のプログラムはPDFドキュメントを作成し、次のメッセージを表示します。
Object drawn on pdf successfully
指定したパスを確認すると、以下に示すように、作成されたPDFドキュメントを見つけることができます。
iText-線を描く
この章では、iTextライブラリを使用してPDFドキュメントに線を引く方法を説明します。
PDFに線を描く
*Document* クラスをインスタンス化することにより、空のPDFドキュメントを作成できます。 このクラスをインスタンス化する際、 *PdfDocument* オブジェクトをパラメーターとしてコンストラクターに渡す必要があります。
PdfDocumentに線を引くには、パッケージ com.itextpdf.kernel.pdf.canvas の PdfCanvas クラスをインスタンス化し、このクラスの* moveTo()および lineTO()*メソッドを使用して線を作成します。
以下は、pdfドキュメントに線を引く手順です。
ステップ1:PdfWriterオブジェクトの作成
*PdfWriter* クラスは、PDFのDocWriterを表します。 このクラスは、パッケージ *com.itextpdf.kernel.pdf* に属します。 このクラスのコンストラクターは、PDFが作成されるファイルのパスを表す文字列を受け入れます。
以下に示すように、文字列値(PDFを作成する必要があるパスを表す)をコンストラクターに渡すことにより、PdfWriterクラスをインスタンス化します。
//Creating a PdfWriter
String dest = "C:/itextExamples/drawingLine.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:PdfCanvasオブジェクトの作成
*PdfDocument* クラスの* addNewPage()*メソッドを使用して、新しい *PdfPage* クラスを作成します。
以下に示すように、上記で作成した PdfPage オブジェクトをこのクラスのコンストラクターに渡すことにより、パッケージ com.itextpdf.kernel.pdf.canvas の PdfCanvas オブジェクトをインスタンス化します。
//Creating a new page
PdfPage pdfPage = pdfDoc.addNewPage();
//Creating a PdfCanvas object
PdfCanvas canvas = new PdfCanvas(pdfPage);
ステップ5:線を引く
以下に示すように、 Canvas クラスの* moveTO()*メソッドを使用して、行の初期点を設定します。
//Initial point of the line
canvas.moveTo(100, 300);
次に、以下に示すように、* lineTo()*メソッドを使用して、このポイントから別のポイントまで線を引きます。
//Drawing the line
canvas.lineTo(500, 300);
ステップ6:ドキュメントを閉じる
以下に示すように、 Document クラスの* close()*メソッドを使用してドキュメントを閉じます。
//Closing the document
document.close();
例
次のJavaプログラムは、iTextライブラリを使用してPDF文書に線を描画する方法を示しています。 drawingLine.pdf という名前のPDF文書を作成し、その中に弧を描き、パス C:/itextExamples/ に保存します
このコードを DrawingLine.java という名前のファイルに保存します。
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Document;
public class DrawingLine {
public static void main(String args[]) throws Exception {
//Creating a PdfWriter
String dest = "C:/itextExamples/drawingLine.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 new page
PdfPage pdfPage = pdfDoc.addNewPage();
//Creating a PdfCanvas object
PdfCanvas canvas = new PdfCanvas(pdfPage);
//Initial point of the line
canvas.moveTo(100, 300);
//Drawing the line
canvas.lineTo(500, 300);
//Closing the path stroke
canvas.closePathStroke();
//Closing the document
doc.close();
System.out.println("Object drawn on pdf successfully");
}
}
次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します-
javac DrawingLine.java
java DrawingLine
実行時に、上記のプログラムはPDFドキュメントを作成し、次のメッセージを表示します。
Object drawn on pdf successfully
指定したパスを確認すると、以下に示すように、作成されたPDFドキュメントを見つけることができます。
iText-円を描く
この章では、iTextライブラリを使用してPDFドキュメントに円を描く方法を説明します。
PDFに円を描く
*Document* クラスをインスタンス化することにより、空のPDFドキュメントを作成できます。 このクラスをインスタンス化する際、コンストラクターにパラメーターとして *PdfDocument* オブジェクトを渡す必要があります。
PdfDocumentに円を描くには、パッケージ com.itextpdf.kernel.pdf .canvasの PdfCanvas クラスをインスタンス化し、このクラスの* circle()*メソッドを呼び出します。
以下は、PDFドキュメントに円を描く手順です。
ステップ1:PdfWriterオブジェクトの作成
*PdfWriter* クラスは、PDFのDocWriterを表します。 このクラスは、パッケージ *com.itextpdf.kernel.pdf* に属します。 このクラスのコンストラクターは、PDFが作成されるファイルのパスを表す文字列を受け入れます。
以下に示すように、文字列値(PDFを作成する必要があるパスを表す)をコンストラクターに渡すことにより、PdfWriterクラスをインスタンス化します。
//Creating a PdfWriter
String dest = "C:/itextExamples/drawingCircle.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:PdfCanvasオブジェクトの作成
*PdfDocument* クラスの* addNewPage()*メソッドを使用して、新しい *PdfPage* クラスを作成します。 以下に示すように、このクラスのコンストラクターに *PdfPage* オブジェクトを渡すことにより、パッケージ *com.itextpdf.kernel.pdf.canvas* の *PdfCanvas* オブジェクトをインスタンス化します。
//Creating a new page
PdfPage pdfPage = pdfDoc.addNewPage();
//Creating a PdfCanvas object
PdfCanvas canvas = new PdfCanvas(pdfPage);
ステップ5色を設定する
以下に示すように、 Canvas クラスの* setColor()*メソッドを使用して、円の色を設定します。
//Setting color to the circle
Color color = Color.GREEN;
canvas.setColor(color, true);
ステップ6:円を描く
以下に示すように、 Canvas の* circle()*メソッドを呼び出して円を描きます。
//creating a circle
canvas.circle(300, 400, 200);
ステップ7:ドキュメントを閉じる
以下に示すように、 Document クラスの* close()*メソッドを使用してドキュメントを閉じます。
//Closing the document
document.close();
例
次のJavaプログラムは、iTextライブラリを使用してPDFドキュメントに円を描く方法を示しています。 drawingCircle.pdf という名前のPDF文書を作成し、その中に円を描き、パス C:/itextExamples/ に保存します
このコードを DrawingCircle.java という名前のファイルに保存します。
import com.itextpdf.kernel.color.Color;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Document;
public class DrawingCircle {
public static void main(String args[]) throws Exception {
//Creating a PdfWriter
String dest = "C:/itextExamples/drawingCircle.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 new page
PdfPage pdfPage = pdfDoc.addNewPage();
//Creating a PdfCanvas object
PdfCanvas canvas = new PdfCanvas(pdfPage);
//Setting color to the circle
Color color = Color.GREEN;
canvas.setColor(color, true);
//creating a circle
canvas.circle(300, 400, 200);
//Filling the circle
canvas.fill();
//Closing the document
doc.close();
System.out.println("Object drawn on pdf successfully");
}
}
次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します。
javac DrawingCircle.java
java DrawingCircle
実行時に、上記のプログラムは、次のメッセージを表示するPDFドキュメントを作成します。
Object drawn on pdf successfully
指定したパスを確認すると、以下に示すように、作成されたPDFドキュメントを見つけることができます。
iText-フォントの設定
この章では、iTextライブラリを使用してPDFドキュメント内のテキストに色とフォントを設定する方法を説明します。
PDFのテキストのフォントの設定
*Document* クラスをインスタンス化することにより、空のPDFドキュメントを作成できます。 このクラスをインスタンス化する際、コンストラクターにパラメーターとして *PdfDocument* オブジェクトを渡す必要があります。
文書に段落を追加するには、 Paragraph クラスをインスタンス化し、* add()メソッドを使用してこのオブジェクトを文書に追加する必要があります。 それぞれ setFontColor()および setFont()*メソッドを使用して、テキストに色とフォントを設定できます。
以下は、PDFドキュメントのテキストに色とフォントを設定する手順です。
ステップ1:PdfWriterオブジェクトの作成
*PdfWriter* クラスは、PDFのDocWriterを表します。 このクラスは、パッケージ *com.itextpdf.kernel.pdf* に属します。 このクラスのコンストラクターは、PDFが作成されるファイルのパスを表す文字列を受け入れます。
以下に示すように、文字列値(PDFを作成する必要があるパスを表す)をコンストラクターに渡すことにより、PdfWriterクラスをインスタンス化します。
//Creating a PdfWriter
String dest = "C:/itextExamples/fonts.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:テキストの作成
以下に示すように、パッケージ com.itextpdf.layout.element の Text クラスをインスタンス化して、テキストを作成します。
//Creating text object
Text text = new Text("finddevguides");
ステップ5:テキストにフォントと色を設定する
以下に示すように、パッケージ com.itextpdf.kernel.font のクラス PdfFontFactory の* createFont()メソッドを使用して、 *PdfFont オブジェクトを作成します。
//Setting font of the text PdfFont
font = PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD);
次に、このメソッドに Text クラスの* setFont()メソッドを使用して、フォントをテキストに設定します。 以下に示すように、パラメータとして *PdfFont オブジェクトを渡します。
text1.setFont(font);
テキストに色を設定するには、次に示すように、Textクラスの* setFontColor()*メソッドを呼び出します。
//Setting font color
text.setFontColor(Color.GREEN);
ステップ6:段落にテキストを追加する
*Paragraph* クラスオブジェクトを作成し、以下に示すように、* add()*メソッドを使用して上記で作成したテキストを追加します。
//Creating Paragraph
Paragraph paragraph = new Paragraph();
//Adding text to the paragraph
paragraph.add(text);
ステップ7:文書に段落を追加する
以下に示すように、 Document クラスの* add()*メソッドを使用して、文書に段落を追加します。
doc.add(paragraph1)
ステップ8:ドキュメントを閉じる
以下に示すように、 Document クラスの* close()*メソッドを使用してドキュメントを閉じます。
//Closing the document
document.close();
例
次のJavaプログラムは、iTextライブラリを使用してPDFのテキストに色とフォントを設定する方法を示しています。 fonts.pdf という名前のPDFドキュメントを作成し、テキストをフォーマットし、パス C:/itextExamples/ に保存します
このコードを FormatingTheText.java という名前のファイルに保存します。
import com.itextpdf.io.font.FontConstants;
import com.itextpdf.kernel.color.Color;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Text;
public class FormatingTheText {
public static void main(String args[]) throws Exception {
//Creating a PdfWriter object
String dest = "C:/itextExamples/fonts.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 text object
Text text1 = new Text("finddevguides");
//Setting font of the text
PdfFont font = PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD);
text1.setFont(font);
//Setting font color
text1.setFontColor(Color.GREEN);
//Creating text object
Text text2 = new Text("Simply Easy Learning");
text2.setFont(PdfFontFactory.createFont(FontConstants.HELVETICA));
//Setting font color
text2.setFontColor(Color.BLUE);
//Creating Paragraph
Paragraph paragraph1 = new Paragraph();
//Adding text1 to the paragraph
paragraph1.add(text1);
paragraph1.add(text2);
//Adding paragraphs to the document
doc.add(paragraph1);
doc.close();
System.out.println("Text added to pdf ..");
}
}
次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します-
javac FormatingTheText.java
java FormatingTheText
実行時に、上記のプログラムは、次のメッセージを表示するPDFドキュメントを作成します。
Text added to pdf ..
指定したパスを確認すると、以下に示すように、作成されたPDFドキュメントを見つけることができます。
iText-コンテンツの縮小
この章では、iTextライブラリを使用してPDFドキュメントの画像を拡大縮小する方法を説明します。
PDFのコンテンツを縮小する
以下は、iTextライブラリを使用してPDFページのコンテンツを縮小する手順です。
ステップ1:PdfWriterおよびPdfReaderオブジェクトの作成
*PdfWriter* クラスは、PDFのDocWriterを表します。 このクラスは、パッケージ *com.itextpdf.kernel.pdf* に属します。 このクラスのコンストラクターは、PDFが作成されるファイルのパスを表す文字列を受け入れます。
以下に示すように、文字列値(PDFを作成する必要があるパスを表す)をコンストラクターに渡すことにより、PdfWriterクラスをインスタンス化します。
//Creating a PdfWriter object
String dest = "C:/itextExamples/shrinking.pdf";
PdfWriter writer = new PdfWriter(dest);
既存のPDFからデータを読み取るには、以下に示すように PdfReader オブジェクトを作成します。
//Creating a PdfReader
String src = "C:/itextExamples/pdfWithImage.pdf";
PdfReader reader = new PdfReader(src);
ステップ2:PdfDocumentオブジェクトの作成
*PdfDocument* クラスは、iTextでPDFドキュメントを表すクラスです。 このクラスは、パッケージ *com.itextpdf.kernel.pdf* に属します。 (書き込みモードで)このクラスをインスタンス化するには、クラス *PdfWriter* のオブジェクトをコンストラクターに渡す必要があります。
以下に示すように、 PdfWriter および PdfReader オブジェクトをコンストラクターに渡すことにより、ソースおよび宛先PDFドキュメントを作成します。
//Creating a PdfDocument objects
PdfDocument destpdf = new PdfDocument(writer);
PdfDocument srcPdf = new PdfDocument(reader);
ステップ3:既存のPDFからページを開く
*PdfPage* クラスの* getPage()*メソッドを使用して、ソースPDFからページを取得します。 このオブジェクトを使用して、以下に示すように、ソースドキュメントのページのサイズを取得します。
//Opening a page from the existing PDF
PdfPage origPage = srcPdf.getPage(1);
//Getting the page size
Rectangle orig = origPage.getPageSizeWithRotation();
ステップ4:ソースPDFの内容を縮小する
以下に示すように、 AffineTransform クラスの* getScaleInstance()*メソッドを使用して、ソースドキュメントのページのコンテンツを縮小します。
//Shrink original page content using transformation matrix
AffineTransform transformationMatrix = AffineTransform.getScaleInstance(
page.getPageSize().getWidth()/orig.getWidth()/2,
page.getPageSize().getHeight()/orig.getHeight()/2);
ステップ5:ページのコピー
以下に示すように、前の手順で作成された* affine変換マトリックス*を、宛先PDFドキュメントの canvas オブジェクトのマトリックスに連結します。
//Concatenating the affine transform matrix to the current matrix
PdfCanvas canvas = new PdfCanvas(page);
canvas.concatMatrix(transformationMatrix);
次に、以下に示すように、コピー先PDFの canvas オブジェクトにページコピーをソースドキュメントに追加します。
//Add the object to the canvas
PdfFormXObject pageCopy = origPage.copyAsFormXObject(destpdf);
canvas.addXObject(pageCopy, 0, 0);
ステップ6:Documentオブジェクトの作成
パッケージ com.itextpdf.layout の Document クラスは、自給自足のPDFを作成する際のルート要素です。 このクラスのコンストラクターの1つは、クラスPdfDocumentのオブジェクトを受け入れます。
以下に示すように、クラス PdfDocument のオブジェクトを渡すことにより、 Document クラスをインスタンス化します。
//Creating a Document
Document document = new Document(destpdf);
ステップ7:ドキュメントを閉じる
以下に示すように、 Document クラスの* close()*メソッドを使用してドキュメントを閉じます。
//Closing the document
document.close();
例
次のJavaプログラムは、iTextライブラリを使用してPDFページのコンテンツを縮小する方法を示しています。 shrinkingPDF.pdf という名前のPDFドキュメントを作成し、pdf内の画像を縮小し、パス C:/itextExamples/ に保存します
このコードを ShrinkingPDF.java という名前のファイルに保存します。
import com.itextpdf.kernel.geom.AffineTransform;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
import com.itextpdf.layout.Document;
public class ShrinkPDF {
public static void main(String args[]) throws Exception {
//Creating a PdfWriter object
String dest = "C:/itextExamples/shrinking.pdf";
PdfWriter writer = new PdfWriter(dest);
//Creating a PdfReader
String src = "C:/itextExamples/pdfWithImage.pdf";
PdfReader reader = new PdfReader(src);
//Creating a PdfDocument objects
PdfDocument destpdf = new PdfDocument(writer);
PdfDocument srcPdf = new PdfDocument(reader);
//Opening a page from the existing PDF
PdfPage origPage = srcPdf.getPage(1);
//Getting the page size
Rectangle orig = origPage.getPageSizeWithRotation();
//Adding a page to destination Pdf
PdfPage page = destpdf.addNewPage();
//Scaling the image in a Pdf page
AffineTransform transformationMatrix = AffineTransform.getScaleInstance(
page.getPageSize().getWidth()/orig.getWidth()/2,
page.getPageSize().getHeight()/orig.getHeight()/2);
//Shrink original page content using transformation matrix
PdfCanvas canvas = new PdfCanvas(page);
canvas.concatMatrix(transformationMatrix);
//Add the object to the canvas
PdfFormXObject pageCopy = origPage.copyAsFormXObject(destpdf);
canvas.addXObject(pageCopy, 0, 0);
//Creating a Document object
Document doc = new Document(destpdf);
//Closing the document
doc.close();
System.out.println("Table created successfully..");
}
}
次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します-
javac ShrinkingPDF.java
java ShrinkingPDF
実行時に、上記のプログラムはPDFドキュメントを作成し、次のメッセージを表示します。
Table created successfully..
指定されたパスを確認すると、以下に示すように作成されたPDFドキュメントを見つけることができます。
iText-PDFページのタイリング
次のJavaプログラムは、iTextライブラリを使用してPDFページのコンテンツをさまざまなページにタイル化する方法を示しています。 tilingPdfPages.pdf という名前のPDFドキュメントを作成し、パス C:/itextExamples/ に保存します。
このコードを TilingPDFPages.java という名前のファイルに保存します。
import com.itextpdf.kernel.geom.AffineTransform;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
public class TilingPDFPages {
public static void main(String args[]) throws Exception {
//Creating a PdfWriter object
String dest = "C:/itextExamples/tilingPdfPages.pdf";
PdfWriter writer = new PdfWriter(dest);
//Creating a PdfReader
String src = "C:/itextExamples/pdfWithImage.pdf";
PdfReader reader = new PdfReader(src);
//Creating a PdfDocument objects
PdfDocument destpdf = new PdfDocument(writer);
PdfDocument srcPdf = new PdfDocument(reader);
//Opening a page from the existing PDF
PdfPage origPage = srcPdf.getPage(1);
//Getting the page size
Rectangle orig = origPage.getPageSizeWithRotation();
//Getting the size of the page
PdfFormXObject pageCopy = origPage.copyAsFormXObject(destpdf);
//Tile size
Rectangle tileSize = PageSize.A4.rotate();
AffineTransform transformationMatrix =
AffineTransform.getScaleInstance(tileSize.getWidth()/orig.getWidth() *
2f, tileSize.getHeight()/orig.getHeight()* 2f);
//The first tile
PdfPage page =
destpdf.addNewPage(PageSize.A4.rotate());
PdfCanvas canvas = new PdfCanvas(page);
canvas.concatMatrix(transformationMatrix);
canvas.addXObject(pageCopy, 0, -orig.getHeight()/2f);
//The second tile
page = destpdf.addNewPage(PageSize.A4.rotate());
canvas = new PdfCanvas(page);
canvas.concatMatrix(transformationMatrix);
canvas.addXObject(pageCopy, -orig.getWidth()/2f, -orig.getHeight()/2f);
//The third tile
page = destpdf.addNewPage(PageSize.A4.rotate());
canvas = new PdfCanvas(page);
canvas.concatMatrix(transformationMatrix);
canvas.addXObject(pageCopy, 0, 0);
//The fourth tile
page = destpdf.addNewPage(PageSize.A4.rotate());
canvas = new PdfCanvas(page);
canvas.concatMatrix(transformationMatrix);
canvas.addXObject(pageCopy, -orig.getWidth()/2f, 0);
//closing the documents
destpdf.close();
srcPdf.close();
System.out.println("PDF created successfully..");
}
}
次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します-
javac TilingPDFPages.java
java TilingPDFPages
実行時に、上記のプログラムはPDFドキュメントを作成し、次のメッセージを表示します。
PDF created successfully..
あなたが指定されたパスを確認した場合、以下に示すように、作成されたPDF文書を見つけることができます-
iText-Nアップ
次のJavaプログラムは、iTextライブラリを使用してPDFページでNアップを実行する方法を示しています。 nUppingPDF.pdf という名前のPDFドキュメントを作成し、パス C:/itextExamples/ に保存します
このコードを NUppingPDF.java という名前のファイルに保存します。
import com.itextpdf.kernel.geom.AffineTransform;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
public class NUppingPDF {
public static void main(String args[]) throws Exception {
//Creating a PdfWriter object
String dest = "C:/itextExamples/nUppingPDF.pdf";
PdfWriter writer = new PdfWriter(dest);
//Creating a PdfReader
String src = "C:/itextExamples/pdfWithImage.pdf";
PdfReader reader = new PdfReader(src);
//Creating a PdfDocument objects
PdfDocument destpdf = new PdfDocument(writer);
PdfDocument srcPdf = new PdfDocument(reader);
//Opening a page from the existing PDF
PdfPage origPage = srcPdf.getPage(1);
Rectangle orig = origPage.getPageSize();
PdfFormXObject pageCopy = origPage.copyAsFormXObject(destpdf);
//N-up page
PageSize nUpPageSize = PageSize.A4.rotate();
PdfPage page = destpdf.addNewPage(nUpPageSize);
PdfCanvas canvas = new PdfCanvas(page);
//Scale page
AffineTransform transformationMatrix = AffineTransform.getScaleInstance(
nUpPageSize.getWidth()/orig.getWidth()/
2f, nUpPageSize.getHeight()/orig.getHeight()/2f);
canvas.concatMatrix(transformationMatrix);
//Add pages to N-up page
canvas.addXObject(pageCopy, 0, orig.getHeight());
canvas.addXObject(pageCopy, orig.getWidth(), orig.getHeight());
canvas.addXObject(pageCopy, 0, 0);
canvas.addXObject(pageCopy, orig.getWidth(), 0);
//closing the documents
destpdf.close();
srcPdf.close();
System.out.println("PDF created successfully..");
}
}
次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します-
javac NUppingPDF.java
java NUppingPDF
実行時に、上記のプログラムは、次のメッセージを表示するPDFドキュメントを作成します。
PDF created successfully..
指定したパスを確認すると、以下に示すように、作成されたPDFドキュメントを見つけることができます。