C++ version PDF processing control Aspose.PDF function demo: Use PDF API for C++ to dynamically create PDF files

With the power of the Internet, everything has become digital and paperless systems are becoming more and more popular. Digital documents (i.e. PDFs) are an integral part of paperless systems that make life easier by providing automated generation and processing. Most businesses use PDF document automation to dynamically generate reports, receipts, invoices and other business documents.

In order to create PDF files, we will use Aspose.PDF for C++ API , which is a native C++ library that can programmatically manipulate PDF documents. It can create interactive PDF documents with the support of various PDF elements.

In this article, I will demonstrate how to integrate the functionality of PDF automation in a C++-based application, and how to dynamically generate PDF files using C++.


Create a simple PDF file in C++

First, we'll create a simple PDF file and add some text to the first page in paragraphs. Here are the steps to follow:

  • Create a Document object.
  • Add the page to the Document's PageCollection.
  • Get the paragraphs 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();
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(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(u"This example shows how to create a PDF with text using Aspose.PDF for C++."));
// Save PDF file
doc->Save(u"Example1.pdf");

input HTML file

Create PDF file using TextBuilder in C++

In this section, I'll show you how to use the TextBuilder class to attach various text fragments and paragraphs to a page. Also, in this example, you will learn how to set the position of a text fragment on the page. Here are the steps to do this:

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

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

// Create Document object
auto doc = MakeObject();
auto pages = doc->get_Pages();
pages->Add();
// Create TextBuilder
auto tb = MakeObject(pages->idx_get(1));
// Create TextFragment
auto text = MakeObject(u"Hello world!");
text->set_Position(MakeObject(100, 800));
// Append TextFragment
tb->AppendText(text);
// Create another TextFragment
text = MakeObject(u"This example is created by Aspose.Pdf for C++.");
text->set_Position(MakeObject(150, 750));
tb->AppendText(text);
// Create another TextFragment
text = MakeObject(u"It demonstrates how to use TextBuilder to append text into PDF file.");
text->set_Position(MakeObject(200, 700));
tb->AppendText(text);
// Create TextParagraph
auto par = MakeObject();
par->set_Position(MakeObject(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");

input HTML file

Create a PDF file with images in C++

Use Aspose.PDF for C++ to create images and add images to PDF documents. Here are the steps to do this:

  • Create a Document object.
  • Add pages to the document.
  • Create the image to add.
  • Add images to PDF files.
  • Save the 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();
auto pages = doc->get_Pages();
pages->Add();
auto page = pages->idx_get(1);
// Create an image
auto stream = MakeObject();
SharedPtrbitmap = MakeObject(200, 200);
SharedPtrgraphics = 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(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");

input HTML file

Create a PDF file with attachments using C++

The PDF format also allows attachments to be added to the document. A variety of file formats can be added to PDF files as attachments. Here are the steps to embed a file into PDF using Aspose.PDF for C++:

  • Load the file to be appended into the SharedPtr.
  • Create an object of the Document class.
  • Embed the file into the document.
  • Save the PDF file.

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

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

input HTML file

Guess you like

Origin blog.csdn.net/m0_67129275/article/details/131674965