はじめに

Aspose.PDF FOSS for C++ は、PDF ドキュメントを扱うためのオープンソース C++20 ライブラリです。API の範囲は、ドキュメントおよびページ管理、テキスト抽出、ラスターレンダリング、暗号化、注釈、そしてインタラクティブな AcroForm フィールドまで及びます――C++ アプリケーションが外部の PDF エンジンや商用 SDK に依存せずに PDF ファイルを作成、検査、変換する際に最も一般的に必要とされる操作です。

このライブラリは C++ 標準ライブラリ以外には何にもリンクしません。BMP、JPEG、TIFF のラスタエンコーダやページラスタライザなどの内部コンポーネントは、プロジェクト内でゼロから実装されているため、システムの PDF レンダラや商用 PDF コンポーネント、サードパーティ製画像コーデックへの依存はありません。これにより、C++20 に準拠したコンパイラと標準ライブラリ以外の依存フットプリントはゼロに抑えられます。

Aspose.PDF FOSS for C++ は MIT ライセンスでリリースされています。本稿では、Document クラスとその補助型が提供する主要な機能領域を順に解説し、続いてクイックスタート例と現在確認されているフォーマットサポートを紹介します。


主な機能

ドキュメントとページの管理

Document クラスは、PDF ファイルのオープン、検査、保存のエントリーポイントです。ページは Document.Pages() を通じてアクセスされ、PageCollection を返します。この PageCollection は Count()、Add()、Insert(pageNumber)、Delete(pageNumber) をサポートしています。ドキュメントレベルのメタデータは、Document.Info()(DocumentInfo インスタンス)を介して読み書きするか、Document.SetTitle() で直接設定できます。

#include <aspose/pdf/document.hpp>
#include <aspose/pdf/page_collection.hpp>
#include <iostream>

int main() {
    Aspose::Pdf::Document doc("input.pdf");
    std::cout << "Page count: " << doc.Pages().Count() << "\n";

    // Insert a blank page after page 1, then drop the last page
    doc.Pages().Insert(2);
    doc.Pages().Delete(doc.Pages().Count());

    doc.SetTitle("Generated Report");
    doc.Save("output.pdf");
}

Document.Optimize() と Document.OptimizeResources() は、保存前に未使用リソースを統合および削除することで、出力ファイルサイズを削減します。

テキスト抽出

Aspose::Pdf::Text::TextAbsorber は、文書全体または単一ページのプレーンテキストコンテンツを抽出します。Visit() は対象を走査し、Text() は蓄積された文字列を返します。TextFragmentAbsorber はより細かい粒度で動作し、TextFragmentCollection を通じて個々のテキストランを公開します。

#include <aspose/pdf/document.hpp>
#include <aspose/pdf/text_absorber.hpp>
#include <aspose/pdf/text_fragment_absorber.hpp>
#include <iostream>

int main() {
    Aspose::Pdf::Document doc("input.pdf");

    // Extract all text from the document
    Aspose::Pdf::Text::TextAbsorber absorber;
    absorber.Visit(doc);
    std::cout << absorber.Text() << "\n";

    // Extract text fragments from page 1
    Aspose::Pdf::Text::TextFragmentAbsorber fragmentAbsorber;
    fragmentAbsorber.Visit(doc.Pages()[1]);
    auto fragments = fragmentAbsorber.TextFragments();
    std::cout << "Fragments on page 1: " << fragments.Count() << "\n";
}

ラスタ画像へのレンダリング

ページは、フォーマットごとのデバイスクラス — PngDevice、JpegDevice、BmpDevice、および TiffDevice — を通じてラスタ形式にレンダリングされ、各クラスは Process(page, output) を実装しています。出力解像度は Resolution クラスで制御されます。

#include <aspose/pdf/document.hpp>
#include <aspose/pdf/png_device.hpp>
#include <aspose/pdf/bmp_device.hpp>
#include <aspose/pdf/resolution.hpp>
#include <fstream>

int main() {
    Aspose::Pdf::Document doc("input.pdf");
    auto page = doc.Pages()[1];

    Aspose::Pdf::Devices::PngDevice png(Aspose::Pdf::Devices::Resolution(150));
    std::ofstream pngOut("page1.png", std::ios::binary);
    png.Process(page, pngOut);

    Aspose::Pdf::Devices::BmpDevice bmp(Aspose::Pdf::Devices::Resolution(150));
    std::ofstream bmpOut("page1.bmp", std::ios::binary);
    bmp.Process(page, bmpOut);
}

JpegDevice は JPEG 出力に対して同じ Process(page, output) パターンに従います。TiffDevice はさらに、Process(document, fromPage, toPage, output) を介した単一呼び出しでのマルチページ出力をサポートし、フォームフィールドがラスタ出力にどのようにレンダリングされるかを制御するための RenderingOptions() を公開します。

