Convert Microsoft Word Document to Other Formats in C++
Tags: cpp, api, fileformat, word
Published: 2019-11-21 10:51:43

Sometimes we need to programmatically convert Microsoft Word Document to PDF, HTML, EPUB or various other formats. But, most of the existing libraries require us to write a humungous amount of code. Moreover, complex documents are not converted correctly, either content is disturbed of the resulting document, fonts are missing, tables and lists are rendered incorrectly. 

Fortunately, [Aspose.Words for C++](https://products.aspose.com/words/cpp), a native class library, let us easily and reliably converts documents from one format to another. It just requires two lines of code:

1. Load a document into a *Document* object using one of its constructors. By default, Aspose.Words will even auto-detect the file format for us.
2. Invoke one of the *Document.Save* methods on the *Document* object and specify the desired output format.

### Convert a Word Document to PDF
To convert a Microsoft Word document to PDF simply invoke the *Document.Save* method and specify a file name with the “.pdf” extension. The code sample below converts a whole document from DOC to PDF using default options.

{% gist https://gist.github.com/aspose-com-gists/518f03cac02abb105e02f55edb7de9f9.js file=cpp-Loading-and-Saving-Doc2Pdf-Doc2Pdf.cpp %}

### Convert a Word Document to HTML
To convert a Microsoft Word document to HTML simply invoke the *Document.Save* method and specify a file name with the “.html” extension.

{% gist https://gist.github.com/aspose-com-gists/518f03cac02abb105e02f55edb7de9f9.js file=cpp-Loading-and-Saving-ConvertDocumentToHtmlWithRoundtrip-ConvertDocumentToHtmlWithRoundtrip.cpp %}

### Export Fonts to HTML in Base64 Encoding
Using Aspose.Words, we can check whether font resources should be embedded to HTML in base 64 encodings. By default, the value is false and fonts are written to separate files. If this option is set to true, fonts will be embedded into the document's CSS in Base64 encoding. The property affects only HTML format and doesn't affect EPUB and MHTML. This is an extension to *saveOptions->set_ExportFontResources* option and *ExportFontsAsBase64* will work only if this property set to true. Below example shows how to export fonts to HTML in Base64 encoding.

{% gist https://gist.github.com/aspose-com-gists/518f03cac02abb105e02f55edb7de9f9.js file=cpp-Loading-and-Saving-ExportFontsAsBase64-ExportFontsAsBase64.cpp %}

For more HtmlSaveOptions, please check the [documentation](https://docs.aspose.com/display/wordscpp/Converting+a+Document) and [API Reference](https://apireference.aspose.com/cpp/words/class/aspose.words.saving.html_save_options/).

### Convert a Document to EPUB
The electronic publication (an EPUB) document is an HTML-based format commonly used for electronic book distribution. This format is fully supported in Aspose.Words for exporting electronic books compatible with the majority of devices used for reading. This code sample shows how to convert simple MS Word document to EPUB with a few lines of code.

{% gist https://gist.github.com/aspose-com-gists/518f03cac02abb105e02f55edb7de9f9.js file=cpp-Loading-and-Saving-ConvertDocumentToEPUB-ConvertDocumentToEPUB.cpp %}

### Installation
You may be wondering how to install Aspose.Words for C++ library. Please check [this article](https://docs.aspose.com/display/wordscpp/Installation), it provides step by step instructions to install the library.

Conversion is just one feature of Aspose.Words for C++, please check the [Documentation](https://docs.aspose.com/display/wordscpp/Home) to know about a number of other features.

If you need any assistance regarding Aspose.Words for C++, please visit [Aspose.Forums](https://forum.aspose.com/c/words). You can create a new topic over Aspose.Words for C++ forums and your post will be answered within a few hours.