Many developers use Microsoft Office Interop library for creating Word documents but we often see many of them inquiring that how to create Word document in C++ by using simple and easy code. You can easily create a DOCX using C++ and without interop or Microsoft Office automation using simple steps.
Steps to Create Word Document using C++
- Use Aspose.Words.Cpp NuGet package
- Include reference to Aspose::Words and Saving namespaces
- Create a new Document Class instance
- Instantiate DocumentBuilder Class object to work with Word file
- Set some text and textual properties using DocumentBuilder object
- Save the output Word document on disk
In the following example, we are going to witness, how to generate DOCX programmatically using C++. We have illustrated a simple scenario where by we have added few lines of text and applied bold formatting to document. These steps help you understand how to perform other actions on Word documents that you can achieve using Microsoft Interop and MS Word with no reliance on any of two.
Code to Create Word Document using C++
#include <Aspose.Words.Cpp/Document.h> | |
#include <Aspose.Words.Cpp/DocumentBuilder.h> | |
#include <Aspose.Words.Cpp/Saving/DocSaveOptions.h> | |
#include <Aspose.Words.Cpp/Saving/SaveOutputParameters.h> | |
#include <system/io/file.h> | |
#include <Aspose.Words.Cpp/License.h> | |
using System::ArrayPtr; | |
using System::MakeArray; | |
using System::MakeObject; | |
using System::SharedPtr; | |
using System::String; | |
using namespace Aspose::Words; | |
using namespace Aspose::Words::Saving; | |
class AddingWordDocumentinCPP | |
{ | |
public: | |
void CreateWordDocumentinCPP() | |
{ | |
// File name and path of license file | |
System::String testLicenseFileName = u"Aspose.Total.NET.lic"; | |
// Setting the Aspose.Words before creating Word document | |
SharedPtr<License> wordsLicense = System::MakeObject<License>(); | |
// Setting license | |
wordsLicense->SetLicense(testLicenseFileName); | |
// Create an instance of Document class of Aspose.Words for C++ | |
// to add a blank Word document | |
SharedPtr<Document> WordDocumentUsingCPP = MakeObject<Document>(); | |
// Instantiate DocumentBuilder class to add content to the Word Document | |
SharedPtr<DocumentBuilder> WordDocumentBuilder = MakeObject<DocumentBuilder>(WordDocumentUsingCPP); | |
// Add some text in word file with formatting | |
WordDocumentBuilder->set_Bold(true); | |
WordDocumentBuilder->Writeln(u"We're adding this line of text in the word document using DocumentBuilder Class"); | |
WordDocumentBuilder->Writeln(u"This does not require Office Interop or Office Automation"); | |
// Save the generated word document to docx format | |
WordDocumentUsingCPP->Save(u"Word_Document_Created_using_CPP.docx"); | |
} | |
}; |
Previously, we looked into How to Create Word Document in C# without Interop. Now, we have learnt how to create DOCX using C++. In order to use above example you don’t need to install Microsoft Word on your computer or the server where application will be installed. This makes creating word document a lot easier and quicker and no dependency on Microsoft platform.