Class DocxToPdfConverter
- Namespace
- DocToolkit
- Assembly
- DocToolkit.dll
Renders a Word (.docx) package to PDF. Pure managed - no browser, no LibreOffice.
public static class DocxToPdfConverter
- Inheritance
-
DocxToPdfConverter
- Inherited Members
Methods
Convert(byte[])
Renders the .docx in docx and returns PDF bytes.
public static byte[] Convert(byte[] docx)
Parameters
docxbyte[]
Returns
- byte[]
Examples
byte[] pdf = DocxToPdfConverter.Convert(docx);
Convert(byte[], PdfFontOptions?)
Renders the .docx in docx, using fonts for characters
the renderer cannot otherwise encode.
public static byte[] Convert(byte[] docx, PdfFontOptions? fonts)
Parameters
docxbyte[]The document to render.
fontsPdfFontOptionsFonts to fall back to, or null for none.
Returns
- byte[]
Remarks
Whether a document containing non-Latin text renders otherwise depends on the machine. The renderer falls back to whatever fonts the host happens to have, and a Windows box offers ones that do not cover Cyrillic - so the same document converts on one machine and is refused on another. Supplying the font removes the machine from the answer.
See PdfFontOptions for the one side effect worth knowing about.
Exceptions
- ArgumentNullException
docxis null.- ArgumentException
docxis empty.- DocumentConversionException
The document could not be rendered.
ConvertAsync(Stream, Stream, PdfFontOptions?, CancellationToken)
Reads a .docx from source and writes the rendered PDF to
destination, using fonts for characters the renderer
cannot otherwise encode.
public static Task ConvertAsync(Stream source, Stream destination, PdfFontOptions? fonts, CancellationToken ct = default)
Parameters
sourceStreamThe stream the .docx package is read from.
destinationStreamThe stream the PDF is written to.
fontsPdfFontOptionsFonts to fall back to, or null for none.
ctCancellationTokenCancels the read, the render and the write.
Returns
Exceptions
- ArgumentNullException
Either stream is null.
- ArgumentException
sourceis not readable or held no bytes, ordestinationis not writable.- OperationCanceledException
ctwas cancelled.- DocumentConversionException
The document could not be rendered.
ConvertAsync(Stream, Stream, CancellationToken)
Reads a .docx from source and writes the rendered PDF to
destination.
source is read to its end and destination is
written; neither is disposed or closed, and destination is never
sought or read back, so both may be sockets, files or HTTP message bodies. Neither has to be
seekable.
The PDF is rendered whole and then written, so a failure leaves
destination UNTOUCHED rather than carrying a truncated document.
Until 2026-08-20 it was written straight through as the renderer produced it; that
prevented the repair-and-retry the array overloads apply, and measurably diverged from
them - see HtmlToPdfConverter's private EmitAsync for the numbers.
No network access, and safe in an air-gapped environment, as for Convert(byte[]).
public static Task ConvertAsync(Stream source, Stream destination, CancellationToken ct = default)
Parameters
sourceStreamThe stream the .docx package is read from.
destinationStreamThe stream the PDF is written to.
ctCancellationTokenCancels the read, the render and the write.
Returns
Exceptions
- ArgumentNullException
Either stream is null.
- ArgumentException
sourceis not readable or held no bytes, ordestinationis not writable.- OperationCanceledException
ctwas cancelled.- DocumentConversionException
The DOCX could not be rendered.
ConvertFile(string, string)
Renders inputPath to a PDF at outputPath.
public static void ConvertFile(string inputPath, string outputPath)