Java を使用したテキストベースの注釈
テキストベースの注釈により、レビューアや開発者は PDF ドキュメントに対して、コアコンテンツを変更することなく、インタラクティブなノート、ハイライト、マークアップを追加できます。このセクションでは、文書レビューのワークフロー、コンプライアンスシナリオ、コラボレーティブなフィードバックサイクルで使用される実用的な 5 つの注釈タイプについて説明します。
クイックリファレンス:アノテーションタイプ
この記事では、次のテキストベースの注釈タイプについて説明します。
- Free Text:メモやコメントを追加するための編集可能なテキストボックス
- Highlight:重要なテキストの箇所に対する視覚的強調
- Strikeout:レビュー中に削除または修正のためにテキストに印を付けます
- Squiggly:エラーや懸念を示すための波形下線
- Underline:伝統的な下線の強調(オプションで四点精度)
フリーテキスト注釈の追加、取得、削除
フリーテキスト注釈は、文書構造に影響を与えることなく編集可能な浮動テキストボックスとして機能します。これらの例を使用して、コメントボックスを追加したり、プロパティを確認したり、削除したりしてください。
フリーテキスト注釈の追加
- ソース PDF の Document を開いてください。
- FreeTextAnnotation を、矩形と外観設定で作成してください。
- ページに注釈を追加し、ドキュメントを保存してください。
public static void freeTextAnnotationAdd(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
FreeTextAnnotation freeTextAnnotation = new FreeTextAnnotation(
document.getPages().get_Item(1),
new Rectangle(299, 713, 308, 720, true),
new DefaultAppearance());
freeTextAnnotation.setTitle("Aspose User");
freeTextAnnotation.setColor(Color.getLightGreen());
document.getPages().get_Item(1).getAnnotations().add(freeTextAnnotation);
document.save(outputFile.toString());
}
}
フリーテキスト注釈の取得
- ソース PDF の Document を開いてください。
- ページ上のアノテーションを反復処理し、AnnotationType.FreeText でフィルタリングしてください。
- 注釈のプロパティまたは境界を取得してください。
public static void freeTextAnnotationGet(Path inputFile) {
try (Document document = new Document(inputFile.toString())) {
for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
if (annotation.getAnnotationType() == AnnotationType.FreeText) {
System.out.println(annotation.getRect());
}
}
}
}
フリーテキスト注釈の削除
- ソース PDF の Document を開いてください。
- ページのアノテーションを反復処理し、タイプでフィルタリングすることで、フリーテキスト注釈を見つけてください。
- 一致する注釈を削除リストに追加し、ページから削除してください。
- 更新された文書を保存してください。
public static void freeTextAnnotationDelete(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
List<Annotation> toDelete = new ArrayList<>();
for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
if (annotation.getAnnotationType() == AnnotationType.FreeText) {
toDelete.add(annotation);
}
}
for (Annotation annotation : toDelete) {
document.getPages().get_Item(1).getAnnotations().delete(annotation);
}
document.save(outputFile.toString());
}
}
ハイライト注釈を追加、取得、削除
ハイライト注釈は、半透明のオーバーレイで重要な箇所をマークします。これらの例を使用して、文書レビュー用のハイライトを作成し、既存のハイライトを検索し、マークアップをクリーンアップしてください。
ハイライト注釈の追加
- ソース PDF の Document を開いてください。
- HighlightAnnotation を、ハイライト領域を定義する長方形とともに作成してください。
- ページに注釈を追加し、ドキュメントを保存してください。
public static void textHighlightAnnotationAdd(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
HighlightAnnotation highlightAnnotation = new HighlightAnnotation(
document.getPages().get_Item(1),
new Rectangle(300, 750, 320, 770, true));
document.getPages().get_Item(1).getAnnotations().add(highlightAnnotation);
document.save(outputFile.toString());
}
}
ハイライト注釈の取得
- ソース PDF の Document を開いてください。
- 注釈を反復処理し、AnnotationType.Highlight でフィルタリングしてください。
- バウンドや色などの注釈プロパティを読み取ってください。
public static void textHighlightAnnotationGet(Path inputFile) {
try (Document document = new Document(inputFile.toString())) {
for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
if (annotation.getAnnotationType() == AnnotationType.Highlight) {
System.out.println(annotation.getRect());
}
}
}
}
ハイライト注釈の削除
- ソース PDF の Document を開いてください。
- タイプで注釈をフィルタリングして、ハイライト注釈を収集してください。
- ページから各注釈を削除してください。
- 更新された文書を保存してください。
public static void textHighlightAnnotationDelete(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
List<Annotation> toDelete = new ArrayList<>();
for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
if (annotation.getAnnotationType() == AnnotationType.Highlight) {
toDelete.add(annotation);
}
}
for (Annotation annotation : toDelete) {
document.getPages().get_Item(1).getAnnotations().delete(annotation);
}
document.save(outputFile.toString());
}
}
ストライクアウト注釈を追加、取得、削除
取り消し線注釈は、削除、却下、または修正を示すためにテキストに取り消し線を引きます。ドキュメントレビュー中に取り消し線マークアップを適用し、マークされたテキストを検索し、取り消し線注釈を削除するには、これらの例を使用してください。
取り消し線注釈の追加
- ソース PDF の Document を開いてください。
- StrikeOutAnnotation を四角形、タイトル、色を指定して作成してください。
- ページに注釈を追加し、ドキュメントを保存してください。
public static void textStrikeoutAnnotationAdd(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
StrikeOutAnnotation strikeoutAnnotation = new StrikeOutAnnotation(
document.getPages().get_Item(1),
new Rectangle(299.988, 713.664, 308.708, 720.769, true));
strikeoutAnnotation.setTitle("Aspose User");
strikeoutAnnotation.setSubject("Inserted text 1");
strikeoutAnnotation.setFlags(AnnotationFlags.Print);
strikeoutAnnotation.setColor(Color.getBlue());
document.getPages().get_Item(1).getAnnotations().add(strikeoutAnnotation);
document.save(outputFile.toString());
}
}
取り消し線の注釈の取得
- ソース PDF の Document を開いてください。
- 注釈を反復処理し、AnnotationType.StrikeOut でフィルタリングしてください。
- 注釈のメタデータまたは境界を読み取ってください。
public static void textStrikeoutAnnotationGet(Path inputFile) {
try (Document document = new Document(inputFile.toString())) {
for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
if (annotation.getAnnotationType() == AnnotationType.StrikeOut) {
System.out.println(annotation.getRect());
}
}
}
}
取り消し線注釈の削除
- ソース PDF の Document を開いてください。
- タイプでフィルタリングして取り消し線注釈を収集してください。
- ページから各注釈を削除してください。
- 更新された文書を保存してください。
public static void textStrikeoutAnnotationDelete(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
List<Annotation> toDelete = new ArrayList<>();
for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
if (annotation.getAnnotationType() == AnnotationType.StrikeOut) {
toDelete.add(annotation);
}
}
for (Annotation annotation : toDelete) {
document.getPages().get_Item(1).getAnnotations().delete(annotation);
}
document.save(outputFile.toString());
}
}
波線注釈の追加、取得、削除
波線注釈(波形の下線)は、潜在的なエラー、懸念、または注意が必要な項目を強調表示します。これらの例を使用して問題のあるテキストにマーキングし、波線注釈を検査し、文書から削除してください。
波線注釈の追加
- ソース PDF の Document を開いてください。
- SquigglyAnnotation を四角形とタイトルを指定して作成してください。
- ページに注釈を追加し、ドキュメントを保存してください。
public static void textSquigglyAnnotationAdd(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
Page page = document.getPages().get_Item(1);
SquigglyAnnotation squigglyAnnotation = new SquigglyAnnotation(
page,
new Rectangle(67, 317, 261, 459, true));
squigglyAnnotation.setTitle("John Smith");
squigglyAnnotation.setColor(Color.getBlue());
page.getAnnotations().add(squigglyAnnotation);
document.save(outputFile.toString());
}
}
波線アノテーションの取得
- ソース PDF の Document を開いてください。
- 注釈を反復処理し、AnnotationType.Squiggly でフィルタリングしてください。
- アノテーションの境界またはメタデータを読み取ってください。
public static void textSquigglyAnnotationGet(Path inputFile) {
try (Document document = new Document(inputFile.toString())) {
for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
if (annotation.getAnnotationType() == AnnotationType.Squiggly) {
System.out.println(annotation.getRect());
}
}
}
}
波線注釈の削除
- ソース PDF の Document を開いてください。
- タイプでフィルタリングして波線アノテーションを収集してください。
- ページから各注釈を削除してください。
- 更新された文書を保存してください。
public static void textSquigglyAnnotationDelete(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
List<Annotation> toDelete = new ArrayList<>();
for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
if (annotation.getAnnotationType() == AnnotationType.Squiggly) {
toDelete.add(annotation);
}
}
for (Annotation annotation : toDelete) {
document.getPages().get_Item(1).getAnnotations().delete(annotation);
}
document.save(outputFile.toString());
}
}
下線アノテーションを追加、取得、削除
下線アノテーションは、伝統的な下線で重要な箇所を強調します。これらの例を使用して下線を作成し、マークされたテキスト内容を読み取り、ページから下線アノテーションを削除してください。
下線注釈の追加
- ソース PDF の Document を開いてください。
- UnderlineAnnotation を矩形と色で作成してください。
- ページに注釈を追加し、ドキュメントを保存してください。
public static void textUnderlineAnnotationAdd(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
UnderlineAnnotation underlineAnnotation = new UnderlineAnnotation(
document.getPages().get_Item(1),
new Rectangle(299.988, 713.664, 308.708, 720.769, true));
underlineAnnotation.setTitle("Aspose User");
underlineAnnotation.setSubject("Inserted Underline 1");
underlineAnnotation.setFlags(AnnotationFlags.Print);
underlineAnnotation.setColor(Color.getBlue());
document.getPages().get_Item(1).getAnnotations().add(underlineAnnotation);
document.save(outputFile.toString());
}
}
下線注釈の取得
- ソース PDF の Document を開いてください。
- 注釈を反復処理し、AnnotationType.Underline でフィルタリングしてください。
- 注釈のプロパティまたは境界を読み取ってください。
public static void textUnderlineAnnotationGet(Path inputFile) {
try (Document document = new Document(inputFile.toString())) {
for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
if (annotation.getAnnotationType() == AnnotationType.Underline) {
System.out.println(annotation.getRect());
}
}
}
}
下線注釈の削除
- ソース PDF の Document を開いてください。
- タイプでフィルタリングし、下線注釈を収集してください。
- ページから各注釈を削除してください。
- 更新された文書を保存してください。
public static void textUnderlineAnnotationDelete(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
List<Annotation> toDelete = new ArrayList<>();
for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
if (annotation.getAnnotationType() == AnnotationType.Underline) {
toDelete.add(annotation);
}
}
for (Annotation annotation : toDelete) {
document.getPages().get_Item(1).getAnnotations().delete(annotation);
}
document.save(outputFile.toString());
}
}
quad points を使用した下線アノテーションの追加
この例では、矩形から導出されたクアッドポイントを使用して、下線領域を明示的に定義しています。
- ソース PDF の Document を開いてください。
- UnderlineAnnotation を作成し、その四隅点を計算してください。
- ページに注釈を追加し、ドキュメントを保存してください。
public static void textUnderlineWithQuadPointsAdd(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
Rectangle rect = new Rectangle(299.988, 713.664, 308.708, 720.769, true);
UnderlineAnnotation underlineAnnotation = new UnderlineAnnotation(
document.getPages().get_Item(1), rect);
underlineAnnotation.setTitle("Aspose User");
underlineAnnotation.setSubject("Inserted Underline with Quad Points");
underlineAnnotation.setFlags(AnnotationFlags.Print);
underlineAnnotation.setColor(Color.getBlue());
underlineAnnotation.setQuadPoints(new com.aspose.pdf.Point[]{
new com.aspose.pdf.Point(rect.getLLX(), rect.getLLY()),
new com.aspose.pdf.Point(rect.getURX(), rect.getLLY()),
new com.aspose.pdf.Point(rect.getURX(), rect.getURY()),
new com.aspose.pdf.Point(rect.getLLX(), rect.getURY())
});
document.getPages().get_Item(1).getAnnotations().add(underlineAnnotation);
document.save(outputFile.toString());
}
}
下線注釈からマークされたテキストの取得
下線注釈でカバーされている実際のテキスト内容を取得します。これらの例では、2つのアプローチを示しています:マークされたテキスト全体を単一の文字列として読む方法、またはテキストフラグメントを個別に処理して詳細な分析を行う方法です。
- ソース PDF の Document を開いてください。
- ページ上の下線注釈を順に処理してください。
getMarkedText()またはgetMarkedTextFragments()のいずれかを呼び出して、結果を印刷してください。
public static void textUnderlineMarkedTextGet(Path inputFile) {
try (Document document = new Document(inputFile.toString())) {
for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
if (annotation.getAnnotationType() == AnnotationType.Underline) {
UnderlineAnnotation ua = (UnderlineAnnotation) annotation;
System.out.println("Marked text: " + ua.getMarkedText());
}
}
}
}
public static void textUnderlineMarkedFragmentsGet(Path inputFile) {
try (Document document = new Document(inputFile.toString())) {
for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
if (annotation.getAnnotationType() == AnnotationType.Underline) {
UnderlineAnnotation ua = (UnderlineAnnotation) annotation;
for (TextFragment fragment : ua.getMarkedTextFragments()) {
System.out.println("Fragment text: " + fragment.getText());
}
}
}
}
}
タイトルによる下線アノテーションの削除
タイトルなどのメタデータプロパティでフィルタリングすることで、注釈を選択的に削除できます。このアプローチにより、著者や目的別に注釈のターゲットとなるクリーンアップが可能になります。
- ソース PDF の Document を開いてください。
- タイトルで下線アノテーションをフィルタリングしてください。
- 一致する注釈を削除し、更新されたドキュメントを保存してください。
public static void textUnderlineByTitleDelete(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
List<UnderlineAnnotation> toDelete = new ArrayList<>();
for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
if (annotation.getAnnotationType() == AnnotationType.Underline) {
UnderlineAnnotation ua = (UnderlineAnnotation) annotation;
if ("Aspose User".equals(ua.getTitle())) {
toDelete.add(ua);
}
}
}
for (UnderlineAnnotation ua : toDelete) {
document.getPages().get_Item(1).getAnnotations().delete(ua);
}
document.save(outputFile.toString());
}
}
アンダーライン注釈の追加とフラット化
インタラクティブな下線アノテーションをフラット化して、永久的なページコンテンツに変換します。これにより、さらに編集できなくなりますが、すべての PDF ビューアで下線の外観が保持されます。
- ソース PDF の Document を開いてください。
- ページへ UnderlineAnnotation を追加してください。
- 注釈に対して
flatten()を呼び出し、出力ファイルを保存してください。
public static void textUnderlineFlattenAdd(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
UnderlineAnnotation underlineAnnotation = new UnderlineAnnotation(
document.getPages().get_Item(1),
new Rectangle(299.988, 713.664, 308.708, 720.769, true));
underlineAnnotation.setTitle("Aspose User");
underlineAnnotation.setSubject("Inserted Underline to Flatten");
underlineAnnotation.setFlags(AnnotationFlags.Print);
underlineAnnotation.setColor(Color.getBlue());
document.getPages().get_Item(1).getAnnotations().add(underlineAnnotation);
underlineAnnotation.flatten();
document.save(outputFile.toString());
}
}