Here is an article that guides on how to convert HTML to Word using C# by providing detailed information about the environment settings and also containing a runnable C# code to load the source HTML file for conversion. The options are available to configure the output DOCX file by using the DocSaveOptions class object. In this tutorial for writing HTML to Word converter C# language has been used.
Steps to Write HTML Content to Word Document using C#
- Add the reference to Aspose.HTML to the project for converting HTML to a Word file
- Open the source HTML file by loading it into HTMLDocument by providing file path
- Create the DocSaveOptions class object for configuring the output file
- Save the Word file converted from the source HTML file using the specified configuration
These simple steps describe how using C# convert HTML to Word file like DOCX is easily achieved. First information is shared for configuring the environment and then the source HTML file is loaded. In the next step, the DocSaveOptions class object is used for configuring the output DOCX file however you have the option to use all the default settings of this class. In the last and final step, the file is saved as DOCX, whereas you can also save it to any other format supported by MS Word.
Code to Convert HTML to Word using C#
In this sample code, we have learned how to convert HTML to Word in C# by using a few lines of code where in the beginning the source HTML file is loaded into HTMLDocument. Note that it is not necessary to load an existing HTML file but you can fetch an HTML string from some external source or use some URL also while calling the Converter.ConvertHTML() function. A lot of other configurations are also possible using the DocSaveOptions class object like setting the page size and margins of the output DOCX file.
In this how-to tutorial, we have learned how to write HTML content to Word document using C#. If you are interested in converting the HTML file to text, refer to the article on how to convert HTML to text in C#.