1. Products
  2.   PDF
  3.   C++
  4.   PoDoFo
 
  

Free C++ API to Create, Manipulate & Parse PDF Files.

A Leading Open Source PDF Manipulating Library enables C++ Developers to Effortlessly Create, Manipulate, Encrypt, Manage Fonts, and Parse PDF Documents inside C++ Apps.

What is PoDoFo Library?

In the world of C++ development, the ability to programmatically create, modify, and extract data from PDF files is a common requirement. The PoDoFo library, an open-source and portable C++17 library, emerges as a powerful and versatile solution for all things PDF. PoDoFo is a freely available and is designed for working with the PDF file format. It enables software developers to parse PDF files, inspect high-level PDF entities (like annotations and form fields), modify them in memory, and write changes back to disk inside their own applications. The library also makes it easy to build new PDFs from scratch without any external decencies.

PoDoFo is a robust C++ library that empowers software developers to work with the Portable Document Format (PDF) programmatically. Licensed under LGPL v2.0 or later for the library, and GPL v2.0 or later for its accompanying tools, the PoDoFo library ensures flexibility and openness for both commercial and open-source projects. It provides a rich set of tools to parse, manipulate, and generate PDF documents from scratch. Whether you need to automate report generation, extract text for data analysis, or add digital signatures for security, PoDoFo offers a comprehensive and flexible API to meet your needs.

Previous Next

Getting Started with PoDoFo

The recommend way to install PoDoFo is using CMake. Please use the following command for a smooth installation.

Install PPTXTemplater from GitHub

 vcpkg install fontconfig freetype libxml2 openssl libjpeg-turbo libpng tiff zlib
mkdir build
cd build
cmake -DCMAKE_TOOLCHAIN_FILE=C:\vcpkg\scripts\buildsystems\vcpkg.cmake -DCMAKE_BUILD_TYPE=Debug ..
cmake --build . --config Debug

Install PPTXTemplater from GitHub

 git clone https://github.com/podofo/podofo.git

You can also install it manually; download the latest release files directly from GitHub repository.

PDF Creation and Modification

The open source PoDoFo library has included complete support for PDF documents creation and manipulation inside C++ library. At its core, PoDoFo excels at creating new PDF documents and modifying existing ones. Software developers can add new pages, draw text, and insert various graphical elements, open encrypted document with ease. The following simple example demonstrates, how software developers can create a Simple PDF document inside C++ applications

How to Create a Simple PDF via C++ Library?

#include 

using namespace PoDoFo;

int main() {
    PdfStreamedDocument document("HelloWorld.pdf");
    PdfPainter painter;
    PdfPage* pPage;

    try {
        pPage = document.CreatePage(PdfPage::CreateStandardPageSize(ePdfPageSize_A4));
        painter.SetPage(pPage);
        PdfFont* pFont = document.CreateFont("Arial");
        pFont->SetFontSize(18.0);
        painter.SetFont(pFont);
        painter.DrawText(56.69, pPage->GetPageSize().GetHeight() - 56.69, "Hello World!");
        painter.FinishPage();
        document.Close();
    } catch (const PdfError & e) {
        e.PrintErrorMsg();
        return e.GetCode();
    }

    return 0;
}
 

Text Extraction from PDF Files via C++

The PoDoFo library has included a powerful mechanism to load and extract text from PDF documents with just a couple of lines of C++ code. This feature is invaluable for applications that require content indexing, data mining, or document analysis. In the following code example, developers can load a PDF into a PdfMemDocument, iterate through its pages, and use the ExtractTextTo method to retrieve the text content.

How to Load and Extract Text from PDF Files via C++ API?

#include 
using namespace PoDoFo;

PdfMemDocument pdf("document.pdf");
int pageCount = pdf.GetPageCount();
for (int i = 0; i < pageCount; ++i) {
    PdfPage* page = pdf.GetPage(i);
    PdfContentsTokenizer tokenizer(page);

    const char* token = nullptr;
    PdfVariant var;
    EPdfContentsType type;

    while (tokenizer.ReadNext(type, token, var)) {
        if (type == ePdfContentsType_Keyword) {
            printf("Operator: %s\n", token);
        } else if (type == ePdfContentsType_Variant) {
            // Examine var as needed
        }
    }
}

 

Add Digital Signatures to PDF via C++ API?

Security is paramount when dealing with official documents. The PoDoFo library supports the addition of digital signatures to PDF files, ensuring the authenticity and integrity of your documents. This is particularly useful for contracts, invoices, and other legal documents. While a full digital signature example is complex and requires a valid certificate, the following conceptual code illustrates the process.

How to Add a Digital Signature to PDF via C++?

 // Conceptual Code - Not a complete working example
#include 

using namespace PoDoFo;

int main() {
    PdfMemDocument document("unsigned.pdf");
    PdfPage* pPage = document.GetPage(0);

    // Create a signature field
    PdfSignatureField sigField(pPage,
        PdfRect(100, pPage->GetPageSize().GetHeight() - 100, 200, 50),
        &document);

    // Set signature properties
    // ...

    // Sign the document using a custom signer implementation
    // MySigner signer("my_certificate.pfx", "password");
    // document.Sign(sigField, signer);

    document.Write("signed.pdf");

    return 0;
}
 

Font Handling and Unicode Support

The free PoDoFo library provides robust support for various font types, including TrueType and Type1 fonts. The library also handles font subsetting, which helps to reduce the file size of the generated PDFs. Furthermore, its excellent Unicode support ensures that you can work with a wide range of languages and character sets. Full support for PDF signing (PAdES-B compliance), RSA/ECDSA encryption, and asynchronous or deferred signing workflows.