Create PDF via C++

PDF file creation programmatically using Aspose.PDF for C++ Library

How to generate PDF File via C++

In order to create a PDF file, we’ll use Aspose.PDF for C++ API which is a feature-rich, powerful, and easy-to-use document manipulation API for cpp platform. Open NuGet package manager, search for Aspose.PDF and install. You may also use the following command from the Package Manager Console.

Package Manager Console

PM > Install-Package Aspose.PDF.Cpp

How to Create PDF via C++


It is easy for the developers to create, load, modify and convert PDF files directly from C++ application in just a few lines of code.

  1. Include the namespace in your class file
  2. Initialize the Document class object.
  3. Add a page using Pages.Add() method.
  4. Create a new TextFragment object and set its text.
  5. Add TextFragment to the Paragraphs collection of the page.
  6. Save the PDF using Save(String) method.

Following source code shows how to create a PDF file using C++

This sample code shows how to create PDF using C++


    // String for path name.
    String _dataDir("C:\\Samples\\");

    // String for file name.
    String filename("sample-new.pdf");

    // Initialize document object
    auto document = MakeObject<Document>();
    // Add page
    auto page = document->get_Pages()->Add();

    // Add text to new page
    auto textFragment = MakeObject<TextFragment>(u"Hello World!");
    page->get_Paragraphs()->Add(textFragment);

    // Save updated PDF
    String outputFileName = _dataDir + filename;

    document->Save(outputFileName);