HTML (HyperText Markup Language) is a leading file format for web pages supported by all browsers. In various cases, we may need to convert HTML files or content from live webpages into Word documents (DOC, DOCX, DOT, DOTM, DOCM). It helps to edit the text of HTML web pages or apply the text formatting. In this article, we will learn how to convert HTML to a Word document using C#.
The following topics shall be covered in this article:
- C# API to Convert HTML to DOCX — Free Download
- Convert HTML to Word in C#
- Convert a Web Page to Word from URL in C#
- HTML String to Word Conversion using C#
C# API to Convert HTML to DOCX — Free Download
For converting HTML files or webpages to Word processing file formats, we will be using Aspose.Words for .NET API. It is a complete solution to create, edit, convert or analyze Word documents programmatically. Please either download the DLL of the API or install it using NuGet.
Convert HTML to Word in C#
We can easily convert HTML files to Word documents programmatically by following the steps given below:
- Load an HTML file using the Document class.
- Call the Document.Save(string, SaveFormat) method to save the HTML file as “output.docx”.
The SaveFormat enumeration in Document.Save() method specifies the format in which you want to convert the HTML file. The following code sample shows how to convert an HTML file to DOCX using C#.
Convert a Web Page to Word from URL in C#
We can also convert an HTML web page directly from a live URL to a Word document by following the steps given below:
- Firstly, download web page content as a System.Byte array from the specified URL.
- Next, initiate MemoryStream object with an array object as argument.
- Then, create an instance of the HtmlLoadOptions class.
- After that, create an instance of the Document class and initialize it with MemoryStream and HtmlLoadOptions objects.
- Finally, call the Document.Save(string, SaveFormat) method to save the HTML file as “output.docx”.
The following code sample shows how to convert an HTML web page to DOCX using C#.
Convert an HTML String to Word using C#
We can generate a Word document from an HTML string dynamically by following the steps given below:
- Firstly, create an instance of the Document class.
- Next, create an instance of the DocumentBuilder class with Document object.
- Then, insert HTML into the document using DocumentBuilder.InsertHtml(string) method.
- Finally, save the Word document using the Document.Save(string, SaveFormat) method.
The following code sample shows how to convert an HTML string to DOCX using C#.
Get a Free License
Please try the API without evaluation limitations by requesting a free temporary license.
In this article, we have learned how to convert HTML to a Word document using C#. We have also seen how to convert live web pages from a URL to Word files programmatically. Besides, you can learn more about Aspose.Words for .NET API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.