How to create a PDF file in C++

Hello everyone! In this tutorial, We are going to learn how we can create a PDF file in C++. We will use apose.pdf for C++.  Let’s know a little about what is Apose.pdf

Apose.pdf (C++ PDF Library )

Apose.PDF is a native C++ library that helps to make PDFs. It is available on NuGet as well easily in the Documents section from the Apose.

Steps to create a Pdf file.

  1. We will create a pdf file. For creating the pdf use auto doc, auto docwhich helps to make object documents and auto to get the pages of the pdf file.
  2. Then add the page number index of the pdf file starting from 1 using idx_get(1).
  3. Text fragment object will be created.
  4. Then you need to set text font style and font size.
  5. After that add paragraph.
  6. Add text to the paragraph.
  7. Save pdf file.

Code to create pdf file in C++

auto doc = MakeObject<Document>();
auto pages = doc->get_Pages();

auto page = pages->idx_get(1);// Numbering of pages starts from index 1.
auto paragraps = page->get_Paragraphs();	

auto text = MakeObject<TextFragment>(u"Hello World"); // Creating text fragment
auto ts = text->get_TextState(); //Text state means font size and font style

ts->set_FontSize(16); //Set text size
ts->set_FontStyle(FontStyles::Bold); //Set the font style
paragraps->Add(text);// Adding to the paragraph

paragraps->Add(MakeObject<TextFragment>(u"How to create pdf files in C++")); // Adding text in paragraph

doc->Save(u"xyz.pdf");// This saves the PDF file


A pdf will be generated as shown



One response to “How to create a PDF file in C++”

  1. Fatima Hashem says:

    hello my code is gving me an errors at the first lines and at the text Fragments i guess i missed some library kindly may you help me

Leave a Reply

Your email address will not be published. Required fields are marked *