Blog
Convert HTML to Docx with C# / .NET Core
Intro
If you are writing c# / .net core code, you might be tasked with generating a Microsoft Word (Docx) file. After some research, you discover that this is not as trivial as you hoped it would be. You might have ended up on stackoverflow seeing old, outdated libraries or confusing answers that ask you to use interop or LibreOffice. Let’s make your pain go away right now.
Our API will take your .html file and convert it to a docx. Just make sure your HTML is saved as a .html file and accessible at a URL that our service can ingest. For example, see this: http://www.api2pdf.com/wp-content/uploads/2021/01/sampleHtml.html — Ideally a file storage provider like S3 or Azure Blob Storage. See the code sample below.
Generate Word or .docx file from HTML with C# / .NET Core
Step 1) Open up your package manager and run the command
Install-Package Api2Pdf -Version 2.0.0
Step 2) Grab an API key from https://portal.api2pdf.com. Only takes 60 seconds.
Step 3) Use the sample code below and replace “YOUR-API-KEY” with the api key you acquired in step 2.
var a2pClient = new Api2Pdf("YOUR-API-KEY");
var request = new LibreFileConversionRequest
{
Url = "http://www.api2pdf.com/wp-content/uploads/2021/01/sampleHtml.html"
}
var apiResponse = a2pClient.LibreOffice.HtmlToDocx(request);
Console.WriteLine(apiResponse.FileUrl);
Console.ReadLine();
And that’s it! Modify the code as you see fit. Hopefully this saves you time and makes converting converting HTML to word files easy and painless for those writing C# / .NET core code.
See full github library
We have a whole .net based client library for our API that does a lot more than just this. Check out the full library capabilities here: https://github.com/Api2Pdf/api2pdf.dotnet