Make PDF or Word documents in C++

Create a new document in almost any format programmatically using our high fidelity C++ library

Using our programming API, C++ developers can easily make a document in PDF, DOC, DOCX, HTML, EPUB, and many other formats with just a few lines of code.

View code snippet

Make a document using C++

With the given powerful API, C++ developers can create documents in almost any format. To do this, you need to follow a few steps using our C++ library:

  1. Add file name
  2. Start creating a document using C++
  3. Save the created document in the selected format

It is worth noting that a blank document is technically supposed to contain one paragraph, so when you programmatically create a document, you will get exactly that basic document structure.

Note that you can instantly add content to a newly created document. Thus, you will get not just an empty document, but a document containing the necessary content. For more information on how to edit a document, see the Editing page.

Create a document in C++ programmatically

The given C++ library allows you to programmatically create a document in any supported format – PDF, DOCX, DOC, RTF, ODT, EPUB, HTML and other.

Try our powerful functionality and see how to create a document in some formats using the following example:

Make a new document using C++
Select the target format from the list
Run code
using namespace Aspose::Words;

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);

auto font = builder->get_Font();
font->set_Name(u"Courier New");
font->set_Color(System::Drawing::Color::get_Blue());
font->set_Size(36);
font->set_HighlightColor(System::Drawing::Color::get_Yellow());

builder->Write(u"Morbi enim nunc faucibus a.");

doc->Save(u"Output.docx");
using namespace Aspose::Words; auto doc = MakeObject<Document>(); auto builder = MakeObject<DocumentBuilder>(doc); auto firstRun = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed. "); auto secondRun = MakeObject<Run>(doc, u"Morbi enim nunc faucibus a."); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(firstRun); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(secondRun); builder->MoveTo(secondRun); builder->StartBookmark(u"Aspose bookmark"); // If NextSibling is null, then most likely this is the last Run in the Paragraph. if (secondRun->get_NextSibling() != NULL) builder->MoveTo(secondRun->get_NextSibling()); else builder->MoveTo(secondRun->get_ParentParagraph()); builder->EndBookmark(u"Aspose bookmark"); doc->Save(u"Output.docx"); using namespace Aspose::Words; auto doc = MakeObject<Document>(); auto builder = MakeObject<DocumentBuilder>(doc); auto firstRun = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed. "); auto secondRun = MakeObject<Run>(doc, u"Morbi enim nunc faucibus a."); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(firstRun); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(secondRun); builder->MoveTo(secondRun); builder->StartBookmark(u"Aspose bookmark"); // If NextSibling is null, then most likely this is the last Run in the Paragraph. if (secondRun->get_NextSibling() != NULL) builder->MoveTo(secondRun->get_NextSibling()); else builder->MoveTo(secondRun->get_ParentParagraph()); builder->EndBookmark(u"Aspose bookmark"); auto saveOptions = MakeObject<PdfSaveOptions>(); saveOptions->get_OutlineOptions()->get_BookmarksOutlineLevels()->Add(u"Aspose bookmark", 1); doc->Save(u"Output.docx", saveOptions);
using namespace Aspose::Words;

auto doc = MakeObject<Document>();

auto run = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed.");
auto para = doc->get_FirstSection()->get_Body()->get_FirstParagraph();
para->AppendChild(run);

auto comment = MakeObject<Comment>(doc, u"John Doe", u"JD", System::DateTime::get_Today());
comment->SetText(u"Quisque fringilla leo.");

auto commentRangeStart = MakeObject<CommentRangeStart>(doc, comment->get_Id());
auto commentRangeEnd = MakeObject<CommentRangeEnd>(doc, comment->get_Id());

run->get_ParentNode()->InsertBefore(commentRangeStart, run);
run->get_ParentNode()->InsertAfter(commentRangeEnd, run);
commentRangeEnd->get_ParentNode()->InsertAfter(comment, commentRangeEnd);

comment->AddReply(u"Jane Doe", u"JD", System::DateTime::get_Now(),
    u"Pellentesque vel sapien justo.");

doc->Save(u"Output.docx");
using namespace Aspose::Words; auto doc = MakeObject<Document>(); auto builder = MakeObject<DocumentBuilder>(doc); auto firstRun = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed. "); auto secondRun = MakeObject<Run>(doc, u"Morbi enim nunc faucibus a."); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(firstRun); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(secondRun); builder->MoveTo(secondRun); builder->StartBookmark(u"Aspose bookmark"); // If NextSibling is null, then most likely this is the last Run in the Paragraph. if (secondRun->get_NextSibling() != NULL) builder->MoveTo(secondRun->get_NextSibling()); else builder->MoveTo(secondRun->get_ParentParagraph()); builder->EndBookmark(u"Aspose bookmark"); doc->Save(u"Output.docx"); using namespace Aspose::Words; auto doc = MakeObject<Document>(); auto builder = MakeObject<DocumentBuilder>(doc); auto firstRun = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed. "); auto secondRun = MakeObject<Run>(doc, u"Morbi enim nunc faucibus a."); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(firstRun); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(secondRun); builder->MoveTo(secondRun); builder->StartBookmark(u"Aspose bookmark"); // If NextSibling is null, then most likely this is the last Run in the Paragraph. if (secondRun->get_NextSibling() != NULL) builder->MoveTo(secondRun->get_NextSibling()); else builder->MoveTo(secondRun->get_ParentParagraph()); builder->EndBookmark(u"Aspose bookmark"); auto saveOptions = MakeObject<PdfSaveOptions>(); saveOptions->get_OutlineOptions()->get_BookmarksOutlineLevels()->Add(u"Aspose bookmark", 1); doc->Save(u"Output.docx", saveOptions);
using namespace Aspose::Words;

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);

