Interface IDocxEditor
- Namespace
- DocToolkit.Extensions.DependencyInjection
- Assembly
- DocToolkit.Extensions.DependencyInjection.dll
Creates, reads and edits Word (.docx) documents. Registered by AddDocToolkit(IServiceCollection, Action<DocToolkitOptions>?).
public interface IDocxEditor
Methods
Create(IEnumerable<DocxBlock>)
Builds a document from blocks — headings, paragraphs, tables and inline
images. Content comes from data rather than markup, so there is no HTML to escape and a value
containing < cannot corrupt the document's structure. An empty sequence is valid.
byte[] Create(IEnumerable<DocxBlock> blocks)
Parameters
blocksIEnumerable<DocxBlock>
Returns
- byte[]
Exceptions
- ArgumentNullException
blocksis null.- ArgumentException
An element of
blocksis null.- DocumentConversionException
The document could not be built.
Create(IEnumerable<DocxBlock>, PageSetup)
As above, laid out on page rather than the A4 default.
byte[] Create(IEnumerable<DocxBlock> blocks, PageSetup page)
Parameters
blocksIEnumerable<DocxBlock>The content, written in order.
pagePageSetupThe page size, orientation and margins.
Returns
- byte[]
Exceptions
- ArgumentNullException
blocksorpageis null.- ArgumentException
An element of
blocksis null.- DocumentConversionException
The document could not be built.
CreateAsync(IEnumerable<DocxBlock>, PageSetup, Stream, CancellationToken)
As above, laid out on page rather than the A4 default.
Task CreateAsync(IEnumerable<DocxBlock> blocks, PageSetup page, Stream destination, CancellationToken ct = default)
Parameters
blocksIEnumerable<DocxBlock>The content, written in order.
pagePageSetupThe page size, orientation and margins.
destinationStreamThe stream the document is written to.
ctCancellationTokenCancels the build and the write.
Returns
Exceptions
- ArgumentNullException
Any argument is null.
- ArgumentException
An element of
blocksis null, ordestinationis not writable.- OperationCanceledException
ctwas cancelled.- DocumentConversionException
The document could not be built or written.
CreateAsync(IEnumerable<DocxBlock>, Stream, CancellationToken)
Builds a document from blocks and writes it to
destination. See Create(IEnumerable<DocxBlock>) for the block semantics.
destination is written and is neither disposed, closed nor sought,
so an HTTP response body is a valid destination.
Task CreateAsync(IEnumerable<DocxBlock> blocks, Stream destination, CancellationToken ct = default)
Parameters
blocksIEnumerable<DocxBlock>destinationStreamctCancellationToken
Returns
Exceptions
- ArgumentNullException
blocksordestinationis null.- ArgumentException
An element of
blocksis null, ordestinationis not writable.- OperationCanceledException
ctwas cancelled.- DocumentConversionException
The document could not be built or written.
ExtractText(byte[])
Returns the plain text of the document body. Headers, footers, footnotes and endnotes are not included.
string ExtractText(byte[] docx)
Parameters
docxbyte[]
Returns
Exceptions
- ArgumentNullException
docxis null.- ArgumentException
docxis empty.- DocumentConversionException
The package could not be opened or read.
ExtractText(byte[], bool)
Returns the plain text of the document. When includeHeadersAndFooters is true, headers and footers follow the body text; footnotes and endnotes are never included.
string ExtractText(byte[] docx, bool includeHeadersAndFooters)
Parameters
Returns
Exceptions
- ArgumentNullException
docxis null.- ArgumentException
docxis empty.- DocumentConversionException
The package could not be opened or read.
ExtractTextAsync(Stream, bool, CancellationToken)
Reads a .docx from source and returns its plain text. See
ExtractText(byte[], bool) for what includeHeadersAndFooters
controls. source is read to its end and is neither disposed, closed
nor sought.
Task<string> ExtractTextAsync(Stream source, bool includeHeadersAndFooters, CancellationToken ct = default)
Parameters
sourceStreamincludeHeadersAndFootersboolctCancellationToken
Returns
Exceptions
- ArgumentNullException
sourceis null.- ArgumentException
sourceis not readable or held no bytes.- OperationCanceledException
ctwas cancelled.- DocumentConversionException
The package could not be opened or read.
ExtractTextAsync(Stream, CancellationToken)
Reads a .docx from source and returns the plain text of its body. Headers,
footers, footnotes and endnotes are not included. source is read to
its end and is neither disposed, closed nor sought.
Task<string> ExtractTextAsync(Stream source, CancellationToken ct = default)
Parameters
sourceStreamctCancellationToken
Returns
Exceptions
- ArgumentNullException
sourceis null.- ArgumentException
sourceis not readable or held no bytes.- OperationCanceledException
ctwas cancelled.- DocumentConversionException
The package could not be opened or read.
FillRows(byte[], string, IEnumerable<IReadOnlyDictionary<string, string>>)
Expands a template table row once per record. A row holding {{collection.Field}}
markers becomes one row per record, each keeping the template row's formatting.
byte[] FillRows(byte[] docx, string collection, IEnumerable<IReadOnlyDictionary<string, string>> rows)
Parameters
docxbyte[]collectionstringrowsIEnumerable<IReadOnlyDictionary<string, string>>
Returns
- byte[]
Exceptions
- ArgumentNullException
Any argument is null.
- ArgumentException
docxis empty, orcollectionis blank.- DocumentConversionException
The package could not be opened or edited.
FillRowsAsync(Stream, string, IEnumerable<IReadOnlyDictionary<string, string>>, Stream, CancellationToken)
Reads a .docx from source, expands the template row once per record, and
writes the result to destination. See FillRows(byte[], string, IEnumerable<IReadOnlyDictionary<string, string>>) for the
expansion rules. Neither stream is disposed, closed or sought.
Task FillRowsAsync(Stream source, string collection, IEnumerable<IReadOnlyDictionary<string, string>> rows, Stream destination, CancellationToken ct = default)
Parameters
sourceStreamcollectionstringrowsIEnumerable<IReadOnlyDictionary<string, string>>destinationStreamctCancellationToken
Returns
Exceptions
- ArgumentNullException
Any argument is null.
- ArgumentException
sourceis not readable or held no bytes,destinationis not writable, orcollectionis blank.- OperationCanceledException
ctwas cancelled.- DocumentConversionException
The package could not be opened or edited.
ReplaceImage(byte[], string, byte[], double?, double?)
Replaces a text placeholder with an image, sized from the image's own header unless a dimension is given. PNG and JPEG only, decided by magic bytes rather than by filename.
byte[] ReplaceImage(byte[] docx, string placeholder, byte[] image, double? widthPoints = null, double? heightPoints = null)
Parameters
Returns
- byte[]
Exceptions
- ArgumentNullException
Any argument is null.
- ArgumentException
docxorimageis empty,placeholderis blank, or the image is neither PNG nor JPEG.- ArgumentOutOfRangeException
A supplied size is zero or negative, or the resulting size is larger than a drawing extent can hold.
- DocumentConversionException
The package could not be opened or edited.
ReplaceImageAsync(Stream, string, byte[], Stream, double?, double?, CancellationToken)
Reads a .docx from source, replaces the placeholder with an image, and
writes the result to destination. See ReplaceImage(byte[], string, byte[], double?, double?) for
sizing and format rules. Neither stream is disposed, closed or sought.
Task ReplaceImageAsync(Stream source, string placeholder, byte[] image, Stream destination, double? widthPoints = null, double? heightPoints = null, CancellationToken ct = default)
Parameters
sourceStreamplaceholderstringimagebyte[]destinationStreamwidthPointsdouble?heightPointsdouble?ctCancellationToken
Returns
Exceptions
- ArgumentNullException
Any argument is null.
- ArgumentException
sourceis not readable or held no bytes,destinationis not writable,placeholderis blank, or the image is neither PNG nor JPEG.- ArgumentOutOfRangeException
A supplied size is zero or negative, or the resulting size is larger than a drawing extent can hold.
- OperationCanceledException
ctwas cancelled.- DocumentConversionException
The package could not be opened or edited.
ReplaceText(byte[], IReadOnlyDictionary<string, string>)
Replaces every key with its value across the document body, headers, footers, footnotes and endnotes.
byte[] ReplaceText(byte[] docx, IReadOnlyDictionary<string, string> replacements)
Parameters
docxbyte[]replacementsIReadOnlyDictionary<string, string>
Returns
- byte[]
Exceptions
- ArgumentNullException
Either argument is null.
- ArgumentException
docxis empty.- DocumentConversionException
The package could not be opened or edited.
ReplaceTextAsync(Stream, IReadOnlyDictionary<string, string>, Stream, CancellationToken)
Reads a .docx from source, replaces every key with its value, and writes
the result to destination. See ReplaceText(byte[], IReadOnlyDictionary<string, string>) for exactly what
counts as a match. source is read to its end and
destination is written; neither is disposed, closed or sought.
Task ReplaceTextAsync(Stream source, IReadOnlyDictionary<string, string> replacements, Stream destination, CancellationToken ct = default)
Parameters
sourceStreamreplacementsIReadOnlyDictionary<string, string>destinationStreamctCancellationToken
Returns
Exceptions
- ArgumentNullException
Any argument is null.
- ArgumentException
sourceis not readable or held no bytes, ordestinationis not writable.- OperationCanceledException
ctwas cancelled.- DocumentConversionException
The package could not be opened or edited.