C++: Create PDF Files

PDF stands for Portable Document Format, which is a standardized and versatile file format developed by Adobe in 1992. PDF files have many advantages, for example, they can be opened on almost any hardware or operating system without the need for a PDF reader, their layout can remain unchanged no matter what hardware or operating system is used to open them, and their content is not easy to edit. This article will demonstrate how to programmatically create PDF Files using C++.

C++ Library to Create PDF Files

In order to create PDF files in C++ applications, this article uses a third-party library – Spire.PDF for C++. Spire.PDF for C++ is a professional PDF API that can be applied to create, write, edit, convert, and read PDF files in C++ applications.

NuGet is the easiest way to integrate Spire.PDF for C++ into your application, and there are two ways to install Spire.PDF for C++ through NuGet.

1. Install Spire.PDF for C++ using Manage NuGet Packages.

  • Create or open your project in Visual Studio.
  • Click Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution…
  • Search for “Spire.PDF.Cpp” and click install.

2. Install Spire.PDF for C++ using Package Manager Console.

  • Create or open your project in Visual Studio.
  • Click Tools -> NuGet Package Manager -> Package Manager Console.
  • Type the command “Install-Package Spire.PDF.Cpp” and press enter.

In case you can’t install the library through NuGet, you can download the package from the official site and then manually import it into your project. For more details, you can check the documentation — How to Integrate Spire.PDF for C++ in a C++ Application.

Create PDF Files in C++

A PDF file can contain a wide variety of elements, such as text, images, tables, hyperlinks, signatures, fillable form fields, attachments and annotations. The following steps will explain how to create a simple PDF file with text and image:

  • Create a PdfDocument instance.
  • Add a page using PdfDocument->GetPages()->Add() method.
  • Create two PdfSolidBrush instances.
  • Create two PdfTrueTypeFont instances.
  • Create a PdfStringFormat instance and set the text alignment to center using PdfStringFormat ->SetAlignment(PdfTextAlignment::Center) method.
  • Specify the title text.
  • Draw title text on the center of the page with specific brush, font, and string format using PdfPageBase->GetCanvas()->DrawString() method.
  • Specify body text.
  • Create a PdfTextWidget instance based on the body text.
  • Create a RectangleF instance which will then be used to specify the page area for drawing body text.
  • Create a PdfTextLayout instance and set the layout type to paginate to make the content paginated automatically.
  • Draw body text on specific area of the page using PdfTextWidget.Draw() method.
  • Create a PdfImage instance and load an image using PdfImage->FromFile() method.
  • Draw image on the page using PdfPageBase->GetCanvas()->DrawImage() method.
  • Save the result file using PdfDocument->SaveToFile() method.
#include "Spire.Pdf.o.h";

using namespace Spire::Pdf;
using namespace Spire::Common;
using namespace std;

int main()
{
    //Create a PdfDocument instance
    PdfDocument* pdf = new PdfDocument();
    //Add a page
    PdfPageBase* page = pdf->GetPages()->Add();

    //Create two PdfSolidBrush instances
    PdfSolidBrush* brush1 = new PdfSolidBrush(new PdfRGBColor(Color::GetBlue()));
    PdfSolidBrush* brush2 = new PdfSolidBrush(new PdfRGBColor(Color::GetBlack()));

    //Create two PdfTrueTypeFont instances
    PdfTrueTypeFont* font1 = new PdfTrueTypeFont(L"Times New Roman", 20, PdfFontStyle::Bold, true);
    PdfTrueTypeFont* font2 = new PdfTrueTypeFont(L"Times New Roman", 12, PdfFontStyle::Regular, true);

    //Create a PdfStringFormat instance
    PdfStringFormat* format = new PdfStringFormat();
    //Set the text alignment to center
    format->SetAlignment(PdfTextAlignment::Center);

    //Specify title text
    wstring title = L"Introduction to C++ Programming Language";

    //Draw title on the center of the page
    page->GetCanvas()->DrawString(title.c_str(), font1, brush1, new PointF((float)page->GetCanvas()->GetClientSize()->GetWidth() / 2, 20), format);

    //Specify body text
    wstring body = L"C++ (pronounced C plus plus) is a computer programming language based on C. It was created for writing programs for many different purposes. In the 1990s, C++ became one of the most used programming languages in the world.";

    //Create a PdfTextWidget instance
    PdfTextWidget* widget = new PdfTextWidget(body.c_str(), font2, brush2);
    //Create a RectangleF instance
    RectangleF* rect = new RectangleF(new PointF(0, 50), new SizeF((float)page->GetCanvas()->GetClientSize()->GetWidth(), (float)page->GetCanvas()->GetClientSize()->GetHeight()));

    //Create a PdfTextLayout instance
PdfTextLayout* layout = new PdfTextLayout();
    //Set the PdfLayoutType to Paginate to make the content paginated automatically
    layout->SetLayout(PdfLayoutType::Paginate);

    PdfNewPage* newPage = Object::Convert<PdfNewPage>(page);

    //Draw body text on the page
    widget->Draw(newPage, rect, layout);

    //Load an image
    PdfImage* image = new PdfImage();
    image = image->FromFile(L"C++.png");
    float width = image->GetWidth()*0.35;
    float height = image->GetHeight()*0.35;
    float x = (page->GetCanvas()->GetClientSize()->GetWidth() - width) / 2;

    //Draw image on the page
    page->GetCanvas()->DrawImage(image, x, 100, width, height);

    //Save the result file
    pdf->SaveToFile(L"CreatePdf.pdf");
    pdf->Close();
    delete pdf;
    delete brush1;
    delete brush2;
    delete font1;
    delete font2;
    delete format;
    delete widget;
    delete rect;
    delete layout;
    delete image;
}
Create PDF File in C++
Design a site like this with WordPress.com
Get started