auto firstRun = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed. ");
auto secondRun = MakeObject<Run>(doc, u"Morbi enim nunc faucibus a.");
doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(firstRun);
doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(secondRun);

builder->MoveTo(secondRun);
builder->StartBookmark(u"Aspose bookmark");
// If NextSibling is null, then most likely this is the last Run in the Paragraph.
if (secondRun->get_NextSibling() != NULL)
    builder->MoveTo(secondRun->get_NextSibling());
else
    builder->MoveTo(secondRun->get_ParentParagraph());
builder->EndBookmark(u"Aspose bookmark");

doc->Save(u"Output.docx");
using namespace Aspose::Words; auto doc = MakeObject<Document>(); auto builder = MakeObject<DocumentBuilder>(doc); auto firstRun = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed. "); auto secondRun = MakeObject<Run>(doc, u"Morbi enim nunc faucibus a."); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(firstRun); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(secondRun); builder->MoveTo(secondRun); builder->StartBookmark(u"Aspose bookmark"); // If NextSibling is null, then most likely this is the last Run in the Paragraph. if (secondRun->get_NextSibling() != NULL) builder->MoveTo(secondRun->get_NextSibling()); else builder->MoveTo(secondRun->get_ParentParagraph()); builder->EndBookmark(u"Aspose bookmark"); doc->Save(u"Output.docx"); using namespace Aspose::Words; auto doc = MakeObject<Document>(); auto builder = MakeObject<DocumentBuilder>(doc); auto firstRun = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed. "); auto secondRun = MakeObject<Run>(doc, u"Morbi enim nunc faucibus a."); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(firstRun); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(secondRun); builder->MoveTo(secondRun); builder->StartBookmark(u"Aspose bookmark"); // If NextSibling is null, then most likely this is the last Run in the Paragraph. if (secondRun->get_NextSibling() != NULL) builder->MoveTo(secondRun->get_NextSibling()); else builder->MoveTo(secondRun->get_ParentParagraph()); builder->EndBookmark(u"Aspose bookmark"); auto saveOptions = MakeObject<PdfSaveOptions>(); saveOptions->get_OutlineOptions()->get_BookmarksOutlineLevels()->Add(u"Aspose bookmark", 1); doc->Save(u"Output.docx", saveOptions);
using namespace Aspose::Words;

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);

auto shape = builder->InsertChart(ChartType::Pie, 432, 252);
auto chart = shape->get_Chart();
chart->get_Title()->set_Text(u"Demo Chart");

chart->get_Series()->Clear();
chart->get_Series()->Add(u"Series 1",
    MakeArray<String>({ u"Category1", u"Category2", u"Category3" }),
    MakeArray<double>({ 2.7, 3.2, 0.8 }));

doc->Save(u"Output.docx");
using namespace Aspose::Words; auto doc = MakeObject<Document>(); auto builder = MakeObject<DocumentBuilder>(doc); auto firstRun = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed. "); auto secondRun = MakeObject<Run>(doc, u"Morbi enim nunc faucibus a."); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(firstRun); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(secondRun); builder->MoveTo(secondRun); builder->StartBookmark(u"Aspose bookmark"); // If NextSibling is null, then most likely this is the last Run in the Paragraph. if (secondRun->get_NextSibling() != NULL) builder->MoveTo(secondRun->get_NextSibling()); else builder->MoveTo(secondRun->get_ParentParagraph()); builder->EndBookmark(u"Aspose bookmark"); doc->Save(u"Output.docx"); using namespace Aspose::Words; auto doc = MakeObject<Document>(); auto builder = MakeObject<DocumentBuilder>(doc); auto firstRun = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed. "); auto secondRun = MakeObject<Run>(doc, u"Morbi enim nunc faucibus a."); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(firstRun); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(secondRun); builder->MoveTo(secondRun); builder->StartBookmark(u"Aspose bookmark"); // If NextSibling is null, then most likely this is the last Run in the Paragraph. if (secondRun->get_NextSibling() != NULL) builder->MoveTo(secondRun->get_NextSibling()); else builder->MoveTo(secondRun->get_ParentParagraph()); builder->EndBookmark(u"Aspose bookmark"); auto saveOptions = MakeObject<PdfSaveOptions>(); saveOptions->get_OutlineOptions()->get_BookmarksOutlineLevels()->Add(u"Aspose bookmark", 1); doc->Save(u"Output.docx", saveOptions);
Run code

How to to make a document in C++

  1. Install Aspose.Words for C++
  2. Add a library reference (import the library) to your C++ project
  3. Create a new document
  4. Call the Save() method, passing the file name
  5. Get the result as a separate file

C++ library to create documents

There are three options to install Aspose.Words for C++ to your developer environment. Please choose one that resembles your needs and follow the step-by-step instructions:

System Requirements

You can use this C++ library to develop software on Microsoft Windows, Linux and macOS operating systems:

  • GCC >= 6.3.0 and Clang >= 3.9.1 are required for Linux
  • Xcode >= 12.5.1, Clang and libc++ are required for macOS

If you develop software for Linux or macOS, please check information on additional library dependencies (fontconfig and mesa-glu open-source packages) in Product Documentation.

Most popular file formats

5%

Subscribe to Aspose Product Updates

Get monthly newsletters and offers directly delivered to your mailbox.

© Aspose Pty Ltd 2001-2024. All Rights Reserved.