チュートリアル

簡単にライブラリーを使用するためのチュートリアルコレクション

チュートリアル»Java»Spire.PDF for Java»注釈»Java:PDF にポップアップとテキストボックスの注釈を追加する方法
2023-02-14

Java:PDF にポップアップとテキストボックスの注釈を追加する方法

PDF の注釈は、ユーザーがコメントをつけたり、テキストにマークをつけたりするのに役立ちます。レポートへのフィードバック、難しい言葉の説明、メモの作成など、PDF ファイルが関わるさまざまな場面で利用されています。ユーザーは、ポップアップ、テキストボックス、リンク、線の注釈など、PDF 文書に注釈の多くの種類を追加することができます。この記事では、Spire.PDF for Java を使用して、PDF 文書にポップアップとテキストボックスの注釈を追加する方法を説明します。

Spire.PDF for Java をインストールします

まず、Spire. PDF for Java を Java プロジェクトに追加する必要があります。JAR ファイルは、このリンクからダウンロードできます。Maven を使用する場合は、次のコードをプロジェクトの pom.xml ファイルに追加する必要があります。

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url> https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf</artifactId>
        <version>9.1.4</version>
    </dependency>
</dependencies>

PDF 文書にポップアップ注釈を追加する

ポップアップ形式の注釈は、PDF 文書内にボタンとして表示されます。PDF ページには注釈テキストは表示されませんが、PDF ビューアの注釈インターフェイスにのみ表示されます。PDF 文書にポップアップ注釈を追加する詳細な手順は、以下のとおりです。

  • PdfDocument クラスのインスタンスを生成します。
  • PdfDocument.loadFromFile() メソッドを使用して PDF ドキュメントを読み込みます。
  • PdfDocument.getPages().get() メソッドを使用して、ドキュメントの最初のページを取得します。
  • PdfPageBase.findText().getFinds() メソッドを使用して、注釈を付けるテキストを検索します。
  • PdfPopupAnnotation オブジェクトを作成し、注釈のテキストと位置を設定します。
  • PdfPopupAnnotation クラスの下のメソッドを使用して、注釈のスタイルと色を設定します。
  • PdfPageBase.getAnnotationsWidget().add() メソッドを使用して、注釈をページに追加します。
  • PdfDocument.saveToFile() メソッドを使用して、ドキュメントを保存します。
  • Java
import com.spire.pdf.*;
import com.spire.pdf.annotations.*;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.*;
import com.spire.pdf.general.find.PdfTextFind;

public class PDFPopUpAnnotation {
    public static void main(String[] args)  {

        //PdfDocumentクラスのオブジェクトを作成する
        PdfDocument pdf = new PdfDocument();

        //PDFドキュメントの読み込み
        pdf.loadFromFile("生まれてこのように.pdf");

        //最初のページを取得する
        PdfPageBase page = pdf.getPages().get(0);

        //注釈を付けるテキストを検索する
        PdfTextFind[] find = page.findText("成長学者").getFinds();

        //PdfPopupAnnotationオブジェクトを作成し、注釈のテキストと位置を設定する
        String text = "成長学者は子供の成長過程を探求し、まとめる研究者である。";
        float a = (float)(find[0].getPosition().getX() + find[0].getSize().getWidth()-20);
        float b = (float)(find[0].getPosition().getY() + find[0].getSize().getHeight());
        Rectangle2D rectangle2D = new Rectangle.Float();
        rectangle2D.setFrame(new Point2D.Double(a,b),new Dimension());
        PdfPopupAnnotation annotation = new PdfPopupAnnotation(rectangle2D, text);

        //注釈のスタイルと色を設定する
        annotation.setIcon(PdfPopupIcon.Note);
        annotation.setColor(new PdfRGBColor(Color.red));

        //最初のページに注釈を追加する
        page.getAnnotationsWidget().add(annotation);

        //ドキュメントを保存する
        pdf.saveToFile("ポップアップ注釈.pdf");
        pdf.close();
    }
}

Java:PDF にポップアップとテキストボックスの注釈を追加する方法

PDF 文書にテキストボックス注釈を追加する

テキストボックスの注釈は、フリーテキスト注釈とも呼ばれ、テキストボックスとテキストボックス内の注釈テキストで構成されています。 テキストボックスの注釈テキストは、ページ上のテキストボックスに直接表示されるだけでなく、PDF リーダーの「注釈」インターフェイスに表示されます。 PDF 文書で、テキストボックスの注釈を追加する手順は次のとおりです。

  • PdfDocument クラスのインスタンスを生成します。
  • PdfDocument.loadFromFile() メソッドで PDF ドキュメントを読み込みます。
  • PdfDocument.getPages().get() メソッドで1ページ目を取得します。
  • PdfPageBase.findText.getFinds() メソッドで注釈を付けるテキストを検索します。
  • PdfFreeTextAnnotation オブジェクトを作成し、注釈の位置を設定します。
  • PdfFreeTextAnnotation クラスのメソッドを使用して、注釈のテキスト、フォント、テキスト色、テキストボックスの書式を設定します。
  • PdfPageBase.getAnnotationsWidget().add() メソッドを使用して、注釈をページ上に追加します。
  • PdfDocument.saveToFile() メソッドを使用して、ドキュメントを保存します。
  • Java
import com.spire.pdf.*;
import com.spire.pdf.annotations.*;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.*;
import com.spire.pdf.general.find.PdfTextFind;
public class PDFTextBoxAnnotation {
    public static void main(String[] args)  {

        //PdfDocumentクラスのオブジェクトを作成する
        PdfDocument doc = new PdfDocument();

        //PDFファイルを読み込む
        doc.loadFromFile("生まれてこのように.pdf");

        //最初のページを取得する
        PdfPageBase page = doc.getPages().get(0);

        //注釈を付ける文章を検索する
        PdfTextFind[] find = page.findText("成長学者").getFinds();

        //PdfFreeTextAnnotationオブジェクトを作成し、注釈の位置を設定する
        float x = (float)(find[0].getPosition().getX() + find[0].getSize().getWidth() - 20);
        float y = (float)(find[0].getPosition().getY() + find[0].getSize().getHeight());
        Rectangle2D.Float rect = new Rectangle2D.Float(x, y, 150, 30);
        PdfFreeTextAnnotation textAnnotation = new PdfFreeTextAnnotation(rect);

        //注釈のテキスト、フォント、フォント色、テキストボックスの書式を設定する
        textAnnotation.setMarkupText("成長学者は子供の成長過程を探求し、まとめる研究者である。");
        PdfCjkStandardFont font = new PdfCjkStandardFont(PdfCjkFontFamily.Heisei_Mincho_W_3, 12);
        textAnnotation.setFont(font);
        PdfAnnotationBorder border = new PdfAnnotationBorder(0.3f);
        textAnnotation.setBorder(border);
        textAnnotation.setBorderColor(new PdfRGBColor(Color.pink));
        textAnnotation.setColor(new PdfRGBColor(Color.YELLOW));
        textAnnotation.setOpacity(0.7f);
        textAnnotation.setTextMarkupColor(new PdfRGBColor(Color.black));

        //最初のページに注釈を追加する
        page.getAnnotationsWidget().add(textAnnotation);

        //ドキュメントを保存する
        doc.saveToFile("テキストボックス注釈.pdf");
        doc.close();
    }
}

Java:PDF にポップアップとテキストボックスの注釈を追加する方法

一時ライセンスを申請する

結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。

Read 726 times