PDF TOOLS

How To Convert PDF to PDFA in C++

Curtis Chau
Curtis Chau
Updated: October 18, 2026

The Portable Document Format (PDF) has been the go-to format for sharing documents across various platforms. However, with the increasing significance of digital archiving, there is a growing need to archive documents in a standardized format that ensures long-term preservation and accessibility. The PDF/A (PDFA) format is specifically designed for this purpose, offering a stable and self-contained format that is suitable for archiving.

Understanding PDF/A

PDF/A, an ISO-standardized PDF variant, is designed to preserve documents over time, maintaining content accessibility and integrity. In contrast to a regular PDF file, a PDF/A file comes with specific restrictions that make it self-contained.

This article shows how to convert a PDF to PDF/A from C++. QPDF handles the PDF structure and the inspection, Ghostscript performs the PDF/A conversion, and veraPDF validates the result.

QPDF C++ Library

QPDF is a C++ library and command-line tool designed to support working with, transforming, and parsing PDF files. It allows developers to programmatically access and manipulate the content of PDF files. QPDF is an open-source project that offers a range of features, including encryption, decryption, linearization, and optimization. QPDF preserves content; it does not convert to PDF/A.

Prerequisites

Before you begin, ensure that you have the following set up:

  1. Code::Blocks IDE: Download and install Code::Blocks from the official website (Code::Blocks Official Site).
  2. QPDF Library: Download the latest version of the QPDF library for your operating system from the QPDF website (QPDF Official Site).
  3. Ghostscript: Download it from ghostscript.com. It is AGPL licensed, and a commercial licence is available from Artifex.
  4. veraPDF: Download the validator from verapdf.org to check the converted file.

Creating a PDF to PDF/A Project

  1. Open Code::Blocks: Launch the Code::Blocks IDE on your computer.
  2. Create a New Project: Click File in the top menu, then select New, followed by Project.
  3. Choose Project Type: In the New from template window, choose Console application and click Go. Select C/C++ language and click Next.
  4. Enter Project Details: Provide a project name in the Project title field (e.g., "PDFtoPDFA"). Choose where to save project files and click Next.
  5. Select Compiler: Choose a compiler, and if needed, manually select one from the list. Click Finish.

Adding QPDF to the Project

To include the QPDF header files in Code::Blocks, follow these steps:

  1. Click Project in the menu.
  2. Select Build options from the context menu.
  3. In the Build options dialog, go to the Search directories tab.
  4. In the Compiler tab, click the Add button and browse to the directory containing the QPDF header files (usually located in the include folder).
  5. Then, in the Link tab, click the Add button and include lib and bin directories.
  6. Click OK to close the Build options dialog.

Additionally, you need to establish a connection with the QPDF library during the linking phase. Follow these steps:

  1. In the Build options dialog, go to the Linker settings tab.
  2. Under Link Libraries, click Add, then browse to the directory with QPDF library files (usually with a .a or .lib file extension on Windows).
  3. Click Add again, and enter the QPDF library's name (e.g., libqpdf.a or qpdf.lib).
  4. Click OK to close the Build options dialog.

Steps to Convert PDF to PDF/A File

Why QPDF Alone Cannot Produce PDF/A

PDF/A requires embedded fonts, an OutputIntent with an ICC color profile, XMP metadata, and no encryption or JavaScript. QPDF rewrites the file structure and leaves the content as it is, so it cannot add or fix those parts. Its setQDFMode option writes QDF, a human-readable layout meant for debugging and hand editing, which has nothing to do with PDF/A.

The conversion itself comes from Ghostscript, which has a pdfwrite device with PDF/A output. The C++ program below drives Ghostscript, then uses QPDF to inspect the result.

Include Necessary Header Files

#include <cstdlib>
#include <iostream>
#include <string>
#include <qpdf/QPDF.hh>
#include <qpdf/QPDFObjectHandle.hh>
#include <qpdf/QPDFPageDocumentHelper.hh>
C++

Set Paths for Input and Output Files

In the main function (starting point of the C++ program), set paths for input and output files:

std::string input_pdf_path = "input.pdf";
std::string output_pdf_a_path = "output-pdfa.pdf";
C++

Convert with Ghostscript from C++

Build the Ghostscript command and run it with std::system, checking the return value. On Windows the console executable is gswin64c instead of gs.

