Convert DOCX to HTML in C#

DocxToHtmlConverter turns a Word document into HTML and keeps its structure: headings stay headings, tables stay tables. Images travel inside the HTML as data: URIs, so the result stands on its own.

using DocToolkit;

byte[] docx = File.ReadAllBytes("input.docx");
string html = DocxToHtmlConverter.Convert(docx);
File.WriteAllText("output.html", html);

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

  • Synchronous: Convert takes the .docx as a byte[] and returns a string.
  • The result is a complete HTML document, with <html>, <head> and <body>, not a fragment. To embed it in a page, take the body out with an HTML parser.
  • Images in the document are embedded as data: URIs, so nothing in the output points at a file that does not exist.
  • ConvertWithReport also lists what the HTML could not carry. A plain DOCX reports at least one entry, SectionLayoutFlattened, because page geometry is exported without its section data.

Install

dotnet add package Ank.DocToolkit

NuGetDocumentationGitHub