このチュートリアルでは、Java を使用して PDF にテキスト フィールド注釈を追加するすべての手順について説明します。テキスト フィールド注釈は、ユーザーが PDF ドキュメントの特定の領域にテキストを追加できる注釈の一種です。このタイプの注釈は、フォームに入力したり、ドキュメントの特定のセクションにコメントを追加したりするときに役立ちます。注釈ライブラリを利用し、Java で PDF にテキスト フィールド注釈を挿入するための基本的なプログラムを作成するためのガイダンスを提供します。次のセクションでは、Java プログラミング言語を使用して PDF にテキスト フィールドを挿入する方法について説明します。
Java を使用して PDF にテキスト フィールド注釈を追加する手順
- Maven リポジトリを利用して GroupDocs.Annotation for Java をプロジェクトに統合します
- PDF にテキスト フィールド注釈を追加するための必須クラスを追加します。
- PDF ファイルのパスをそのコンストラクターに渡して、Annotator クラスのインスタンスを作成します。
- TextFieldAnnotation クラスのインスタンスを作成し、位置、ページ番号などのプロパティを設定します。
- Annotator.add メソッドを呼び出し、それに TextFieldAnnotation オブジェクトを渡します
- Annotator.save メソッドを呼び出して、PDF をディスクに保存します
テキスト フィールドの注釈を PDF ドキュメントに追加すると、フォームへの入力、コメントの追加、または追加の詳細の提供に非常に役立ちます。前述のプロセスは、追加のソフトウェアをインストールする必要なく、Java をサポートする任意のシステムで *Java を使用して PDF にテキスト フィールド注釈を作成するために使用できます。さらに、この図で使用されているライブラリは複数のプラットフォームと互換性があるため、Java がインストールされている任意のシステムで特定のコード例を実行できます。
Java を使用して PDF にテキスト フィールド注釈を追加するコード
import com.groupdocs.annotation.Annotator; | |
import com.groupdocs.annotation.licenses.License; | |
import com.groupdocs.annotation.models.PenStyle; | |
import com.groupdocs.annotation.models.Rectangle; | |
import com.groupdocs.annotation.models.annotationmodels.TextFieldAnnotation; | |
import java.util.Calendar; | |
public class AddTextFieldAnnotationinPDFusingJava { | |
public static void main(String[] args) { | |
// Set License to avoid the limitations of Annotation library | |
License license = new License(); | |
license.setLicense("GroupDocs.Annotation.lic"); | |
// Create an instance of Annotator class | |
Annotator annotator = new Annotator("input.pdf"); | |
// Create an instance of TextFieldAnnotation class and set options | |
TextFieldAnnotation textField = new TextFieldAnnotation(); | |
textField.setBackgroundColor(65535); | |
textField.setBox(new Rectangle(100, 100, 100, 100)); | |
textField.setCreatedOn(Calendar.getInstance().getTime()); | |
textField.setText("Some text"); | |
textField.setFontColor(65535); | |
textField.setFontSize((double)12); | |
textField.setMessage("This is text field annotation"); | |
textField.setOpacity(0.7); | |
textField.setPageNumber(0); | |
textField.setPenStyle(PenStyle.DOT); | |
textField.setPenWidth((byte) 3); | |
// Add text field annotation to Annotator | |
annotator.add(textField); | |
// Save the final PDF to disk | |
annotator.save("result.pdf"); | |
} | |
} |
前のセクションでは、テキスト フィールドの注釈を PDF Java に追加する方法を、簡単なコード図で完全に説明しました。ドキュメント注釈ライブラリをインストールし、入力および出力ファイル パスに必要な変更を加えたら、前述のコード例をアプリケーションに簡単に統合できます。おめでとう! Java を使用してテキスト フィールド注釈を PDF ドキュメントに正常に追加しました。
以前、PDF への矢印注釈の挿入に関する記事を公開しました。詳細については、Javaを使用してPDFに矢印注釈を追加する方法 のガイドを参照してください。