Convert DOCX to PDF in C#

DocxToPdfConverter renders a Word document to PDF in one call, with no Word, no LibreOffice and no Office interop. It is managed code throughout, so it runs the same in a Linux container and on arm64 as on a Windows desktop.

using DocToolkit;

byte[] docx = File.ReadAllBytes("input.docx");
byte[] pdf = DocxToPdfConverter.Convert(docx);
File.WriteAllBytes("output.pdf", pdf);

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 the PDF as a byte[]; ConvertFile(inputPath, outputPath) goes from path to path.
  • An encrypted DOCX is refused with a DocumentConversionException; remove the password with DocxEditor.Unprotect first.
  • Non-Latin text depends on the fonts the host machine has. Pass enough fonts in a PdfFontOptions to Convert(docx, fonts): they replace the host's fallbacks rather than add to them, so too few is worse than none.
  • Remote images in the document are not fetched: the conversion succeeds without them.

Install

dotnet add package Ank.DocToolkit

NuGetDocumentationGitHub