PDF ファサード

このガイドでは、org.aspose.pdf.facades パッケージ内のタスク指向ファサードクラスの使用方法を示します。各ファサードは同じライフサイクルに従います: bindPdf() → 操作 → save()。注釈のフラット化、ブックマークの管理、テキストの置換、文書の暗号化、テキストの抽出、ページ番号の追加を学びます。

ファサードパターン

org.aspose.pdf.facades のすべてのファサードクラスは同じライフサイクルに従います: bindPdf() → 操作 → save()。ファイルパス、ストリーム、または Document オブジェクトを受け取ります。

PdfAnnotationEditor

文書全体の注釈をフラット化、削除、またはカウントします。flattenAnnotations() はすべての注釈をページコンテンツストリームにマージし、永続的で編集不可にします:

try (PdfAnnotationEditor editor = new PdfAnnotationEditor()) {
    editor.bindPdf("input.pdf");
    editor.flattenAnnotations();
    editor.save("output.pdf");
}

deleteAnnotations(annotationType) は名前で特定の注釈タイプを削除します。

PdfBookmarkEditor

PDF のアウトラインエントリ(ブックマーク)を作成および削除します。deleteBookmarks() は、保存前にバインドされたドキュメントからすべてのアウトラインエントリを削除します:

try (PdfBookmarkEditor editor = new PdfBookmarkEditor()) {
    editor.bindPdf("input.pdf");
    editor.deleteBookmarks();
    editor.save("output.pdf");
}

PdfContentEditor

PDF コンテンツストリーム内のテキストを、ドキュメント全体または特定のページで置換します。replaceText(searchString, replacement) は一致するすべてのテキスト文字列を検索し、置換します:

try (PdfContentEditor editor = new PdfContentEditor()) {
    editor.bindPdf("template.pdf");
    editor.replaceText("{{Name}}", "John Smith");
    editor.save("output.pdf");
}

PdfFileSecurity

AES-256 を使用してドキュメントを暗号化し、DocumentPrivilege によってアクセス権限を設定します。encryptFile(userPass, ownerPass, privilege, keySize) は暗号化を適用し、結果を設定された出力ファイルに書き込みます:

try (PdfFileSecurity security = new PdfFileSecurity("input.pdf", "out.pdf")) {
    DocumentPrivilege priv = DocumentPrivilege.getForbidAll();
    priv.setAllowPrint(true);
    security.encryptFile("user", "owner", priv, KeySize.x256);
}

PdfExtractor

PDF ドキュメントからページごとにテキストを抽出します。extractText() を呼び出した後、hasNextPageText() と getNextPageText() を使用して各ページの抽出コンテンツを反復処理します:

try (PdfExtractor extractor = new PdfExtractor()) {
    extractor.bindPdf("document.pdf");
    extractor.extractText();
    while (extractor.hasNextPageText()) {
        String text = extractor.getNextPageText();
        System.out.println(text);
    }
}

PdfFileStamp

PDFページにページ番号スタンプを追加するには、addPageNumber(FormattedText)を使用します。ページ番号ラベルを含むFormattedTextインスタンスを渡します:

try (PdfFileStamp stamp = new PdfFileStamp("in.pdf", "out.pdf")) {
    stamp.addPageNumber(new FormattedText("Page #"));
    stamp.close();
}

addHeader(FormattedText, float)またはaddFooter(FormattedText, float)を使用して、各ページの上部または下部から指定した余白に繰り返しテキストを追加します。

参照

 日本語