C++ PDF Library – Create PDF Files Programmatically in C++

With the power of the Internet, everything has gone digital and paperless systems have become popular. The digital documents, i.e. PDF, are among the building blocks of paperless systems that have made life easier by providing automated generation and processing features. Most of the businesses use PDF document automation for generating reports, receipts, invoices, and other business documents dynamically. Therefore, in this article, I’ll demonstrate how to integrate the features of PDF automation in C++ based applications and generate PDF files using C++.

C++ PDF Library

For creating PDF files, we’ll use Aspose.PDF for C++ API which is a native C++ library to work with PDF documents programmatically. It allows you to create interactive PDF documents with the support of a variety of PDF elements. Aspose.PDF for C++ is available on NuGet as well as in the Downloads section of Aspose.

Create a PDF File in C++

First of all, we will create a simple PDF file and add some text to the first page in the form of a paragraph. The following are the steps to be followed:

  • Create a Document object.
  • Add a page to PageCollection of the Document.
  • Get the paragraph of the page.
  • Create a TextFragment object and add it to the paragraph.
  • Save the PDF document.

The following code sample shows how to create a PDF document in C++.

Output

Create PDF Files in C++

Create a PDF File using TextBuilder in C++

In this section, I’ll show you how to use the TextBuilder class to append various text fragments and paragraphs on the page. Furthermore, in this example, you will learn how to set the position of the text fragment on the page. The following are the steps to perform this operation:

  • Create a Document object.
  • Add a page to the document.
  • Create a TextBuilder object.
  • Create a TextFragment and add its text.
  • Set the position of the TextFragment.
  • Append TextFragment using the TextBuilder.
  • Save PDF document.

The following code sample shows how to create a PDF using TextBuilder in C++.

Output

C++ PDF API

Create a PDF File with Image in C++

You can also create and add images to the PDF documents using Aspose.PDF for C++. The following are the steps to perform this operation:

  • Create a Document object.
  • Add a page to the document.
  • Create an image to be added.
  • Add the image to the PDF file.
  • Save PDF file.

The following code sample shows how to create and add an image to a PDF document in C++.

Output

C++ PDF Library

Create a PDF File with Attachments in C++

The PDF format also allows adding attachments to the document. There is a variety of file formats that can be added as an attachment to the PDF file. The following are the steps to embed a file in PDF using Aspose.PDF for C++:

  • Load a file to be attached in SharedPtr<FileSpecification>.
  • Create an object of Document class.
  • Embed the file to the Document.
  • Save the PDF file.

The following code sample shows how to add an attachment to the PDF document in C++.

Output

Generate PDF in C++

Learn more about Aspose.PDF for C++

You can learn more about Aspose.PDF for C++ from the documentation and source code examples.

See Also