Document Class

Represents a Word document.
Inheritance Hierarchy

Namespace:  Aspose.Words
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.3
Syntax
public class Document : DocumentBase

The Document type exposes the following members.

Constructors
  NameDescription
Public methodCode exampleDocument
Creates a blank Word document.
Public methodCode exampleDocument(Stream)
Opens an existing document from a stream. Automatically detects the file format.
Public methodCode exampleDocument(String)
Opens an existing document from a file. Automatically detects the file format.
Public methodCode exampleDocument(Stream, LoadOptions)
Opens an existing document from a stream. Allows to specify additional options such as an encryption password.
Public methodCode exampleDocument(String, LoadOptions)
Opens an existing document from a file. Allows to specify additional options such as an encryption password.
Properties
  NameDescription
Public propertyCode exampleAttachedTemplate
Gets or sets the full path of the template attached to the document.
Public propertyCode exampleAutomaticallyUpdateStyles
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.
Public propertyCode exampleBackgroundShape
Gets or sets the background shape of the document. Can be null.
(Inherited from DocumentBase.)
Public propertyCode exampleBuiltInDocumentProperties
Returns a collection that represents all the built-in document properties of the document.
Public propertyCode exampleChildNodes
Gets all immediate child nodes of this node.
(Inherited from CompositeNode.)
Public propertyCode exampleCompatibilityOptions
Provides access to document compatibility options (that is, the user preferences entered on the Compatibility tab of the Options dialog in Word).
Public propertyCode exampleCompliance
Gets the OOXML compliance version determined from the loaded document content. Makes sense only for OOXML documents.
Public propertyCode exampleCount
Gets the number of immediate children of this node.
(Inherited from CompositeNode.)
Public propertyCode exampleCustomDocumentProperties
Returns a collection that represents all the custom document properties of the document.
Public propertyCode exampleCustomXmlParts
Gets or sets the collection of Custom XML Data Storage Parts.
Public propertyCode exampleDefaultTabStop
Gets or sets the interval (in points) between the default tab stops.
Public propertyCode exampleDigitalSignatures
Gets the collection of digital signatures for this document and their validation results.
Public propertyDocument (Inherited from DocumentBase.)
Public propertyCode exampleEndnoteOptions
Provides options that control numbering and positioning of endnotes in this document.
Public propertyCode exampleFieldOptions
Gets a FieldOptions object that represents options to control field handling in the document.
Public propertyCode exampleFirstChild
Gets the first child of the node.
(Inherited from CompositeNode.)
Public propertyCode exampleFirstSection
Gets the first section in the document.
Public propertyCode exampleFontInfos
Provides access to properties of fonts used in this document.
(Inherited from DocumentBase.)
Public propertyCode exampleFontSettings
Gets or sets document font settings.
Public propertyCode exampleFootnoteOptions
Provides options that control numbering and positioning of footnotes in this document.
Public propertyCode exampleGlossaryDocument
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.
Public propertyCode exampleHasChildNodes
Returns true if this node has any child nodes.
(Inherited from CompositeNode.)
Public propertyCode exampleHasMacros
Returns true if the document has a VBA project (macros).
Public propertyCode exampleHasRevisions
Returns true if the document has any tracked changes.
Public propertyCode exampleHyphenationOptions
Provides access to document hyphenation options.
Public propertyCode exampleIsComposite
Returns true as this node can have child nodes.
(Inherited from CompositeNode.)
Public propertyCode exampleLastChild
Gets the last child of the node.
(Inherited from CompositeNode.)
Public propertyCode exampleLastSection
Gets the last section in the document.
Public propertyCode exampleLayoutOptions
Gets a LayoutOptions object that represents options to control the layout process of this document.
Public propertyCode exampleLists
Provides access to the list formatting used in the document.
(Inherited from DocumentBase.)
Public propertyCode exampleMailMerge
Returns a MailMerge object that represents the mail merge functionality for the document.
Public propertyCode exampleMailMergeSettings
Gets or sets the object that contains all of the mail merge information for a document.
Public propertyCode exampleNextSibling
Gets the node immediately following this node.
(Inherited from Node.)
Public propertyCode exampleNodeChangingCallback
Called when a node is inserted or removed in the document.
(Inherited from DocumentBase.)
Public propertyCode exampleNodeType
Returns NodeType.Document.
(Overrides NodeNodeType.)
Public propertyCode exampleOriginalFileName
Gets the original file name of the document.
Public propertyCode exampleOriginalLoadFormat
Gets the format of the original document that was loaded into this object.
Public propertyCode examplePackageCustomParts
Gets or sets the collection of custom parts (arbitrary content) that are linked to the OOXML package using "unknown relationships".
Public propertyCode examplePageColor
Gets or sets the page color of the document. This property is a simpler version of BackgroundShape.
(Inherited from DocumentBase.)
Public propertyCode examplePageCount
Gets the number of pages in the document as calculated by the most recent page layout operation.
Public propertyCode exampleParentNode
Gets the immediate parent of this node.
(Inherited from Node.)
Public propertyCode examplePreviousSibling
Gets the node immediately preceding this node.
(Inherited from Node.)
Public propertyCode exampleProtectionType
Gets the currently active document protection type.
Public propertyCode exampleRange
Returns a Range object that represents the portion of a document that is contained in this node.
(Inherited from Node.)
Public propertyCode exampleRemovePersonalInformation
Gets or sets a flag indicating that Microsoft Word will remove all user information from comments, revisions and document properties upon saving the document.
Public propertyCode exampleResourceLoadingCallback
Allows to control how external resources are loaded.
(Inherited from DocumentBase.)
Public propertyCode exampleRevisions
Gets a collection of revisions (tracked changes) that exist in this document.
Public propertyCode exampleRevisionsView
Gets or sets a value indicating whether to work with the original or revised version of a document.
Public propertyCode exampleSections
Returns a collection that represents all sections in the document.
Public propertyCode exampleShadeFormData
Specifies whether to turn on the gray shading on form fields.
Public propertyCode exampleStyles
Returns a collection of styles defined in the document.
(Inherited from DocumentBase.)
Public propertyCode exampleTheme
Gets the Theme object for this document.
Public propertyCode exampleTrackRevisions
True if changes are tracked when this document is edited in Microsoft Word.
Public propertyCode exampleVariables
Returns the collection of variables added to a document or template.
Public propertyCode exampleVbaProject
Gets or sets a VbaProject.
Public propertyCode exampleVersionsCount
Gets the number of document versions that was stored in the DOC document.
Public propertyCode exampleViewOptions
Provides options to control how the document is displayed in Microsoft Word.
Public propertyCode exampleWarningCallback
Called during various document processing procedures when an issue is detected that might result in data or formatting fidelity loss.
(Inherited from DocumentBase.)
Public propertyWebExtensionTaskPanes
Returns a collection that represents a list of task pane add-ins.
Public propertyCode exampleWriteProtection
Provides access to the document write protection options.
Methods
  NameDescription
