はじめに
Aspose.PDF FOSS for C++ は、C++20 のコードから PDF ドキュメントを操作するための新しいオープンソースライブラリです。MIT ライセンスでリリースされており、C++ 標準ライブラリ以外のランタイム依存はありません — BMP、JPEG、TIFF エンコーダ、ページラスタライズ、PDF パーサは内部で実装されているため、商用 PDF エンジンや外部画像コーデック、サードパーティの暗号ライブラリへのリンクはありません。
このライブラリは Aspose::Pdf::Document クラスを中心に構成されており、ファイルパスから PDF を読み込み、Document.Pages() を通じて個々の Page オブジェクトからなる PageCollection として内容を公開します。そこから、API はテキスト抽出、ページラスタライズ、ドキュメント暗号化、注釈、AcroForm フィールドをカバーし、既存の PDF ファイルを読み取り、検査、変更する、またはゼロから新しい PDF を組み立てるために必要な操作を提供します。
このリリースは、商用ライセンスやサードパーティの依存関係を追加せずに C++ コードベース内で PDF 処理が必要な開発者向けです。ドキュメントパイプライン、コンテンツ抽出サービス、PDF から画像への変換ツール、そしてビルド全体の一部としてフォームデータを暗号化したり読み戻したりする必要があるアプリケーションなどが対象です。
含まれるもの
ドキュメントの読み込みとページアクセス
既存の PDF を開くにはコンストラクタ呼び出しを一度行うだけです。Document はファイルを解析し、Pages() を通じてページを公開します。このメソッドは 1 ベースのインデックス付け、Count()、およびページ順序を再構成するための Add()/Insert()/Delete() をサポートする PageCollection を返します。
#include <aspose/pdf/document.hpp>
#include <iostream>
int main() {
Aspose::Pdf::Document doc("input.pdf");
std::cout << "Page count: " << doc.Pages().Count() << "\n";
auto page = doc.Pages()[1];
std::cout << "First page number: " << page.Number() << "\n";
}
テキスト抽出
Aspose::Pdf::Text::TextAbsorber は Document または単一の Page を走査し、見つけたテキストコンテンツを収集します。Visit() をドキュメントまたはページに対して呼び出し、結果は Text() で取得します。HasErrors() は、吸収器が処理できなかったコンテンツに遭遇したかどうかを報告します。
#include <aspose/pdf/document.hpp>
#include <aspose/pdf/text_absorber.hpp>
#include <iostream>
int main() {
Aspose::Pdf::Document doc("input.pdf");
Aspose::Pdf::Text::TextAbsorber absorber;
absorber.Visit(doc);
std::cout << absorber.Text() << "\n";
if (absorber.HasErrors()) {
std::cerr << "Some content could not be extracted\n";
}
}
ラスター画像へのレンダリング
ページは BmpDevice、JpegDevice、TiffDevice クラスを介して BMP、JPEG、または TIFF にレンダリングされます。各デバイスは Resolution で構築され、レンダリングされた画像を出力ストリームに書き込む Process(page, output) メソッドを公開します。
#include <aspose/pdf/document.hpp>
#include <aspose/pdf/bmp_device.hpp>
#include <aspose/pdf/resolution.hpp>
#include <fstream>
int main() {
Aspose::Pdf::Document doc("input.pdf");
Aspose::Pdf::Devices::BmpDevice bmp(Aspose::Pdf::Devices::Resolution(150));
std::ofstream out("page1.bmp", std::ios::binary);
bmp.Process(doc.Pages()[1], out);
}
暗号化
Document.Encrypt() はユーザーパスワード、オーナーパスワード、権限値、および CryptoAlgorithm の選択でドキュメントを保護します。列挙型は RC4-40(RC4x40)、RC4-128(RC4x128)、AES-128(AESx128)、および AES-256(AESx256)をカバーしています。Document.Decrypt() はこの操作を元に戻し、IsEncrypted() はドキュメントが現在暗号化されているかどうかを報告します。
#include <aspose/pdf/document.hpp>
int main() {
Aspose::Pdf::Document doc("input.pdf");
doc.Encrypt("user-password", "owner-password",
Aspose::Pdf::Permissions::PrintDocument,
Aspose::Pdf::CryptoAlgorithm::AESx256);
doc.Save("encrypted.pdf");
}
注釈
各 Page は Annotations() を通じて AnnotationCollection を公開します。TextAnnotation は所有ドキュメントに対して構築され、Rectangle で位置を指定し、Add() を使ってページのコレクションに追加されます。
#include <aspose/pdf/document.hpp>
#include <aspose/pdf/annotations/text_annotation.hpp>
#include <aspose/pdf/rectangle.hpp>
int main() {
Aspose::Pdf::Document doc("input.pdf");
Aspose::Pdf::Annotations::TextAnnotation note(doc);
note.Rect(Aspose::Pdf::Rectangle(100.0, 700.0, 200.0, 720.0, false));
note.Contents("Reviewed - see section 2");
doc.Pages()[1].Annotations().Add(note);
doc.Save("annotated.pdf");
}
AcroForm フィールド
Document.Form() はドキュメントの Form を返し、Fields() を通じて既存のフィールドを std::vector<Field*> として公開します。各 Field は PartialName() と Value() で名前と現在の値を報告し、Form.Flatten() はすべてのフィールドの外観をページコンテンツに焼き付け、インタラクティブ性を除去します。
#include <aspose/pdf/document.hpp>
#include <iostream>
int main() {
Aspose::Pdf::Document doc("form.pdf");
auto& form = doc.Form();
std::cout << "Field count: " << form.Count() << "\n";
for (auto* field : form.Fields()) {
std::cout << field->PartialName() << " = " << field->Value() << "\n";
}
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)
ドキュメントを開き、ページ数を表示し、テキストを抽出し、最初のページを BMP ファイルにレンダリングします:
#include <aspose/pdf/document.hpp>
#include <aspose/pdf/text_absorber.hpp>
#include <aspose/pdf/bmp_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::BmpDevice bmp(Aspose::Pdf::Devices::Resolution(150));
std::ofstream out("page1.bmp", std::ios::binary);
bmp.Process(doc.Pages()[1], out);
}
サポートされているフォーマット
| フォーマット | 拡張子 | 読み取り | 書き込み |
|---|---|---|---|
| BMP | .bmp | — | ✓ |
| JPEG | .jpg | — | ✓ |
| TIFF | .tiff | — | ✓ |
| Text | .txt | — | ✓ |
| SVG | .svg | ✓ | — |
BMP、JPEG、TIFF はそれぞれのデバイスクラスが生成するページレンダリング出力です。テキストは TextAbsorber を使用して PDF から抽出されます。SVG はライブラリの SVG ロードパスを通じてベクターコンテンツとしてインポートされます。
オープンソースとライセンス
Aspose.PDF FOSS for C++ は MIT ライセンスの下で配布されており、完全なソースは以下で入手可能です github.com/aspose-pdf-foss/Aspose.PDF-FOSS-for-Cpp. ライセンスは、別途契約なしで商用利用、改変、再配布を許可します。