Introduction
Aspose.PDF FOSS for C++ is an open-source C++20 library for working with PDF documents. The API surface spans document and page management, text extraction, raster rendering, encryption, annotations, and interactive AcroForm fields — the operations most commonly needed when a C++ application has to create, inspect, or transform PDF files without depending on an external PDF engine or a commercial SDK.
The library links against nothing but the C++ standard library. Internal components such as the BMP, JPEG, and TIFF raster encoders and the page rasterizer are implemented from scratch inside the project, so there is no dependency on a system PDF renderer, a commercial PDF component, or any third-party image codec. This keeps the dependency footprint at zero beyond a C++20-conformant compiler and standard library.
Aspose.PDF FOSS for C++ is released under the MIT license. This post walks through the
main feature areas exposed by the Document class and its supporting types, followed by
a Quick Start example and the currently confirmed format support.
Key Features
Document and Page Management
The Document class is the entry point for opening, inspecting, and saving PDF files.
Pages are accessed through Document.Pages(), which returns a PageCollection supporting
Count(), Add(), Insert(pageNumber), and Delete(pageNumber). Document-level metadata
is read and updated through Document.Info() (a DocumentInfo instance) or set directly
with 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() and Document.OptimizeResources() reduce output file size by
consolidating and pruning unused resources before saving.
Text Extraction
Aspose::Pdf::Text::TextAbsorber extracts the plain-text content of a whole document or a
single page. Visit() walks the target and Text() returns the accumulated string.
TextFragmentAbsorber works at a finer granularity, exposing individual text runs through
a 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";
}
Rendering to Raster Images
Pages render to raster formats through per-format device classes — PngDevice,
JpegDevice, BmpDevice, and TiffDevice — each implementing Process(page, output).
Output resolution is controlled with the Resolution class.
#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 follows the same Process(page, output) pattern for JPEG output.
TiffDevice additionally supports multi-page output in a single call through
Process(document, fromPage, toPage, output), and exposes RenderingOptions() for
controlling how form fields are rendered into the raster output.
Encryption and Access Permissions
Document.Encrypt() protects a document with a user and owner password, selecting the
cipher through the CryptoAlgorithm enum: RC4x40, RC4x128, AESx128, or AESx256.
Granted permissions are described with the Permissions type, whose flag values include
PrintDocument, ModifyContent, ExtractContent, ModifyTextAnnotations, FillForm,
ExtractContentWithDisabilities, AssembleDocument, and 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");
}
An already-open encrypted document is decrypted in place with Document.Decrypt().
Document.IsEncrypted() reports whether a loaded document currently carries an encryption
dictionary.
Annotations
Page-level annotations — text notes, shapes, and markup — are reached through
Page.Annotations(), which returns an AnnotationCollection. Individual entries expose
shared properties such as Contents(), Rect(), Color(), Border(), and
AnnotationType(), and can be removed from the page or flattened into static content.
#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");
}
Any single Annotation can also be converted to static page content directly with its own
Flatten() method, independent of the form-flattening workflow described below.
Interactive Forms
AcroForm fields on a document are managed through Document.Form(), which returns a
Form instance supporting Count(), HasField(fieldName), Delete(fieldName), and
Flatten(). New fields are attached with Form.Add(field, pageNumber), where field is
a concrete Field subtype such as TextBoxField, CheckboxField, ChoiceField, or
ButtonField.
#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");
}
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, extract its text, and render the first page to 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);
}
Supported Formats
| Format | Extension | Read | Write |
|---|---|---|---|
| BMP | bmp | — | ✓ |
| JPEG | jpeg | — | ✓ |
| TIFF | tiff | — | ✓ |
| Text | txt | — | ✓ |
| SVG | svg | ✓ | — |
Open Source & Licensing
Aspose.PDF FOSS for C++ is released under the MIT license, permitting commercial use, modification, and redistribution without royalties. The source is available at github.com/aspose-pdf-foss/Aspose.PDF-FOSS-for-Cpp.