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