
In various scenarios, you may need to display the content of Word DOCX files in your web or desktop applications. One of the suitable options in such cases is converting DOCX to HTML. To achieve this from .NET applications, this article explains how to convert DOCX files to HTML using C#. Furthermore, you will learn how to dynamically control the DOCX to HTML conversion using different options.
C# DOCX to HTML Converter API – Free Download
To perform DOCX to HTML conversion within .NET applications, we use Aspose.Words for .NET. It is a powerful class library that allows you to create and manipulate Word documents. Moreover, you can convert DOCX documents to various other file formats. You can either download the API or install it via NuGet.
PM> Install-Package Aspose.Words
Convert DOCX to HTML in C#
Aspose.Words for .NET makes it quite simple to convert a DOCX document to HTML. Simply load the DOCX document and save it as an HTML file. Below are the steps to convert a DOCX file to HTML in C#.
- Load the DOCX document using the Document class.
- Convert the DOCX document to HTML using the Document.Save(string, SaveFormat) method.
The following code sample demonstrates how to convert a DOCX to HTML using C#.
// Load the document from disk
Document doc = new Document("Document.docx");
// Save the document as HTML
doc.Save("Document.html", SaveFormat.Html);
Customize DOCX to HTML Conversion in C#
Aspose.Words for .NET also allows you to customize the DOCX to HTML conversion with various options. For example, you can specify a folder to store resource files (CSS, fonts, and images) separately. Additionally, you can export round-trip information of the document. The following sections show how to use these options.
C# DOCX to HTML – Export Roundtrip Information
MS Word provides a variety of features, not all of which are supported by HTML. To keep the HTML content close to the original document, round-trip information is used. The following steps demonstrate how to export round-trip information during DOCX to HTML conversion in C#.
- Load the DOCX document using the Document class.
- Create an object of the HtmlSaveOptions class.
- Enable the export of round-trip information using the HtmlSaveOptions.ExportRoundtripInformation property.
- Convert the DOCX document to HTML using the Document.Save(string, HtmlSaveOptions) method.
The following code sample shows how to export round-trip information during DOCX to HTML conversion in C# .NET.
// Load the document from disk
Document doc = new Document("Document.docx");
// Enable round-trip information
HtmlSaveOptions options = new HtmlSaveOptions();
options.ExportRoundtripInformation = true;
// Save as HTML
doc.Save("ExportRoundtripInformation_out.html", options);
C# DOCX to HTML Conversion – Specify the Resource Folder
During DOCX to HTML conversion, you can save resource files in a separate folder. Additionally, you can specify folders to store fonts and images separately. Below are the steps to achieve this.
- Load the DOCX document using the Document class.
- Create an object of the HtmlSaveOptions class.
- Set the resource folder using the HtmlSaveOptions.ResourceFolder property.
- Convert the DOCX document to HTML using the Document.Save(string, HtmlSaveOptions) method.
The following code sample shows how to store resources in a separate folder during C# DOCX to HTML conversion.
// Load the document
Document doc = new Document("document.docx");
// Specify the folder for resources
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.CssStyleSheetType = CssStyleSheetType.External;
saveOptions.ExportFontResources = true;
saveOptions.ResourceFolder = dataDir + @"\Resources";
saveOptions.ResourceFolderAlias = "http://example.com/resources";
// Convert the document to HTML
doc.Save("ExportResourcesUsingHtmlSaveOptions.html", saveOptions);
Learn more about working with resource folders during DOCX to HTML conversion.
C# API for DOCX to HTML Conversion – Get a Free License
You can get a temporary license to use Aspose.Words for .NET without evaluation limitations.
Conclusion
In this article, you learned how to convert DOCX files to HTML using C#. Furthermore, you saw how to use various options to customize the DOCX to HTML conversion. You can also explore other features of Aspose.Words for .NET using the documentation. Additionally, you can post your questions in our forum.