Class Document
- Namespace
- ComOfficeKit
- Assembly
- ComOfficeKit.dll
An open document. Open(string) returns a WordDocument, a Spreadsheet or a Presentation; what only one kind can do is on that type.
public abstract class Document : IDisposable
- Inheritance
-
Document
- Implements
- Derived
- Inherited Members
Remarks
A document owns everything it opened in the engine and any temporary file it made. Dispose it when you are done; every other member of a disposed document throws ObjectDisposedException. A document that is never disposed is released when the garbage collector finalises it.
Calls on one document take turns: a call made while another is running on the same document waits for it. Different documents can be used from different threads at once.
Properties
FontSubstitutions
Each font the document asked for that it was drawn in another: on a machine without the document's fonts, which family stood in for which. Each pair is listed once, in the order the document's text first met it. The list fills as the text is measured: a Word document's when it is laid out (by PageCount and everything that draws or writes its pages), a workbook's and a deck's when their pages are drawn or written (RenderPage(int, double), ExportPdf(string)). Before that it is empty.
public IReadOnlyList<FontSubstitution> FontSubstitutions { get; }
Property Value
Remarks
A family the machine does not have is drawn in a stand-in, so the pages still render, but their lines can break and their pages fill differently from the author's. A server that must reproduce documents faithfully installs the families listed here. On Windows a family is also listed when it is drawn in the one Windows has under another name (Helvetica in Arial). On Linux the two fonts the package carries stand in for Calibri and Cambria, and are listed when they do; where the folders are searched is FontDirectories's.
Format
The document's format: the one it was opened in, or the one it was last saved in with SaveAs(string). SaveAs(Stream) writes this format.
public DocumentFormat Format { get; }
Property Value
Kind
What kind of document this is.
public DocumentKind Kind { get; }
Property Value
PageCount
How many pages the document has: a Word document's pages as laid out; the pages ActiveSheet prints; a deck's slides, hidden ones included.
public int PageCount { get; }
Property Value
Exceptions
- LicenseException
The licence does not allow laying the document out.
SourcePath
The file the document was opened from, or last saved to with SaveAs(string), as a full path; null for a document opened from a stream and not saved to a file since.
public string? SourcePath { get; }
Property Value
Methods
CreateBlank(DocumentFormat, string)
Writes a new, blank document to path, replacing a file already there: a Word
document of one empty A4 page, a workbook of one empty sheet, or a deck of one title slide.
Open it with Open(string).
public static void CreateBlank(DocumentFormat format, string path)
Parameters
formatDocumentFormatpathstringWhere to write it. Its extension must be the format's.
Exceptions
- ComOfficeKitException
IO when the file cannot be written.
- LicenseException
The licence does not allow it.
Dispose()
Releases what the document holds in the engine and deletes its temporary files. Safe to call more than once.
public void Dispose()
ExportPdf(Stream)
Writes the document as a PDF to destination, at its current position, as
ExportPdf(string) does to a file. The stream is flushed and left open.
public void ExportPdf(Stream destination)
Parameters
destinationStreamA writable stream.
Exceptions
- ComOfficeKitException
As ExportPdf(string): Internal when a font the document uses cannot be read.
- LicenseException
The licence does not allow exporting.
ExportPdf(Stream, CancellationToken)
ExportPdf(Stream), stopped when cancellationToken is cancelled while the
engine writes the PDF: nothing is written to destination then.
public void ExportPdf(Stream destination, CancellationToken cancellationToken)
Parameters
destinationStreamA writable stream.
cancellationTokenCancellationTokenCancels the export, from any thread.
Exceptions
- OperationCanceledException
The token was cancelled before the PDF was written.
- ComOfficeKitException
- LicenseException
The licence does not allow exporting.
ExportPdf(string)
Writes the document as a PDF to path, replacing a file already there only
once the whole PDF is written: a Word document's pages; the pages
ActiveSheet prints; a deck's slides that are not hidden, as
PowerPoint prints them. Text stays text, with its fonts embedded.
public void ExportPdf(string path)
Parameters
pathstringWhere to write the PDF.
Remarks
The engine writes the PDF itself: it needs no printer and no Print Spooler service, so it works on a server that runs without them. Text is written as the glyphs the layout measured, in embedded fonts, so it can be searched and copied.
Exceptions
- ComOfficeKitException
IO when the file cannot be written; Internal when a font the document uses cannot be read, the font named in the message.
- LicenseException
The licence does not allow exporting.
ExportPdf(string, CancellationToken)
ExportPdf(string), stopped when cancellationToken is cancelled: the engine
checks it at each page, and a cancelled export leaves the file that was at path as it was.
public void ExportPdf(string path, CancellationToken cancellationToken)
Parameters
pathstringWhere to write the PDF.
cancellationTokenCancellationTokenCancels the export, from any thread.
Exceptions
- OperationCanceledException
The token was cancelled before the PDF replaced the file.
- ComOfficeKitException
- LicenseException
The licence does not allow exporting.
GetPageCount(CancellationToken)
PageCount, stopped when cancellationToken is cancelled while a Word
document is laid out, which is what counting its pages takes the first time.
public int GetPageCount(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenCancels the layout, from any thread.
Returns
- int
How many pages the document has.
Exceptions
- OperationCanceledException
The token was cancelled before the pages were counted.
- LicenseException
The licence does not allow laying the document out.
GetPageText(int)
The text page index (from 0) draws and where it draws it: what an application lays over the
page's image from RenderPage(int, double) so its user can select, copy and search it, or reads to
find where a word is on the page. It lies on the glyphs that call draws, with the same pages and the same
points: a Word document's pages as laid out, the pages ActiveSheet prints, a deck's
slides, hidden ones included.
public PageText GetPageText(int index)
Parameters
Returns
- PageText
The page's text in lines, which the caller owns.
Exceptions
- ArgumentOutOfRangeException
indexis negative or not less than PageCount.- LicenseException
The licence does not allow rendering.
GetPageText(int, CancellationToken)
GetPageText(int), stopped when cancellationToken is cancelled.
public PageText GetPageText(int index, CancellationToken cancellationToken)
Parameters
indexintThe page, counting from 0; less than PageCount.
cancellationTokenCancellationTokenCancels the read, from any thread.
Returns
- PageText
The page's text in lines, which the caller owns.
Exceptions
- OperationCanceledException
The token was cancelled before the text was read.
- ArgumentOutOfRangeException
As GetPageText(int).
- LicenseException
The licence does not allow rendering.
Open(Stream, DocumentFormat)
Opens a document read from stream, from its current position to its end.
The stream is read before this returns and is left open.
public static Document Open(Stream stream, DocumentFormat format)
Parameters
streamStreamA readable stream holding the file.
formatDocumentFormatThe file's format: a stream has no extension to tell.
Returns
- Document
A WordDocument, a Spreadsheet or a Presentation, whose SourcePath is null.
Exceptions
- ComOfficeKitException
Parse when the bytes are not a readable file of
format.- LicenseException
The licence does not allow it.
Open(Stream, DocumentFormat, CancellationToken)
Open(Stream, DocumentFormat), stopped when cancellationToken is cancelled
while the engine reads the document: a cancelled open opens nothing. The stream is read whole first.
public static Document Open(Stream stream, DocumentFormat format, CancellationToken cancellationToken)
Parameters
streamStreamA readable stream holding the file.
formatDocumentFormatThe file's format.
cancellationTokenCancellationTokenCancels the open, from any thread.
Returns
- Document
A WordDocument, a Spreadsheet or a Presentation, whose SourcePath is null.
Exceptions
- OperationCanceledException
The token was cancelled before the document opened.
- ComOfficeKitException
- LicenseException
The licence does not allow it.
Open(string)
Opens the file at path. The format comes from its extension, in any case:
.docx, .xlsx, .xlsm, .pptx, .csv or .tsv.
public static Document Open(string path)
Parameters
pathstringThe file to open.
Returns
- Document
A WordDocument, a Spreadsheet or a Presentation.
Exceptions
- ComOfficeKitException
UnsupportedFormat for another extension; IO when the file cannot be read; Parse when it is not a readable file of its format.
- LicenseException
The licence does not allow it.
Open(string, CancellationToken)
Open(string), stopped when cancellationToken is cancelled: the engine
checks it as it unpacks and reads the file, and a cancelled open opens nothing.
public static Document Open(string path, CancellationToken cancellationToken)
Parameters
pathstringThe file to open.
cancellationTokenCancellationTokenCancels the open, from any thread.
Returns
- Document
A WordDocument, a Spreadsheet or a Presentation.
Exceptions
- OperationCanceledException
The token was cancelled before the document opened.
- ComOfficeKitException
As Open(string).
- LicenseException
The licence does not allow it.
RenderPage(int, double)
Draws page index (from 0) as an image: opaque, on white, at
dpi dots per inch. Its size in pixels is the page's size in points
× dpi ÷ 72, rounded up.
public PageImage RenderPage(int index, double dpi = 96)
Parameters
indexintThe page, counting from 0; less than PageCount.
dpidoubleFrom 24 to 600: a finite number, never NaN or an infinity. 96 is the size Windows shows at 100 %.
Returns
- PageImage
The page's pixels, which the caller owns.
Exceptions
- ArgumentOutOfRangeException
indexis negative or not less than PageCount;dpiis not a number from 24 to 600 (NaN and the infinities included); or atdpithe page would be more than 16,384 pixels a side. Checked here, before the engine is called, as every argument is.- LicenseException
The licence does not allow rendering.
RenderPage(int, double, CancellationToken)
RenderPage(int, double), stopped when cancellationToken is cancelled: while a
Word document is laid out for it, or before the page is drawn. A page being drawn is drawn to its end.
public PageImage RenderPage(int index, double dpi, CancellationToken cancellationToken)
Parameters
indexintThe page, counting from 0; less than PageCount.
dpidoubleFrom 24 to 600; 96 is the size Windows shows at 100 %.
cancellationTokenCancellationTokenCancels the drawing, from any thread.
Returns
- PageImage
The page's pixels, which the caller owns.
Exceptions
- ArgumentOutOfRangeException
- OperationCanceledException
The token was cancelled before the page was drawn.
- LicenseException
The licence does not allow rendering.
Save()
Saves the document to SourcePath, in Format. Parts of the file the document did not change are carried over as they were.
public void Save()
Exceptions
- InvalidOperationException
The document has no SourcePath: it was opened from a stream. Use SaveAs(string).
- ComOfficeKitException
IO when the file cannot be written.
- LicenseException
The licence does not allow saving.
Save(SaveOptions)
Save(), with options for this save.
public void Save(SaveOptions options)
Parameters
optionsSaveOptionsHow to save; AcceptContentLoss writes an .odt even when it leaves content out.
Exceptions
- ContentLossException
A save as .odt would leave content out, and
optionsdoes not accept it. Nothing was written.- InvalidOperationException
As Save().
- ComOfficeKitException
As Save().
- LicenseException
The licence does not allow saving.
Save(SaveOptions, CancellationToken)
Save(SaveOptions), stopped when cancellationToken is cancelled.
public void Save(SaveOptions options, CancellationToken cancellationToken)
Parameters
optionsSaveOptionsHow to save.
cancellationTokenCancellationTokenCancels the save, from any thread.
Exceptions
- OperationCanceledException
The token was cancelled before the file was replaced.
- ContentLossException
- ComOfficeKitException
As Save().
- LicenseException
The licence does not allow saving.
Save(CancellationToken)
Save(), stopped when cancellationToken is cancelled: a cancelled save leaves
the file as it was.
public void Save(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenCancels the save, from any thread.
Exceptions
- InvalidOperationException
As Save().
- OperationCanceledException
The token was cancelled before the file was replaced.
- ComOfficeKitException
As Save().
- LicenseException
The licence does not allow saving.
SaveAs(Stream)
Writes the document to destination in Format, at the stream's
current position. The stream is flushed and left open; SourcePath does not change.
public void SaveAs(Stream destination)
Parameters
destinationStreamA writable stream.
Exceptions
- LicenseException
The licence does not allow saving.
SaveAs(Stream, SaveOptions)
SaveAs(Stream), with options for this save.
public void SaveAs(Stream destination, SaveOptions options)
Parameters
destinationStreamA writable stream.
optionsSaveOptionsHow to save; AcceptContentLoss writes an .odt even when it leaves content out.
Exceptions
- ContentLossException
A save as .odt would leave content out, and
optionsdoes not accept it. Nothing was written todestination.- LicenseException
The licence does not allow saving.
SaveAs(Stream, SaveOptions, CancellationToken)
SaveAs(Stream, SaveOptions), stopped when cancellationToken is cancelled.
public void SaveAs(Stream destination, SaveOptions options, CancellationToken cancellationToken)
Parameters
destinationStreamA writable stream.
optionsSaveOptionsHow to save.
cancellationTokenCancellationTokenCancels the save, from any thread.
Exceptions
- OperationCanceledException
The token was cancelled before the document was written.
- ContentLossException
- LicenseException
The licence does not allow saving.
SaveAs(Stream, CancellationToken)
SaveAs(Stream), stopped when cancellationToken is cancelled while the engine
writes the document: nothing is written to destination then.
public void SaveAs(Stream destination, CancellationToken cancellationToken)
Parameters
destinationStreamA writable stream.
cancellationTokenCancellationTokenCancels the save, from any thread.
Exceptions
- OperationCanceledException
The token was cancelled before the document was written.
- LicenseException
The licence does not allow saving.
SaveAs(string)
Saves the document to path, which then becomes its SourcePath.
The format comes from the extension and must suit the kind: a Word document saves as .docx or
.odt; a workbook as .xlsx, .xlsm, .csv or .tsv; a deck as .pptx. Parts of the file the document
did not change are carried over as they were. A save as .odt that would leave content out throws
ContentLossException; see SaveAs(string, SaveOptions).
public void SaveAs(string path)
Parameters
pathstringWhere to save. A file already there is replaced.
Exceptions
- ComOfficeKitException
IO when the file cannot be written.
- LicenseException
The licence does not allow saving.
SaveAs(string, SaveOptions)
SaveAs(string), with options for this save.
public void SaveAs(string path, SaveOptions options)
Parameters
pathstringWhere to save. A file already there is replaced.
optionsSaveOptionsHow to save; AcceptContentLoss writes an .odt even when it leaves content out.
Exceptions
- ContentLossException
A save as .odt would leave content out, and
optionsdoes not accept it. Nothing was written, and SourcePath is unchanged.- ComOfficeKitException
As SaveAs(string).
- LicenseException
The licence does not allow saving.
SaveAs(string, SaveOptions, CancellationToken)
SaveAs(string, SaveOptions), stopped when cancellationToken is cancelled.
public void SaveAs(string path, SaveOptions options, CancellationToken cancellationToken)
Parameters
pathstringWhere to save. A file already there is replaced.
optionsSaveOptionsHow to save.
cancellationTokenCancellationTokenCancels the save, from any thread.
Exceptions
- OperationCanceledException
The token was cancelled before the file was replaced.
- ContentLossException
- ComOfficeKitException
As SaveAs(string).
- LicenseException
The licence does not allow saving.
SaveAs(string, CancellationToken)
SaveAs(string), stopped when cancellationToken is cancelled: a cancelled
save leaves the file at path as it was, and SourcePath unchanged.
public void SaveAs(string path, CancellationToken cancellationToken)
Parameters
pathstringWhere to save. A file already there is replaced.
cancellationTokenCancellationTokenCancels the save, from any thread.
Exceptions
- OperationCanceledException
The token was cancelled before the file was replaced.
- ComOfficeKitException
As SaveAs(string).
- LicenseException
The licence does not allow saving.