Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create PDF 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");
// 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");
// 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");
// 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");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.