|
ASPOSE home |
java.lang.ObjectNode
CompositeNode
com.aspose.words.Document
public class Document
The Document is a central object in the Aspose.Words library. To open an existing DOC document, 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 DOC format.
Document stores document-wide information such as 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 The Document can contain only In Microsoft Word, a valid document needs to have at least one section.
Constructor Summary |
---|
Document()
Creates a blank Word document. |
Document(java.lang.String fileName)
Opens an existing document from a file. |
Document(java.lang.String fileName, int loadFormat, java.lang.String password)
Opens an existing document from a file. |
Document(java.nio.ByteBuffer byteBuffer)
Opens an existing document from a ByteBuffer. |
Document(java.io.InputStream stream)
Opens an existing document from a stream. |
Document(java.nio.ByteBuffer byteBuffer, java.lang.String baseUri, int loadFormat, java.lang.String password)
Opens an existing document from a ByteBuffer. |
Document(java.io.InputStream stream, java.lang.String baseUri, int loadFormat, java.lang.String password)
Opens an existing document from a stream. |
Property Getters/Setters Summary | ||
---|---|---|
java.lang.String | getAttachedTemplate() | |
void | setAttachedTemplate(java.lang.String value) | |
Gets or sets the full path of the template attached to the document. | ||
BuiltInDocumentProperties | getBuiltInDocumentProperties() | |
Returns a collection that represents all the built-in document properties of the document. | ||
NodeCollection | getChildNodes() | → inherited from CompositeNode |
Gets all immediate child nodes of this node. | ||
CompatibilityOptions | getCompatibilityOptions() | |
Provides document compatibility options (that is, the user preferences entered on the Compatibility tab of the Options dialog in Word). | ||
int | getCount() | → inherited from CompositeNode |
Gets the number of immediate children of this node. | ||
CustomDocumentProperties | getCustomDocumentProperties() | |
Returns a collection that represents all the custom document properties of the document. | ||
double | getDefaultTabStop() | |
void | setDefaultTabStop(double value) | |
Gets or sets the interval (in points) between the default tab stops. | ||
Document | getDocument() | → inherited from Node |
Gets the document to which this node belongs. | ||
Node | getFirstChild() | → inherited from CompositeNode |
Gets the first child of the node. | ||
Section | getFirstSection() | |
Gets the first section in the document. | ||
boolean | hasChildNodes() | → inherited from CompositeNode |
Returns true if this node has any child nodes. | ||
boolean | hasMacros() | |
Returns true if the document has a VBA project. | ||
boolean | hasRevisions() | |
Returns true if the document has any tracked changes. | ||
boolean | isComposite() | → inherited from CompositeNode |
Returns true as this node can have child nodes. | ||
boolean | isTemplate() | |
void | isTemplate(boolean value) | |
Specifies whether the document is a template or a normal document. | ||
Node | getLastChild() | → inherited from CompositeNode |
Gets the last child of the node. | ||
Section | getLastSection() | |
Gets the last section in the document. | ||
Lists | getLists() | |
Provides access to the list formatting used in the document. | ||
MailMerge | getMailMerge() | |
Returns a MailMerge object that represents the mail merge functionality for the document. | ||
Node | getNextSibling() | → inherited from Node |
Gets the node immediately following this node. | ||
int | getNodeType() | |
Returns NodeType.Document. The value of the property is NodeType integer constant. | ||
java.lang.String | getOriginalFileName() | |
Gets the original file name of the document. | ||
CompositeNode | getParentNode() | → inherited from Node |
Gets the immediate parent of this node. | ||
Node | getPreviousSibling() | → inherited from Node |
Gets the node immediately preceding this node. | ||
int | getProtectionType() | |
void | setProtectionType(int value) | |
Gets or sets the document protection type. The value of the property is ProtectionType integer constant. | ||
Range | getRange() | → inherited from Node |
Returns a Range object that represents the portion of a document that is contained in this node. | ||
Sections | getSections() | |
Returns a Sections collection that represents the sections in the document. | ||
boolean | getShadeFormData() | |
void | setShadeFormData(boolean value) | |
Specifies whether to turn on the gray shading on form fields. | ||
Styles | getStyles() | |
Returns a collection of styles defined in the document. | ||
Variables | getVariables() | |
Returns the collection of variables added to a document or template. | ||
ViewOptions | getViewOptions() | |
Provides options to control how the document is displayed in Microsoft Word. |
Method Summary | ||
---|---|---|
boolean | accept(DocumentVisitor visitor) | |
Accepts a visitor. | ||
void | acceptAllRevisions() | |
Accepts all tracked changes in the document. | ||
void | addNodeChangedEventHandler(NodeChangedEventHandler newListener) | |
Adds the NodeChangedEventHandler to the list of subscribed listeners of one of Node Changed event. | ||
Node | appendChild(Node newChild) | → inherited from CompositeNode |
Adds the specified node to the end of the list of child nodes for this node. | ||
Document | deepClone() | |
Performs a deep copy of the |
||
Node | deepClone(boolean isCloneChildren) | |
Creates a duplicate of the document. | ||
void | ensureMinimum() | |
If the document contains no sections, creates one section with one paragraph. | ||
Node | getAncestor(java.lang.Class ancestorType) | → inherited from Node |
Gets the first ancestor of the specified type. | ||
Node | getChild(int nodeType, int index, boolean isDeep) | → inherited from CompositeNode |
Returns an Nth child node that matches the specified type. | ||
NodeCollection | getChildNodes(int nodeType, boolean isDeep) | → inherited from CompositeNode |
Returns a live collection of child nodes that match the specified type. | ||
NodeCollection | getChildNodes(int nodeType, boolean isDeep, boolean isLive) | → inherited from CompositeNode |
Returns a "live" or "snapshot" collection of child node that match the specified type. | ||
java.lang.String | getText() | → inherited from CompositeNode |
Gets the text of this node and of all its children. | ||
Node | importNode(Node srcNode, boolean isImportChildren) | |
Imports a node from another document to the current document. |
||
Node | importNode(Node srcNode, boolean isImportChildren, int importFormatMode) | |
Imports a node from another document to the current document with an option to control formatting. |
||
int | indexOf(Node child) | → inherited from CompositeNode |
Returns the index of the specified child node in the child node array. | ||
Node | insertAfter(Node newChild, Node refChild) | → inherited from CompositeNode |
Inserts the specified node immediately after the specified reference node. | ||
Node | insertBefore(Node newChild, Node refChild) | → inherited from CompositeNode |
Inserts the specified node immediately before the specified reference node. | ||
java.util.Iterator | iterator() | → inherited from CompositeNode |
Provides support for the for each style iteration over the child nodes of this node. | ||
Node | nextPreOrder(Node rootNode) | → inherited from Node |
Gets next node according to the pre-order tree traversal algorithm. | ||
Node | prependChild(Node newChild) | → inherited from CompositeNode |
Adds the specified node to the beginning of the list of child nodes for this node. | ||
Node | previousPreOrder(Node rootNode) | → inherited from Node |
Gets the previous node according to the pre-order tree traversal algorithm. | ||
void | protect(int type) | |
Protects the document from changes. | ||
void | remove() | → inherited from Node |
Removes itself from the parent. | ||
void | removeAllChildren() | → inherited from CompositeNode |
Removes all the child nodes of the current node. | ||
Node | removeChild(Node oldChild) | → inherited from CompositeNode |
Removes the specified child node. | ||
void | removeMacros() | |
Removes all macros (the VBA project) from the document. | ||
void | removeNodeChangedEventHandler(NodeChangedEventHandler oldListener) | |
Removes the NodeChangedEventHandler from the list of subscribed listeners of Node Changed event. | ||
void | save(java.io.OutputStream outputStream, int saveFormat) | |
Saves the document to a stream. | ||
void | save(java.lang.String fileName) | |
Saves the document to a file. Automatically determine the file format from the extension. | ||
void | save(java.lang.String fileName, int fileFormat) | |
Saves the document with a new name or format. | ||
NodeList | selectNodes(java.lang.String xpath) | → inherited from CompositeNode |
Selects a list of nodes matching the XPath expression. | ||
Node | selectSingleNode(java.lang.String xpath) | → inherited from CompositeNode |
Selects the first Node that matches the XPath expression. | ||
java.lang.String | toTxt() | → inherited from Node |
Exports the content of the node into a string in TXT (plain text) format. | ||
void | unprotect() | |
Removes protection from the document. | ||
void | updateTableLayout() | |
Updates widths of cells and tables according to their preferred widths and content. | ||
void | updateWordCount() | |
Updates word count properties of the document. |
Constructor Detail |
---|
public Document() throws java.lang.Exception
The document paper size is Letter by default. If you want to change page setup, use
After creation, you can use
public Document(java.lang.String fileName) throws java.lang.Exception
fileName
- File name of the document to open.public Document(java.lang.String fileName, int loadFormat, java.lang.String password) throws java.lang.Exception
If you know the format of the document upfront, you can use this constructor
and specify the format. It will work slightly faster than
other constructors that detect the format of the file automatically.
It is only
Use this constructor to open an encrypted Microsoft Word document. You need to know the password to open an encrypted document. If the document is not encrypted, pass null or empty string.
fileName
- File name of the document to open.loadFormat
- A password
- A password to open an encrypted Word document. Pass null or empty string
if the document is not encrypted.public Document(java.nio.ByteBuffer byteBuffer) throws java.lang.Exception
The document must be stored at the beginning of the ByteBuffer. Note: the ctor calls ByteBufer.rewind() to set zero position and ByteBufer.remaining() to get the length, so limit mast be set properly.
public Document(java.io.InputStream stream) throws java.lang.Exception
The document must be stored at the beginning of the stream. For reading java.nio Channels one can use java.nio.Channels.newInputStream(ReadableByteChannel) to convert Channel to InputStream
stream
- Stream where to load the document from.public Document(java.nio.ByteBuffer byteBuffer, java.lang.String baseUri, int loadFormat, java.lang.String password) throws java.lang.Exception
The document must be stored at the beginning of the ByteBuffer. Note: the ctor calls ByteBufer.rewind() to set zero position and ByteBufer.remaining() to get the length, so limit mast be set properly.
If you know the format of the document upfront, you can use this constructor
and specify the format. It will work slightly faster than
other constructors that detect the format of the file automatically.
It is only
Use this constructor to open an encrypted Microsoft Word document. You need to know the password to open an encrypted document. If the document is not encrypted, pass null or empty string.
The baseUri parameter is used with HTML and PDF document formats and not supported in Java at the moment.
byteBuffer
- ByteBuffer where to load the document from.baseUri
- The base URI. Can be null or empty string.
In Java ignored at the moment.loadFormat
- A password
- A password to open an encrypted Word document. Pass null or empty string
if the document is not encrypted.public Document(java.io.InputStream stream, java.lang.String baseUri, int loadFormat, java.lang.String password) throws java.lang.Exception
The document must be stored at the beginning of the stream. For reading java.nio Channels one can use java.nio.Channels.newInputStream(ReadableByteChannel) to convert Channel to InputStream
If you know the format of the document upfront, you can use this constructor
and specify the format. It will work slightly faster than
other constructors that detect the format of the file automatically.
It is only
Use this constructor to open an encrypted Microsoft Word document. You need to know the password to open an encrypted document. If the document is not encrypted, pass null or empty string.
The baseUri parameter is used with HTML and PDF document formats and not supported in Java at the moment.
stream
- The stream where to load the document from.baseUri
- The base URI. Can be null or empty string. In Java ignored at the moment.loadFormat
- A password
- A password to open an encrypted Word document. Pass null or empty string
if the document is not encrypted.Property Getters/Setters Detail |
---|
getAttachedTemplate/setAttachedTemplate | |
public java.lang.String getAttachedTemplate() public void setAttachedTemplate(java.lang.String value) |
Empty string means the document is attached to the Normal template.
isTemplate/isTemplate | |
public boolean isTemplate() public void isTemplate(boolean value) |
The type of the document (template or document) is controlled by this property and stored inside the document. The type of the document is not controlled by the file extension.
Aspose.Words sets this property to false when the document is loaded into memory. This means that when you save a document, it will be saved as a Microsoft Word Document by default. If you want to save your document as a Microsoft Word Document Template, you need to set this property to true before saving.
A likely scenario for Aspose.Words is to open a document (.DOC or .DOT), populate it with data and save as a .DOC document. If Aspose.Words did not reset this property to false automatically, you could end up saving Microsoft Word Document Templates.
When the document is a template, Microsoft Word 2003 suggests the Document Template file type with the .DOT extension in the Save As dialog box. Earlier versions of Microsoft Word disallow selecting the file type altogether.
getShadeFormData/setShadeFormData | |
public boolean getShadeFormData() public void setShadeFormData(boolean value) |
getNodeType | |
public int getNodeType() |
getBuiltInDocumentProperties | |
public BuiltInDocumentProperties getBuiltInDocumentProperties() |
getCustomDocumentProperties | |
public CustomDocumentProperties getCustomDocumentProperties() |
getVariables | |
public Variables getVariables() |
getStyles | |
public Styles getStyles() |
getMailMerge | |
public MailMerge getMailMerge() |
getProtectionType/setProtectionType | |
public int getProtectionType() public void setProtectionType(int value) |
ProtectionType allows to retrieve and change document protection type.
Protect and Unprotect methods allow to change protection type, but do not allow to retrieve it and are available to mimic Microsoft Word object model.
When a document is protected, the user can make only limited changes, such as adding annotations, making revisions, or completing a form.
When you protect a document, and the document does not have a protection password, Aspose.Words assigns a random password that makes it impossible to unprotect the document in MS Word, but you still can unprotect the document in Aspose.Words as it does not require a password when unprotecting.
When you protect a document, and the document already has a protection password, the existing protection password is not changed.
getSections | |
public Sections getSections() |
getFirstSection | |
public Section getFirstSection() |
getLastSection | |
public Section getLastSection() |
getViewOptions | |
public ViewOptions getViewOptions() |
getCompatibilityOptions | |
public CompatibilityOptions getCompatibilityOptions() |
hasRevisions | |
public boolean hasRevisions() |
hasMacros | |
public boolean hasMacros() |
getDefaultTabStop/setDefaultTabStop | |
public double getDefaultTabStop() public void setDefaultTabStop(double value) |
getLists | |
public Lists getLists() |
For more information see the description of the
getOriginalFileName | |
public java.lang.String getOriginalFileName() |
Returns null if the document was loaded from a stream or created blank.
isComposite | → inherited from CompositeNode |
public boolean isComposite() |
hasChildNodes | → inherited from CompositeNode |
public boolean hasChildNodes() |
getChildNodes | → inherited from CompositeNode |
public NodeCollection getChildNodes() |
If there are no child nodes, this property returns an empty collection.
ChildNodes is equivalent to calling GetChildNodes(NodeType.Any, false)
.
getFirstChild | → inherited from CompositeNode |
public Node getFirstChild() |
getLastChild | → inherited from CompositeNode |
public Node getLastChild() |
getCount | → inherited from CompositeNode |
public int getCount() |
getParentNode | → inherited from Node |
public CompositeNode getParentNode() |
If a node has just been created and not yet added to the tree, or if it has been removed from the tree, the parent is null.
getDocument | → inherited from Node |
public Document getDocument() |
The node always belongs to a document even if it has just been created and not yed added to the tree, or if it has been removed from the tree.
getPreviousSibling | → inherited from Node |
public Node getPreviousSibling() |
If there is no preceding node, a null is returned.
Note: Calculating the value of this property iterates from the first child node of ParenNode to this node.
getNextSibling | → inherited from Node |
public Node getNextSibling() |
getRange | → inherited from Node |
public Range getRange() |
Method Detail |
---|
iterator | → inherited from CompositeNode |
public java.util.Iterator iterator() |
getText | → inherited from CompositeNode |
public java.lang.String getText() |
The returned string includes all control and special characters as described in
accept | |
public boolean accept(DocumentVisitor visitor) throws java.lang.Exception |
Enumerates over this node and all of its children. Each node calls a corresponding method on DocumentVisitor.
For more info see the Visitor design pattern.
visitor
- The visitor that will visit the nodes.deepClone | |
public Node deepClone(boolean isCloneChildren) throws java.lang.Exception |
If you need to generate many documents from a single document, just load the document into memory once, clone it and populate the cloned document with your data. This speeds up generation of documents because there is no need to load and parse the document from file every time.
isCloneChildren
- True to recursively clone the content of the document;
false to clone only the document node and document-wide objects styles, lists, document properties etc.deepClone | |
public Document deepClone() throws java.lang.Exception |
save | |
public void save(java.lang.String fileName) throws java.lang.Exception |
fileName
- The name for the document. If a document with the
specified file name already exists, the existing document is overwritten.save | |
public void save(java.lang.String fileName, int fileFormat) throws java.lang.Exception |
fileName
- The name for the document. If a document with the
specified file name already exists, the existing document is overwritten.fileFormat
- A save | |
public void save(java.io.OutputStream outputStream, int saveFormat) throws java.lang.Exception |
stream
- Stream where to save the document.saveFormat
-
A importNode | |
public Node importNode(Node srcNode, boolean isImportChildren) throws java.lang.Exception |
Imports a node from another document to the current document.
This method uses the
Importing a node creates a copy of the source node belonging to the importing document. The returned node has no parent. The source node is not altered or removed from the original document.
Before a node from another document can be inserted into this document, it must be imported.
During import, document-specific properties such as references to styles and lists are translated
from the original to the importing document. After the node was imported, it can be inserted
into the appropriate place in the document using
If the source node already belongs to the destination document, then simply a deep clone of the source node is created.
srcNode
- The node being imported.isImportChildren
- True to import all child nodes recursively; otherwise, false.importNode | |
public Node importNode(Node srcNode, boolean isImportChildren, int importFormatMode) throws java.lang.Exception |
Imports a node from another document to the current document with an option to control formatting.
This overload is useful to control how styles and list formatting are imported.
Importing a node creates a copy of the source node belonging to the importing document. The returned node has no parent. The source node is not altered or removed from the original document.
Before a node from another document can be inserted into this document, it must be imported.
During import, document-specific properties such as references to styles and lists are translated
from the original to the importing document. After the node was imported, it can be inserted
into the appropriate place in the document using
If the source node already belongs to the destination document, then simply a deep clone of the source node is created.
srcNode
- The node to imported.isImportChildren
- True to import all child nodes recursively; otherwise, false.importFormatMode
- A ensureMinimum | |
public void ensureMinimum() throws java.lang.Exception |
acceptAllRevisions | |
public void acceptAllRevisions() throws java.lang.Exception |
protect | |
public void protect(int type) |
When a document is protected, the user can make only limited changes, such as adding annotations, making revisions, or completing a form.
When you protect a document, and the document does not have a protection password, Aspose.Words assigns a random password that makes it impossible to unprotect the document in MS Word, but you still can unprotect the document in Aspose.Words as it does not require a password when unprotecting.
When you protect a document, and the document already has a protection password, the existing protection password is not changed.
type
- A unprotect | |
public void unprotect() |
updateWordCount | |
public void updateWordCount() throws java.lang.Exception |
UpdateWordCount recalculates and updates Characters, Words and Paragraphs
properties in the
Note that UpdateWordCount does not update number of lines and pages properties.
When you use an evaluation version, the evaluation watermark will also be included in the word count.
updateTableLayout | |
public void updateTableLayout() throws java.lang.Exception |
You don't normally need to call this method as cell and table widths are maintained automatically. You might need to call this method before exporting to PDF, only in rare cases where a table appear incorrectly laid out in PDF output.
removeMacros | |
public void removeMacros() |
By removing all macros from a document you can ensure the document contains no macro viruses.
getChildNodes | → inherited from CompositeNode |
public NodeCollection getChildNodes(int nodeType, boolean isDeep) |
The collection of nodes returned by this method is always live.
A live collection is always in sync with the document. For example, if you selected all sections in a document and enumerate through the collection deleting the sections, the section is removed from the collection immediately when it is removed from the document.
nodeType
- A isDeep
- True to select from all child nodes recursively.
False to select only among immediate children. getChildNodes | → inherited from CompositeNode |
public NodeCollection getChildNodes(int nodeType, boolean isDeep, boolean isLive) |
Snapshot collections require less resources than live collections because a snapshot collection of nodes is not updated when the document is changed.
Snapshot collections are useful for tasks, that do not require immediate update of the collection when the document is changed. For example, select all shapes and delete them is more efficient using a snapshot collection.
A live collection is always in sync with the document. For example, if you selected all sections in a document and enumerate through the collection deleting the sections, the section is removed from the collection immediately when it is removed from the document.
nodeType
- A isDeep
- True to select from all child nodes recursively.
False to select only among immediate children. isLive
- True to return a live collection; false to return a snapshot collection.getChild | → inherited from CompositeNode |
public Node getChild(int nodeType, int index, boolean isDeep) |
If index is out of range, a null is returned.
nodeType
- A index
- Zero based index of the child node to select.
Negative indexes are also allowed and indicate access from the end,
that is -1 means the last node.isDeep
- True to select from all child nodes recursively.
False to select only among immediate children. selectNodes | → inherited from CompositeNode |
public NodeList selectNodes(java.lang.String xpath) throws org.jaxen.JaxenException |
Only expressions with element names are supported at the moment. Expressions that use attribute names are not supported.
xpath
- The XPath expression.selectSingleNode | → inherited from CompositeNode |
public Node selectSingleNode(java.lang.String xpath) throws org.jaxen.JaxenException |
Only expressions with element names are supported at the moment. Expressions that use attribute names are not supported.
xpath
- The XPath expression.appendChild | → inherited from CompositeNode |
public Node appendChild(Node newChild) throws java.lang.Exception |
If the newChild is already in the tree, it is first removed.
If the node being inserted was created from another document, you should use
newChild
- The node to add.prependChild | → inherited from CompositeNode |
public Node prependChild(Node newChild) throws java.lang.Exception |
If the newChild is already in the tree, it is first removed.
If the node being inserted was created from another document, you should use
newChild
- The node to add.insertAfter | → inherited from CompositeNode |
public Node insertAfter(Node newChild, Node refChild) throws java.lang.Exception |
If refChild is null, inserts newChild at the beginning of the list of child nodes.
If the newChild is already in the tree, it is first removed.
If the node being inserted was created from another document, you should use
newChild
- The Node to insert.refChild
- The Node that is the reference node. The newNode is placed after the refNode. insertBefore | → inherited from CompositeNode |
public Node insertBefore(Node newChild, Node refChild) throws java.lang.Exception |
If refChild is null, inserts newChild at the end of the list of child nodes.
Note: InsertBefore could be slower than InsertAfter because it needs to iterate over all nodes from the first child to the reference node.
If the newChild is already in the tree, it is first removed.
If the node being inserted was created from another document, you should use
newChild
- The Node to insert.refChild
- The Node that is the reference node. The newChild is placed before this node.removeChild | → inherited from CompositeNode |
public Node removeChild(Node oldChild) throws java.lang.Exception |
The parent of oldChild is set to null after the node is removed.
oldChild
- The node to remove.removeAllChildren | → inherited from CompositeNode |
public void removeAllChildren() throws java.lang.Exception |
indexOf | → inherited from CompositeNode |
public int indexOf(Node child) |
getAncestor | → inherited from Node |
public Node getAncestor(java.lang.Class ancestorType) |
The ancestor type matches if it is equal to ancestorType or derived from ancestorType.
ancestorType
- The type of the ancestor to retrieve.remove | → inherited from Node |
public void remove() throws java.lang.Exception |
nextPreOrder | → inherited from Node |
public Node nextPreOrder(Node rootNode) |
rootNode
- The top node (limit) of traversal.previousPreOrder | → inherited from Node |
public Node previousPreOrder(Node rootNode) |
rootNode
- The top node (limit) of traversal.toTxt | → inherited from Node |
public java.lang.String toTxt() throws java.lang.Exception |
This method removes field codes and Microsoft Word control characters, uses CrLf combination
to mark ends of paragraphs in the resulting string. It produces a different result from
addNodeChangedEventHandler | |
public void addNodeChangedEventHandler(NodeChangedEventHandler newListener) |
removeNodeChangedEventHandler | |
public void removeNodeChangedEventHandler(NodeChangedEventHandler oldListener) |
|
ASPOSE home |