Itext-markup-annotation

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

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.layoutDocument クラスは、自給自足のPDFを作成する際のルート要素です。 このクラスのコンストラクターの1つは、 PdfDocument クラスのオブジェクトを受け入れます。

以下に示すように、前の手順で作成した PdfDocument クラスのオブジェクトを渡すことにより、 Document クラスをインスタンス化します。

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

ステップ4:PdfAnnotationオブジェクトの作成

パッケージ com.itextpdf.kernel.pdf.annotPdfAnnotation クラスは、すべての注釈のスーパークラスを表します。

派生クラスの中で、 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ドキュメントを見つけることができます。

マークアップ注釈