Aspose.PDF FOSS for C++ Features

Aspose.PDF FOSS for C++ Features

Aspose.PDF FOSS for C++ Features

Aspose.PDF FOSS for C++ is a C++20 library for creating, opening, modifying, and saving PDF documents. This page surveys the major capability areas and the classes that act as their entry points. Each area is covered in depth on its own developer-guide page.


Document Management

Aspose::Pdf::Document is the root object for every operation. It opens an existing file from a path, exposes the page collection through Pages(), and persists changes with Save(). Optimize() and OptimizeResources() reduce file size by removing unused or duplicated resources.

#include <aspose/pdf/document.hpp>

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

    auto& pages = doc.Pages();
    pages.Add();                 // append a blank page
    pages.Delete(1);             // remove the original first page

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

Text Extraction

Aspose::Pdf::Text::TextAbsorber walks a document or a single page and collects plain text, which is retrieved with Text() after calling Visit(). TextFragmentAbsorber performs the same walk but returns individual TextFragment objects through TextFragments(), giving access to each fragment’s position and text state.

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

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

    Aspose::Pdf::Text::TextAbsorber absorber;
    absorber.Visit(doc);

    std::cout << absorber.Text() << "\n";
}

Rendering to Raster Images

Page-rendering devices convert a Page into a raster image. BmpDevice, PngDevice, JpegDevice, and TiffDevice share the same Process(page, output) signature and are constructed with a Resolution that sets the output DPI.

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

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

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

Encryption

Document::Encrypt() protects a document with a user password (needed to open it) and an owner password (needed to change permissions), using an algorithm selected from the CryptoAlgorithm enum: RC4x40, RC4x128, AESx128, or AESx256. IsEncrypted() reports the current state and Decrypt() removes protection from an already-open document.

#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

Annotations attach visible notes, shapes, and markup to a page. Each annotation type derives from Annotation and is constructed with the target Page and a placement Rectangle, then added to the page through Annotations() (an AnnotationCollection).

#include <aspose/pdf/document.hpp>
#include <aspose/pdf/text_annotation.hpp>
#include <aspose/pdf/rectangle.hpp>

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

    Aspose::Pdf::Annotations::TextAnnotation note(
        page, Aspose::Pdf::Rectangle(50.0, 700.0, 250.0, 740.0, false));
    note.Contents("Reviewed and approved.");

    page.Annotations().Add(note);
    doc.Save("annotated.pdf");
}

Forms

AcroForm fields are managed through Aspose::Pdf::Forms::Form, reachable via Document::Form(). Field types such as TextBoxField are constructed like annotations — with a Page and a Rectangle — and registered with Form::Add().

#include <aspose/pdf/document.hpp>
#include <aspose/pdf/text_box_field.hpp>
#include <aspose/pdf/rectangle.hpp>

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

    Aspose::Pdf::Forms::TextBoxField field(
        page, Aspose::Pdf::Rectangle(100.0, 650.0, 300.0, 670.0, false));
    field.PartialName("customer_name");
    field.Value("Jane Doe");

    doc.Form().Add(field);
    doc.Save("form_filled.pdf");
}

Tips and Best Practices

  • Open input files by path with the Document constructor; call Save() with an explicit output path rather than overwriting the source while other handles to it may still be open.
  • Call Optimize() before Save() when a document has been heavily edited — it removes resources that are no longer referenced by any page.
  • Construct raster devices (BmpDevice, PngDevice, JpegDevice, TiffDevice) once per resolution and reuse them across pages instead of recreating a device per page.
  • Set both a user password and an owner password when calling Encrypt() — an empty user password lets any viewer open the file, while the owner password gates permission changes.
  • Keep annotation and field objects (TextAnnotation, TextBoxField, and similar) alive until after Save() returns, since the document holds references to the page content they were added to.

Common Issues

IssueCauseFix
Save() produces a file with unchanged contentEdits were made to a page or annotation copy rather than the object obtained from doc.Pages()Modify the object returned directly by Pages(), Annotations(), or Form()
Rendered image is empty or blankWrong page index passed to Process()PageCollection is 1-indexed — page 1 is doc.Pages()[1], not [0]
Encrypt() produces a file that still opens without a passwordUser password left emptyPass a non-empty string as the userPassword argument
Extracted text is empty for a scanned PDFThe page contains only raster image content, not text operatorsTextAbsorber reads text operators; image-only pages have none to extract
Field added with Form::Add() does not appear on the pageField’s Rectangle falls outside the page’s MediaBoxConfirm the rectangle coordinates are within Page::MediaBox() bounds

FAQ

Does Aspose.PDF FOSS for C++ depend on any commercial PDF library?

No. It is a from-scratch C++20 implementation with no dependency on any commercial PDF stack, released under the MIT license.

Can I create a new PDF from scratch instead of opening an existing one?

Yes. Document has a default constructor that creates an empty document; call Pages().Add() to append pages before saving.

Which raster formats can I render to?

BmpDevice, PngDevice, JpegDevice, and TiffDevice render individual pages to BMP, PNG, JPEG, and TIFF respectively.

What encryption algorithms are supported?

CryptoAlgorithm offers RC4x40, RC4x128, AESx128, and AESx256, passed to Document::Encrypt().

Where do I go next?

The developer guide includes dedicated pages for document and page management, PDF-native graphics and shapes, annotations and interactive forms, and the higher-level facade classes that wrap common file operations.


API Reference Summary

Class/MethodDescription
DocumentRoot object — open, create, save, and optimize a PDF
Document.Pages()Returns the document’s PageCollection
Document.Encrypt() / Decrypt()Password-protect or remove protection from a document
Document.Form()Returns the document’s AcroForm container
TextAbsorberExtracts plain text from a document or page
TextFragmentAbsorberExtracts positioned text fragments
BmpDevice / PngDevice / JpegDevice / TiffDeviceRender a page to a raster image format
ResolutionOutput DPI passed to a rendering device
CryptoAlgorithmEncryption algorithm selection: RC4 or AES, 40–256 bit
PermissionsAccess-control flags passed to Encrypt()
Annotation / AnnotationCollectionBase type and container for page annotations
TextAnnotationText note annotation
FormAcroForm field container reached via Document.Form()
TextBoxFieldSingle-line/multi-line text form field

See Also