Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created August 24, 2020 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/6d4a3300f974c38f3d5f775c35d649e5 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/6d4a3300f974c38f3d5f775c35d649e5 to your computer and use it in GitHub Desktop.
Create Word Documents using C++
System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
// Add a logo to the top left of the page. The image is placed in front of all other text.
System::SharedPtr<Shape> shape = builder->InsertImage( u"Aspose Logo.png", RelativeHorizontalPosition::Page, 60.0, RelativeVerticalPosition::Page, 60.0, -1.0, -1.0, WrapType::None);
doc->Save(u"document_with_image.docx");
System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
// Create a numbered list based on one of the Microsoft Word list templates and
// apply it to the current paragraph in the document builder.
builder->get_ListFormat()->set_List(doc->get_Lists()->Add(ListTemplate::NumberArabicDot));
// There are 9 levels in this list, lets try them all.
for (int32_t i = 0; i < 9; i++)
{
builder->get_ListFormat()->set_ListLevelNumber(i);
builder->Writeln(System::String(u"Level ") + i);
}
// Create a bulleted list based on one of the Microsoft Word list templates
// and apply it to the current paragraph in the document builder.
builder->get_ListFormat()->set_List(doc->get_Lists()->Add(ListTemplate::BulletDiamonds));
// There are 9 levels in this list, lets try them all.
for (int32_t i = 0; i < 9; i++)
{
builder->get_ListFormat()->set_ListLevelNumber(i);
builder->Writeln(System::String(u"Level ") + i);
}
// This is a way to stop list formatting.
builder->get_ListFormat()->set_List(nullptr);
// Save the document to disk.
builder->get_Document()->Save(u"document_with_list.docx");
System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<Table> table = System::MakeObject<Table>(doc);
// Add the table to the document.
doc->get_FirstSection()->get_Body()->AppendChild(table);
System::SharedPtr<Row> row = System::MakeObject<Row>(doc);
row->get_RowFormat()->set_AllowBreakAcrossPages(true);
table->AppendChild(row);
// We can now apply any auto fit settings.
table->AutoFit(AutoFitBehavior::FixedColumnWidths);
// Create a cell and add it to the row
System::SharedPtr<Cell> cell = System::MakeObject<Cell>(doc);
cell->get_CellFormat()->get_Shading()->set_BackgroundPatternColor(System::Drawing::Color::get_LightBlue());
cell->get_CellFormat()->set_Width(80);
// Add a paragraph to the cell as well as a new run with some text.
cell->AppendChild(System::MakeObject<Paragraph>(doc));
cell->get_FirstParagraph()->AppendChild(System::MakeObject<Run>(doc, u"Row 1, Cell 1 Text"));
// Add the cell to the row.
row->AppendChild(cell);
// We would then repeat the process for the other cells and rows in the table.
// We can also speed things up by cloning existing cells and rows.
row->AppendChild((System::StaticCast<Node>(cell))->Clone(false));
row->get_LastCell()->AppendChild(System::MakeObject<Paragraph>(doc));
row->get_LastCell()->get_FirstParagraph()->AppendChild(System::MakeObject<Run>(doc, u"Row 1, Cell 2 Text"));
// Save the document to disk.
doc->Save(u"document_with_table.docx");
// Initialize a Document.
System::SharedPtr<Document> doc = System::MakeObject<Document>();
// Use a document builder to add content to the document.
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
// Add text
builder->Writeln(u"Hello World!");
// Save the document to disk.
doc->Save(u"document.docx");
// Initialize a Document.
System::SharedPtr<Document> doc = System::MakeObject<Document>(u"document.docx");
// Use a document builder to add content to the document.
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
// Get section
auto section = doc->get_Sections()->idx_get(0);
// Get body
auto body = section->get_Body();
// Get first paragraph
auto para = body->get_FirstParagraph();
// Update text
auto run = para->get_Runs()->idx_get(0);
run->set_Text(u"This is the updated text.");
// Save the document to disk.
doc->Save(u"updated_document.docx");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment