Class DocxToHtmlConverter
- Namespace
- DocToolkit
- Assembly
- DocToolkit.dll
Converts a Word (.docx) package to HTML, keeping the structure
ExtractText(byte[]) throws away: headings stay headings, tables stay
tables. Use ExtractText when flat text is what you want.
The result is a complete HTML document — <html><head>…<body> —
not a fragment, and there is no option to change that. Embedding the output in a larger page
means extracting the body with an HTML parser you already trust; this package will not do it by
string surgery on the renderer's output.
The result is self-contained. Images in the source document are embedded as
data: URIs, so nothing in the output points at a file that does not exist.
public static class DocxToHtmlConverter
- Inheritance
-
DocxToHtmlConverter
- Inherited Members
Methods
Convert(byte[])
Converts the .docx in docx to a complete HTML document.
public static string Convert(byte[] docx)
Parameters
docxbyte[]The document to convert.
Returns
Examples
// A complete HTML document, not a fragment, with images embedded as data: URIs so the
// result is self-contained. Extract the body with a parser if you are embedding it.
string html = DocxToHtmlConverter.Convert(docx);
Exceptions
- ArgumentNullException
docxis null.- ArgumentException
docxis empty.- DocumentConversionException
The document could not be converted.
ConvertAsync(Stream, CancellationToken)
Reads a .docx from source and returns it as a complete HTML document.
source is read to its end and is not disposed, closed or sought, so
it may be forward-only — an HTTP request body, for instance.
public static Task<string> ConvertAsync(Stream source, CancellationToken ct = default)
Parameters
sourceStreamThe stream the .docx package is read from.
ctCancellationTokenCancels the read.
Returns
Exceptions
- ArgumentNullException
sourceis null.- ArgumentException
sourceis not readable or held no bytes.- OperationCanceledException
ctwas cancelled.- DocumentConversionException
The document could not be converted.
ConvertWithReport(byte[])
Converts the .docx in docx to HTML, and reports what the conversion could
not carry across.
public static ConversionResult<string> ConvertWithReport(byte[] docx)
Parameters
docxbyte[]The document to convert.
Returns
Remarks
Convert(byte[]) returns the same HTML and is unchanged; this overload exists
because the underlying converter already produces a loss report on every call and
nothing here used to surface it. A plain DOCX reports at least one entry today —
SectionLayoutFlattened, an Approximation raised
because page geometry is exported without section metadata — so a caller who needs to know
whether the HTML is faithful had no way to find out.
The result is always usable: this reports loss, it does not refuse. A caller processing a
mixed folder would rather branch on HasLoss than wrap a
normal case in try/catch.
Exceptions
- ArgumentNullException
docxis null.- ArgumentException
docxis empty.- DocumentConversionException
The document could not be converted.
ConvertWithReportAsync(Stream, CancellationToken)
Reads a .docx from source and converts it to HTML, reporting what the
conversion could not carry across.
source is read to its end and is not disposed, closed or sought.
public static Task<ConversionResult<string>> ConvertWithReportAsync(Stream source, CancellationToken ct = default)
Parameters
sourceStreamThe stream the .docx package is read from.
ctCancellationTokenCancels the read.
Returns
Remarks
Convert(byte[]) returns the same HTML and is unchanged; this overload exists
because the underlying converter already produces a loss report on every call and
nothing here used to surface it. A plain DOCX reports at least one entry today —
SectionLayoutFlattened, an Approximation raised
because page geometry is exported without section metadata — so a caller who needs to know
whether the HTML is faithful had no way to find out.
The result is always usable: this reports loss, it does not refuse. A caller processing a
mixed folder would rather branch on HasLoss than wrap a
normal case in try/catch.
Exceptions
- ArgumentNullException
docxis null.- ArgumentException
docxis empty.- DocumentConversionException
The document could not be converted.
- ArgumentException
sourceis not readable or held no bytes.- OperationCanceledException
ctwas cancelled.