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++.

// Create document
auto doc = MakeObject<Document>();
auto pages = doc->get_Pages();
pages->Add();
// Numeration of Pages starts from 1
auto page = pages->idx_get(1);
auto paragraps = page->get_Paragraphs();
// Create text fragment
auto text = MakeObject<TextFragment>(u"PDF API for C++");
auto ts = text->get_TextState();
// Set text state
ts->set_FontSize(16);
ts->set_FontStyle(FontStyles::Italic);
// Add to paragraph
paragraps->Add(text);
// Add text to paragraph
paragraps->Add(MakeObject<TextFragment>(u"This example shows how to create a PDF with text using Aspose.PDF for C++."));
// Save PDF file
doc->Save(u"Example1.pdf");

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++.

// Create Document object
auto doc = MakeObject<Document>();
auto pages = doc->get_Pages();
pages->Add();
// Create TextBuilder
auto tb = MakeObject<TextBuilder>(pages->idx_get(1));
// Create TextFragment
auto text = MakeObject<TextFragment>(u"Hello world!");
text->set_Position(MakeObject<Position>(100, 800));
// Append TextFragment
tb->AppendText(text);
// Create another TextFragment
text = MakeObject<TextFragment>(u"This example is created by Aspose.Pdf for C++.");
text->set_Position(MakeObject<Position>(150, 750));
tb->AppendText(text);
// Create another TextFragment
text = MakeObject<TextFragment>(u"It demonstrates how to use TextBuilder to append text into PDF file.");
text->set_Position(MakeObject<Position>(200, 700));
tb->AppendText(text);
// Create TextParagraph
auto par = MakeObject<TextParagraph>();
par->set_Position(MakeObject<Position>(250,650));
par->AppendLine(u"New paragraph");
par->AppendLine(u"Line 2");
par->AppendLine(u"Line 3");
tb->AppendParagraph(par);
// Save PDF document
doc->Save(u"Created PDF.pdf");

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++.

// Create Document object
auto doc = MakeObject<Document>();
auto pages = doc->get_Pages();
pages->Add();
auto page = pages->idx_get(1);
// Create an image
auto stream = MakeObject<IO::MemoryStream>();
SharedPtr<Bitmap> bitmap = MakeObject<Bitmap>(200, 200);
SharedPtr<Graphics> graphics = Graphics::FromImage(bitmap);
graphics->Clear(System::Drawing::Color::get_Yellow());
graphics->FillRectangle(Brushes::get_Blue(), System::Drawing::Rectangle(0, 0, 200, 100));
bitmap->Save(stream, Imaging::ImageFormat::get_Bmp());
// Create rectangle
double x = 100.0, y = 600.0, width = 200.0, height = 200.0;
auto rect = MakeObject<Aspose::Pdf::Rectangle>(x, y, x + width, y + height);
// Add image to PDF
stream->Seek(0, System::IO::SeekOrigin::Begin);
page->AddImage(stream, rect);
// Save PDF document
doc->Save(u"Created PDF.pdf");

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.
  • 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++.

// Create a text file
System::IO::File::WriteAllText(u"Attachment.txt", u"Some info");
SharedPtr<FileSpecification> fileSpecification = MakeObject<FileSpecification>(u"Attachment.txt", u"Sample text file");
// Add attachment to document's attachment collection
auto doc = MakeObject<Document>();
doc->get_EmbeddedFiles()->Add(fileSpecification);
// Add a page to PDF
doc->get_Pages()->Add();
// Save PDF document
doc->Save(u"Created PDF.pdf");

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