Convert HTML to DOCX in C#

HtmlToDocxConverter builds a Word document from HTML, laid out on A4 unless you choose another page. Nothing the markup points at is fetched, so it is safe on a server with no network.

using DocToolkit;

string html = "<h1>Invoice 2026-114</h1><p>Due on receipt</p>";
byte[] docx = await HtmlToDocxConverter.ConvertAsync(html);
File.WriteAllBytes("output.docx", docx);

What it needs

  • No browser, headless or otherwise.
  • No Office install, and no LibreOffice.
  • No native files: managed code only.
  • No network at runtime, unless you ask for it.

Good to know

  • Asynchronous: ConvertAsync takes the HTML as a string and returns a Task<byte[]>.
  • A4 with one-inch margins by default; pass a PageSetup such as PageSetup.Letter for another size.
  • Remote images are off by default, and only data: images are embedded. RemoteImageOptions turns them on per call, with an optional host allow-list.
  • A <link rel="stylesheet"> is never fetched; inline <style> blocks and style attributes are honoured.

Install

dotnet add package Ank.DocToolkit

NuGetDocumentationGitHub