How to Convert HTML to Word in C# .NET Core

Cloudmersive
1 min readApr 25, 2024

We can easily convert HTML strings to Word (DOCX) documents without writing a ton of complex code.

Using code examples below, we can call a free API to make our conversion. We’ll just need a free Cloudmersive API key to make up to 800 conversions per month (our total will reset the following month once we reach it).

First, let’s run the below command in our Package Manager console to install the client SDK via NuGet:

Install-Package Cloudmersive.APIClient.NETCore.DocumentAndDataConvert -Version 2.2.1

Next, let’s call the function. We can enter our API key in the authorization snippet:

using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Model;

namespace Example
{
public class ConvertWebHtmlToDocxExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");

var apiInstance = new ConvertWebApi();
var inputRequest = new HtmlToOfficeRequest(); // HtmlToOfficeRequest | HTML input to convert to DOCX

try
{
// Convert HTML to Word DOCX Document
byte[] result = apiInstance.ConvertWebHtmlToDocx(inputRequest);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling ConvertWebApi.ConvertWebHtmlToDocx: " + e.Message );
}
}
}
}

That’s all there is to it — now we can easily make HTML to DOCX format conversions with just a few lines of code.

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.