Public methodCode exampleAccept
Accepts a visitor.
(Overrides NodeAccept(DocumentVisitor).)
Public methodCode exampleAcceptAllRevisions
Accepts all tracked changes in the document.
Public methodCode exampleAppendChild
Adds the specified node to the end of the list of child nodes for this node.
(Inherited from CompositeNode.)
Public methodCode exampleAppendDocument(Document, ImportFormatMode)
Appends the specified document to the end of this document.
Public methodCode exampleAppendDocument(Document, ImportFormatMode, ImportFormatOptions)
Appends the specified document to the end of this document.
Public methodCode exampleCleanup
Cleans unused styles and lists from the document.
Public methodCode exampleCleanup(CleanupOptions)
Cleans unused styles and lists from the document depending on given CleanupOptions.
Public methodCode exampleClone
Performs a deep copy of the Document.
Public methodCode exampleClone(Boolean) (Inherited from Node.)
Public methodCode exampleCompare(Document, String, DateTime)
Compares this document with another document producing changes as number of edit and format revisions Revision.
Public methodCode exampleCompare(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.
Public methodCode exampleCopyStylesFromTemplate(String)
Copies styles from the specified template to a document.
Public methodCode exampleCopyStylesFromTemplate(Document)
Copies styles from the specified template to a document.
Public methodCode exampleEnsureMinimum
If the document contains no sections, creates one section with one paragraph.
Public methodEquals (Inherited from Object.)
Public methodCode exampleExpandTableStylesToDirectFormatting
Converts formatting specified in table styles into direct formatting on tables in the document.
Public methodCode exampleGetAncestor(Type)
Gets the first ancestor of the specified object type.
(Inherited from Node.)
Public methodCode exampleGetAncestor(NodeType)
Gets the first ancestor of the specified NodeType.
(Inherited from Node.)
Public methodCode exampleGetChild
Returns an Nth child node that matches the specified type.
(Inherited from CompositeNode.)
Public methodCode exampleGetChildNodes
Returns a live collection of child nodes that match the specified type.
(Inherited from CompositeNode.)
Public methodCode exampleGetEnumerator
Provides support for the for each style iteration over the child nodes of this node.
(Inherited from CompositeNode.)
Public methodGetHashCode (Inherited from Object.)
Public methodGetPageInfo
Gets the page size, orientation and other information about a page that might be useful for printing or rendering.
Public methodCode exampleGetText
Gets the text of this node and of all its children.
(Inherited from CompositeNode.)
Public methodGetType (Inherited from Object.)
Public methodCode exampleImportNode(Node, Boolean)

Imports a node from another document to the current document.

(Inherited from DocumentBase.)
Public methodCode exampleImportNode(Node, Boolean, ImportFormatMode)

Imports a node from another document to the current document with an option to control formatting.

(Inherited from DocumentBase.)
Public methodCode exampleIndexOf
Returns the index of the specified child node in the child node array.
(Inherited from CompositeNode.)
Public methodCode exampleInsertAfter
Inserts the specified node immediately after the specified reference node.
(Inherited from CompositeNode.)
Public methodCode exampleInsertBefore
Inserts the specified node immediately before the specified reference node.
(Inherited from CompositeNode.)
Public methodCode exampleJoinRunsWithSameFormatting
Joins runs with same formatting in all paragraphs of the document.
Public methodCode exampleNextPreOrder
Gets next node according to the pre-order tree traversal algorithm.
(Inherited from Node.)
Public methodCode exampleNormalizeFieldTypes
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.
Public methodCode examplePrependChild
Adds the specified node to the beginning of the list of child nodes for this node.
(Inherited from CompositeNode.)
Public methodCode examplePreviousPreOrder
Gets the previous node according to the pre-order tree traversal algorithm.
(Inherited from Node.)
Public methodCode examplePrint
Prints the whole document to the default printer.
Public methodCode examplePrint(PrinterSettings)
Prints the document according to the specified printer settings, using the standard (no User Interface) print controller.
Public methodCode examplePrint(String)
Print the whole document to the specified printer, using the standard (no User Interface) print controller.
Public methodCode examplePrint(PrinterSettings, String)
Prints the document according to the specified printer settings, using the standard (no User Interface) print controller and a document name.
Public methodCode exampleProtect(ProtectionType)
Protects the document from changes without changing the existing password or assigns a random password.
Public methodCode exampleProtect(ProtectionType, String)
Protects the document from changes and optionally sets a protection password.
Public methodCode exampleRemove
Removes itself from the parent.
(Inherited from Node.)
Public methodCode exampleRemoveAllChildren
Removes all the child nodes of the current node.
(Inherited from CompositeNode.)
Public methodCode exampleRemoveChild
Removes the specified child node.
(Inherited from CompositeNode.)
Public methodCode exampleRemoveExternalSchemaReferences
Removes external XML schema references from this document.
Public methodCode exampleRemoveMacros
Removes all macros (the VBA project) as well as toolbars and command customizations from the document.
Public methodCode exampleRemoveSmartTags
Removes all SmartTag descendant nodes of the current node.
(Inherited from CompositeNode.)
Public methodCode exampleRenderToScale
Renders a document page into a Graphics object to a specified scale.
Public methodCode exampleRenderToSize
Public methodCode exampleSave(String)
Saves the document to a file. Automatically determines the save format from the extension.
Public methodCode exampleSave(Stream, SaveFormat)
Saves the document to a stream using the specified format.
Public methodCode exampleSave(Stream, SaveOptions)
Saves the document to a stream using the specified save options.
Public methodCode exampleSave(String, SaveFormat)
Saves the document to a file in the specified format.
Public methodCode exampleSave(String, SaveOptions)
Saves the document to a file using the specified save options.
Public methodCode exampleSave(HttpResponse, String, ContentDisposition, SaveOptions)
Sends the document to the client browser.
Public methodCode exampleSelectNodes
Selects a list of nodes matching the XPath expression.
(Inherited from CompositeNode.)
Public methodCode exampleSelectSingleNode
Selects the first Node that matches the XPath expression.
(Inherited from CompositeNode.)
Public methodCode exampleStartTrackRevisions(String)
Starts automatically marking all further changes you make to the document programmatically as revision changes.
Public methodCode exampleStartTrackRevisions(String, DateTime)
Starts automatically marking all further changes you make to the document programmatically as revision changes.
Public methodCode exampleStopTrackRevisions
Stops automatic marking of document changes as revisions.
Public methodToString (Inherited from Object.)
Public methodCode exampleToString(SaveFormat)
Exports the content of the node into a string in the specified format.
(Inherited from Node.)
Public methodCode exampleToString(SaveOptions)
Exports the content of the node into a string using the specified save options.
(Inherited from Node.)
Public methodCode exampleUnlinkFields
Unlinks fields in the whole document.
Public methodCode exampleUnprotect
Removes protection from the document regardless of the password.
Public methodCode exampleUnprotect(String)
Removes protection from the document if a correct password is specified.
Public methodCode exampleUpdateFields
Updates the values of fields in the whole document.
Public methodCode exampleUpdateListLabels
Updates list labels for all list items in the document.
Public methodCode exampleUpdatePageLayout
Rebuilds the page layout of the document.
Public methodCode exampleUpdateTableLayout
Updates widths of cells and tables in the document according to their preferred widths and content. You do not need to call this method if the tables appear correct in the output document.
Public methodCode exampleUpdateThumbnail
Updates Thumbnail of the document using default options.
Public methodCode exampleUpdateThumbnail(ThumbnailGeneratingOptions)
Updates Thumbnail of the document according to the specified options.
Public methodCode exampleUpdateWordCount
Updates word count properties of the document.
Public methodCode exampleUpdateWordCount(Boolean)
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(Int32, Graphics, Single, Single, Single) or RenderToSize(Int32, Graphics, Single, Single, Single, Single) method.

To print the document, use one of the Print(String) 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:

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
Executes mail merge from an ADO.NET DataTable.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertField(" MERGEFIELD CustomerName ");
builder.InsertParagraph();
builder.InsertField(" MERGEFIELD Address ");

// This example creates a table, but you would normally load table from a database
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" });

// Field values from the table are inserted into the mail merge fields found in the document
doc.MailMerge.Execute(table);

doc.Save(ArtifactsDir + "MailMerge.ExecuteDataTable.doc");

// Create a copy of our document to perform another mail merge
doc = new Document();
builder = new DocumentBuilder(doc);
builder.InsertField(" MERGEFIELD CustomerName ");
builder.InsertParagraph();
builder.InsertField(" MERGEFIELD Address ");

// We can also source values for a mail merge from a single row in the table
doc.MailMerge.Execute(table.Rows[1]);

doc.Save(ArtifactsDir + "MailMerge.ExecuteDataTable.OneRow.doc");
See Also