Document
Document class
Represents a Word document.
To learn more, visit the Working with Document documentation article.
public class Document : DocumentBase
Constructors
Name | Description |
---|---|
Document() | Creates a blank Word document. |
Document(Stream) | Opens an existing document from a stream. Automatically detects the file format. |
Document(string) | Opens an existing document from a file. Automatically detects the file format. |
Document(Stream, LoadOptions) | Opens an existing document from a stream. Allows to specify additional options such as an encryption password. |
Document(string, LoadOptions) | Opens an existing document from a file. Allows to specify additional options such as an encryption password. |
Properties
Name | Description |
---|---|
AttachedTemplate { get; set; } | Gets or sets the full path of the template attached to the document. |
AutomaticallyUpdateStyles { get; set; } | Gets or sets a flag indicating whether the styles in the document are updated to match the styles in the attached template each time the document is opened in MS Word. |
BackgroundShape { get; set; } | Gets or sets the background shape of the document. Can be null . |
Bibliography { get; } | Gets the Bibliography object that represents the list of sources available in the document. |
BuiltInDocumentProperties { get; } | Returns a collection that represents all the built-in document properties of the document. |
CompatibilityOptions { get; } | Provides access to document compatibility options (that is, the user preferences entered on the Compatibility tab of the Options dialog in Word). |
Compliance { get; } | Gets the OOXML compliance version determined from the loaded document content. Makes sense only for OOXML documents. |
Count { get; } | Gets the number of immediate children of this node. |
CustomDocumentProperties { get; } | Returns a collection that represents all the custom document properties of the document. |
CustomNodeId { get; set; } | Specifies custom node identifier. |
CustomXmlParts { get; set; } | Gets or sets the collection of Custom XML Data Storage Parts. |
DefaultTabStop { get; set; } | Gets or sets the interval (in points) between the default tab stops. |
DigitalSignatures { get; } | Gets the collection of digital signatures for this document and their validation results. |
override Document { get; } | Gets this instance. |
EndnoteOptions { get; } | Provides options that control numbering and positioning of endnotes in this document. |
FieldOptions { get; } | Gets a FieldOptions object that represents options to control field handling in the document. |
FirstChild { get; } | Gets the first child of the node. |
FirstSection { get; } | Gets the first section in the document. |
FontInfos { get; } | Provides access to properties of fonts used in this document. |
FontSettings { get; set; } | Gets or sets document font settings. |
FootnoteOptions { get; } | Provides options that control numbering and positioning of footnotes in this document. |
FootnoteSeparators { get; } | Provides access to the footnote/endnote separators defined in the document. |
Frameset { get; } | Returns a Frameset instance if this document represents a frames page. |
GlossaryDocument { get; set; } | Gets or sets the glossary document within this document or template. A glossary document is a storage for AutoText, AutoCorrect and Building Block entries defined in a document. |
GrammarChecked { get; set; } | Returns true if the document has been checked for grammar. |
HasChildNodes { get; } | Returns true if this node has any child nodes. |
HasMacros { get; } | Returns true if the document has a VBA project (macros). |
HasRevisions { get; } | Returns true if the document has any tracked changes. |
HyphenationOptions { get; } | Provides access to document hyphenation options. |
IncludeTextboxesFootnotesEndnotesInStat { get; set; } | Specifies whether to include textboxes, footnotes and endnotes in word count statistics. |
override IsComposite { get; } | Returns true as this node can have child nodes. |
JustificationMode { get; set; } | Gets or sets the character spacing adjustment of a document. |
LastChild { get; } | Gets the last child of the node. |
LastSection { get; } | Gets the last section in the document. |
LayoutOptions { get; } | Gets a LayoutOptions object that represents options to control the layout process of this document. |
Lists { get; } | Provides access to the list formatting used in the document. |
MailMerge { get; } | Returns a MailMerge object that represents the mail merge functionality for the document. |
MailMergeSettings { get; set; } | Gets or sets the object that contains all of the mail merge information for a document. |
NextSibling { get; } | Gets the node immediately following this node. |
NodeChangingCallback { get; set; } | Called when a node is inserted or removed in the document. |
override NodeType { get; } | Returns Document. |
OriginalFileName { get; } | Gets the original file name of the document. |
OriginalLoadFormat { get; } | Gets the format of the original document that was loaded into this object. |
PackageCustomParts { get; set; } | Gets or sets the collection of custom parts (arbitrary content) that are linked to the OOXML package using “unknown relationships”. |
PageColor { get; set; } | Gets or sets the page color of the document. This property is a simpler version of BackgroundShape . |
PageCount { get; } | Gets the number of pages in the document as calculated by the most recent page layout operation. |
ParentNode { get; } | Gets the immediate parent of this node. |
PreviousSibling { get; } | Gets the node immediately preceding this node. |
ProtectionType { get; } | Gets the currently active document protection type. |
PunctuationKerning { get; set; } | Specifies whether kerning applies to both Latin text and punctuation. |
Range { get; } | Returns a Range object that represents the portion of a document that is contained in this node. |
RemovePersonalInformation { get; set; } | Gets or sets a flag indicating that Microsoft Word will remove all user information from comments, revisions and document properties upon saving the document. |
ResourceLoadingCallback { get; set; } | Allows to control how external resources are loaded. |
Revisions { get; } | Gets a collection of revisions (tracked changes) that exist in this document. |
RevisionsView { get; set; } | Gets or sets a value indicating whether to work with the original or revised version of a document. |
Sections { get; } | Returns a collection that represents all sections in the document. |
ShadeFormData { get; set; } | Specifies whether to turn on the gray shading on form fields. |
ShowGrammaticalErrors { get; set; } | Specifies whether to display grammar errors in this document. |
ShowSpellingErrors { get; set; } | Specifies whether to display spelling errors in this document. |
SpellingChecked { get; set; } | Returns true if the document has been checked for spelling. |
Styles { get; } | Returns a collection of styles defined in the document. |
Theme { get; } | Gets the Theme object for this document. |
TrackRevisions { get; set; } | True if changes are tracked when this document is edited in Microsoft Word. |
Variables { get; } | Returns the collection of variables added to a document or template. |
VbaProject { get; set; } | Gets or sets a VbaProject . |
VersionsCount { get; } | Gets the number of document versions that was stored in the DOC document. |
ViewOptions { get; } | Provides options to control how the document is displayed in Microsoft Word. |
WarningCallback { get; set; } | Called during various document processing procedures when an issue is detected that might result in data or formatting fidelity loss. |
Watermark { get; } | Provides access to the document watermark. |
WebExtensionTaskPanes { get; } | Returns a collection that represents a list of task pane add-ins. |
WriteProtection { get; } | Provides access to the document write protection options. |
Methods
Name | Description |
---|---|
override Accept(DocumentVisitor) | Accepts a visitor. |
AcceptAllRevisions() | Accepts all tracked changes in the document. |
override AcceptEnd(DocumentVisitor) | Accepts a visitor for visiting the end of the document. |
override AcceptStart(DocumentVisitor) | Accepts a visitor for visiting the start of the document. |
AppendChild<T>(T) | Adds the specified node to the end of the list of child nodes for this node. |
AppendDocument(Document, ImportFormatMode) | Appends the specified document to the end of this document. |
AppendDocument(Document, ImportFormatMode, ImportFormatOptions) | Appends the specified document to the end of this document. |
Cleanup() | Cleans unused styles and lists from the document. |
Cleanup(CleanupOptions) | Cleans unused styles and lists from the document depending on given CleanupOptions . |
Clone() | Performs a deep copy of the Document . |
Clone(bool) | Creates a duplicate of the node. |
Compare(Document, string, DateTime) | Compares this document with another document producing changes as number of edit and format revisions Revision . |
Compare(Document, string, DateTime, CompareOptions) | Compares this document with another document producing changes as a number of edit and format revisions Revision . Allows to specify comparison options using CompareOptions . |
CopyStylesFromTemplate(Document) | Copies styles from the specified template to a document. |
CopyStylesFromTemplate(string) | Copies styles from the specified template to a document. |
CreateNavigator() | Creates navigator which can be used to traverse and read nodes. |
EnsureMinimum() | If the document contains no sections, creates one section with one paragraph. |
ExpandTableStylesToDirectFormatting() | Converts formatting specified in table styles into direct formatting on tables in the document. |
ExtractPages(int, int) | Returns the Document object representing specified range of pages. |
GetAncestor(NodeType) | Gets the first ancestor of the specified NodeType . |
GetAncestor(Type) | Gets the first ancestor of the specified object type. |
GetChild(NodeType, int, bool) | Returns an Nth child node that matches the specified type. |
GetChildNodes(NodeType, bool) | Returns a live collection of child nodes that match the specified type. |
GetEnumerator() | Provides support for the for each style iteration over the child nodes of this node. |
GetPageInfo(int) | Gets the page size, orientation and other information about a page that might be useful for printing or rendering. |
override GetText() | Gets the text of this node and of all its children. |
ImportNode(Node, bool) | Imports a node from another document to the current document. |
ImportNode(Node, bool, ImportFormatMode) | Imports a node from another document to the current document with an option to control formatting. |
IndexOf(Node) | Returns the index of the specified child node in the child node array. |
InsertAfter<T>(T, Node) | Inserts the specified node immediately after the specified reference node. |
InsertBefore<T>(T, Node) | Inserts the specified node immediately before the specified reference node. |
JoinRunsWithSameFormatting() | Joins runs with same formatting in all paragraphs of the document. |
NextPreOrder(Node) | Gets next node according to the pre-order tree traversal algorithm. |
NormalizeFieldTypes() | Changes field type values FieldType of FieldStart , FieldSeparator , FieldEnd in the whole document so that they correspond to the field types contained in the field codes. |
PrependChild<T>(T) | Adds the specified node to the beginning of the list of child nodes for this node. |
PreviousPreOrder(Node) | Gets the previous node according to the pre-order tree traversal algorithm. |
Print() | Prints the whole document to the default printer. |
Print(PrinterSettings) | Prints the document according to the specified printer settings, using the standard (no User Interface) print controller. |
Print(string) | Print the whole document to the specified printer, using the standard (no User Interface) print controller. |
Print(PrinterSettings, string) | Prints the document according to the specified printer settings, using the standard (no User Interface) print controller and a document name. |
Protect(ProtectionType) | Protects the document from changes without changing the existing password or assigns a random password. |
Protect(ProtectionType, string) | Protects the document from changes and optionally sets a protection password. |
Remove() | Removes itself from the parent. |
RemoveAllChildren() | Removes all the child nodes of the current node. |
RemoveBlankPages() | Removes blank pages from the document. |
RemoveChild<T>(T) | Removes the specified child node. |
RemoveExternalSchemaReferences() | Removes external XML schema references from this document. |
RemoveMacros() | Removes all macros (the VBA project) as well as toolbars and command customizations from the document. |
RemoveSmartTags() | Removes all SmartTag descendant nodes of the current node. |
RenderToScale(int, Graphics, float, float, float) | Renders a document page into a Graphics object to a specified scale. |
RenderToSize(int, Graphics, float, float, float, float) | Renders a document page into a Graphics object to a specified size. |
Save(string) | Saves the document to a file. Automatically determines the save format from the extension. |
Save(Stream, SaveFormat) | Saves the document to a stream using the specified format. |
Save(Stream, SaveOptions) | Saves the document to a stream using the specified save options. |
Save(string, SaveFormat) | Saves the document to a file in the specified format. |
Save(string, SaveOptions) | Saves the document to a file using the specified save options. |
Save(HttpResponse, string, ContentDisposition, SaveOptions) | Sends the document to the client browser. |
SelectNodes(string) | Selects a list of nodes matching the XPath expression. |
SelectSingleNode(string) | Selects the first Node that matches the XPath expression. |
StartTrackRevisions(string) | Starts automatically marking all further changes you make to the document programmatically as revision changes. |
StartTrackRevisions(string, DateTime) | Starts automatically marking all further changes you make to the document programmatically as revision changes. |
StopTrackRevisions() | Stops automatic marking of document changes as revisions. |
ToString(SaveFormat) | Exports the content of the node into a string in the specified format. |
ToString(SaveOptions) | Exports the content of the node into a string using the specified save options. |
UnlinkFields() | Unlinks fields in the whole document. |
Unprotect() | Removes protection from the document regardless of the password. |
Unprotect(string) | Removes protection from the document if a correct password is specified. |
UpdateActualReferenceMarks() | Updates the ActualReferenceMark property of all footnotes and endnotes in the document. |
UpdateFields() | Updates the values of fields in the whole document. |
UpdateListLabels() | Updates list labels for all list items in the document. |
UpdatePageLayout() | Rebuilds the page layout of the document. |
UpdateThumbnail() | Updates Thumbnail of the document using default options. |
UpdateThumbnail(ThumbnailGeneratingOptions) | Updates Thumbnail of the document according to the specified options. |
UpdateWordCount() | Updates word count properties of the document. |
UpdateWordCount(bool) | Updates word count properties of the document, optionally updates Lines property. |
Remarks
The Document
is a central object in the Aspose.Words library.
To load an existing document in any of the LoadFormat
formats, pass a file name or a stream into one of the Document
constructors. To create a blank document, call the constructor without parameters.
Use one of the Save method overloads to save the document in any of the SaveFormat
formats.
To draw document pages directly onto a Graphics object use RenderToScale
or RenderToSize
method.
To print the document, use one of the Print
methods.
MailMerge
is the Aspose.Words’s reporting engine that allows to populate reports designed in Microsoft Word with data from various data sources quickly and easily. The data can be from a DataSet, DataTable, DataView, IDataReader or an array of values. MailMerge will go through the records found in the data source and insert them into mail merge fields in the document growing it as necessary.
Document
stores document-wide information such as Styles
, BuiltInDocumentProperties
, CustomDocumentProperties
, lists and macros. Most of these objects are accessible via the corresponding properties of the Document
.
The Document
is a root node of a tree that contains all other nodes of the document. The tree is a Composite design pattern and in many ways similar to XmlDocument. The content of the document can be manipulated freely programmatically:
- The nodes of the document can be accessed via typed collections, for example
Sections
,ParagraphCollection
etc. - The nodes of the document can be selected by their node type using
GetChildNodes
or using an XPath query withSelectNodes
orSelectSingleNode
. - Content nodes can be added or removed from anywhere in the document using
InsertBefore
,InsertAfter
,RemoveChild
and other methods provided by the base classCompositeNode
. - The formatting attributes of each node can be changed via the properties of that node.
Consider using DocumentBuilder
that simplifies the task of programmatically creating or populating the document tree.
The Document
can contain only Section
objects.
In Microsoft Word, a valid document needs to have at least one section.
Examples
Shows how to execute a mail merge with data from a DataTable.
public void ExecuteDataTable()
{
DataTable table = new DataTable("Test");
table.Columns.Add("CustomerName");
table.Columns.Add("Address");
table.Rows.Add(new object[] { "Thomas Hardy", "120 Hanover Sq., London" });
table.Rows.Add(new object[] { "Paolo Accorti", "Via Monte Bianco 34, Torino" });
// Below are two ways of using a DataTable as the data source for a mail merge.
// 1 - Use the entire table for the mail merge to create one output mail merge document for every row in the table:
Document doc = CreateSourceDocExecuteDataTable();
doc.MailMerge.Execute(table);
doc.Save(ArtifactsDir + "MailMerge.ExecuteDataTable.WholeTable.docx");
// 2 - Use one row of the table to create one output mail merge document:
doc = CreateSourceDocExecuteDataTable();
doc.MailMerge.Execute(table.Rows[1]);
doc.Save(ArtifactsDir + "MailMerge.ExecuteDataTable.OneRow.docx");
}
/// <summary>
/// Creates a mail merge source document.
/// </summary>
private static Document CreateSourceDocExecuteDataTable()
{
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertField(" MERGEFIELD CustomerName ");
builder.InsertParagraph();
builder.InsertField(" MERGEFIELD Address ");
return doc;
}
See Also
- class DocumentBase
- namespace Aspose.Words
- assembly Aspose.Words