Class XlsxToPdfConverter
- Namespace
- DocToolkit
- Assembly
- DocToolkit.dll
Renders an Excel (.xlsx) workbook to PDF. Pure managed — no browser, no LibreOffice, no Excel.
The same three members as DocxToPdfConverter and PptxToPdfConverter, deliberately: a consumer who has used one has used all three.
No page-setup parameter, for the same reason DocxToPdfConverter has none — the workbook already carries its own print setup, per worksheet, and the renderer honours it. Taking a PageSetup here would mean overriding something the caller's own document already states.
Fidelity is bounded, and features the renderer cannot represent are dropped rather than reported: this package has no warning channel, and adding one is a decision deferred until somebody needs it rather than made by accident here. See README's Known limitations.
Text is the exception: it is refused, not dropped. A cell holding characters the fonts this renderer can reach do not cover throws, naming the character - measured on Windows for Hebrew, Arabic, Persian and Cyrillic. Pass PdfFontOptions to Convert(byte[], PdfFontOptions?). Merged cells render; a sheet's right-to-left direction does not.
public static class XlsxToPdfConverter
- Inheritance
-
XlsxToPdfConverter
- Inherited Members
Methods
Convert(byte[])
Renders the .xlsx in xlsx and returns PDF bytes.
No network access, and safe in an air-gapped environment. The resource policy handed to the renderer refuses both remote resolution and local file access, set explicitly rather than inherited — see DocToolkit.PdfRenderPolicy.
public static byte[] Convert(byte[] xlsx)
Parameters
xlsxbyte[]The workbook to render.
Returns
- byte[]
Examples
byte[] pdf = XlsxToPdfConverter.Convert(xlsx);
Exceptions
- ArgumentNullException
xlsxis null.- ArgumentException
xlsxis empty.- DocumentConversionException
The workbook could not be rendered.
Convert(byte[], PdfFontOptions?)
Renders the .xlsx in xlsx, using fonts for characters
the renderer cannot otherwise encode.
public static byte[] Convert(byte[] xlsx, PdfFontOptions? fonts)
Parameters
xlsxbyte[]The workbook to render.
fontsPdfFontOptionsFonts to fall back to, or null for none.
Returns
- byte[]
Remarks
Without fonts, whether non-Latin text renders depends on the host. Where the renderer cannot encode it, it is refused rather than rendered with gaps - measured 2026-11-02, Persian, Hebrew, Arabic and Cyrillic cells all fail on a Windows host with a DocumentConversionException naming the first character that could not be encoded. Supplying the font removes the machine from the answer, and also turns on Hebrew and Arabic shaping. See PdfFontOptions for what that does and does not cover.
Exceptions
- ArgumentNullException
xlsxis null.- ArgumentException
xlsxis empty.- DocumentConversionException
The workbook could not be rendered.
ConvertAsync(Stream, Stream, PdfFontOptions?, CancellationToken)
Reads an .xlsx 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 .xlsx 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
Remarks
The stream contract is ConvertAsync(Stream, Stream, CancellationToken)'s; the fonts are Convert(byte[], PdfFontOptions?)'s.
Exceptions
- ArgumentNullException
Either stream is null.
- ArgumentException
sourceis not readable or held no bytes, ordestinationis not writable.- OperationCanceledException
ctwas cancelled.- DocumentConversionException
The workbook could not be rendered.
ConvertAsync(Stream, Stream, CancellationToken)
Reads an .xlsx 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.
As with ConvertAsync(Stream, Stream, CancellationToken), the PDF is written straight through as
the renderer produces it rather than assembled into an array first — so a failure part-way
leaves whatever had already been produced on destination.
public static Task ConvertAsync(Stream source, Stream destination, CancellationToken ct = default)
Parameters
sourceStreamThe stream the .xlsx 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 workbook could not be rendered.
ConvertFile(string, string)
Renders inputPath to a PDF at outputPath.
public static void ConvertFile(string inputPath, string outputPath)
Parameters
inputPathstringThe .xlsx to read.
outputPathstringWhere to write the PDF. Overwritten if it exists.
Exceptions
- ArgumentException
Either path is blank.
- DocumentConversionException
The workbook could not be rendered.