Create MS Word Documents (DOC/DOCX) using C++

Create Word Documents in C++

Aspose.Words is a feature-rich collection of APIs that lets you create, edit, and convert MS Word documents programmatically. It provides a wide range of basic as well as advanced features for manipulating word processing documents. In this article, you will learn how to use Aspose.Words for C++ and create MS Word documents from scratch using C++. The step by step guide as well as code samples will let you know how to insert text, images, tables, lists, and other elements in Word documents.

C++ API to Create MS Word Documents

Aspose.Words for C++ lets you generate and manipulate the word processing documents within C++ applications without MS Word. You can download the API or install it within your C++ applications using NuGet with the following command.

PM> Install-Package Aspose.Words.Cpp

Create MS Word Documents using C++

Let’s first create a simple Word document and save it as a .doc or .docx file. For this, you need to follow the below steps:

The following code sample shows how to create a Word DOCX document using C++.

Edit or Update Existing Word DOC/DOCX using C++

You may also edit the existing Word documents using Aspose.Words for C++. For this, the API uses the Document Object Model (DOM) for the in-memory representation of a document. The DOM lets you access the elements of a Word document such as header/footer, paragraphs, tables, and etc. Read more about the DOM here.

For updating a Word document, simply load it using Document class and process it as desired. The following are the steps to edit and update an existing Word document.

  • Load the Word document using Document class.
  • Create an object of DocumentBuilder class to access the content.
  • Access the desired paragraph (or any other element) and update the content.
  • Save the updated document using Document->Save() method.

The following code sample shows how to update the text of a paragraph in a Word document using C++.

Insert Images in Word Document using C++

The following are the steps to insert an image within MS Word documents using C++.

The following code sample shows how to insert an image into a Word document using C++.

Insert Table in Word Documents using C++

The table is an important element of the Word documents to keep the data in the form of rows and columns. In order to generate a table within the Word documents, follow the below steps.

  • Create a new Word document using Document class.
  • Create an object of Table class.
  • Insert table into the document using Document->get_FirstSection()->get_Body()->AppendChild() method.
  • Create a new row using the Row class.
  • Insert row into the table using Table->AppendChild(row) method.
  • Create and new Cell and insert text into it using Cell->get_FirstParagraph()->AppendChild() method.
  • Insert cell into the row using Row->AppendChild() method.
  • Repeat the process for adding multiple rows.
  • Save the document using Document->Save() method.

The following code sample shows how to insert a table in Word document using C++.

Add Lists in Word Documents using C++

Last but not the least, creating a list in the Word documents. The following are the steps to create a bullet list.

  • Create a new Word document or load an existing one using Document class.
  • Define a new DocumentBuilder object and initialize it with the Document object.
  • Create list using DocumentBuilder->get_ListFormat()->set_List(Document->get_Lists()->Add(ListTemplate::NumberArabicDot)) method.
  • Populate the list and set the list level.
  • Save the document as a file.

The following code sample shows how to create a list in a Word document using C++.

Conclusion

In this article, you have seen how to create MS Word documents from scratch using C++. Furthermore, you have learned how to insert elements such as text, images, tables, and lists in new or existing Word documents. You can explore more about the advanced features of the API using the documentation.

See Also