Java用のAspose.PDF FOSSのファサードレイヤー
Aspose.PDF FOSS for Java にはファサード層が含まれています — 高レベルでタスク指向のクラスの集合で、PDFドキュメント全体のオブジェクトモデルをナビゲートすることなく、PDF文書に対する特定の操作を提供します。各ファサードクラスは同じパターンに従います:ドキュメントをバインドし、操作を実行し、結果を保存します。
すべてのファサードクラスは org.aspose.pdf.facades パッケージに存在します:
import org.aspose.pdf.facades.*;
PdfAnnotationEditor: アノテーションのフラット化と削除
PdfAnnotationEditor はアノテーションを一括で管理する操作を提供します:ページコンテンツにフラット化する、すべてのアノテーションを削除する、または名前で特定のアノテーションタイプを削除する。
try (PdfAnnotationEditor editor = new PdfAnnotationEditor()) {
editor.bindPdf("annotated.pdf");
// Flatten all annotations into the page content stream
editor.flattenAnnotations();
editor.save("flat.pdf");
}
deleteAnnotations(annotationType) のオーバーロードは、"Highlight" や "Widget" のようなアノテーションタイプ文字列を受け取り、そのタイプのアノテーションのみを削除します。countAnnotations() は全ページのアノテーション総数を返します。
PdfBookmarkEditor: ブックマークの作成と削除
PdfBookmarkEditor は PDF のアウトラインツリー(ブックマーク)を管理します。createBookmarks() を使用して新しいブックマークを作成するメソッド、deleteBookmarks() ですべてのブックマークを削除するメソッド、deleteBookmarkByTitle() でタイトルでブックマークを削除するメソッドを提供します。
try (PdfBookmarkEditor bookmarkEditor = new PdfBookmarkEditor()) {
bookmarkEditor.bindPdf("input.pdf");
// Remove all bookmarks from the document
bookmarkEditor.deleteBookmarks();
bookmarkEditor.save("no-bookmarks.pdf");
}
bindPdf(document) のオーバーロードは Document オブジェクトを直接受け取り、ドキュメントがすでにメモリに読み込まれているパイプラインでの使用を可能にします。
PdfContentEditor: テキスト置換
PdfContentEditor は PDF コンテンツストリーム内での対象テキスト置換をサポートします。replaceText(searchText, replaceText) は文字列の出現すべてを文書全体で置換し、replaceText(searchText, pageNumber, replaceText) は置換を単一ページに限定します。
try (PdfContentEditor contentEditor = new PdfContentEditor()) {
contentEditor.bindPdf("template.pdf");
contentEditor.replaceText("{{CompanyName}}", "Acme Corp");
contentEditor.save("output.pdf");
}
TextReplaceOptions は置換時に元のフォントとサイズを保持するか、周囲のコンテンツからテキスト属性を継承するかを制御します。getStamps(pageNumber) はページ上のすべてのスタンプに対する StampInfo オブジェクトを返し、deleteStampById() は識別子で特定のスタンプを削除します。
PdfFileSecurity: 暗号化と権限
PdfFileSecurity は暗号化、復号、アクセス権限を含むドキュメントレベルのセキュリティを管理します。コンストラクタのバリエーションはファイルパス、ストリーム、または既存の Document インスタンスを出力先とともに受け取ります。
DocumentPrivilege は暗号化されたドキュメントのアクセス権限を表します。すべてのユーザー操作を拒否するには DocumentPrivilege.getForbidAll() を使用し、完全なアクセスを許可するには DocumentPrivilege.getAllowAll() を使用します。
try (PdfFileSecurity security = new PdfFileSecurity("input.pdf", "secured.pdf")) {
DocumentPrivilege privilege = DocumentPrivilege.getForbidAll();
privilege.setAllowPrint(true);
security.encryptFile("userPass", "ownerPass", privilege, KeySize.x256);
}
decryptFile(ownerPassword) は所有者パスワードが判明している場合に暗号化を解除します。Algorithm 列挙型は RC4 と AES のアルゴリズムファミリを提供し、KeySize は鍵長を指定します。
PdfFileStamp: スタンプとページ番号の追加
PdfFileStamp は PDF ページにテキストおよび画像スタンプを追加し、書式設定されたページ番号を自動的に生成できます。スタンプは各ページで座標ベースの配置を使用して配置されます。
try (PdfFileStamp stamp = new PdfFileStamp("input.pdf", "stamped.pdf")) {
// Add formatted page numbers to all pages
stamp.addPageNumber("Page #", PageNumberingStyle.NumeralsArabic,
0, 0, 0, 20);
}
PdfExtractor: テキストと画像の抽出
PdfExtractor は PDF ドキュメントからテキストと画像を抽出するためのプルベース API を提供します。ドキュメントをバインドした後、extractText() を呼び出してテキスト抽出をトリガーし、続いて hasNextPageText() と getNextPageText() でイテレートします:
try (PdfExtractor extractor = new PdfExtractor()) {
extractor.bindPdf("document.pdf");
extractor.extractText();
while (extractor.hasNextPageText()) {
String pageText = extractor.getNextPageText();
// Collect extracted page content
}
}
画像抽出の場合、extractImage() が抽出をトリガーし、hasNextImage() / getNextImage(outputStream) が抽出された画像をイテレートします。getPassword() と setPassword() メソッドは暗号化されたドキュメントをサポートします。
はじめに
Maven の依存関係を追加してください:
<dependency>
<groupId>org.aspose</groupId>
<artifactId>aspose-pdf-foss</artifactId>
<version>26.8.0</version>
</dependency>すべてのファサードクラスは Java 11 以降が必要です。 Aspose.PDF FOSS for Java は MIT ライセンスで、https://github.com/aspose-pdf-foss/Aspose.PDF-FOSS-for-Java で入手可能です。