Introduction

Aspose.PDF FOSS for C++ is a new open-source library for working with PDF documents from C++20 code. It is released under the MIT license and carries no runtime dependency beyond the C++ standard library — the BMP, JPEG, and TIFF encoders, the page rasteriser, and the PDF parser are implemented internally, so there is no link to a commercial PDF engine, an external image codec, or a third-party cryptography library.

The library is organized around the Aspose::Pdf::Document class, which loads a PDF from a file path and exposes its contents through Document.Pages(), a PageCollection of individual Page objects. From there, the API covers text extraction, page rasterisation, document encryption, annotations, and AcroForm fields — the operations needed to read, inspect, and modify existing PDF files, or assemble new ones from scratch.

This release is for developers who need PDF processing inside a C++ codebase without adding a commercial license or a third-party dependency chain: document pipelines, content-extraction services, PDF-to-image conversion tools, and applications that need to encrypt or read back form data as part of a larger build.


What’s Included

Document Loading and Page Access

Opening an existing PDF is a single constructor call. Document parses the file and exposes its pages through Pages(), which returns a PageCollection supporting 1-based indexing, Count(), and Add()/Insert()/Delete() for restructuring page order.

#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";
}

Text Extraction

Aspose::Pdf::Text::TextAbsorber walks a Document or a single Page and collects the text content it finds. Call Visit() with either a document or a page, then read the result with Text(). HasErrors() reports whether the absorber encountered content it could not process.

#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";
    }
}

Rendering to Raster Images

Pages render to BMP, JPEG, or TIFF through the BmpDevice, JpegDevice, and TiffDevice classes. Each device is constructed with a Resolution and exposes a Process(page, output) method that writes the rendered image to an output stream.

#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);
}

Encryption

Document.Encrypt() protects a document with a user password, an owner password, a permission value, and a CryptoAlgorithm selection. The enum covers RC4-40 (RC4x40), RC4-128 (RC4x128), AES-128 (AESx128), and AES-256 (AESx256). Document.Decrypt() reverses the operation, and IsEncrypted() reports whether a document is currently encrypted.

#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");
}

Annotations

Each Page exposes an AnnotationCollection through Annotations(). A TextAnnotation is constructed against the owning document, positioned with a Rectangle, and added to the page’s collection with 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 Fields

Document.Form() returns the document’s Form, which exposes existing fields through Fields() as a std::vector<Field*>. Each Field reports its name and current value through PartialName() and Value(), and Form.Flatten() bakes every field’s appearance into the page content, removing interactivity.

#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");
}

Quick Start

Add the library as a CMake subdirectory and link against the aspose_pdf_foss target:

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

Open a document, print its page count, extract its text, and render the first page to a BMP file:

#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);
}

Supported Formats

FormatExtensionReadWrite
BMP.bmp
JPEG.jpg
TIFF.tiff
Text.txt
SVG.svg

BMP, JPEG, and TIFF are page-rendering outputs produced by their respective device classes. Text is extracted from a PDF via TextAbsorber. SVG is imported as vector content through the library’s SVG load path.


Open Source & Licensing

Aspose.PDF FOSS for C++ is distributed under the MIT license, with the full source available at github.com/aspose-pdf-foss/Aspose.PDF-FOSS-for-Cpp. The license permits commercial use, modification, and redistribution without a separate agreement.


Getting Started