Itext-text-annotation
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ドキュメントを見つけることができます。