How to Convert HTML to Word using C#

This short tutorial walks you through setting up the environment for document conversion as well as providing extensive instructions on how to convert HTML to Word using C#. The document conversion can be done quickly and easily with just a few API calls and no third-party software. A working example of how to convert HTML to Word in C# can be found in this guide.

Steps to Convert HTML to Word using C#

  1. Install GroupDocs.Conversion for .NET package from the NuGet.org in the .NET project
  2. Add a reference to the GroupDocs.Conversion namespace for performing document conversion from HTML to Word
  3. Load the input HTML file by creating an instance of the Converter class
  4. Create an instance of the WordProcessingConvertOptions class and set convert options for the output Word document
  5. Call the Convert method of the Converter class, pass the output file name and WordProcessingConvertOptions to it

In the above section, you can find the step-by-step guide for developing the HTML to Word C# capability. To create C# HTML to Word converter feature, you only need to follow these steps in order and write a few lines of code. After establishing the project with the relevant library and importing the necessary namespace, all you have to do now is load the input HTML file for conversion. Then, you can customize the output DOCX file by initializing the WordProcessingConvertOptions class and finally, invoke the Convert method to save the output file.

Code to Convert HTML to Word using C#

using System;
using GroupDocs.Conversion.Options.Convert;
namespace ConvertHtmlToWordUsingCSharp
{
class Program
{
public static void Main(string[] args) // Main function to convert HTML to Word using C#
{
// Remove the watermark in output Word by adding license
string licensePath = "GroupDocs.Conversion.lic";
GroupDocs.Conversion.License lic = new GroupDocs.Conversion.License();
lic.SetLicense(licensePath);
// Load the source HTML file for conversion to Word
var converter = new GroupDocs.Conversion.Converter("sample.html");
// Set the convert options for the output Word
var convertOptions = new WordProcessingConvertOptions();
// Convert and save the HTML in DOCX format
converter.Convert("converted.docx", convertOptions);
Console.WriteLine("Done");
}
}
}

You can see how the convert HTML to DOCX C# capability is implemented in the example above. It’s a functioning example that you can use as-is or modify with the WordProcessingConvertOptions class to tailor the output file. You can also convert HTML documents to a variety of other document formats such as PDF, RTF, PNG, XLSX, PSD, and many more.

We have discussed the process to convert HTML to Word and produced sample code for it in this guide. If you are interested to read our recent article on, visit how to convert MHTML to HTML using C#.

 English