How to convert HTML to a Word DOCX Document using C# in .NET Framework

Converting between HTML and DOCX, while often necessary, is not always easy, especially when working with large volumes of files. That’s where today’s tutorial comes in; we will be accomplishing this conversion with a bare minimum of effort and time.

First, let’s run the following command in Package Manager’s console to install the API client that we need.

Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 1.3.4

From here we simply call ConvertWebHtmlToDocx and feed it the HTML file in question.

using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Model;
namespace Example
{
public class ConvertWebHtmlToDocxExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
// Configuration.Default.AddApiKeyPrefix("Apikey", "Bearer");
var apiInstance = new ConvertWebApi();
var inputRequest = new HtmlToOfficeRequest(); // HtmlToOfficeRequest |
try
{
// HTML to DOCX
byte[] result = apiInstance.ConvertWebHtmlToDocx(inputRequest);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling ConvertWebApi.ConvertWebHtmlToDocx: " + e.Message );
}
}
}
}

That’s it. Seriously, it’s just that easy. Our document/conversion API also allows for a wide range of other useful similar functions. These include converting HTML and other file types to PDF, manipulating documents, and changing image formats.

Image for post
Image for post

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store