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