暗号化とアクセス権限

Document.Encrypt() はユーザーとオーナーパスワードで文書を保護し、CryptoAlgorithm 列挙体を介して暗号を選択します:RC4x40、RC4x128、AESx128、または AESx256。付与された権限は Permissions 型で記述され、そのフラグ値には PrintDocument、ModifyContent、ExtractContent、ModifyTextAnnotations、FillForm、ExtractContentWithDisabilities、AssembleDocument、および PrintingQuality が含まれます。

#include <aspose/pdf/document.hpp>

int main() {
    Aspose::Pdf::Document doc("input.pdf");

    doc.Encrypt("user-password", "owner-password",
                Aspose::Pdf::Permissions(),
                Aspose::Pdf::CryptoAlgorithm::AESx256);

    doc.Save("encrypted.pdf");
}

すでに開かれている暗号化ドキュメントは、Document.Decrypt() を使用してその場で復号化されます。Document.IsEncrypted() は、読み込まれたドキュメントが現在暗号化辞書を保持しているかどうかを報告します。

注釈

ページレベルの注釈—テキストノート、図形、マークアップ—は Page.Annotations() を通じて取得でき、AnnotationCollection を返します。個々のエントリは Contents()、Rect()、Color()、Border()、AnnotationType() といった共通プロパティを公開し、ページから削除したり静的コンテンツにフラット化したりできます。

#include <aspose/pdf/document.hpp>
#include <iostream>

int main() {
    Aspose::Pdf::Document doc("input.pdf");
    auto& annotations = doc.Pages()[1].Annotations();

    std::cout << "Annotation count: " << annotations.Count() << "\n";
    for (int i = 0; i < static_cast<int>(annotations.Count()); ++i) {
        std::cout << "Contents: " << annotations[i].Contents() << "\n";
    }

    // Remove the first annotation on the page
    if (annotations.Count() > 0) {
        annotations.Delete(annotations[0]);
    }

    doc.Save("output.pdf");
}

任意の単一 Annotation は、以下で説明するフォームフラット化ワークフローとは独立して、独自の Flatten() メソッドを使用して直接静的ページコンテンツに変換することもできます。

インタラクティブフォーム

AcroForm フィールドは Document.Form() を介して管理され、Form インスタンスを返し、Count()、HasField(fieldName)、Delete(fieldName)、Flatten() をサポートします。新しいフィールドは Form.Add(field, pageNumber) で付加され、field は TextBoxField、CheckboxField、ChoiceField、または ButtonField のような具体的な Field サブタイプです。

#include <aspose/pdf/document.hpp>
#include <iostream>

int main() {
    Aspose::Pdf::Document doc("input.pdf");
    auto& form = doc.Form();

    std::cout << "Form fields: " << form.Count() << "\n";

    if (form.HasField("Signature1")) {
        form.Delete("Signature1");
    }

    // Convert every remaining field into static page content
    form.Flatten();
    doc.Save("flattened.pdf");
}

クイックスタート

ライブラリを CMake のサブディレクトリとして追加し、aspose_pdf_foss ターゲットにリンクします:

add_subdirectory(aspose.pdf-foss-for-cpp)
target_link_libraries(your_app PRIVATE aspose_pdf_foss)

ドキュメントを開き、テキストを抽出し、最初のページを PNG にレンダリングします:

#include <aspose/pdf/document.hpp>
#include <aspose/pdf/text_absorber.hpp>
#include <aspose/pdf/png_device.hpp>
#include <aspose/pdf/resolution.hpp>
#include <fstream>
#include <iostream>

int main() {
    Aspose::Pdf::Document doc("input.pdf");
    std::cout << "Pages: " << doc.Pages().Count() << "\n";

    Aspose::Pdf::Text::TextAbsorber absorber;
    absorber.Visit(doc);
    std::cout << absorber.Text() << "\n";

    Aspose::Pdf::Devices::PngDevice png(Aspose::Pdf::Devices::Resolution(150));
    std::ofstream out("page1.png", std::ios::binary);
    png.Process(doc.Pages()[1], out);
}

サポートされているフォーマット

フォーマット拡張子読み取り書き込み
BMPbmp—✓
JPEGjpeg—✓
TIFFtiff—✓
Texttxt—✓
SVGsvg✓—

オープンソースとライセンス

Aspose.PDF FOSS for C++ は MIT ライセンスの下でリリースされており、ロイヤリティなしで商用利用、改変、再配布を許可します。ソースは github.com/aspose-pdf-foss/Aspose.PDF-FOSS-for-Cpp で入手可能です。

MIT ライセンスの条件の下で商用利用が許可されています。プロジェクトでエンタープライズ向けサポート、拡張フォーマットのカバレッジ、またはクラウド統合が必要な場合、Aspose.PDF — Enterprise Product Family は別途の商用オファーです。


はじめに

関連リソース