Document Class
Represents a Word document.
Inheritance Hierarchy

Namespace: Aspose.Words
Assembly: Aspose.Words (in Aspose.Words.dll) Version: 17.10
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 propertyAutomaticallyUpdateSyles
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 propertyBackgroundShape
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 propertyCompatibilityOptions
Provides access to document compatibility options (that is, the user preferences entered on the Compatibility tab of the Options dialog in Word).
Public propertyCompliance
Gets the OOXML compliance version determined from the loaded document content. Makes sense only for OOXML documents.
Public propertyCount
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 propertyCustomXmlParts
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 propertyCode exampleDocument
Gets the document to which this node belongs.
(Inherited from Node.)
Public propertyEndnoteOptions
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 propertyFontSettings
Gets or sets document font settings.
Public propertyCode exampleFootnoteOptions
Provides options that control numbering and positioning of footnotes in this document.
Public propertyGlossaryDocument
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 propertyHasMacros
Returns true if the document has a VBA project (macros).
Public propertyHasRevisions
Returns true if the document has any tracked changes.
Public propertyHyphenationOptions
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 propertyLastSection
Gets the last section in the document.
Public propertyLayoutOptions
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 propertyMailMergeSettings
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 propertyPackageCustomParts
Gets or sets the collection of custom parts (arbitrary content) that are linked to the OOXML package using "unknown relationships".
Public propertyPageColor
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 propertyResourceLoadingCallback
Allows to control how external resources are loaded.
(Inherited from DocumentBase.)
Public propertyRevisions
Gets a collection of revisions (tracked changes) that exist in this document.
Public propertyCode exampleSections
Returns a collection that represents all sections in the document.
Public propertyShadeFormData
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 propertyTheme
Gets the Theme object for this document.
Public propertyTrackRevisions
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 propertyVersionsCount
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 propertyWarningCallback
Called during various document processing procedures when an issue is detected that might result in data or formatting fidelity loss.
(Inherited from DocumentBase.)
Public propertyWriteProtection
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.
Protected methodAcceptChildren (Inherited from CompositeNode.)
Protected methodAcceptCore (Inherited from CompositeNode.)
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
Appends the specified document to the end of this document.
Public methodCleanup
Cleans unused styles and lists from the document.
Public methodCleanup(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)
Creates a duplicate of the node.
(Inherited from Node.)
Public methodCompare(Document, String, DateTime)
Compares this document with another document producing changes as number of edit and format revisions Revision.
Public methodCompare(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.
Protected methodCoreRemoveSelfOnly (Inherited from CompositeNode.)
Public methodCreateNavigator
Reserved for system use. IXPathNavigable.
(Inherited from CompositeNode.)
Public methodCode exampleEnsureMinimum
If the document contains no sections, creates one section with one paragraph.
Public methodEquals
Determines whether the specified Object is equal to the current Object.
(Inherited from Object.)
Public methodCode exampleExpandTableStylesToDirectFormatting
Converts formatting specified in table styles into direct formatting on tables in the document.
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetAncestor(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(NodeType, Boolean)
Returns a live collection of child nodes that match the specified type.
(Inherited from CompositeNode.)
Public methodGetChildNodes(NodeType, Boolean, Boolean) Obsolete.
Returns a 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
Serves as a hash function for a particular type.
(Inherited from Object.)
Public methodCode exampleGetPageInfo
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
Gets the type of the current instance.
(Inherited from Object.)
Public methodImportNode(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 methodInsertBefore
Inserts the specified node immediately before the specified reference node.
(Inherited from CompositeNode.)
Public methodInvalidateFieldTypes Obsolete.
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 exampleJoinRunsWithSameFormatting
Joins runs with same formatting in all paragraphs of the document.
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodCode exampleNextPreOrder
Gets next node according to the pre-order tree traversal algorithm.
(Inherited from Node.)
Public methodNormalizeFieldTypes
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 methodPreviousPreOrder
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 methodRemoveExternalSchemaReferences
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 methodRemoveUnusedResources Obsolete.
Cleans unused resources from the document.
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 methodStartTrackRevisions(String)
Starts automatically marking all further changes you make to the document programmatically as revision changes.
Public methodStartTrackRevisions(String, DateTime)
Starts automatically marking all further changes you make to the document programmatically as revision changes.
Public methodStopTrackRevisions
Stops automatic marking of document changes as revisions.
Public methodToString
Returns a string that represents the current object.
(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 methodToTxt Obsolete.
Exports the content of the node into a string in plain text format.
(Inherited from Node.)
Public methodUnlinkFields
Unlinks fields in the whole document.
Public methodCode exampleUnprotect
Removes protection from the document regardless of the password.
Public methodUnprotect(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 methodUpdateThumbnail
Updates Thumbnail of the document using default options.
Public methodUpdateThumbnail(ThumbnailGeneratingOptions)
Updates Thumbnail of the document according to the specified options.
Public methodCode exampleUpdateWordCount
Updates word count properties of the document.
Public methodUpdateWordCount(Boolean)
Updates word count properties of the document, optionally updates Lines property.
Protected methodWriteDocToHttResponse
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
Converts a whole document from DOC to PDF using default options.
[C#]

Document doc = new Document(MyDir + "Document.doc");

doc.Save(MyDir + "Document.Doc2PdfSave Out.pdf");
[Visual Basic]

Dim doc As New Document(MyDir & "Document.doc")

doc.Save(MyDir & "Document.Doc2PdfSave Out.pdf")
Examples
Executes mail merge from an ADO.NET DataTable.
[C#]

Document doc = new Document(MyDir + "MailMerge.ExecuteDataTable.doc");

// 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(MyDir + "MailMerge.ExecuteDataTable Out.doc");
[Visual Basic]

Dim doc As New Document(MyDir & "MailMerge.ExecuteDataTable.doc")

' This example creates a table, but you would normally load table from a database. 
Dim table As 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(MyDir & "MailMerge.ExecuteDataTable Out.doc")
See Also