std::string command =
    "gs -dPDFA=2 -dBATCH -dNOPAUSE -sDEVICE=pdfwrite "
    "-sColorConversionStrategy=RGB -dPDFACompatibilityPolicy=1 "
    "-sOutputFile=\"" + output_pdf_a_path + "\" PDFA_def.ps \"" + input_pdf_path + "\"";

int result = std::system(command.c_str());
if (result != 0) {
    std::cerr << "Ghostscript failed with code " << result << std::endl;
    return 1;
}
C++

The command comes from the Ghostscript pdfwrite documentation. Two pieces need a short explanation:

  • PDFA_def.ps: a PostScript prefix file that declares the OutputIntent. Copy lib/PDFA_def.ps from your Ghostscript installation next to your program, then edit its /ICCProfile entry to the fully qualified path of an sRGB ICC profile. Ghostscript ships one as iccprofiles/srgb.icc.
  • -dPDFACompatibilityPolicy=1: tells Ghostscript to drop any feature that cannot be written in a compliant way and print a warning for each one. The default policy of 0 can leave you with a file that is not compliant.

Ghostscript supports conformance level b for PDF/A-1, PDF/A-2 and PDF/A-3, and -dPDFA=2 selects PDF/A-2. Ghostscript is released under the AGPL, and Artifex sells a commercial licence for use that the AGPL does not allow.

Inspect the Result with QPDF

This is where QPDF is useful. Open the converted file and check that it has an OutputIntents entry and a page count:

QPDF output_pdf;
output_pdf.processFile(output_pdf_a_path.c_str());

bool has_output_intents = output_pdf.getRoot().hasKey("/OutputIntents");
std::cout << "OutputIntents present: " << (has_output_intents ? "yes" : "no") << std::endl;
std::cout << "Pages: " << QPDFPageDocumentHelper(output_pdf).getAllPages().size() << std::endl;
C++

You can run a structural check from a shell with qpdf --check output-pdfa.pdf. For a converted file, expect OutputIntents to be present and the page count to match input.pdf. QPDF does not validate PDF/A, so this only confirms that the file is intact and carries an OutputIntent.

Validate with veraPDF

Use veraPDF to check the file against the PDF/A-2b profile:

verapdf --flavour 2b output-pdfa.pdf
SHELL

A passing file reports isCompliant="true" in the veraPDF report. If it reports failed rules, fix them before archiving the file, since only the validator can confirm PDF/A conformance.

IronPDF for C++ (Coming Soon)

IronPDF for C++ is in development. The planned library will generate PDFs from HTML text or a URL, combine, split and modify existing PDFs, and extract text and images from them. Join the IronPDF for C++ beta program to get early access and help shape the API.

Convert PDF to PDF/A in C#

IronPDF Library is a .NET PDF library that provides functionality for working with PDF documents. It allows developers to create, modify, and convert PDF documents using C# or VB.NET. Users can use IronPDF for generating PDF files from HTML, ASPX, Word documents (.doc), or images dynamically and incorporate rich elements like charts, tables, and image data (like JPG, PNG formats). It also enables merging, splitting, and editing pages of existing PDF files, along with text extraction and content manipulation of PDF data.

IronPDF converts PDF to the PDF/A-3b Format standard in a few lines of code. The following code helps to achieve this task:

using IronPdf;

// Create a PdfDocument object or open any PDF file
PdfDocument pdf = PdfDocument.FromFile("wikipedia.pdf");

// Use the SaveAsPdfA method to save to file
pdf.SaveAsPdfA("pdf-a3-wikipedia.pdf", PdfAVersions.PdfA3);

// The above C# code demonstrates the use of IronPDF to convert a PDF document to a PDF/A-3b compliant file.

IronPDF also converts HTML, images, and Word documents to PDF files. For further information, please visit this IronPDF Documentation Page. IronPDF provides a free trial of IronPDF to test the functionality for commercial production. Download IronPDF directly.

Conclusion

This article converted a PDF to PDF/A from C++ by running Ghostscript, inspecting the output with QPDF, and validating it with veraPDF. QPDF manipulates PDFs; the PDF/A conversion itself comes from Ghostscript, and veraPDF proves it.

IronPDF for C++ is coming, with HTML to PDF, merging, splitting and editing planned for C++. Join the beta for early access.

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

Read More

Related Articles