java.lang.ObjectNode
CompositeNode
DocumentBase
com.aspose.words.Document
public class Document
The Document is a central object in the Aspose.Words library. To load an existing document in any of the Use one of the Save method overloads to save the document in any of the
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. Example:
// Open the template document
Document doc = new Document(getMyDir() + "MailingLabelsDemo.doc");
// Open a DSN-less connection.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String connString = "jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};" +
"DBQ=" + getDatabaseDir() + "Northwind.mdb;UID=Admin";
Connection conn = DriverManager.getConnection(connString);
// Get the data.
Statement statement = conn.createStatement();
ResultSet table = statement.executeQuery("SELECT TOP 50 * FROM Customers ORDER BY Country, CompanyName");
// Perform mail merge.
doc.getMailMerge().execute(table);
// Close the database.
conn.close();
doc.save(getMyDir() + "MailMerge.ExecuteResultSet Out.doc");
Constructor Summary |
---|
Document()
Creates a blank Word document. |
Document(java.lang.String fileName)
Opens an existing document from a file. Automatically detects the file format. |
Document(java.lang.String fileName, int loadFormat, java.lang.String password)
Opens an existing document from a file. |
Document(java.io.InputStream stream)
Opens an existing document from a stream. Automatically detects the file format. |
Document(java.io.InputStream stream, java.lang.String baseUri)
Opens an existing document from a stream. Automatically detects the file format. |
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. | ||
DocumentBase | getDocument() | → inherited from Node |
Gets the document to which this node belongs. | ||
FootnoteOptions | getEndnoteOptions() | |
Provides options that control numbering and positioning of endnotes in this document. | ||
Node | getFirstChild() | → inherited from CompositeNode |
Gets the first child of the node. | ||
Section | getFirstSection() | |
Gets the first section in the document. | ||
FontInfoCollection | getFontInfos() | → inherited from DocumentBase |
Provides access to properties of fonts used in this document. | ||
FootnoteOptions | getFootnoteOptions() | |
Provides options that control numbering and positioning of footnotes in this document. | ||
GlossaryDocument | getGlossaryDocument() | |
void | setGlossaryDocument(GlossaryDocument value) | |
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. | ||
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 (macros). | ||
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 will be saved as a Microsoft Word 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. | ||
ListCollection | getLists() | → inherited from DocumentBase |
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. | ||
int | getOriginalLoadFormat() | |
Gets the format of the original document that was loaded into this object. The value of the property is LoadFormat integer constant. | ||
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() | |
Gets the currently active 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. | ||
SaveOptions | getSaveOptions() | |
void | setSaveOptions(SaveOptions value) | |
Provides options to control how the document is saved. | ||
SectionCollection | getSections() | |
Returns a collection that represents all sections in the document. | ||
boolean | getShadeFormData() | |
void | setShadeFormData(boolean value) | |
Specifies whether to turn on the gray shading on form fields. | ||
StyleCollection | getStyles() | → inherited from DocumentBase |
Returns a collection of styles defined in the document. | ||
boolean | getTrackRevisions() | |
void | setTrackRevisions(boolean value) | |
True if changes are tracked when this document is edited in Microsoft Word. | ||
VariableCollection | getVariables() | |
Returns the collection of variables added to a document or template. | ||
int | getVersionsCount() | |
Gets the number of document versions that was stored in the DOC document. | ||
ViewOptions | getViewOptions() | |
Provides options to control how the document is displayed in Microsoft Word. | ||
WriteProtection | getWriteProtection() | |
Provides access to the document write protection options. |
Method Summary | ||
---|---|---|
boolean | accept(DocumentVisitor visitor) | |
Accepts a visitor. | ||
void | acceptAllRevisions() | |
Accepts all tracked changes in the document. | ||
void | addNodeChangedEventHandler(NodeChangedEventHandler newListener) | → inherited from DocumentBase |
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. | ||
void | appendDocument(Document srcDoc, int importFormatMode) | |
Appends the specified document to the end of this document. | ||
void | clearSectionAttrs() | |
Reserved for internal use. | ||
Document | deepClone() | |
Performs a deep copy of the |
||
Node | deepClone(boolean isCloneChildren) | |
Creates a duplicate of the document. | ||
static int | detectFileFormat(java.lang.String fileName) | |
Detects and returns the format of a document stored in a disk file. | ||
void | ensureMinimum() | |
If the document contains no sections, creates one section with one paragraph. | ||
void | expandTableStylesToDirectFormatting() | |
Converts formatting specified in table styles into direct formatting on tables in the document. | ||
java.lang.Object | fetchInheritedSectionAttr(int key) | |
Reserved for internal use. | ||
Node | getAncestor(int ancestorType) | → inherited from Node |
Gets the first ancestor of the specified |
||
Node | getAncestor(java.lang.Class ancestorType) | → inherited from Node |
Gets the first ancestor of the specified object 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.Object | getDirectSectionAttr(int key) | |
Reserved for internal use. | ||
java.lang.String | getText() | → inherited from CompositeNode |
Gets the text of this node and of all its children. | ||
Node | importNode(Node srcNode, boolean isImportChildren) | → inherited from DocumentBase |
Imports a node from another document to the current document. |
||
Node | importNode(Node srcNode, boolean isImportChildren, int importFormatMode) | → inherited from DocumentBase |
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. | ||
int | joinRunsWithSameFormatting() | |
Joins runs with same formatting in all paragraphs of the document. | ||
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 without changing the existing password or assigns a random password. | ||
void | protect(int type, java.lang.String password) | |
Protects the document from changes and optionally sets a protection password. | ||
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) | → inherited from DocumentBase |
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. | ||
void | setSectionAttr(int key, java.lang.Object value) | |
Reserved for internal use. | ||
java.lang.String | toTxt() | → inherited from Node |
Exports the content of the node into a string in plain text format. | ||
void | unprotect() | |
Removes protection from the document. | ||
void | updateFields() | |
Updates the values of document fields. Not all field types are updated. | ||
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
Example:
Shows how to add a formatted run of text to a document using the object model.// Create an empty document. It contains one empty paragraph. Document doc = new Document(); // Create a new run of text. Run run = new Run(doc, "Hello"); // Specify character formatting for the run of text. Font f = run.getFont(); f.setName("Courier New"); f.setSize(36); f.setHighlightColor(Color.YELLOW); // Append the run of text to the end of the first paragraph // in the body of the first section of the document. doc.getFirstSection().getBody().getParagraphs().get(0).appendChild(run);
public Document(java.lang.String fileName) throws java.lang.Exception
fileName
- File name of the document to open.Example:
Opens a document from a file.// Open a document. The file is opened read only and only for the duration of the constructor. Document doc = new Document(getMyDir() + "Document.doc");
public Document(java.lang.String fileName, int loadFormat, java.lang.String password) throws java.lang.Exception
It is recommended that you specify the
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.Example:
Loads a Microsoft Word document encrypted with a password.Document doc = new Document(getMyDir() + "Document.LoadEncrypted.doc", LoadFormat.DOC, "qwerty");
Example:
Explicitly loads a document as HTML without automatic file format detection.Document doc = new Document(getMyDir() + "Document.LoadFormat.html", LoadFormat.HTML, null);
public Document(java.io.InputStream stream) throws java.lang.Exception
The document must be stored at the beginning of the stream.
stream
- Stream where to load the document from.Example:
Opens a document from a stream.// Open the stream. Read only access is enough for Aspose.Words to load a document. InputStream stream = new FileInputStream(getMyDir() + "Document.doc"); // Load the entire document into memory. Document doc = new Document(stream); // You can close the stream now, it is no longer needed because the document is in memory. stream.close(); // ... do something with the document Assert.assertEquals("Hello World!\u000c",doc.getText());
public Document(java.io.InputStream stream, java.lang.String baseUri) throws java.lang.Exception
The document must be stored at the beginning of the stream.
The baseUri parameter is used to resolve relative URIs found in the document to absolute URIs in the following cases:
stream
- The stream where to load the document from.baseUri
- The base URI. Can be null or empty string.Example:
Opens an HTML document with images from a stream with a base URI.// We are opening this HTML file: /* <html> <body> <p>Simple file.</p> <p><img src="Aspose.Words.gif" width="80" height="60"></p> </body> </html> */ String fileName = getMyDir() + "Document.OpenFromStreamWithBaseUri.html"; // Open the stream. InputStream stream = new FileInputStream(fileName); // Open the document. Note the Document constructor detects HTML format automatically. // Pass the URI of the base folder so any images with relative URIs in the file can be found. Document doc = new Document(stream, getMyDir()); // You can close the stream now, it is no longer needed because the document is in memory. stream.close(); // Lets make sure the image was imported successfully into a Shape node. // Get the 1st shape node in the document. Shape shape = (Shape)doc.getChild(NodeType.SHAPE, 0, true); // Verify some properties of the image. Assert.assertTrue(shape.isImage()); Assert.assertNotNull(shape.getImageData().getImageBytes()); Assert.assertEquals(80.0, ConvertUtil.pointToPixel(shape.getWidth())); Assert.assertEquals(60.0, ConvertUtil.pointToPixel(shape.getHeight())); // Save in the DOC format. doc.save(getMyDir() + "Document.OpenFromStreamWithBaseUri Out.doc");
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.
It is recommended that you specify the
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 to resolve relative URIs found in the document to absolute URIs in the following cases:
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.Example:
Loads a Microsoft Word document encrypted with a password from a stream.InputStream stream = new FileInputStream(getMyDir() + "Document.LoadEncrypted.doc"); Document doc = new Document(stream, null, LoadFormat.DOC, "qwerty"); stream.close();
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.
Example:
Opens a document, makes sure it is no longer attached to a template and saves the document.Document doc = new Document(getMyDir() + "Document.doc"); doc.setAttachedTemplate(""); doc.save(getMyDir() + "Document.DetachTemplate Out.doc");
isTemplate/isTemplate | |
public boolean isTemplate() / public void isTemplate(boolean value) |
This property allows to control whether the document will be saved as a template when writing to a stream. If you are writing to a file, then the file name extension will control whether the document is saved as a template (e.g. .DOC vs .DOT and .DOCX vs .DOTX).
When any document is opened (normal or template), this property is always false. If you want to save your document as a template, you need to set this property to true before saving.
getShadeFormData/setShadeFormData | |
public boolean getShadeFormData() / public void setShadeFormData(boolean value) |
getTrackRevisions/setTrackRevisions | |
public boolean getTrackRevisions() / public void setTrackRevisions(boolean value) |
Setting this option only instructs Microsoft Word whether the track changes is turned on or off. This property has no effect on changes to the document that you make programmatically via Aspose.Words. The changes that you make via Aspose.Words are never tracked as revisions.
getNodeType | |
public int getNodeType() |
getBuiltInDocumentProperties | |
public BuiltInDocumentProperties getBuiltInDocumentProperties() |
Example:
Enumerates through all built-in and custom properties in a document.String fileName = getMyDir() + "Properties.doc"; Document doc = new Document(fileName); System.out.println(MessageFormat.format("1. Document name: {0}", fileName)); System.out.println("2. Buil-in Properties"); for (DocumentProperty prop : doc.getBuiltInDocumentProperties()) System.out.println(MessageFormat.format("{0} : {1}", prop.getName(), prop.getValue())); System.out.println("3. Custom Properties"); for (DocumentProperty prop : doc.getCustomDocumentProperties()) System.out.println(MessageFormat.format("{0} : {1}", prop.getName(), prop.getValue()));
getCustomDocumentProperties | |
public CustomDocumentProperties getCustomDocumentProperties() |
Example:
Enumerates through all built-in and custom properties in a document.String fileName = getMyDir() + "Properties.doc"; Document doc = new Document(fileName); System.out.println(MessageFormat.format("1. Document name: {0}", fileName)); System.out.println("2. Buil-in Properties"); for (DocumentProperty prop : doc.getBuiltInDocumentProperties()) System.out.println(MessageFormat.format("{0} : {1}", prop.getName(), prop.getValue())); System.out.println("3. Custom Properties"); for (DocumentProperty prop : doc.getCustomDocumentProperties()) System.out.println(MessageFormat.format("{0} : {1}", prop.getName(), prop.getValue()));
getMailMerge | |
public MailMerge getMailMerge() |
Example:
Executes mail merge from a java.sql.ResultSet.// Open the template document Document doc = new Document(getMyDir() + "MailingLabelsDemo.doc"); // Open a DSN-less connection. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String connString = "jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};" + "DBQ=" + getDatabaseDir() + "Northwind.mdb;UID=Admin"; Connection conn = DriverManager.getConnection(connString); // Get the data. Statement statement = conn.createStatement(); ResultSet table = statement.executeQuery("SELECT TOP 50 * FROM Customers ORDER BY Country, CompanyName"); // Perform mail merge. doc.getMailMerge().execute(table); // Close the database. conn.close(); doc.save(getMyDir() + "MailMerge.ExecuteResultSet Out.doc");
Example:
Executes a mail merge with repeatable regions.public void executeWithRegionsDataTable() throws Exception { Document doc = new Document(getMyDir() + "MailMerge.ExecuteWithRegions.doc"); int orderId = 10444; // Perform several mail merge operations populating only part of the document each time. ResultSet orderTable = getTestOrder(orderId); doc.getMailMerge().executeWithRegions("Orders", orderTable); ResultSet orderDetailsTable = getTestOrderDetails(orderId, "ExtendedPrice DESC"); doc.getMailMerge().executeWithRegions("OrderDetails", orderDetailsTable); doc.save(getMyDir() + "MailMerge.ExecuteWithRegionsStringResultSet Out.doc"); } private static ResultSet getTestOrder(int orderId) throws Exception { return executeDataTable(String.format( "SELECT * FROM AsposeWordOrders WHERE OrderId = %d", orderId)); } private static ResultSet getTestOrderDetails(int orderId, String sort) throws Exception { String query = String.format( "SELECT * FROM AsposeWordOrderDetails WHERE OrderId = %d ORDER BY ProductID", orderId); if (sort != null && !"".equals(sort)) { query = String.format( "SELECT * FROM AsposeWordOrderDetails WHERE OrderId = %d ORDER BY %s", orderId, sort); } return executeDataTable(query); } /// <summary> /// Utility function that creates a connection, command, /// executes the command and return the result in a ResultSet. /// </summary> private static ResultSet executeDataTable(String commandText) throws Exception { // Open DSN-less DB connection. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String connString = "jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};" + "DBQ=" + getDatabaseDir() + "Northwind.mdb;UID=Admin"; Connection conn = DriverManager.getConnection(connString); // Get the data. Statement mStatement = conn.createStatement(); return mStatement.executeQuery(commandText); }
getProtectionType | |
public int getProtectionType() |
This property allows to retrieve the currently set document protection type.
To change the document protection type use the
When a document is protected, the user can make only limited changes, such as adding annotations, making revisions, or completing a form.
Note that document protection is different from write protection.
Write protection is specified using the
Example:
Shows how to get protection type currently set in the document.Document doc = new Document(getMyDir() + "Document.doc"); int protectionType = doc.getProtectionType();
getSections | |
public SectionCollection getSections() |
Example:
Specifies how the section starts, from a new page, on the same page or other.Document doc = new Document(); doc.getSections().get(0).getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
Example:
Shows how to add/remove sections in a document.// Open the document. Document doc = new Document(getMyDir() + "Section.AddRemove.doc"); // This shows what is in the document originally. The document has two sections. Assert.assertEquals("Hello1\u000cHello2\u000c", doc.getText()); // Delete the first section from the document doc.getSections().removeAt(0); // Duplicate the last section and append the copy to the end of the document. int lastSectionIdx = doc.getSections().getCount() - 1; Section newSection = doc.getSections().get(lastSectionIdx).deepClone(); doc.getSections().add(newSection); // Check what the document contains after we changed it. Assert.assertEquals("Hello2\u000cHello2\u000c", doc.getText());
getFirstSection | |
public Section getFirstSection() |
null
if there are no sections.
Example:
Shows how to replace text in the document footer.// Open the template document, containing obsolete copyright information in the footer. Document doc = new Document(getMyDir() + "HeaderFooter.ReplaceText.doc"); HeaderFooterCollection headersFooters = doc.getFirstSection().getHeadersFooters(); HeaderFooter footer = headersFooters.getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY); footer.getRange().replace("(C) 2006 Aspose Pty Ltd.", "Copyright (C) 2008 by Aspose Pty Ltd.", false, false); doc.save(getMyDir() + "HeaderFooter.ReplaceText Out.doc");
Example:
Shows how you can enumerate through children of a composite node and detect types of the children nodes.// Open a document. Document doc = new Document(getMyDir() + "Section.BodyNodeType.doc"); // Get the first section in the document. Section section = doc.getFirstSection(); // A Section is a composite node and therefore can contain child nodes. // Section can contain only Body and HeaderFooter nodes. for (Node node : (NodeCollection<Node>)section.getChildNodes()) { // Every node has the NodeType property. switch (node.getNodeType()) { case NodeType.BODY: { // If the node type is Body, we can cast the node to the Body class. Body body = (Body)node; // Write the content of the main story of the section to the console. System.out.println("*** Body ***"); System.out.println(body.getText()); break; } case NodeType.HEADER_FOOTER: { // If the node type is HeaderFooter, we can cast the node to the HeaderFooter class. HeaderFooter headerFooter = (HeaderFooter)node; // Write the content of the header footer to the console. System.out.println("*** HeaderFooter ***"); System.out.println(headerFooter.getHeaderFooterType()); System.out.println(headerFooter.getText()); break; } default: { // Other types of nodes never occur inside a Section node. throw new Exception("Unexpected node type in a section."); } } }
getLastSection | |
public Section getLastSection() |
null
if there are no sections.
getSaveOptions/setSaveOptions | |
public SaveOptions getSaveOptions() / public void setSaveOptions(SaveOptions value) |
Example:
Shows how to set save options before saving a document.Document doc = new Document(getMyDir() + "Document.doc"); // Set an option to export form fields as plain text, not as HTML input elements. doc.getSaveOptions().setHtmlExportTextInputFormFieldAsText(true); doc.save(getMyDir() + "Document.SaveWithOptions Out.html");
getViewOptions | |
public ViewOptions getViewOptions() |
Example:
The following code shows how to make sure the document is displayed at 50% zoom when opened in Microsoft Word.Document doc = new Document(getMyDir() + "Document.doc"); doc.getViewOptions().setViewType(ViewType.PAGE_LAYOUT); doc.getViewOptions().setZoomPercent(50); doc.save(getMyDir() + "Document.SetZoom Out.doc");
getWriteProtection | |
public WriteProtection getWriteProtection() |
getCompatibilityOptions | |
public CompatibilityOptions getCompatibilityOptions() |
hasRevisions | |
public boolean hasRevisions() |
hasMacros | |
public boolean hasMacros() |
getVersionsCount | |
public int getVersionsCount() |
Versions in Microsoft Word are accessed via the File/Versions menu. Microsoft Word supports versions only for DOC files.
This property allows to detect if there were document versions stored in this document before it was opened in Aspose.Words. Aspose.Words provides no other support for document versions. If you save this document using Aspose.Words, the document will be saved without versions.
getDefaultTabStop/setDefaultTabStop | |
public double getDefaultTabStop() / public void setDefaultTabStop(double value) |
Example:
Changes default tab positions for the document and inserts text with some tab characters.DocumentBuilder builder = new DocumentBuilder(); // Set default tab stop to 72 points (1 inch). builder.getDocument().setDefaultTabStop(72); builder.writeln("Hello" + ControlChar.TAB + "World!"); builder.writeln("Hello" + ControlChar.TAB_CHAR + "World!");
getVariables | |
public VariableCollection getVariables() |
Example:
Shows how to enumerate over document variables.Document doc = new Document(getMyDir() + "Document.doc"); for (Map.Entry entry : doc.getVariables()) { String name = entry.getKey().toString(); String value = entry.getValue().toString(); // Do something useful. System.out.println(MessageFormat.format("Name: {0}, Value: {1}", name, value)); }
getGlossaryDocument/setGlossaryDocument | |
public GlossaryDocument getGlossaryDocument() / public void setGlossaryDocument(GlossaryDocument value) |
This property returns null
if the document does not have a glossary document.
You can add a glossary document to a document by creating a
getOriginalFileName | |
public java.lang.String getOriginalFileName() |
Returns null if the document was loaded from a stream or created blank.
getOriginalLoadFormat | |
public int getOriginalLoadFormat() |
If you created a new blank document, returns the
getFootnoteOptions | |
public FootnoteOptions getFootnoteOptions() |
getEndnoteOptions | |
public FootnoteOptions getEndnoteOptions() |
getFontInfos | → inherited from DocumentBase |
public FontInfoCollection getFontInfos() |
This collection of font definitions is loaded as is from the document. Font definitions might be optional, missing or incomplete in some documents.
Do not rely on this collection to ascertain that a particular font is used in the document. You should only use this collection to get information about fonts that might be used in the document.
getStyles | → inherited from DocumentBase |
public StyleCollection getStyles() |
For more information see the description of the
Example:
Shows how to get access to the collection of styles defined in the document.Document doc = new Document(); StyleCollection styles = doc.getStyles(); for (Style style : styles) System.out.println(style.getName());
Example:
Shows how to create and use a paragraph style with list formatting.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Create a paragraph style and specify some formatting for it. Style style = doc.getStyles().add(StyleType.PARAGRAPH, "MyStyle1"); style.getFont().setSize(24); style.getFont().setName("Verdana"); style.getParagraphFormat().setSpaceAfter(12); // Create a list and make sure the paragraphs that use this style will use this list. style.getListFormat().setList(doc.getLists().add(ListTemplate.BULLET_DEFAULT)); style.getListFormat().setListLevelNumber(0); // Apply the paragraph style to the current paragraph in the document and add some text. builder.getParagraphFormat().setStyle(style); builder.writeln("Hello World: MyStyle1, bulleted."); // Change to a paragraph style that has no list formatting. builder.getParagraphFormat().setStyle(doc.getStyles().get("Normal")); builder.writeln("Hello World: Normal."); builder.getDocument().save(getMyDir() + "Lists.ParagraphStyleBulleted Out.doc");
getLists | → inherited from DocumentBase |
public ListCollection getLists() |
For more information see the description of the
Example:
Shows how to specify list level number when building a list using DocumentBuilder.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Create a numbered list based on one of the Microsoft Word list templates and // apply it to the current paragraph in the document builder. builder.getListFormat().setList(doc.getLists().add(ListTemplate.NUMBER_ARABIC_DOT)); // There are 9 levels in this list, lets try them all. for (int i = 0; i < 9; i++) { builder.getListFormat().setListLevelNumber(i); builder.writeln("Level " + i); } // Create a bulleted list based on one of the Microsoft Word list templates // and apply it to the current paragraph in the document builder. builder.getListFormat().setList(doc.getLists().add(ListTemplate.BULLET_DIAMONDS)); // There are 9 levels in this list, lets try them all. for (int i = 0; i < 9; i++) { builder.getListFormat().setListLevelNumber(i); builder.writeln("Level " + i); } // This is a way to stop list formatting. builder.getListFormat().setList(null); builder.getDocument().save(getMyDir() + "Lists.SpecifyListLevel Out.doc");
isComposite | → inherited from CompositeNode |
public boolean isComposite() |
hasChildNodes | → inherited from CompositeNode |
public boolean hasChildNodes() |
getChildNodes | → inherited from CompositeNode |
public NodeCollection getChildNodes() |
Note, GetChildNodes(NodeType.Any, false)
and creates and returns a new collection every time it is accessed.
If there are no child nodes, this property returns an empty collection.
Example:
Shows how to enumerate immediate children of a CompositeNode using the enumerator provided by the ChildNodes collection.NodeCollection<Node> children = paragraph.getChildNodes(); for (Node child : children) { // Paragraph may contain children of various types such as runs, shapes and so on. if (child.getNodeType() == NodeType.RUN) { // Say we found the node that we want, do something useful. Run run = (Run)child; System.out.println(run.getText()); } }
getFirstChild | → inherited from CompositeNode |
public Node getFirstChild() |
Example:
Shows how to enumerate immediate child nodes of a composite node using NextSibling. In this example we enumerate all paragraphs of a section body.// Get the section that we want to work on. Section section = doc.getSections().get(0); Body body = section.getBody(); // Loop starting from the first child until we reach null. for (Node node = body.getFirstChild(); node != null; node = node.getNextSibling()) { // Output the types of the nodes that we come across. System.out.println(node.getNodeType()); }
Example:
Shows how to efficiently visit all direct and indirect children of a composite node.public void recurseAllNodes() throws Exception { // Open a document. Document doc = new Document(getMyDir() + "Node.RecurseAllNodes.doc"); // Invoke the recursive function that will walk the tree. traverseAllNodes(doc); } /// <summary> /// A simple function that will walk through all children of a specified node recursively /// and print the type of each node to the screen. /// </summary> private void traverseAllNodes(CompositeNode parentNode) { // This is the most efficient way to loop through immediate children of a node. for (Node childNode = parentNode.getFirstChild(); childNode != null; childNode = childNode.getNextSibling()) { // Do some useful work. System.out.println(childNode.getNodeType()); // Recurse into the node if it is a composite node. if (childNode.isComposite()) traverseAllNodes((CompositeNode)childNode); } }
getLastChild | → inherited from CompositeNode |
public Node getLastChild() |
Example:
Demonstrates use of methods of Node and CompositeNode to remove a section before the last section in the document.// Document is a CompositeNode and LastChild returns the last child node in the Document node. // Since the Document can contain only Section nodes, the last child is the last section. Node lastSection = doc.getLastChild(); // Each node knows its next and previous sibling nodes. // Previous sibling of a section is a section before the specified section. // If the node is the first child, PreviousSibling will return null. Node sectionBeforeLast = lastSection.getPreviousSibling(); if (sectionBeforeLast != null) doc.removeChild(sectionBeforeLast);
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.
Example:
Shows how to access the parent node.// Create a new empty document. It has one section. Document doc = new Document(); // The section is the first child node of the document. Node section = doc.getFirstChild(); // The section's parent node is the document. Assert.assertEquals(doc, section.getParentNode());
Example:
Shows that when you create any node, it requires a document that will own the node.// Open a file from disk. Document doc = new Document(); // Creating a new node of any type requires a document passed into the constructor. Paragraph para = new Paragraph(doc); // The new paragraph node does not yet have a parent. Assert.assertNull(para.getParentNode()); // But the paragraph node knows its document. Assert.assertEquals(doc, para.getDocument()); // The fact that a node always belongs to a document allows us to access and modify // properties that reference the document-wide data such as styles or lists. para.getParagraphFormat().setStyleName("Heading 1"); // Now add the paragaph to the main text of the first section. doc.getFirstSection().getBody().appendChild(para); // The paragraph node is now a child of the Body node. Assert.assertNotNull(para.getParentNode());
getDocument | → inherited from Node |
public DocumentBase 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.
Example:
Shows that when you create any node, it requires a document that will own the node.// Open a file from disk. Document doc = new Document(); // Creating a new node of any type requires a document passed into the constructor. Paragraph para = new Paragraph(doc); // The new paragraph node does not yet have a parent. Assert.assertNull(para.getParentNode()); // But the paragraph node knows its document. Assert.assertEquals(doc, para.getDocument()); // The fact that a node always belongs to a document allows us to access and modify // properties that reference the document-wide data such as styles or lists. para.getParagraphFormat().setStyleName("Heading 1"); // Now add the paragaph to the main text of the first section. doc.getFirstSection().getBody().appendChild(para); // The paragraph node is now a child of the Body node. Assert.assertNotNull(para.getParentNode());
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.
Example:
Demonstrates use of methods of Node and CompositeNode to remove a section before the last section in the document.// Document is a CompositeNode and LastChild returns the last child node in the Document node. // Since the Document can contain only Section nodes, the last child is the last section. Node lastSection = doc.getLastChild(); // Each node knows its next and previous sibling nodes. // Previous sibling of a section is a section before the specified section. // If the node is the first child, PreviousSibling will return null. Node sectionBeforeLast = lastSection.getPreviousSibling(); if (sectionBeforeLast != null) doc.removeChild(sectionBeforeLast);
getNextSibling | → inherited from Node |
public Node getNextSibling() |
Example:
Shows how to enumerate immediate child nodes of a composite node using NextSibling. In this example we enumerate all paragraphs of a section body.// Get the section that we want to work on. Section section = doc.getSections().get(0); Body body = section.getBody(); // Loop starting from the first child until we reach null. for (Node node = body.getFirstChild(); node != null; node = node.getNextSibling()) { // Output the types of the nodes that we come across. System.out.println(node.getNodeType()); }
Example:
Shows how to efficiently visit all direct and indirect children of a composite node.public void recurseAllNodes() throws Exception { // Open a document. Document doc = new Document(getMyDir() + "Node.RecurseAllNodes.doc"); // Invoke the recursive function that will walk the tree. traverseAllNodes(doc); } /// <summary> /// A simple function that will walk through all children of a specified node recursively /// and print the type of each node to the screen. /// </summary> private void traverseAllNodes(CompositeNode parentNode) { // This is the most efficient way to loop through immediate children of a node. for (Node childNode = parentNode.getFirstChild(); childNode != null; childNode = childNode.getNextSibling()) { // Do some useful work. System.out.println(childNode.getNodeType()); // Recurse into the node if it is a composite node. if (childNode.isComposite()) traverseAllNodes((CompositeNode)childNode); } }
getRange | → inherited from Node |
public Range getRange() |
Method Detail |
---|
addNodeChangedEventHandler | → inherited from DocumentBase |
public void addNodeChangedEventHandler(NodeChangedEventHandler newListener) |
removeNodeChangedEventHandler | → inherited from DocumentBase |
public void removeNodeChangedEventHandler(NodeChangedEventHandler oldListener) |
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 such as styles and lists.
deepClone | |
public Document deepClone() throws java.lang.Exception |
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.Example:
Shows how to use the Visitor pattern to add new operations to the Aspose.Words object model. In this case we create a simple document converter into a text format.public void toText() throws Exception { // Open the document we want to convert. Document doc = new Document(getMyDir() + "Visitor.ToText.doc"); // Create an object that inherits from the DocumentVisitor class. myDocToTxtWriter myConverter = new myDocToTxtWriter(); // This is the well known Visitor pattern. Get the model to accept a visitor. // The model will iterate through itself by calling the corresponding methods // on the visitor object (this is called visiting). // // Note that every node in the object model has the Accept method so the visiting // can be executed not only for the whole document, but for any node in the document. doc.accept(myConverter); // Once the visiting is complete, we can retrieve the result of the operation, // that in this example, has accumulated in the visitor. System.out.println(myConverter.GetText()); } /// <summary> /// Simple implementation of saving a document in the plain text format. Implemented as a Visitor. /// </summary> public class myDocToTxtWriter extends DocumentVisitor { public myDocToTxtWriter() { mIsSkipText = false; mBuilder = new StringBuilder(); } /// <summary> /// Gets the plain text of the document that was accumulated by the visitor. /// </summary> public String GetText() { return mBuilder.toString(); } /// <summary> /// Called when a Run node is encountered in the document. /// </summary> public int VisitRun(Run run) { AppendText(run.getText()); // Let the visitor continue visiting other nodes. return VisitorAction.CONTINUE; } /// <summary> /// Called when a FieldStart node is encountered in the document. /// </summary> public int VisitFieldStart(FieldStart fieldStart) { // In Microsoft Word, a field code (such as "MERGEFIELD FieldName") follows // after a field start character. We want to skip field codes and output field // result only, therefore we use a flag to suspend the output while inside a field code. // // Note this is a very simplistic implementation and will not work very well // if you have nested fields in a document. mIsSkipText = true; return VisitorAction.CONTINUE; } /// <summary> /// Called when a FieldSeparator node is encountered in the document. /// </summary> public int VisitFieldSeparator(FieldSeparator fieldSeparator) { // Once reached a field separator node, we enable the output because we are // now entering the field result nodes. mIsSkipText = false; return VisitorAction.CONTINUE; } /// <summary> /// Called when a FieldEnd node is encountered in the document. /// </summary> public int VisitFieldEnd(FieldEnd fieldEnd) { // Make sure we enable the output when reached a field end because some fields // do not have field separator and do not have field result. mIsSkipText = false; return VisitorAction.CONTINUE; } /// <summary> /// Called when visiting of a Paragraph node is ended in the document. /// </summary> public int VisitParagraphEnd(Paragraph paragraph) { // When outputting to plain text we output Cr+Lf characters. AppendText(ControlChar.CR_LF); return VisitorAction.CONTINUE; } public int VisitBodyStart(Body body) { // We can detect beginning and end of all composite nodes such as Section, Body, // Table, Paragraph etc and provide custom handling for them. mBuilder.append("*** Body Started ***\r\n"); return VisitorAction.CONTINUE; } public int VisitBodyEnd(Body body) { mBuilder.append("*** Body Ended ***\r\n"); return VisitorAction.CONTINUE; } /// <summary> /// Called when a HeaderFooter node is encountered in the document. /// </summary> public int VisitHeaderFooterStart(HeaderFooter headerFooter) { // Returning this value from a visitor method causes visiting of this // node to stop and move on to visiting the next sibling node. // The net effect in this example is that the text of headers and footers // is not included in the resulting output. return VisitorAction.SKIP_THIS_NODE; } /// <summary> /// Adds text to the current output. Honours the enabled/disabled output flag. /// </summary> private void AppendText(String text) { if (!mIsSkipText) mBuilder.append(text); } private final StringBuilder mBuilder; private boolean mIsSkipText; }
appendDocument | |
public void appendDocument(Document srcDoc, int importFormatMode) throws java.lang.Exception |
srcDoc
- The document to append.importFormatMode
- A detectFileFormat | |
public static int detectFileFormat(java.lang.String fileName) throws java.lang.Exception |
Even if this method detects the document format, it does not guarantee
that the specified document is valid. This method only detects the document format by
reading data that is sufficient for detection. To fully verify that a document is valid
you need to load the document into a
This method throws
fileName
- The file name.Example:
Shows how to detect a document format.int loadFormat = Document.detectFileFormat(getMyDir() + "Document.doc"); System.out.println("The document format is: " + loadFormat);
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.Example:
Saves a document to a file.doc.save(getMyDir() + "Document.OpenFromFile Out.doc");
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 Example:
Performs a simple insertion of data into merge fields in an existing document.// Open an existing document. Document doc = new Document(getMyDir() + "MailMerge.ExecuteArray.doc"); // Fill the fields in the document with user data. doc.getMailMerge().execute( new String[] {"FullName", "Company", "Address", "Address2", "City"}, new Object[] {"James Bond", "MI5 Headquarters", "Milbank", "", "London"}); // Save the document in Word format. doc.save(getMyDir() + "PersonalizedLetter Out.doc", SaveFormat.DOC);
Example:
Converts from DOC to HTML format.Document doc = new Document(getMyDir() + "Document.doc"); doc.save(getMyDir() + "Document.ConvertToHtml Out.html", SaveFormat.HTML);
save | |
public void save(java.io.OutputStream outputStream, int saveFormat) throws java.lang.Exception |
stream
- Stream where to save the document.saveFormat
-
A Example:
Shows how to save a document to a stream.Document doc = new Document(getMyDir() + "Document.doc"); OutputStream dstStream = new ByteArrayOutputStream(); doc.save(dstStream, SaveFormat.DOCX);
ensureMinimum | |
public void ensureMinimum() throws java.lang.Exception |
acceptAllRevisions | |
public void acceptAllRevisions() throws java.lang.Exception |
Example:
Shows how to accept all tracking changes in the document.Document doc = new Document(getMyDir() + "Document.doc"); doc.acceptAllRevisions();
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 already has a protection password, the existing protection password is not changed.
When you protect a document, and the document does not have a protection password, this method assigns a random password that makes it impossible to unprotect the document in Microsoft Word, but you still can unprotect the document in Aspose.Words as it does not require a password when unprotecting.
type
- A Example:
Protects a section so only editing in form fields is possible.// Create a blank document Document doc = new Document(); // Insert two sections with some text DocumentBuilder builder = new DocumentBuilder(doc); builder.writeln("Section 1. Unprotected."); builder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS); builder.writeln("Section 2. Protected."); // Section protection only works when document protection is turned and only editing in form fields is allowed. doc.protect(ProtectionType.ALLOW_ONLY_FORM_FIELDS); // By default, all sections are protected, but we can selectively turn protection off. doc.getSections().get(0).setProtectedForForms(false); builder.getDocument().save(getMyDir() + "Section.Protect Out.doc");
protect | |
public void protect(int type, java.lang.String password) |
When a document is protected, the user can make only limited changes, such as adding annotations, making revisions, or completing a form.
Note that document protection is different from write protection.
Write protection is specified using the
type
- A password
- The password to protect the document with.
Specify null or empty string if you want to protect the document without a password.Example:
Shows how to protect a document.Document doc = new Document(); doc.protect(ProtectionType.ALLOW_ONLY_FORM_FIELDS, "password");
unprotect | |
public void unprotect() |
This method unprotects the document even if it has a protection password.
Note that document protection is different from write protection.
Write protection is specified using the
Example:
Shows how to unprotect any document. Note that the password is not required.doc.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.
updateFields | |
public void updateFields() throws java.lang.Exception |
At the moment updates the results of DOCPROPERTY and DOCVARIABLE fields only.
You can update fields in a specific part of the document
using
joinRunsWithSameFormatting | |
public int joinRunsWithSameFormatting() throws java.lang.Exception |
This is an optimization method. Some documents contain adjacent runs with same formatting. Usually this occurs if a document was intensively edited manually. You can reduce the document size and speed up further processing by joining these runs.
The operation checks every
expandTableStylesToDirectFormatting | |
public void expandTableStylesToDirectFormatting() throws java.lang.Exception |
This method exists because this version of Aspose.Words provides only limited support for table styles (see below). This method might be useful when you load a DOCX or WordprocessingML document that contains tables formatted with table styles and you need to query formatting of tables, cells, paragraphs or text.
importNode | → inherited from DocumentBase |
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 | → inherited from DocumentBase |
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 Example:
Combines two documents into one.public void combineDocuments() throws Exception { // Open the destination document. Document dstDoc = new Document(getMyDir() + "Section.CombineDocuments1.doc"); // Open the source document. Document srcDoc = new Document(getMyDir() + "Section.CombineDocuments2.doc"); // For a twist, let's say I want the second document to start on the same page where // the first document ends. This is controlled by a property of the first section. srcDoc.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS); // Combine the documents, see the function below. appendDoc(dstDoc, srcDoc); // Save the finished document. dstDoc.save(getMyDir() + "Section.CombineDocuments Out.doc"); } /// <summary> /// A useful function that you can use to easily append one document to another. /// </summary> /// <param name="dstDoc">The destination document where to append to.</param> /// <param name="srcDoc">The source document.</param> private void appendDoc(Document dstDoc, Document srcDoc) throws Exception { // Loop through all sections in the source document. // Section nodes are immediate children of the Document node so we can just enumerate the Document. for (Section srcSection : srcDoc.getSections()) { // Because we are copying a section from one document to another, // it is required to import the Section node into the destination document. // This adjusts any document-specific references to styles, lists, etc. // // Importing a node creates a copy of the original node, but the copy // is ready to be inserted into the destination document. Node dstSection = dstDoc.importNode(srcSection, true, ImportFormatMode.KEEP_SOURCE_FORMATTING); // Now the new section node can be appended to the destination document. dstDoc.appendChild(dstSection); } }
getText | → inherited from CompositeNode |
public java.lang.String getText() |
The returned string includes all control and special characters as described in
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.Example:
Shows how to extract images from a document and save them as files.public void extractImagesToFiles() throws Exception { Document doc = new Document(getMyDir() + "Image.SampleImages.doc"); NodeCollection<Shape> shapes = doc.getChildNodes(NodeType.SHAPE, true, false); int imageIndex = 0; for(Shape shape : shapes) { if (shape.hasImage()) { String extension = imageTypeToExtension(shape.getImageData().getImageType()); String imageFileName = MessageFormat.format("Image.ExportImages.{0} Out.{1}", imageIndex, extension); shape.getImageData().save(getMyDir() + imageFileName); imageIndex++; } } } private static String imageTypeToExtension(int imageType) throws Exception { switch (imageType) { case ImageType.BMP: return "bmp"; case ImageType.EMF: return "emf"; case ImageType.JPEG: return "jpeg"; case ImageType.PICT: return "pict"; case ImageType.PNG: return "png"; case ImageType.WMF: return "wmf"; default: throw new Exception("Unknown image type."); } }
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) |
Only expressions with element names are supported at the moment. Expressions that use attribute names are not supported.
xpath
- The XPath expression.Example:
Finds all hyperlinks in a Word document and changes their URL and display name.import com.aspose.words.*; import java.lang.Exception; import java.text.MessageFormat; import java.util.regex.Pattern; import java.util.regex.Matcher; /// <summary> /// Shows how to replace hyperlinks in a Word document. /// </summary> public class ExReplaceHyperlinks extends ExBase { /// <summary> /// Finds all hyperlinks in a Word document and changes their URL and display name. /// </summary> public void replaceHyperlinks() throws Exception { // Specify your document name here. Document doc = new Document(getMyDir() + "ReplaceHyperlinks.doc"); // Hyperlinks in a Word documents are fields, select all field start nodes so we can find the hyperlinks. NodeList fieldStarts = doc.selectNodes("//FieldStart"); for (Node fieldStartNode : fieldStarts) { FieldStart fieldStart = (FieldStart) fieldStartNode; if (fieldStart.getFieldType() == FieldType.FIELD_HYPERLINK) { // The field is a hyperlink field, use the "facade" class to help to deal with the field. Hyperlink hyperlink = new Hyperlink(fieldStart); // Some hyperlinks can be local (links to bookmarks inside the document), ignore these. if (hyperlink.isLocal()) continue; // The Hyperlink class allows to set the target URL and the display name // of the link easily by setting the properties. hyperlink.setTarget(NewUrl); hyperlink.setName(NewName); } } doc.save(getMyDir() + "ReplaceHyperlinks Out.doc"); } private final String NewUrl = "http://www.aspose.com"; private final String NewName = "Aspose - The .NET & Java Component Publisher"; } /// <summary> /// This "facade" class makes it easier to work with a hyperlink field in a Word document. /// /// A hyperlink is represented by a HYPERLINK field in a Word document. A field in Aspose.Words /// consists of several nodes and it might be difficult to work with all those nodes directly. /// Note this is a simple implementation and will work only if the hyperlink code and name /// each consist of one Run only. /// /// [FieldStart][Run - field code][FieldSeparator][Run - field result][FieldEnd] /// /// The field code contains a string in one of these formats: /// HYPERLINK "url" /// HYPERLINK \l "bookmark name" /// /// The field result contains text that is displayed to the user. /// </summary> class Hyperlink { Hyperlink(FieldStart fieldStart) throws Exception { if (fieldStart == null) throw new Exception("Argument 'fieldStart' is null"); if (fieldStart.getFieldType() != FieldType.FIELD_HYPERLINK) throw new Exception("Field start type must be FieldHyperlink."); mFieldStart = fieldStart; // Find the field separator node. mFieldSeparator = findNextSibling(mFieldStart, NodeType.FIELD_SEPARATOR); if (mFieldSeparator == null) throw new Exception("Cannot find field separator."); // Find the field end node. Normally field end will always be found, but in the example document // there happens to be a paragraph break included in the hyperlink and this puts the field end // in the next paragraph. It will be much more complicated to handle fields which span several // paragraphs correctly, but in this case allowing field end to be null is enough for our purposes. mFieldEnd = findNextSibling(mFieldSeparator, NodeType.FIELD_END); // Field code looks something like [ HYPERLINK "http:\\www.myurl.com" ], but it can consist of several runs. String fieldCode = getTextSameParent(mFieldStart.getNextSibling(), mFieldSeparator); Matcher match = gRegex.matcher(fieldCode.trim()); if (match.matches()) { mIsLocal = match.group(1) != null; //The link is local if \l is present in the field code. mTarget = match.group(2); } } /// <summary> /// Gets or sets the display name of the hyperlink. /// </summary> public String getName() throws Exception { return getTextSameParent(mFieldSeparator, mFieldEnd); } public void setName(String value) throws Exception { // Hyperlink display name is stored in the field result which is a Run // node between field separator and field end. Run fieldResult = (Run)mFieldSeparator.getNextSibling(); fieldResult.setText(value); // But sometimes the field result can consist of more than one run, delete these runs. removeSameParent(fieldResult.getNextSibling(), mFieldEnd); } /// <summary> /// Gets or sets the target url or bookmark name of the hyperlink. /// </summary> public String getTarget() { return mTarget; } public void setTarget(String value) throws Exception { mTarget = value; updateFieldCode(); } /// <summary> /// True if the hyperlink's target is a bookmark inside the document. False if the hyperlink is a url. /// </summary> public boolean isLocal() { return mIsLocal; } public void isLocal(boolean value) throws Exception { mIsLocal = value; updateFieldCode(); } private void updateFieldCode() throws Exception { // Field code is stored in a Run node between field start and field separator. Run fieldCode = (Run)mFieldStart.getNextSibling(); fieldCode.setText(MessageFormat.format("HYPERLINK {0}\"{1}\"", ((mIsLocal) ? "\\l " : ""), mTarget)); // But sometimes the field code can consist of more than one run, delete these runs. removeSameParent(fieldCode.getNextSibling(), mFieldSeparator); } /// <summary> /// Goes through siblings starting from the start node until it finds a node of the specified type or null. /// </summary> private static Node findNextSibling(Node startNode, int nodeType) { for (Node node = startNode; node != null; node = node.getNextSibling()) { if (node.getNodeType() == nodeType) return node; } return null; } /// <summary> /// Retrieves text from start up to but not including the end node. /// </summary> private static String getTextSameParent(Node startNode, Node endNode) throws Exception { if ((endNode != null) && (startNode.getParentNode() != endNode.getParentNode())) throw new Exception("Start and end nodes are expected to have the same parent."); StringBuilder builder = new StringBuilder(); for (Node child = startNode; child != endNode; child = child.getNextSibling()) builder.append(child.getText()); return builder.toString(); } /// <summary> /// Removes nodes from start up to but not including the end node. /// Start and end are assumed to have the same parent. /// </summary> private static void removeSameParent(Node startNode, Node endNode) throws Exception { if ((endNode != null) && (startNode.getParentNode() != endNode.getParentNode())) throw new Exception("Start and end nodes are expected to have the same parent."); Node curChild = startNode; while ((curChild != null) && (curChild != endNode)) { Node nextChild = curChild.getNextSibling(); curChild.remove(); curChild = nextChild; } } private final Node mFieldStart; private final Node mFieldSeparator; private final Node mFieldEnd; private boolean mIsLocal; private String mTarget; /// <summary> /// RK I am notoriously bad at regexes. It seems I don't understand their way of thinking. /// </summary> private static Pattern gRegex = Pattern.compile( "\\S+" + // one or more non spaces HYPERLINK or other word in other languages "\\s+" + // one or more spaces "(?:\"\"\\s+)?" + // non capturing optional "" and one or more spaces, found in one of the customers files. "(\\\\l\\s+)?" + // optional \l flag followed by one or more spaces "\"" + // one apostrophe "([^\"]+)" + // one or more chars except apostrophe (hyperlink target) "\"" // one closing apostrophe ); }
selectSingleNode | → inherited from CompositeNode |
public Node selectSingleNode(java.lang.String xpath) |
Only expressions with element names are supported at the moment. Expressions that use attribute names are not supported.
xpath
- The XPath expression.iterator | → inherited from CompositeNode |
public java.util.Iterator iterator() |
Example:
Shows how to enumerate immediate children of a CompositeNode using the enumerator provided by the ChildNodes collection.NodeCollection<Node> children = paragraph.getChildNodes(); for (Node child : children) { // Paragraph may contain children of various types such as runs, shapes and so on. if (child.getNodeType() == NodeType.RUN) { // Say we found the node that we want, do something useful. Run run = (Run)child; System.out.println(run.getText()); } }
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.Example:
Creates a simple document from scratch using the Aspose.Words object model.// Create an "empty" document. Note that like in Microsoft Word, // the empty document has one section, body and one paragraph in it. Document doc = new Document(); // This truly makes the document empty. No sections (not possible in Microsoft Word). doc.removeAllChildren(); // Create a new section node. // Note that the section has not yet been added to the document, // but we have to specify the parent document. Section section = new Section(doc); // Append the section to the document. doc.appendChild(section); // Lets set some properties for the section. section.getPageSetup().setSectionStart(SectionStart.NEW_PAGE); section.getPageSetup().setPaperSize(PaperSize.LETTER); // The section that we created is empty, lets populate it. The section needs at least the Body node. Body body = new Body(doc); section.appendChild(body); // The body needs to have at least one paragraph. // Note that the paragraph has not yet been added to the document, // but we have to specify the parent document. // The parent document is needed so the paragraph can correctly work // with styles and other document-wide information. Paragraph para = new Paragraph(doc); body.appendChild(para); // We can set some formatting for the paragraph para.getParagraphFormat().setStyleName("Heading 1"); para.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER); // So far we have one empty pararagraph in the document. // The document is valid and can be saved, but lets add some text before saving. // Create a new run of text and add it to our paragraph. Run run = new Run(doc); run.setText("Hello World!"); run.getFont().setColor(Color.RED); para.appendChild(run); // As a matter of interest, you can retrieve text of the whole document and // see that \u000c is automatically appended. \u000c is the end of section character. Assert.assertEquals("Hello World!\u000c", doc.getText()); // Save the document. doc.save(getMyDir() + "Section.CreateFromScratch Out.doc");
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.Example:
Demonstrates use of methods of Node and CompositeNode to remove a section before the last section in the document.// Document is a CompositeNode and LastChild returns the last child node in the Document node. // Since the Document can contain only Section nodes, the last child is the last section. Node lastSection = doc.getLastChild(); // Each node knows its next and previous sibling nodes. // Previous sibling of a section is a section before the specified section. // If the node is the first child, PreviousSibling will return null. Node sectionBeforeLast = lastSection.getPreviousSibling(); if (sectionBeforeLast != null) doc.removeChild(sectionBeforeLast);
removeAllChildren | → inherited from CompositeNode |
public void removeAllChildren() throws java.lang.Exception |
Example:
Creates a simple document from scratch using the Aspose.Words object model.// Create an "empty" document. Note that like in Microsoft Word, // the empty document has one section, body and one paragraph in it. Document doc = new Document(); // This truly makes the document empty. No sections (not possible in Microsoft Word). doc.removeAllChildren(); // Create a new section node. // Note that the section has not yet been added to the document, // but we have to specify the parent document. Section section = new Section(doc); // Append the section to the document. doc.appendChild(section); // Lets set some properties for the section. section.getPageSetup().setSectionStart(SectionStart.NEW_PAGE); section.getPageSetup().setPaperSize(PaperSize.LETTER); // The section that we created is empty, lets populate it. The section needs at least the Body node. Body body = new Body(doc); section.appendChild(body); // The body needs to have at least one paragraph. // Note that the paragraph has not yet been added to the document, // but we have to specify the parent document. // The parent document is needed so the paragraph can correctly work // with styles and other document-wide information. Paragraph para = new Paragraph(doc); body.appendChild(para); // We can set some formatting for the paragraph para.getParagraphFormat().setStyleName("Heading 1"); para.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER); // So far we have one empty pararagraph in the document. // The document is valid and can be saved, but lets add some text before saving. // Create a new run of text and add it to our paragraph. Run run = new Run(doc); run.setText("Hello World!"); run.getFont().setColor(Color.RED); para.appendChild(run); // As a matter of interest, you can retrieve text of the whole document and // see that \u000c is automatically appended. \u000c is the end of section character. Assert.assertEquals("Hello World!\u000c", doc.getText()); // Save the document. doc.save(getMyDir() + "Section.CreateFromScratch Out.doc");
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 object type of the ancestor to retrieve.getAncestor | → inherited from Node |
public Node getAncestor(int ancestorType) |
ancestorType
- A remove | → inherited from Node |
public void remove() throws java.lang.Exception |
Example:
Shows how to remove all nodes of a specific type from a composite node. In this example we remove tables from a section body.// Get the section that we want to work on. Section section = doc.getSections().get(0); Body body = section.getBody(); // Select the first child node in the body. Node curNode = body.getFirstChild(); while (curNode != null) { // Save the pointer to the next sibling node because if the current // node is removed from the parent in the next step, we will have // no way of finding the next node to continue the loop. Node nextNode = curNode.getNextSibling(); // A section body can contain Paragraph and Table nodes. // If the node is a Table, remove it from the parent. if (curNode.getNodeType() == NodeType.TABLE) curNode.remove(); // Continue going through child nodes until null (no more siblings) is reached. curNode = nextNode; }
nextPreOrder | → inherited from Node |
public Node nextPreOrder(Node rootNode) |
rootNode
- The top node (limit) of traversal.Example:
Shows how to delete all images from a document using pre-order tree traversal.Node curNode = doc; while (curNode != null) { Node nextNode = curNode.nextPreOrder(doc); if (curNode.getNodeType() == NodeType.SHAPE) { Shape shape = (Shape)curNode; // Several shape types can have an image including image shapes and OLE objects. if (shape.canHaveImage()) shape.remove(); } curNode = nextNode; }
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
(or any other) combination of characters to mark ends of paragraphs in the resulting string.
It produces a different result from
getDirectSectionAttr | |
public java.lang.Object getDirectSectionAttr(int key) |
fetchInheritedSectionAttr | |
public java.lang.Object fetchInheritedSectionAttr(int key) throws java.lang.Exception |
setSectionAttr | |
public void setSectionAttr(int key, java.lang.Object value) |
clearSectionAttrs | |
public void clearSectionAttrs() |