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:
ConvertAsynctakes the HTML as astringand returns aTask<byte[]>. - A4 with one-inch margins by default; pass a
PageSetupsuch asPageSetup.Letterfor another size. - Remote images are off by default, and only
data:images are embedded.RemoteImageOptionsturns them on per call, with an optional host allow-list. - A
<link rel="stylesheet">is never fetched; inline<style>blocks andstyleattributes are honoured.