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
To draw document pages directly onto a Graphics object use
To print the document, use one 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: Example:
Document doc = new Document(getMyDir() + "Document.doc");
doc.save(getArtifactsDir() + "Document.Doc2PdfSave.pdf");
Document doc = new Document(getMyDir() + "MailMerge.ExecuteDataTable.doc");
// This example creates a table, but you would normally load table from a database.
DataTable table = new DataTable("Test");
table.getColumns().add("CustomerName");
table.getColumns().add("Address");
table.getRows().add(new Object[]{"Thomas Hardy", "120 Hanover Sq., London"});
table.getRows().add(new Object[]{"Paolo Accorti", "Via Monte Bianco 34, Torino"});
// Field values from the table are inserted into the mail merge fields found in the document.
doc.getMailMerge().execute(table);
doc.save(getArtifactsDir() + "MailMerge.ExecuteDataTable.doc");
// Open a fresh copy of our document to perform another mail merge.
doc = new Document(getMyDir() + "MailMerge.ExecuteDataTable.doc");
// We can also source values for a mail merge from a single row in the table
doc.getMailMerge().execute(table.getRows().get(1));
doc.save(getArtifactsDir() + "MailMerge.ExecuteDataTable.OneRow.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, LoadOptions loadOptions)
Opens an existing document from a file. Allows to specify additional options such as an encryption password. |
Document(java.io.InputStream stream)
Opens an existing document from a stream. Automatically detects the file format. |
Document(java.io.InputStream stream, LoadOptions loadOptions)
Opens an existing document from a stream. Allows to specify additional options such as an encryption password. |
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. | ||
boolean | getAutomaticallyUpdateSyles() | |
void | setAutomaticallyUpdateSyles(boolean value) | |
Gets or sets a flag indicating whether the styles in the document are updated to match the styles in the attached template each time the document is opened in MS Word. | ||
Shape | getBackgroundShape() | → inherited from DocumentBase |
void | setBackgroundShape(Shape value) | |
Gets or sets the background shape of the document. Can be null. | ||
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 access to document compatibility options (that is, the user preferences entered on the Compatibility tab of the Options dialog in Word). | ||
int | getCompliance() | |
Gets the OOXML compliance version determined from the loaded document content. Makes sense only for OOXML documents. The value of the property is OoxmlCompliance integer constant. | ||
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. | ||
CustomXmlPartCollection | getCustomXmlParts() | |
void | setCustomXmlParts(CustomXmlPartCollection value) | |
Gets or sets the collection of Custom XML Data Storage Parts. | ||
double | getDefaultTabStop() | |
void | setDefaultTabStop(double value) | |
Gets or sets the interval (in points) between the default tab stops. | ||
DigitalSignatureCollection | getDigitalSignatures() | |
Gets the collection of digital signatures for this document and their validation results. | ||
DocumentBase | getDocument() | → inherited from Node |
Gets the document to which this node belongs. | ||
EndnoteOptions | getEndnoteOptions() | |
Provides options that control numbering and positioning of endnotes in this document. | ||
FieldOptions | getFieldOptions() | |
Gets a FieldOptions object that represents options to control field handling in the 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. | ||
FontSettings | getFontSettings() | |
void | setFontSettings(FontSettings value) | |
Gets or sets document font settings. | ||
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. | ||
HyphenationOptions | getHyphenationOptions() | |
Provides access to document hyphenation options. | ||
boolean | isComposite() | → inherited from CompositeNode |
Returns true as this node can have child nodes. | ||
Node | getLastChild() | → inherited from CompositeNode |
Gets the last child of the node. | ||
Section | getLastSection() | |
Gets the last section in the document. | ||
LayoutOptions | getLayoutOptions() | |
Gets a LayoutOptions object that represents options to control the layout process of this 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. | ||
MailMergeSettings | getMailMergeSettings() | |
void | setMailMergeSettings(MailMergeSettings value) | |
Gets or sets the object that contains all of the mail merge information for a document. | ||
Node | getNextSibling() | → inherited from Node |
Gets the node immediately following this node. | ||
INodeChangingCallback | getNodeChangingCallback() | → inherited from DocumentBase |
void | setNodeChangingCallback(INodeChangingCallback value) | |
Called when a node is inserted or removed in the document. | ||
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. | ||
CustomPartCollection | getPackageCustomParts() | |
void | setPackageCustomParts(CustomPartCollection value) | |
Gets or sets the collection of custom parts (arbitrary content) that are linked to the OOXML package using "unknown relationships". | ||
java.awt.Color | getPageColor() | → inherited from DocumentBase |
void | setPageColor(java.awt.Color value) | |
Gets or sets the page color of the document. This property is a simpler version of |
||
int | getPageCount() | |
Gets the number of pages in the document as calculated by the most recent page layout operation. | ||
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. | ||
boolean | getRemovePersonalInformation() | |
void | setRemovePersonalInformation(boolean value) | |
Gets or sets a flag indicating that Microsoft Word will remove all user information from comments, revisions and document properties upon saving the document. | ||
IResourceLoadingCallback | getResourceLoadingCallback() | → inherited from DocumentBase |
void | setResourceLoadingCallback(IResourceLoadingCallback value) | |
Allows to control how external resources are loaded. | ||
RevisionCollection | getRevisions() | |
Gets a collection of revisions (tracked changes) that exist in this document. | ||
int | getRevisionsView() | |
void | setRevisionsView(int value) | |
Gets or sets a value indicating whether to work with the original or revised version of a document. The value of the property is RevisionsView integer constant. | ||
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. | ||
Theme | getTheme() | |
Gets the |
||
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. | ||
VbaProject | getVbaProject() | |
Gets |
||
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. | ||
IWarningCallback | getWarningCallback() | → inherited from DocumentBase |
void | setWarningCallback(IWarningCallback value) | |
Called during various document processing procedures when an issue is detected that might result in data or formatting fidelity loss. | ||
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. | ||
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 | appendDocument(Document srcDoc, int importFormatMode, ImportFormatOptions importFormatOptions) | |
Appends the specified document to the end of this document. | ||
void | cleanup() | |
Cleans unused styles and lists from the document. | ||
void | cleanup(CleanupOptions options) | |
Cleans unused styles and lists from the document depending on given |
||
void | compare(Document document, java.lang.String author, java.util.Date dateTime) | |
Compares this document with another document producing changes as number of edit and format revisions |
||
void | compare(Document document, java.lang.String author, java.util.Date dateTime, CompareOptions options) | |
Compares this document with another document producing changes as a number of edit and format revisions |
||
void | copyStylesFromTemplate(Document template) | |
Copies styles from the specified template to a document. | ||
void | copyStylesFromTemplate(java.lang.String template) | |
Copies styles from the specified template to a document. | ||
Document | deepClone() | |
Performs a deep copy of the |
||
Node | deepClone(boolean isCloneChildren) | → inherited from Node |
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. | ||
CompositeNode | getAncestor(int ancestorType) | → inherited from Node |
Gets the first ancestor of the specified |
||
CompositeNode | 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. | ||
PageInfo | getPageInfo(int pageIndex) | |
Gets the page size, orientation and other information about a page that might be useful for printing or rendering. | ||
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<Node> | 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. | ||
void | normalizeFieldTypes() | |
Changes field type values |
||
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 | print() | |
Prints the whole document to the default printer. | ||
void | print(java.lang.String printerName) | |
Print the whole document to the specified printer, using the standard (no User Interface) print controller. | ||
void | print(javax.print.attribute.AttributeSet printerSettings) | |
Prints the document according to the specified printer settings, using the standard (no User Interface) print controller. | ||
void | print(javax.print.attribute.AttributeSet printerSettings, java.lang.String documentName) | |
Prints the document according to the specified printer settings, using the standard (no User Interface) print controller and a document name. | ||
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 | removeExternalSchemaReferences() | |
Removes external XML schema references from this document. | ||
void | removeMacros() | |
Removes all macros (the VBA project) as well as toolbars and command customizations from the document. | ||
void | removeSmartTags() | → inherited from CompositeNode |
Removes all |
||
java.awt.geom.Point2D.Float | renderToScale(int pageIndex, java.awt.Graphics2D graphics, float x, float y, float scale) | |
Renders a document page into a |
||
float | renderToSize(int pageIndex, java.awt.Graphics2D graphics, float x, float y, float width, float height) | |
void | save(java.io.OutputStream outputStream, SaveOptions saveOptions) | |
Saves the document to a stream using the specified save options. | ||
void | save(java.io.OutputStream outputStream, int saveFormat) | |
Saves the document to a stream using the specified format. | ||
SaveOutputParameters | save(java.lang.String fileName) | |
Saves the document to a file. Automatically determines the save format from the extension. | ||
SaveOutputParameters | save(java.lang.String fileName, SaveOptions saveOptions) | |
Saves the document to a file using the specified save options. | ||
SaveOutputParameters | save(java.lang.String fileName, int saveFormat) | |
Saves the document to a file in the specified 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 | startTrackRevisions(java.lang.String author) | |
Starts automatically marking all further changes you make to the document programmatically as revision changes. | ||
void | startTrackRevisions(java.lang.String author, java.util.Date dateTime) | |
Starts automatically marking all further changes you make to the document programmatically as revision changes. | ||
void | stopTrackRevisions() | |
Stops automatic marking of document changes as revisions. | ||
java.lang.String | toString(SaveOptions saveOptions) | → inherited from Node |
Exports the content of the node into a string using the specified save options. | ||
java.lang.String | toString(int saveFormat) | → inherited from Node |
Exports the content of the node into a string in the specified format. | ||
void | unlinkFields() | |
Unlinks fields in the whole document. | ||
void | unprotect() | |
Removes protection from the document regardless of the password. | ||
boolean | unprotect(java.lang.String password) | |
Removes protection from the document if a correct password is specified. | ||
void | updateFields() | |
Updates the values of fields in the whole document. | ||
void | updateListLabels() | |
Updates list labels for all list items in the document. | ||
void | updatePageLayout() | |
Rebuilds the page layout of the document. | ||
void | updateTableLayout() | |
Updates widths of cells and tables in the document according to their preferred widths and content. You do not need to call this method if the tables appear correct in the output document. | ||
void | updateThumbnail() | |
Updates |
||
void | updateThumbnail(ThumbnailGeneratingOptions options) | |
Updates |
||
void | updateWordCount() | |
Updates word count properties of the document. | ||
void | updateWordCount(boolean updateLinesCount) | |
Updates word count properties of the document, optionally updates |
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().getFirstParagraph().appendChild(run);
Example:
Shows how to create a blank document. Note the blank document contains one section and one paragraph.Document doc = new Document();
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, LoadOptions loadOptions) throws java.lang.Exception
fileName
- File name of the document to open.loadOptions
- Additional options to use when loading a document. Can be null.Example:
Explicitly loads a document as HTML without automatic file format detection.LoadOptions loadOptions = new LoadOptions(); loadOptions.setLoadFormat(com.aspose.words.LoadFormat.HTML); Document doc = new Document(getMyDir() + "Document.LoadFormat.html", loadOptions);
Example:
Loads a Microsoft Word document encrypted with a password.Document doc = new Document(getMyDir() + "Document.LoadEncrypted.doc", new LoadOptions("qwerty"));
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:
Retrieves a document from a URL and saves it to disk in a different format.// This is the URL pointing to where to find the document. URL url = new URL("http://www.aspose.com/demos/.net-components/aspose.words/csharp/general/Common/Documents/DinnerInvitationDemo.doc"); // The easiest way to load our document from the internet is make use of the URLConnection class. URLConnection webClient = url.openConnection(); // Download the bytes from the location referenced by the URL. InputStream inputStream = webClient.getInputStream(); // Convert the input stream to a byte array. int pos; ByteArrayOutputStream bos = new ByteArrayOutputStream(); while ((pos = inputStream.read()) != -1) bos.write(pos); byte[] dataBytes = bos.toByteArray(); // Wrap the bytes representing the document in memory into a stream object. ByteArrayInputStream byteStream = new ByteArrayInputStream(dataBytes); // Load this memory stream into a new Aspose.Words Document. // The file format of the passed data is inferred from the content of the bytes itself. // You can load any document format supported by Aspose.Words in the same way. Document doc = new Document(byteStream); // Convert the document to any format supported by Aspose.Words. doc.save(getArtifactsDir() + "Document.OpenFromWeb.docx");
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
public Document(java.io.InputStream stream, LoadOptions loadOptions) throws java.lang.Exception
The document must be stored at the beginning of the stream.
stream
- The stream where to load the document from.loadOptions
- Additional options to use when loading a document. Can be null.Example:
Shows how to insert the HTML contents from a web page into a new document.// The url of the page to load URL url = new URL("http://www.aspose.com/"); // The easiest way to load our document from the internet is make use of the URLConnection class. URLConnection webClient = url.openConnection(); // Download the bytes from the location referenced by the URL. InputStream inputStream = webClient.getInputStream(); // Convert the input stream to a byte array. int pos; ByteArrayOutputStream bos = new ByteArrayOutputStream(); while ((pos = inputStream.read()) != -1) bos.write(pos); byte[] dataBytes = bos.toByteArray(); // Wrap the bytes representing the document in memory into a stream object. ByteArrayInputStream byteStream = new ByteArrayInputStream(dataBytes); // The baseUri property should be set to ensure any relative img paths are retrieved correctly. LoadOptions options = new LoadOptions(LoadFormat.HTML, "", url.getPath()); // Load the HTML document from stream and pass the LoadOptions object. Document doc = new Document(byteStream, options); // Save the document to disk. // The extension of the filename can be changed to save the document into other formats. e.g PDF, DOCX, ODT, RTF. doc.save(getArtifactsDir() + "Document.HtmlPageFromWebpage.doc");
Example:
Opens an HTML document with images from a stream using 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 HTML document can be found. LoadOptions loadOptions = new LoadOptions(); loadOptions.setBaseUri(getImageDir()); Document doc = new Document(stream, loadOptions); // You can close the stream now, it is no longer needed because the document is in memory. stream.close(); // Save in the DOC format. doc.save(getArtifactsDir() + "Document.OpenFromStreamWithBaseUri.doc");
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, new LoadOptions("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(getArtifactsDir() + "Document.DetachTemplate.doc");
Example:
Shows how to set a default .docx document template.Document doc = new Document(); // If we set this flag to true while not having a template attached to the document, // there will be no effect because there is no template document to draw style changes from doc.setAutomaticallyUpdateSyles(true); Assert.assertTrue(doc.getAttachedTemplate().isEmpty()); // We can set a default template document filename in a SaveOptions object to make it apply to // all documents we save with it that have no AttachedTemplate value SaveOptions options = SaveOptions.createSaveOptions("Document.DefaultTemplate.docx"); options.setDefaultTemplate(getMyDir() + "Document.BusinessBrochureTemplate.dotx"); doc.save(getArtifactsDir() + "Document.DefaultTemplate.docx", options);
getAutomaticallyUpdateSyles/setAutomaticallyUpdateSyles | |
public boolean getAutomaticallyUpdateSyles() / public void setAutomaticallyUpdateSyles(boolean value) |
Example:
Shows how to update a document's styles based on its template.Document doc = new Document(); // Empty Microsoft Word documents by default come with an attached template called "Normal.dotm" // There is no default template for Aspose Words documents Assert.assertEquals("", doc.getAttachedTemplate()); // For AutomaticallyUpdateStyles to have any effect, we need a document with a template // We can make a document with word and open it // Or we can attach a template from our file system, as below doc.setAttachedTemplate(getMyDir() + "Document.BusinessBrochureTemplate.dotx"); Assert.assertTrue(doc.getAttachedTemplate().endsWith("Document.BusinessBrochureTemplate.dotx")); // Any changes to the styles in this template will be propagated to those styles in the document doc.setAutomaticallyUpdateSyles(true); doc.save(getArtifactsDir() + "Document.TemplateStylesUpdating.docx");
getBackgroundShape/setBackgroundShape | → inherited from DocumentBase |
public Shape getBackgroundShape() / public void setBackgroundShape(Shape value) |
Microsoft Word allows only a shape that has its
Microsoft Word supports only the fill properties of a background shape. All other properties are ignored.
Setting this property to a non-null value will also set the
Example:
Shows how to set the background shape of a document.Document doc = new Document(); Assert.assertNull(doc.getBackgroundShape()); // A background shape can only be a rectangle // We will set the colour of this rectangle to light blue Shape shapeRectangle = new Shape(doc, ShapeType.RECTANGLE); doc.setBackgroundShape(shapeRectangle); // This rectangle covers the entire page in the output document // We can also do this by setting doc.PageColor shapeRectangle.setFillColor(Color.blue); doc.save(getArtifactsDir() + "DocumentBase.BackgroundShapeFlatColor.docx"); // Setting the image will override the flat background colour with the image shapeRectangle.getImageData().setImage(getImageDir() + "Watermark.png"); Assert.assertTrue(doc.getBackgroundShape().hasImage()); // This image is a photo with a white background // To make it suitable as a watermark, we will need to do some image processing // The default values for these variables are 0.5, so here we are lowering the contrast and increasing the brightness shapeRectangle.getImageData().setContrast(0.2); shapeRectangle.getImageData().setBrightness(0.7); // Microsoft Word does not support images in background shapes, so even though we set the background as an image, // the output will show a light blue background like before // However, we can see our watermark in an output pdf doc.save(getArtifactsDir() + "DocumentBase.BackgroundShapeWatermark.pdf");
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}", doc.getOriginalFileName())); System.out.println("2. Built-in Properties"); for (DocumentProperty docProperty : doc.getBuiltInDocumentProperties()) System.out.println(MessageFormat.format("{0} : {1}", docProperty.getName(), docProperty.getValue())); System.out.println("3. Custom Properties"); for (DocumentProperty docProperty : doc.getCustomDocumentProperties()) System.out.println(MessageFormat.format("{0} : {1}", docProperty.getName(), docProperty.getValue()));
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 children = paragraph.getChildNodes(); for (Node child : (Iterable<Node>) 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()); } }
getCompatibilityOptions | |
public CompatibilityOptions getCompatibilityOptions() |
Example:
Shows how to optimize our document for different word versions.Document doc = new Document(); CompatibilityOptions co = doc.getCompatibilityOptions(); // Here are some default values Assert.assertEquals(co.getGrowAutofit(), true); Assert.assertEquals(co.getDoNotBreakWrappedTables(), false); Assert.assertEquals(co.getDoNotUseEastAsianBreakRules(), false); Assert.assertEquals(co.getSelectFldWithFirstOrLastChar(), false); Assert.assertEquals(co.getUseWord97LineBreakRules(), false); Assert.assertEquals(co.getUseWord2002TableStyleRules(), true); Assert.assertEquals(co.getUseWord2010TableStyleRules(), false); // This example covers only a small portion of all the compatibility attributes // To see the entire list, in any of the output files go into File > Options > Advanced > Compatibility for... doc.save(getArtifactsDir() + "DefaultCompatibility.docx"); // We can hand pick any value and change it to create a custom compatibility // We can also change a bunch of values at once to suit a defined compatibility scheme with the OptimizeFor method doc.getCompatibilityOptions().optimizeFor(MsWordVersion.WORD_2010); Assert.assertEquals(co.getGrowAutofit(), false); Assert.assertEquals(co.getDoNotBreakWrappedTables(), false); Assert.assertEquals(co.getDoNotUseEastAsianBreakRules(), false); Assert.assertEquals(co.getSelectFldWithFirstOrLastChar(), false); Assert.assertEquals(co.getUseWord97LineBreakRules(), false); Assert.assertEquals(co.getUseWord2002TableStyleRules(), false); Assert.assertEquals(co.getUseWord2010TableStyleRules(), true); doc.save(getArtifactsDir() + "Optimised for Word 2010.docx"); doc.getCompatibilityOptions().optimizeFor(MsWordVersion.WORD_2000); Assert.assertEquals(co.getGrowAutofit(), true); Assert.assertEquals(co.getDoNotBreakWrappedTables(), true); Assert.assertEquals(co.getDoNotUseEastAsianBreakRules(), true); Assert.assertEquals(co.getSelectFldWithFirstOrLastChar(), true); Assert.assertEquals(co.getUseWord97LineBreakRules(), false); Assert.assertEquals(co.getUseWord2002TableStyleRules(), true); Assert.assertEquals(co.getUseWord2010TableStyleRules(), false); doc.save(getArtifactsDir() + "Optimised for Word 2000.docx");
getCompliance | |
public int getCompliance() |
If you created a new blank document or load non OOXML document
returns the
Example:
Shows how to get OOXML compliance version.Document doc = new Document(getMyDir() + "Document.doc"); int compliance = doc.getCompliance();
getCount | → inherited from CompositeNode |
public int getCount() |
Example:
Shows how to add, update and delete child nodes from within a CompositeNode.Document doc = new Document(); // An empty document has one paragraph by default Assert.assertEquals(doc.getFirstSection().getBody().getParagraphs().getCount(), 1); // A paragraph is a composite node because it can contain runs, which are another type of node Paragraph paragraph = doc.getFirstSection().getBody().getFirstParagraph(); Run paragraphText = new Run(doc, "Initial text. "); paragraph.appendChild(paragraphText); // We will place these 3 children into the main text of our paragraph Run run1 = new Run(doc, "Run 1. "); Run run2 = new Run(doc, "Run 2. "); Run run3 = new Run(doc, "Run 3. "); // We initialized them but not in our paragraph yet Assert.assertEquals(paragraph.getText(), "Initial text. " + (char) 12); // Insert run2 before initial paragraph text. This will be at the start of the paragraph paragraph.insertBefore(run2, paragraphText); // Insert run3 after initial paragraph text. This will be at the end of the paragraph paragraph.insertAfter(run3, paragraphText); // Insert run1 before every other child node. run2 was the start of the paragraph, now it will be run1 paragraph.prependChild(run1); Assert.assertEquals(paragraph.getText(), "Run 1. Run 2. Initial text. Run 3. " + (char) 12); Assert.assertEquals(paragraph.getChildNodes(NodeType.ANY, true).getCount(), 4); // Access the child node collection and update/delete children ((Run) paragraph.getChildNodes(NodeType.RUN, true).get(1)).setText("Updated run 2. "); paragraph.getChildNodes(NodeType.RUN, true).remove(paragraphText); Assert.assertEquals(paragraph.getText(), "Run 1. Updated run 2. Run 3. " + (char) 12); Assert.assertEquals(paragraph.getChildNodes(NodeType.ANY, true).getCount(), 3);
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}", doc.getOriginalFileName())); System.out.println("2. Built-in Properties"); for (DocumentProperty docProperty : doc.getBuiltInDocumentProperties()) System.out.println(MessageFormat.format("{0} : {1}", docProperty.getName(), docProperty.getValue())); System.out.println("3. Custom Properties"); for (DocumentProperty docProperty : doc.getCustomDocumentProperties()) System.out.println(MessageFormat.format("{0} : {1}", docProperty.getName(), docProperty.getValue()));
getCustomXmlParts/setCustomXmlParts | |
public CustomXmlPartCollection getCustomXmlParts() / public void setCustomXmlParts(CustomXmlPartCollection value) |
Aspose.Words loads and saves Custom XML Parts into OOXML and DOC documents only.
This property cannot be null
.
Example:
Shows how to create structured document tag with a custom XML data.Document doc = new Document(); // Construct an XML part that contains data and add it to the document's collection // Once the "Developer" tab in Mircosoft Word is enabled, // we can find elements from this collection as well as a couple defaults in the "XML Mapping Pane" String xmlPartId = UUID.randomUUID().toString(); String xmlPartContent = "<root><text>Hello, World!</text></root>"; CustomXmlPart xmlPart = doc.getCustomXmlParts().add(xmlPartId, xmlPartContent); // The data we entered resides in these variables Assert.assertEquals(xmlPart.getData(), xmlPartContent.getBytes()); Assert.assertEquals(xmlPart.getId(), xmlPartId); // XML parts can be referenced by collection index or GUID Assert.assertEquals(doc.getCustomXmlParts().get(0), xmlPart); Assert.assertEquals(doc.getCustomXmlParts().getById(xmlPartId), xmlPart); // Once the part is created, we can add XML schema associations like this xmlPart.getSchemas().add("http://www.w3.org/2001/XMLSchema"); // We can also clone parts and insert them into the collection directly CustomXmlPart xmlPartClone = xmlPart.deepClone(); xmlPartClone.setId(UUID.randomUUID().toString()); doc.getCustomXmlParts().add(xmlPartClone); Assert.assertEquals(doc.getCustomXmlParts().getCount(), 2); // Iterate through collection with an enumerator and print the contents of each part Iterator<CustomXmlPart> enumerator = doc.getCustomXmlParts().iterator(); int index = 0; while (enumerator.hasNext()) { CustomXmlPart customXmlPart = enumerator.next(); System.out.println(MessageFormat.format("XML part index {0}, ID: {1}", index, customXmlPart.getId())); System.out.println(MessageFormat.format("\tContent: {0}", customXmlPart.getData())); index++; } // XML parts can be removed by index doc.getCustomXmlParts().removeAt(1); Assert.assertEquals(doc.getCustomXmlParts().getCount(), 1); // The XML part collection itself can be cloned also CustomXmlPartCollection customXmlParts = doc.getCustomXmlParts().deepClone(); // And all elements can be cleared like this customXmlParts.clear(); // Create a StructuredDocumentTag that will display the contents of our part, // insert it into the document and save the document StructuredDocumentTag sdt = new StructuredDocumentTag(doc, SdtType.PLAIN_TEXT, MarkupLevel.BLOCK); sdt.getXmlMapping().setMapping(xmlPart, "/root[1]/text[1]", ""); doc.getFirstSection().getBody().appendChild(sdt); doc.save(getArtifactsDir() + "SDT.CustomXml.docx");
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!");
getDigitalSignatures | |
public DigitalSignatureCollection getDigitalSignatures() |
This collection contains digital signatures that were loaded from the original document.
These digital signatures will not be saved when you save this
This collection is never null. If the document is not signed, it will contain zero elements.
Example:
Shows how to validate all signatures in a document.// Load the signed document. Document doc = new Document(getMyDir() + "Document.DigitalSignature.docx"); DigitalSignatureCollection digitalSignatureCollection = doc.getDigitalSignatures(); if (digitalSignatureCollection.isValid()) { System.out.println("Signatures belonging to this document are valid"); System.out.println(digitalSignatureCollection.getCount()); System.out.println(digitalSignatureCollection.get(0).getSignatureType()); } else { System.out.println("Signatures belonging to this document are NOT valid"); }
Example:
Shows how to validate each signature in a document and display basic information about the signature.// Load the document which contains signature. Document doc = new Document(getMyDir() + "Document.DigitalSignature.docx"); for (DigitalSignature signature : doc.getDigitalSignatures()) { System.out.println("*** Signature Found ***"); System.out.println("Is valid: " + signature.isValid()); System.out.println("Reason for signing: " + signature.getComments()); // This property is available in MS Word documents only. System.out.println("Signature type: " + DigitalSignatureType.toString(signature.getSignatureType())); System.out.println("Time of signing: " + signature.getSignTime()); System.out.println("Subject name: " + signature.getSubjectName()); System.out.println("Issuer name: " + signature.getIssuerName()); System.out.println("Certificate type: " + signature.getCertificateHolder().getCertificate().getClass().getName()); System.out.println(); }
getDocument | → inherited from Node |
public DocumentBase getDocument() |
The node always belongs to a document even if it has just been created and not yet 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. System.out.println("Paragraph has no parent node: " + (para.getParentNode() == null)); // But the paragraph node knows its document. System.out.println("Both nodes' documents are the same: " + (para.getDocument() == doc)); // 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 paragraph to the main text of the first section. doc.getFirstSection().getBody().appendChild(para); // The paragraph node is now a child of the Body node. System.out.println("Paragraph has a parent node: " + (para.getParentNode() != null));
getEndnoteOptions | |
public EndnoteOptions getEndnoteOptions() |
Example:
Shows how access a document's endnote options and see some of its default values.Document doc = new Document(); Assert.assertEquals(doc.getEndnoteOptions().getStartNumber(), 1); Assert.assertEquals(doc.getEndnoteOptions().getPosition(), EndnotePosition.END_OF_DOCUMENT); Assert.assertEquals(doc.getEndnoteOptions().getNumberStyle(), NumberStyle.LOWERCASE_ROMAN); Assert.assertEquals(doc.getEndnoteOptions().getRestartRule(), FootnoteNumberingRule.DEFAULT);
getFieldOptions | |
public FieldOptions getFieldOptions() |
Example:
Shows how to specify where the locale for date formatting during field update and mail merge is chosen from.// Set the culture used during field update to the culture used by the field. doc.getFieldOptions().setFieldUpdateCultureSource(FieldUpdateCultureSource.FIELD_CODE); doc.getMailMerge().execute(new String[]{"Date2"}, new Object[]{new SimpleDateFormat("yyyy/MM/DD").parse("2011/01/01")});
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.nodeTypeToString(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); } /** * A simple function that will walk through all children of a specified node recursively * and print the type of each node to the screen. */ @Test(enabled = false) public void traverseAllNodes(final 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(Node.nodeTypeToString(childNode.getNodeType())); // Recurse into the node if it is a composite node. if (childNode.isComposite()) { traverseAllNodes((CompositeNode) childNode); } } }
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); FindReplaceOptions options = new FindReplaceOptions(); options.setMatchCase(false); options.setFindWholeWordsOnly(false); footer.getRange().replace("(C) 2006 Aspose Pty Ltd.", "Copyright (C) 2011 by Aspose Pty Ltd.", options); doc.save(getArtifactsDir() + "HeaderFooter.ReplaceText.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 : section) { // 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."); } }
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.
Example:
Shows how to save a document with embedded TrueType fontsDocument doc = new Document(getMyDir() + "Document.docx"); FontInfoCollection fontInfos = doc.getFontInfos(); fontInfos.setEmbedTrueTypeFonts(true); fontInfos.setEmbedSystemFonts(false); fontInfos.setSaveSubsetFonts(false); doc.save(getMyDir() + "/Artifacts/Document.docx");
Example:
Shows how to gather the details of what fonts are present in a document.Document doc = new Document(getMyDir() + "Document.doc"); FontInfoCollection fonts = doc.getFontInfos(); int fontIndex = 1; // The fonts info extracted from this document does not necessarily mean that the fonts themselves are // used in the document. If a font is present but not used then most likely they were referenced at some time // and then removed from the Document. for (FontInfo info : fonts) { // Print out some important details about the font. System.out.println(MessageFormat.format("Font #{0}", fontIndex)); System.out.println(MessageFormat.format("Name: {0}", info.getName())); System.out.println(MessageFormat.format("IsTrueType: {0}", info.isTrueType())); fontIndex++; }
getFontSettings/setFontSettings | |
public FontSettings getFontSettings() / public void setFontSettings(FontSettings value) |
This property allows to specify font settings per document. If set to null, default static font settings
The default value is null.
Example:
Shows how to define alternative fonts if original does not existFontSettings fontSettings = new FontSettings(); fontSettings.getSubstitutionSettings().getTableSubstitution().addSubstitutes("Times New Roman", "Slab", "Arvo");
getFootnoteOptions | |
public FootnoteOptions getFootnoteOptions() |
Example:
Shows how to add a footnote to a paragraph in the document using DocumentBuilder.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); for (int i = 0; i <= 100; i++) { builder.write("Some text " + i); builder.insertFootnote(FootnoteType.FOOTNOTE, "Footnote text " + i); builder.insertFootnote(FootnoteType.FOOTNOTE, "Footnote text " + i, "242"); }
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
Example:
Shows how to add a custom building block to a document.public void buildingBlockFields() throws Exception { Document doc = new Document(); // BuildingBlocks live inside the glossary document // If you're making a document from scratch, the glossary document must also be manually created GlossaryDocument glossaryDoc = new GlossaryDocument(); doc.setGlossaryDocument(glossaryDoc); // Create a building block and name it BuildingBlock block = new BuildingBlock(glossaryDoc); block.setName("Custom Block"); // Put in in the document's glossary document glossaryDoc.appendChild(block); Assert.assertEquals(glossaryDoc.getCount(), 1); // All GUIDs are this value by default Assert.assertEquals(block.getGuid().toString(), "00000000-0000-0000-0000-000000000000"); // In Microsoft Word, we can use these attributes to find blocks in Insert > Quick Parts > Building Blocks Organizer Assert.assertEquals(block.getCategory(), "(Empty Category)"); Assert.assertEquals(block.getType(), BuildingBlockType.NONE); Assert.assertEquals(block.getGallery(), BuildingBlockGallery.ALL); Assert.assertEquals(block.getBehavior(), BuildingBlockBehavior.CONTENT); // If we want to use our building block as an AutoText quick part, we need to give it some text and change some properties // All the necessary preparation will be done in a custom document visitor that we will accept BuildingBlockVisitor visitor = new BuildingBlockVisitor(glossaryDoc); block.accept(visitor); // We can find the block we made in the glossary document like this BuildingBlock customBlock = glossaryDoc.getBuildingBlock(BuildingBlockGallery.QUICK_PARTS, "My custom building blocks", "Custom Block"); // Our block contains one section which now contains our text Assert.assertEquals(customBlock.getFirstSection().getBody().getFirstParagraph().getText(), "Text inside " + customBlock.getName() + '\f'); Assert.assertEquals(customBlock.getLastSection(), customBlock.getFirstSection()); Assert.assertNotEquals(customBlock.getGuid().toString(), "00000000-0000-0000-0000-000000000000"); Assert.assertEquals(customBlock.getCategory(), "My custom building blocks"); Assert.assertEquals(customBlock.getType(), BuildingBlockType.NONE); Assert.assertEquals(customBlock.getGallery(), BuildingBlockGallery.QUICK_PARTS); Assert.assertEquals(customBlock.getBehavior(), BuildingBlockBehavior.PARAGRAPH); // Then we can insert it into the document as a new section doc.appendChild(doc.importNode(customBlock.getFirstSection(), true)); // Or we can find it in Microsoft Word's Building Blocks Organizer and place it manually doc.save(getArtifactsDir() + "BuildingBlocks.BuildingBlock.dotx"); } /// <summary> /// Simple implementation of adding text to a building block and preparing it for usage in the document. Implemented as a Visitor. /// </summary> public static class BuildingBlockVisitor extends DocumentVisitor { public BuildingBlockVisitor(final GlossaryDocument ownerGlossaryDoc) { mBuilder = new StringBuilder(); mGlossaryDoc = ownerGlossaryDoc; } public int visitBuildingBlockStart(final BuildingBlock block) { // Change values by default of created BuildingBlock block.setBehavior(BuildingBlockBehavior.PARAGRAPH); block.setCategory("My custom building blocks"); block.setDescription("Using this block in the Quick Parts section of word will place its contents at the cursor."); block.setGallery(BuildingBlockGallery.QUICK_PARTS); block.setGuid(UUID.randomUUID()); // Add content for the BuildingBlock to have an effect when used in the document Section section = new Section(mGlossaryDoc); block.appendChild(section); Body body = new Body(mGlossaryDoc); section.appendChild(body); Paragraph paragraph = new Paragraph(mGlossaryDoc); body.appendChild(paragraph); // Add text that will be visible in the document Run run = new Run(mGlossaryDoc, "Text inside " + block.getName()); block.getFirstSection().getBody().getFirstParagraph().appendChild(run); return VisitorAction.CONTINUE; } public int visitBuildingBlockEnd(final BuildingBlock block) { mBuilder.append("Visited " + block.getName() + "\r\n"); return VisitorAction.CONTINUE; } private StringBuilder mBuilder; private GlossaryDocument mGlossaryDoc; }
hasChildNodes | → inherited from CompositeNode |
public boolean hasChildNodes() |
Example:
Demonstrates how to use the InsertDocument method to insert a document into a merge field during mail merge.public void insertDocumentAtMailMerge() throws Exception { // Open the main document. Document mainDoc = new Document(getMyDir() + "InsertDocument1.doc"); // Add a handler to ApiExamples.Tests.MergeField event mainDoc.getMailMerge().setFieldMergingCallback(new InsertDocumentAtMailMergeHandler()); // The main document has a merge field in it called "Document_1". // The corresponding data for this field contains fully qualified path to the document // that should be inserted to this field. mainDoc.getMailMerge().execute(new String[]{"Document_1"}, new String[]{getMyDir() + "InsertDocument2.doc"}); mainDoc.save(getArtifactsDir() + "InsertDocumentAtMailMerge.doc"); } private class InsertDocumentAtMailMergeHandler implements IFieldMergingCallback { /** * This handler makes special processing for the "Document_1" field. * The field value contains the path to load the document. * We load the document and insert it into the current merge field. */ public void fieldMerging(final FieldMergingArgs args) throws Exception { if ("Document_1".equals(args.getDocumentFieldName())) { // Use document builder to navigate to the merge field with the specified name. DocumentBuilder builder = new DocumentBuilder(args.getDocument()); builder.moveToMergeField(args.getDocumentFieldName()); // The name of the document to load and insert is stored in the field value. Document subDoc = new Document((String) args.getFieldValue()); // Insert the document. insertDocument(builder.getCurrentParagraph(), subDoc); // The paragraph that contained the merge field might be empty now and you probably want to delete it. if (!builder.getCurrentParagraph().hasChildNodes()) { builder.getCurrentParagraph().remove(); } // Indicate to the mail merge engine that we have inserted what we wanted. args.setText(null); } } public void imageFieldMerging(final ImageFieldMergingArgs args) { // Do nothing. } }
hasMacros | |
public boolean hasMacros() |
Example:
Shows how to use MACROBUTTON fields that enable us to run macros by clicking.// Open a document that contains macros Document doc = new Document(getMyDir() + "Document.HasMacro.docm"); DocumentBuilder builder = new DocumentBuilder(doc); Assert.assertTrue(doc.hasMacros()); // Insert a MACROBUTTON field and reference by name a macro that exists within the input document FieldMacroButton field = (FieldMacroButton) builder.insertField(FieldType.FIELD_MACRO_BUTTON, true); field.setMacroName("MyMacro"); field.setDisplayText("Double click to run macro: " + field.getMacroName()); Assert.assertEquals(field.getFieldCode(), " MACROBUTTON MyMacro Double click to run macro: MyMacro"); builder.insertParagraph(); // Reference "ViewZoom200", a macro that was shipped with Microsoft Word, found under "Word commands" // If our document has a macro of the same name as one from another source, the field will select ours to run field = (FieldMacroButton) builder.insertField(FieldType.FIELD_MACRO_BUTTON, true); field.setMacroName("ViewZoom200"); field.setDisplayText("Run " + field.getMacroName()); Assert.assertEquals(field.getFieldCode(), " MACROBUTTON ViewZoom200 Run ViewZoom200"); // Save the document as a macro-enabled document type doc.save(getArtifactsDir() + "Field.MacroButton.docm");
hasRevisions | |
public boolean hasRevisions() |
Example:
Shows how to check if a document has revisions.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Normal editing of the document does not count as a revision builder.write("This does not count as a revision. "); Assert.assertFalse(doc.hasRevisions()); // In order for our edits to count as revisions, we need to declare an author and start tracking them doc.startTrackRevisions("John Doe", new Date()); builder.write("This is revision #1. "); // This flag corresponds to the "Track Changes" option being turned on in Microsoft Word, to track the editing manually // done there and not the programmatic changes we are about to do here Assert.assertFalse(doc.getTrackRevisions()); // As well as nodes in the document, revisions get referenced in this collection Assert.assertTrue(doc.hasRevisions()); Assert.assertEquals(doc.getRevisions().getCount(), 1); Revision revision = doc.getRevisions().get(0); Assert.assertEquals(revision.getAuthor(), "John Doe"); Assert.assertEquals(revision.getParentNode().getText(), "This is revision #1. "); Assert.assertEquals(revision.getRevisionType(), RevisionType.INSERTION); Assert.assertEquals(DocumentHelper.getDateWithoutTimeUsingFormat(revision.getDateTime()), DocumentHelper.getDateWithoutTimeUsingFormat(new Date())); Assert.assertEquals(revision.getGroup(), doc.getRevisions().getGroups().get(0)); // Deleting content also counts as a revision // The most recent revisions are put at the start of the collection doc.getFirstSection().getBody().getFirstParagraph().getRuns().get(0).remove(); Assert.assertEquals(doc.getRevisions().get(0).getRevisionType(), RevisionType.DELETION); Assert.assertEquals(doc.getRevisions().getCount(), 2); // Insert revisions are treated as document text by the GetText() method before they are accepted, // since they are still nodes with text and are in the body Assert.assertEquals(doc.getText().trim(), "This does not count as a revision. This is revision #1."); // Accepting the deletion revision will assimilate it into the paragraph text and remove it from the collection doc.getRevisions().get(0).accept(); Assert.assertEquals(doc.getRevisions().getCount(), 1); // Once the delete revision is accepted, the nodes that it concerns are removed and their text will not show up here Assert.assertEquals(doc.getText().trim(), "This is revision #1."); // The second insertion revision is now at index 0, which we can reject to ignore and discard it doc.getRevisions().get(0).reject(); Assert.assertEquals(doc.getRevisions().getCount(), 0); Assert.assertEquals(doc.getText().trim(), ""); // This takes us back to not counting changes as revisions doc.stopTrackRevisions(); builder.writeln("This also does not count as a revision."); Assert.assertEquals(doc.getRevisions().getCount(), 0); doc.save(getArtifactsDir() + "Document.Revisions.docx");
getHyphenationOptions | |
public HyphenationOptions getHyphenationOptions() |
Example:
Shows how to configure document hyphenation options.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Set this to insert a page break before this paragraph builder.getFont().setSize(24.0); builder.getParagraphFormat().setSuppressAutoHyphens(false); builder.writeln("Lorem ipsum dolor sit amet, consectetur adipiscing elit, " + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."); doc.getHyphenationOptions().setAutoHyphenation(true); doc.getHyphenationOptions().setConsecutiveHyphenLimit(2); doc.getHyphenationOptions().setHyphenationZone(720); // 0.5 inch doc.getHyphenationOptions().setHyphenateCaps(true); // Each paragraph has this flag that can be set to suppress hyphenation Assert.assertFalse(builder.getParagraphFormat().getSuppressAutoHyphens()); doc.save(getArtifactsDir() + "Document.HyphenationOptions.docx");
isComposite | → inherited from CompositeNode |
public boolean isComposite() |
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); } /** * A simple function that will walk through all children of a specified node recursively * and print the type of each node to the screen. */ @Test(enabled = false) public void traverseAllNodes(final 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(Node.nodeTypeToString(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); }
getLastSection | |
public Section getLastSection() |
null
if there are no sections.
Example:
Shows how to edit the last section of a document.// Open the template document, containing obsolete copyright information in the footer Document doc = new Document(getMyDir() + "HeaderFooter.ReplaceText.doc"); // We have a document with 2 sections, this way FirstSection and LastSection are not the same Assert.assertEquals(2, doc.getSections().getCount()); String newCopyrightInformation = MessageFormat.format("Copyright (C) {0} by Aspose Pty Ltd.", Calendar.getInstance().get(Calendar.YEAR)); FindReplaceOptions findReplaceOptions = new FindReplaceOptions(); findReplaceOptions.setMatchCase(false); findReplaceOptions.setFindWholeWordsOnly(false); // Access the first and the last sections HeaderFooter firstSectionFooter = doc.getFirstSection().getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY); firstSectionFooter.getRange().replace("(C) 2006 Aspose Pty Ltd.", newCopyrightInformation, findReplaceOptions); HeaderFooter lastSectionFooter = doc.getLastSection().getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY); lastSectionFooter.getRange().replace("(C) 2006 Aspose Pty Ltd.", newCopyrightInformation, findReplaceOptions); // Sections are also accessible via an array Assert.assertEquals(doc.getFirstSection(), doc.getSections().get(0)); Assert.assertEquals(doc.getLastSection(), doc.getSections().get(1)); doc.save(getArtifactsDir() + "HeaderFooter.ReplaceText.doc");
getLayoutOptions | |
public LayoutOptions getLayoutOptions() |
Example:
Shows how to set a document's layout options.Document doc = new Document(); Assert.assertFalse(doc.getLayoutOptions().getShowHiddenText()); Assert.assertFalse(doc.getLayoutOptions().getShowParagraphMarks()); // The appearance of revisions can be controlled from the layout options property doc.startTrackRevisions("John Doe", new Date()); doc.getLayoutOptions().getRevisionOptions().setInsertedTextColor(RevisionColor.BRIGHT_GREEN); doc.getLayoutOptions().getRevisionOptions().setShowRevisionBars(false); DocumentBuilder builder = new DocumentBuilder(doc); builder.writeln( "This is a revision. Normally the text is red with a bar to the left, but we made some changes to the revision options."); doc.stopTrackRevisions(); // Layout options can be used to show hidden text too builder.writeln("This text is not hidden."); builder.getFont().setHidden(true); builder.writeln( "This text is hidden. It will only show up in the output if we allow it to via doc.LayoutOptions."); doc.getLayoutOptions().setShowHiddenText(true); doc.save(getArtifactsDir() + "Document.LayoutOptions.pdf");
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(getArtifactsDir() + "Lists.SpecifyListLevel.doc");
getMailMerge | |
public MailMerge getMailMerge() |
Example:
Executes mail merge from data stored in a ResultSet.Document doc = new Document(getMyDir() + "MailMerge.ExecuteDataTable.doc"); // This example creates a table, but you would normally load table from a database. DataTable table = new DataTable("Test"); table.getColumns().add("CustomerName"); table.getColumns().add("Address"); table.getRows().add(new Object[]{"Thomas Hardy", "120 Hanover Sq., London"}); table.getRows().add(new Object[]{"Paolo Accorti", "Via Monte Bianco 34, Torino"}); // Field values from the table are inserted into the mail merge fields found in the document. doc.getMailMerge().execute(table); doc.save(getArtifactsDir() + "MailMerge.ExecuteDataTable.doc"); // Open a fresh copy of our document to perform another mail merge. doc = new Document(getMyDir() + "MailMerge.ExecuteDataTable.doc"); // We can also source values for a mail merge from a single row in the table doc.getMailMerge().execute(table.getRows().get(1)); doc.save(getArtifactsDir() + "MailMerge.ExecuteDataTable.OneRow.doc");
Example:
Executes a mail merge with repeatable regions.public void executeWithRegionsDataTable() throws Exception { Document doc = new Document(getMyDir() + "MailMerge.ExecuteWithRegions.doc"); final int orderId = 10444; // Perform several mail merge operations populating only part of the document each time. // Use DataTable as a data source. // The table name property should be set to match the name of the region defined in the document. DataTable orderTable = getTestOrder(orderId); doc.getMailMerge().executeWithRegions(orderTable); DataTable orderDetailsTable = getTestOrderDetails(orderId, "ExtendedPrice DESC"); doc.getMailMerge().executeWithRegions(orderDetailsTable); doc.save(getArtifactsDir() + "MailMerge.ExecuteWithRegionsDataTable.doc"); } private static DataTable getTestOrder(final int orderId) throws Exception { java.sql.ResultSet resultSet = executeDataTable(java.text.MessageFormat.format("SELECT * FROM AsposeWordOrders WHERE OrderId = {0}", Integer.toString(orderId))); return new DataTable(resultSet, "Orders"); } private static DataTable getTestOrderDetails(final int orderId, final String orderBy) throws Exception { StringBuilder builder = new StringBuilder(); builder.append(java.text.MessageFormat.format("SELECT * FROM AsposeWordOrderDetails WHERE OrderId = {0}", Integer.toString(orderId))); if ((orderBy != null) && (orderBy.length() > 0)) { builder.append(" ORDER BY "); builder.append(orderBy); } java.sql.ResultSet resultSet = executeDataTable(builder.toString()); return new DataTable(resultSet, "OrderDetails"); } /** * Utility function that creates a connection, command, * executes the command and return the result in a DataTable. */ private static java.sql.ResultSet executeDataTable(final String commandText) throws Exception { // Loads the driver Class.forName("net.ucanaccess.jdbc.UcanaccessDriver"); // Open the database connection. String connString = "jdbc:ucanaccess://" + getDatabaseDir() + "Northwind.mdb"; // From Wikipedia: The Sun driver has a known issue with character encoding and Microsoft Access databases. // Microsoft Access may use an encoding that is not correctly translated by the driver, leading to the replacement // in strings of, for example, accented characters by question marks. // // In this case I have to set CP1252 for the european characters to come through in the data values. java.util.Properties props = new java.util.Properties(); props.put("charSet", "Cp1252"); props.put("UID", "Admin"); // DSN-less DB connection. java.sql.Connection conn = java.sql.DriverManager.getConnection(connString, props); // Create and execute a command. java.sql.Statement statement = conn.createStatement(); return statement.executeQuery(commandText); }
getMailMergeSettings/setMailMergeSettings | |
public MailMergeSettings getMailMergeSettings() / public void setMailMergeSettings(MailMergeSettings value) |
You can use this object to specify a mail merge data source for a document and this information (along with the available data fields) will appear in Microsoft Word when the user opens this document. Or you can use this object to query mail merge settings that the user has specified in Microsoft Word for this document.
This object is never null.
Example:
Shows how to execute a mail merge with MailMergeSettings.// We'll create a simple document that will act as a destination for mail merge data Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.write("Dear "); builder.insertField("MERGEFIELD FirstName", "<FirstName>"); builder.write(" "); builder.insertField("MERGEFIELD LastName", "<LastName>"); builder.writeln(": "); builder.insertField("MERGEFIELD Message", "<Message>"); // Also we'll need a data source, in this case it will be an ASCII text file // We can use any character we want as a delimiter, in this case we'll choose '|' // The delimiter character is selected in the ODSO settings of mail merge settings String[] lines = {"FirstName|LastName|Message", "John|Doe|Hello! This message was created with Aspose Words mail merge."}; Files.write(Paths.get(getArtifactsDir() + "Document.Lines.txt"), (lines + System.lineSeparator()).getBytes(UTF_8), new StandardOpenOption[]{StandardOpenOption.CREATE, StandardOpenOption.APPEND}); // Set the data source, query and other things MailMergeSettings mailMergeSettings = doc.getMailMergeSettings(); mailMergeSettings.setMainDocumentType(MailMergeMainDocumentType.MAILING_LABELS); mailMergeSettings.setDataType(MailMergeDataType.NATIVE); mailMergeSettings.setDataSource(getArtifactsDir() + "Document.Lines.txt"); mailMergeSettings.setQuery("SELECT * FROM " + doc.getMailMergeSettings().getDataSource()); mailMergeSettings.setLinkToQuery(true); mailMergeSettings.setViewMergedData(true); // Office Data Source Object settings Odso odso = mailMergeSettings.getOdso(); odso.setDataSource(getArtifactsDir() + "Document.Lines.txt"); odso.setDataSourceType(OdsoDataSourceType.TEXT); odso.setColumnDelimiter('|'); odso.setDataSource(getArtifactsDir() + "Document.Lines.txt"); odso.setFirstRowContainsColumnNames(true); // ODSO objects can also be cloned Assert.assertNotSame(odso, odso.deepClone()); // The mail merge will be performed when this document is opened doc.save(getArtifactsDir() + "Document.MailMergeSettings.docx");
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.nodeTypeToString(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); } /** * A simple function that will walk through all children of a specified node recursively * and print the type of each node to the screen. */ @Test(enabled = false) public void traverseAllNodes(final 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(Node.nodeTypeToString(childNode.getNodeType())); // Recurse into the node if it is a composite node. if (childNode.isComposite()) { traverseAllNodes((CompositeNode) childNode); } } }
getNodeChangingCallback/setNodeChangingCallback | → inherited from DocumentBase |
public INodeChangingCallback getNodeChangingCallback() / public void setNodeChangingCallback(INodeChangingCallback value) |
Example:
Shows how to implement custom logic over node insertion in the document by changing the font of inserted HTML content.public void testNodeChangingInDocument() throws Exception { // Create a blank document object Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Set up and pass the object which implements the handler methods. doc.setNodeChangingCallback(new HandleNodeChangingFontChanger()); // Insert sample HTML content builder.insertHtml("<p>Hello World</p>"); doc.save(getArtifactsDir() + "Document.FontChanger.doc"); // Check that the inserted content has the correct formatting Run run = (Run) doc.getChild(NodeType.RUN, 0, true); Assert.assertEquals(run.getFont().getSize(), 24.0); Assert.assertEquals(run.getFont().getName(), "Arial"); } public class HandleNodeChangingFontChanger implements INodeChangingCallback { // Implement the NodeInserted handler to set default font settings for every Run node inserted into the Document public void nodeInserted(final NodeChangingArgs args) { // Change the font of inserted text contained in the Run nodes. if (args.getNode().getNodeType() == NodeType.RUN) { Font font = ((Run) args.getNode()).getFont(); font.setSize(24); font.setName("Arial"); } } public void nodeInserting(final NodeChangingArgs args) { // Do Nothing } public void nodeRemoved(final NodeChangingArgs args) { // Do Nothing } public void nodeRemoving(final NodeChangingArgs args) { // Do Nothing } }
getNodeType | |
public int getNodeType() |
Example:
Shows how to retrieve the NodeType enumeration of nodes.Document doc = new Document(getMyDir() + "Document.doc"); // Let's pick a node that we can't be quite sure of what type it is. // In this case lets pick the first node of the first paragraph in the body of the document Node node = doc.getFirstSection().getBody().getFirstParagraph().getFirstChild(); System.out.println("NodeType of first child: " + Node.nodeTypeToString(node.getNodeType())); // This time let's pick a node that we know the type of. Create a new paragraph and a table node. Paragraph para = new Paragraph(doc); Table table = new Table(doc); // Access to NodeType for typed nodes will always return their specific NodeType. // i.e A paragraph node will always return NodeType.Paragraph, a table node will always return NodeType.Table. System.out.println("NodeType of Paragraph: " + Node.nodeTypeToString(para.getNodeType())); System.out.println("NodeType of Table: " + Node.nodeTypeToString(table.getNodeType()));
getOriginalFileName | |
public java.lang.String getOriginalFileName() |
Returns null if the document was loaded from a stream or created blank.
Example:
Shows how to retrieve the details of the path, filename and LoadFormat of a document from when the document was first loaded into memory.Document doc = new Document(getMyDir() + "Document.doc"); // This property will return the full path and file name where the document was loaded from. String originalFilePath = doc.getOriginalFileName(); // Let's get just the file name from the full path. String originalFileName = new File(originalFilePath).getName(); // This is the original LoadFormat of the document. int loadFormat = doc.getOriginalLoadFormat();
Example:
Shows how to use the FileFormatUtil methods to detect the format of a document without any extension and save it with the correct file extension.// Load the document without a file extension into a stream and use the DetectFileFormat method to detect it's format. // These are both times where you might need extract the file format as it's not visible // The file format of this document is actually ".doc" FileInputStream docStream = new FileInputStream(getMyDir() + "Document.FileWithoutExtension"); FileFormatInfo info = FileFormatUtil.detectFileFormat(docStream); // Retrieve the LoadFormat of the document. int loadFormat = info.getLoadFormat(); // Let's show the different methods of converting LoadFormat enumerations to SaveFormat enumerations. // // Method #1 // Convert the LoadFormat to a String first for working with. The String will include the leading dot in front of the extension. String fileExtension = FileFormatUtil.loadFormatToExtension(loadFormat); // Now convert this extension into the corresponding SaveFormat enumeration int saveFormat = FileFormatUtil.extensionToSaveFormat(fileExtension); // Method #2 // Convert the LoadFormat enumeration directly to the SaveFormat enumeration. saveFormat = FileFormatUtil.loadFormatToSaveFormat(loadFormat); // Load a document from the stream. Document doc = new Document(docStream); // Save the document with the original file name, " Out" and the document's file extension. doc.save(getArtifactsDir() + "Document.WithFileExtension" + FileFormatUtil.saveFormatToExtension(saveFormat));
getOriginalLoadFormat | |
public int getOriginalLoadFormat() |
If you created a new blank document, returns the
Example:
Shows how to retrieve the details of the path, filename and LoadFormat of a document from when the document was first loaded into memory.Document doc = new Document(getMyDir() + "Document.doc"); // This property will return the full path and file name where the document was loaded from. String originalFilePath = doc.getOriginalFileName(); // Let's get just the file name from the full path. String originalFileName = new File(originalFilePath).getName(); // This is the original LoadFormat of the document. int loadFormat = doc.getOriginalLoadFormat();
getPackageCustomParts/setPackageCustomParts | |
public CustomPartCollection getPackageCustomParts() / public void setPackageCustomParts(CustomPartCollection value) |
Do not confuse these custom parts with Custom XML Data. If you need to access Custom XML parts,
use the
This collection contains OOXML parts whose parent is the OOXML package and they targets are of an "unknown relationship".
For more information see
Aspose.Words loads and saves custom parts into OOXML documents only.
This property cannot be null
.
Example:
Shows how to open a document with custom parts and access them.// Open a document that contains custom parts // CustomParts are arbitrary content OOXML parts // Not to be confused with Custom XML data which is represented by CustomXmlParts // This part is internal, meaning it is contained inside the OOXML package Document doc = new Document(getMyDir() + "Document.PackageCustomParts.docx"); Assert.assertEquals(doc.getPackageCustomParts().getCount(), 2); // Clone the second part CustomPart clonedPart = doc.getPackageCustomParts().get(1).deepClone(); // Add the clone to the collection doc.getPackageCustomParts().add(clonedPart); Assert.assertEquals(doc.getPackageCustomParts().getCount(), 3); // Use an enumerator to print information about the contents of each part Iterator<CustomPart> enumerator = doc.getPackageCustomParts().iterator(); int index = 0; while (enumerator.hasNext()) { CustomPart customPart = (CustomPart) enumerator.next(); System.out.println(MessageFormat.format("Part index {0}:", index)); System.out.println(MessageFormat.format("\tName: {0}", customPart.getName())); System.out.println(MessageFormat.format("\tContentType: {0}", customPart.getContentType())); System.out.println(MessageFormat.format("\tRelationshipType: {0}", customPart.getRelationshipType())); if (customPart.isExternal()) { System.out.println("\tSourced from outside the document"); } else { System.out.println(MessageFormat.format("\tSourced from within the document, length: {0} bytes", customPart.getData().length)); } index++; } // Delete parts one at a time based on index doc.getPackageCustomParts().removeAt(2); Assert.assertEquals(doc.getPackageCustomParts().getCount(), 2); // Delete all parts doc.getPackageCustomParts().clear(); Assert.assertEquals(doc.getPackageCustomParts().getCount(), 0);
getPageColor/setPageColor | → inherited from DocumentBase |
public java.awt.Color getPageColor() / public void setPageColor(java.awt.Color value) |
This property provides a simple way to specify a solid page color for the document.
Setting this property creates and sets an appropriate
If the page color is not set (e.g. there is no background shape in the document) returns a zero color.
Example:
Shows how to set the page color.Document doc = new Document(); doc.setPageColor(Color.lightGray); doc.save(getArtifactsDir() + "DocumentBase.SetPageColor.docx");
getPageCount | |
public int getPageCount() |
Example:
Shows how to invoke page layout and retrieve the number of pages in the document.Document doc = new Document(getMyDir() + "Document.doc"); // This invokes page layout which builds the document in memory so note that with large documents this // method can take time. After invoking this method, any rendering operation e.g rendering to PDF or image // will be instantaneous. int pageCount = doc.getPageCount();
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. System.out.println("Section parent is the document: " + (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. System.out.println("Paragraph has no parent node: " + (para.getParentNode() == null)); // But the paragraph node knows its document. System.out.println("Both nodes' documents are the same: " + (para.getDocument() == doc)); // 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 paragraph to the main text of the first section. doc.getFirstSection().getBody().appendChild(para); // The paragraph node is now a child of the Body node. System.out.println("Paragraph has a parent node: " + (para.getParentNode() != null));
getPreviousSibling | → inherited from Node |
public Node getPreviousSibling() |
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); }
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();
getRange | → inherited from Node |
public Range getRange() |
Example:
Shows how to delete all characters of a range.// Open Word document. Document doc = new Document(getMyDir() + "Range.DeleteSection.doc"); // The document contains two sections. Each section has a paragraph of text. System.out.println(doc.getText()); // Delete the first section from the document. doc.getSections().get(0).getRange().delete(); // Check the first section was deleted by looking at the text of the whole document again. System.out.println(doc.getText());
getRemovePersonalInformation/setRemovePersonalInformation | |
public boolean getRemovePersonalInformation() / public void setRemovePersonalInformation(boolean value) |
Example:
Shows how to get or set a flag to remove all user information upon saving the MS Word document.Document doc = new Document(getMyDir() + "Document.docx"); // If flag sets to 'true' that MS Word will remove all user information from comments, revisions and // document properties upon saving the document. In MS Word 2013 and 2016 you can see this using // File -> Options -> Trust Center -> Trust Center Settings -> Privacy Options -> then the // checkbox "Remove personal information from file properties on save". doc.setRemovePersonalInformation(true); doc.save(getArtifactsDir() + "Document.RemovePersonalInformation.docx");
getResourceLoadingCallback/setResourceLoadingCallback | → inherited from DocumentBase |
public IResourceLoadingCallback getResourceLoadingCallback() / public void setResourceLoadingCallback(IResourceLoadingCallback value) |
Example:
Shows how to process inserted resources differently.public void resourceLoadingCallback() throws Exception { Document doc = new Document(); // Images belong to NodeType.Shape // There are none in a blank document Assert.assertEquals(doc.getChildNodes(NodeType.SHAPE, true).getCount(), 0); // Enable our custom image loading doc.setResourceLoadingCallback(new ImageNameHandler()); DocumentBuilder builder = new DocumentBuilder(doc); // We usually insert images as a uri or byte array, but there are many other possibilities with ResourceLoadingCallback // In this case we are referencing images with simple names and keep the image fetching logic somewhere else builder.insertImage("Google Logo"); builder.insertImage("Aspose Logo"); builder.insertImage("My Watermark"); Assert.assertEquals(doc.getChildNodes(NodeType.SHAPE, true).getCount(), 3); doc.save(getArtifactsDir() + "DocumentBase.ResourceLoadingCallback.docx"); } private static class ImageNameHandler implements IResourceLoadingCallback { public int resourceLoading(final ResourceLoadingArgs args) throws URISyntaxException, IOException { if (args.getResourceType() == ResourceType.IMAGE) { // builder.InsertImage expects a uri so inputs like "Google Logo" would normally trigger a FileNotFoundException // We can still process those inputs and find an image any way we like, as long as an image byte array is passed to args.SetData() if ("Google Logo".equals(args.getOriginalUri())) { args.setData(DocumentHelper.getBytesFromStream(new URI("http://www.google.com/images/logos/ps_logo2.png").toURL().openStream())); return ResourceLoadingAction.USER_PROVIDED; } if ("Aspose Logo".equals(args.getOriginalUri())) { args.setData(DocumentHelper.getBytesFromStream(getAsposelogoUri().toURL().openStream())); return ResourceLoadingAction.USER_PROVIDED; } // We can find and add an image any way we like, as long as args.SetData() is called with some image byte array as a parameter if ("My Watermark".equals(args.getOriginalUri())) { InputStream imageStream = new FileInputStream(getImageDir() + "Watermark.png"); args.setData(DocumentHelper.getBytesFromStream(imageStream)); return ResourceLoadingAction.USER_PROVIDED; } } // All other resources such as documents, CSS stylesheets and images passed as uris are handled as they were normally return ResourceLoadingAction.DEFAULT; } }
getRevisions | |
public RevisionCollection getRevisions() |
The returned collection is a "live" collection, which means if you remove parts of a document that contain revisions, the deleted revisions will automatically disappear from this collection.
Example:
Shows how to check if a document has revisions.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Normal editing of the document does not count as a revision builder.write("This does not count as a revision. "); Assert.assertFalse(doc.hasRevisions()); // In order for our edits to count as revisions, we need to declare an author and start tracking them doc.startTrackRevisions("John Doe", new Date()); builder.write("This is revision #1. "); // This flag corresponds to the "Track Changes" option being turned on in Microsoft Word, to track the editing manually // done there and not the programmatic changes we are about to do here Assert.assertFalse(doc.getTrackRevisions()); // As well as nodes in the document, revisions get referenced in this collection Assert.assertTrue(doc.hasRevisions()); Assert.assertEquals(doc.getRevisions().getCount(), 1); Revision revision = doc.getRevisions().get(0); Assert.assertEquals(revision.getAuthor(), "John Doe"); Assert.assertEquals(revision.getParentNode().getText(), "This is revision #1. "); Assert.assertEquals(revision.getRevisionType(), RevisionType.INSERTION); Assert.assertEquals(DocumentHelper.getDateWithoutTimeUsingFormat(revision.getDateTime()), DocumentHelper.getDateWithoutTimeUsingFormat(new Date())); Assert.assertEquals(revision.getGroup(), doc.getRevisions().getGroups().get(0)); // Deleting content also counts as a revision // The most recent revisions are put at the start of the collection doc.getFirstSection().getBody().getFirstParagraph().getRuns().get(0).remove(); Assert.assertEquals(doc.getRevisions().get(0).getRevisionType(), RevisionType.DELETION); Assert.assertEquals(doc.getRevisions().getCount(), 2); // Insert revisions are treated as document text by the GetText() method before they are accepted, // since they are still nodes with text and are in the body Assert.assertEquals(doc.getText().trim(), "This does not count as a revision. This is revision #1."); // Accepting the deletion revision will assimilate it into the paragraph text and remove it from the collection doc.getRevisions().get(0).accept(); Assert.assertEquals(doc.getRevisions().getCount(), 1); // Once the delete revision is accepted, the nodes that it concerns are removed and their text will not show up here Assert.assertEquals(doc.getText().trim(), "This is revision #1."); // The second insertion revision is now at index 0, which we can reject to ignore and discard it doc.getRevisions().get(0).reject(); Assert.assertEquals(doc.getRevisions().getCount(), 0); Assert.assertEquals(doc.getText().trim(), ""); // This takes us back to not counting changes as revisions doc.stopTrackRevisions(); builder.writeln("This also does not count as a revision."); Assert.assertEquals(doc.getRevisions().getCount(), 0); doc.save(getArtifactsDir() + "Document.Revisions.docx");
getRevisionsView/setRevisionsView | |
public int getRevisionsView() / public void setRevisionsView(int value) |
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(com.aspose.words.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. System.out.println(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. System.out.println(doc.getText());
getShadeFormData/setShadeFormData | |
public boolean getShadeFormData() / public void setShadeFormData(boolean value) |
Example:
Shows how to apply gray shading to bookmarks.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // By default, bookmarked text is highlighted gray Assert.assertTrue(doc.getShadeFormData()); builder.write("Text before bookmark. "); builder.insertTextInput("My bookmark", TextFormFieldType.REGULAR, "", "If gray shading is turned on, this is the text that will have a gray background.", 0); // Our bookmarked text will appear gray here doc.save(getArtifactsDir() + "Document.ShadeFormDataTrue.docx"); // In this file, shading will be turned off and the bookmarked text will blend in with the other text doc.setShadeFormData(false); doc.save(getArtifactsDir() + "Document.ShadeFormDataFalse.docx");
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(getArtifactsDir() + "Lists.ParagraphStyleBulleted.doc");
getTheme | |
public Theme getTheme() |
Example:
Shows what we can do with the Themes property of Document.Document doc = new Document(); // When creating a blank document, Aspose Words creates a default theme object Theme theme = doc.getTheme(); // These color properties correspond to the 10 color columns that you see // in the "Theme colors" section in the color selector menu when changing font or shading color // We can view and edit the leading color for each column, and the five different tints that // make up the rest of the column will be derived automatically from each leading color // Aspose Words sets the defaults to what they are in the Microsoft Word default theme Assert.assertEquals(theme.getColors().getLight1(), new Color((255), (255), (255), (255))); Assert.assertEquals(theme.getColors().getDark1(), new Color((0), (0), (0), (255))); Assert.assertEquals(theme.getColors().getLight2(), new Color((238), (236), (225), (255))); Assert.assertEquals(theme.getColors().getDark2(), new Color((31), (73), (125), (255))); Assert.assertEquals(theme.getColors().getAccent1(), new Color((79), (129), (189), (255))); Assert.assertEquals(theme.getColors().getAccent2(), new Color((192), (80), (77), (255))); Assert.assertEquals(theme.getColors().getAccent3(), new Color((155), (187), (89), (255))); Assert.assertEquals(theme.getColors().getAccent4(), new Color((128), (100), (162), (255))); Assert.assertEquals(theme.getColors().getAccent5(), new Color((75), (172), (198), (255))); Assert.assertEquals(theme.getColors().getAccent6(), new Color((247), (150), (70), (255))); // Hyperlink colors Assert.assertEquals(theme.getColors().getHyperlink(), new Color((0), (0), (255), (255))); Assert.assertEquals(theme.getColors().getFollowedHyperlink(), new Color((128), (0), (128), (255))); // These appear at the very top of the font selector in the "Theme Fonts" section Assert.assertEquals(theme.getMajorFonts().getLatin(), "Cambria"); Assert.assertEquals(theme.getMinorFonts().getLatin(), "Calibri"); // Change some values to make a custom theme theme.getMinorFonts().setLatin("Bodoni MT"); theme.getMajorFonts().setLatin("Tahoma"); theme.getColors().setAccent1(Color.CYAN); theme.getColors().setAccent2(Color.YELLOW); // Save the document to use our theme doc.save(getArtifactsDir() + "Document.Theme.docx");
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.
If you want to automatically track changes as they are made programmatically by Aspose.Words
to this document use the
Example:
Shows how to check if a document has revisions.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Normal editing of the document does not count as a revision builder.write("This does not count as a revision. "); Assert.assertFalse(doc.hasRevisions()); // In order for our edits to count as revisions, we need to declare an author and start tracking them doc.startTrackRevisions("John Doe", new Date()); builder.write("This is revision #1. "); // This flag corresponds to the "Track Changes" option being turned on in Microsoft Word, to track the editing manually // done there and not the programmatic changes we are about to do here Assert.assertFalse(doc.getTrackRevisions()); // As well as nodes in the document, revisions get referenced in this collection Assert.assertTrue(doc.hasRevisions()); Assert.assertEquals(doc.getRevisions().getCount(), 1); Revision revision = doc.getRevisions().get(0); Assert.assertEquals(revision.getAuthor(), "John Doe"); Assert.assertEquals(revision.getParentNode().getText(), "This is revision #1. "); Assert.assertEquals(revision.getRevisionType(), RevisionType.INSERTION); Assert.assertEquals(DocumentHelper.getDateWithoutTimeUsingFormat(revision.getDateTime()), DocumentHelper.getDateWithoutTimeUsingFormat(new Date())); Assert.assertEquals(revision.getGroup(), doc.getRevisions().getGroups().get(0)); // Deleting content also counts as a revision // The most recent revisions are put at the start of the collection doc.getFirstSection().getBody().getFirstParagraph().getRuns().get(0).remove(); Assert.assertEquals(doc.getRevisions().get(0).getRevisionType(), RevisionType.DELETION); Assert.assertEquals(doc.getRevisions().getCount(), 2); // Insert revisions are treated as document text by the GetText() method before they are accepted, // since they are still nodes with text and are in the body Assert.assertEquals(doc.getText().trim(), "This does not count as a revision. This is revision #1."); // Accepting the deletion revision will assimilate it into the paragraph text and remove it from the collection doc.getRevisions().get(0).accept(); Assert.assertEquals(doc.getRevisions().getCount(), 1); // Once the delete revision is accepted, the nodes that it concerns are removed and their text will not show up here Assert.assertEquals(doc.getText().trim(), "This is revision #1."); // The second insertion revision is now at index 0, which we can reject to ignore and discard it doc.getRevisions().get(0).reject(); Assert.assertEquals(doc.getRevisions().getCount(), 0); Assert.assertEquals(doc.getText().trim(), ""); // This takes us back to not counting changes as revisions doc.stopTrackRevisions(); builder.writeln("This also does not count as a revision."); Assert.assertEquals(doc.getRevisions().getCount(), 0); doc.save(getArtifactsDir() + "Document.Revisions.docx");
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)); }
getVbaProject | |
public VbaProject getVbaProject() |
Example:
Shows how to get access to VBA project information in the document.Document doc = new Document(getMyDir() + "Document.TestButton.docm"); // A VBA project inside the document is defined as a collection of VBA modules VbaProject vbaProject = doc.getVbaProject(); VbaModuleCollection vbaModules = doc.getVbaProject().getModules(); for (VbaModule module : vbaModules) { System.out.println(MessageFormat.format("Module name: {0};\nModule code:\n{1}\n", module.getName(), module.getSourceCode())); }
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.
Example:
Shows how to count how many previous versions a document has.Document doc = new Document(); // No versions are in the document by default // We also can't add any since they are not supported Assert.assertEquals(doc.getVersionsCount(), 0); // Let's open a document with versions doc = new Document(getMyDir() + "Versions.doc"); // We can use this property to see how many there are Assert.assertEquals(doc.getVersionsCount(), 4); doc.save(getArtifactsDir() + "Document.Versions.docx"); doc = new Document(getArtifactsDir() + "Document.Versions.docx"); // If we save and open the document, the versions are lost Assert.assertEquals(doc.getVersionsCount(), 0);
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(getArtifactsDir() + "Document.SetZoom.doc");
getWarningCallback/setWarningCallback | → inherited from DocumentBase |
public IWarningCallback getWarningCallback() / public void setWarningCallback(IWarningCallback value) |
Example:
Shows how to implement the IWarningCallback to be notified of any font substitution during document save.public static class HandleDocumentWarnings implements IWarningCallback { /** * Our callback only needs to implement the "Warning" method. This method is called whenever there is a * potential issue during document processing. The callback can be set to listen for warnings generated during document * load and/or document save. */ public void warning(final WarningInfo info) { // We are only interested in fonts being substituted. if (info.getWarningType() == WarningType.FONT_SUBSTITUTION) { System.out.println("Font substitution: " + info.getDescription()); } } }
Example:
Demonstrates how to receive notifications of font substitutions by using IWarningCallback.// Load the document to render. Document doc = new Document(getMyDir() + "Document.doc"); // Create a new class implementing IWarningCallback and assign it to the PdfSaveOptions class. HandleDocumentWarnings callback = new HandleDocumentWarnings(); doc.setWarningCallback(callback); // We can choose the default font to use in the case of any missing fonts. FontSettings.getDefaultInstance().getSubstitutionSettings().getDefaultFontSubstitution().setDefaultFontName("Arial"); // For testing we will set Aspose.Words to look for fonts only in a folder which doesn't exist. Since Aspose.Words won't // find any fonts in the specified directory, then during rendering the fonts in the document will be substituted with the default // font specified under FontSettings.DefaultFontName. We can pick up on this substitution using our callback. FontSettings.getDefaultInstance().setFontsFolder("", false); // Pass the save options along with the save path to the save method. doc.save(getArtifactsDir() + "Rendering.MissingFontNotification.pdf");
getWriteProtection | |
public WriteProtection getWriteProtection() |
Example:
Shows how to protect a document with a password.Document doc = new Document(); Assert.assertFalse(doc.getWriteProtection().isWriteProtected()); Assert.assertFalse(doc.getWriteProtection().getReadOnlyRecommended()); // Enter a password that's 15 or less characters long doc.getWriteProtection().setPassword("docpassword123"); Assert.assertTrue(doc.getWriteProtection().isWriteProtected()); Assert.assertFalse(doc.getWriteProtection().validatePassword("wrongpassword")); DocumentBuilder builder = new DocumentBuilder(doc); builder.writeln("We can still edit the document at this stage."); // Save the document // Without the password, we can only read this document in Microsoft Word // With the password, we can read and write doc.save(getArtifactsDir() + "Document.WriteProtection.docx"); // Re-open our document Document docProtected = new Document(getArtifactsDir() + "Document.WriteProtection.docx"); DocumentBuilder docProtectedBuilder = new DocumentBuilder(docProtected); docProtectedBuilder.moveToDocumentEnd(); // We can programmatically edit this document without using our password Assert.assertTrue(docProtected.getWriteProtection().isWriteProtected()); docProtectedBuilder.writeln("Writing text in a protected document."); // We will still need the password if we want to open this one with Word docProtected.save(getArtifactsDir() + "Document.WriteProtectionEditedAfter.docx");
Method Detail |
---|
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:
Traverse a document with a visitor that prints all structure nodes that it encounters.public void docStructureToText() throws Exception { // Open the document that has nodes we want to print the info of Document doc = new Document(getMyDir() + "DocumentVisitor.Destination.docx"); // Create an object that inherits from the DocumentVisitor class DocStructurePrinter visitor = new DocStructurePrinter(); // Accepting a visitor lets it start traversing the nodes in the document, // starting with the node that accepted it to then recursively visit every child doc.accept(visitor); // 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(visitor.getText()); } /// <summary> /// This Visitor implementation prints information about sections, bodies, paragraphs and runs encountered in the document. /// </summary> public static class DocStructurePrinter extends DocumentVisitor { public DocStructurePrinter() { 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 Document node is encountered. /// </summary> public int visitDocumentStart(final Document doc) { int childNodeCount = doc.getChildNodes(NodeType.ANY, true).getCount(); // A Document node is at the root of every document, so if we let a document accept a visitor, this will be the first visitor action to be carried out indentAndAppendLine("[Document start] Child nodes: " + childNodeCount); mDocTraversalDepth++; // Let the visitor continue visiting other nodes return VisitorAction.CONTINUE; } /// <summary> /// Called when the visiting of a Document is ended. /// </summary> public int visitDocumentEnd(final Document doc) { // If we let a document accept a visitor, this will be the last visitor action to be carried out mDocTraversalDepth--; indentAndAppendLine("[Document end]"); return VisitorAction.CONTINUE; } /// <summary> /// Called when a Section node is encountered in the document. /// </summary> public int visitSectionStart(final Section section) { // Get the index of our section within the document NodeCollection docSections = section.getDocument().getChildNodes(NodeType.SECTION, false); int sectionIndex = docSections.indexOf(section); indentAndAppendLine("[Section start] Section index: " + sectionIndex); mDocTraversalDepth++; return VisitorAction.CONTINUE; } /// <summary> /// Called when the visiting of a Section node is ended. /// </summary> public int visitSectionEnd(final Section section) { mDocTraversalDepth--; indentAndAppendLine("[Section end]"); return VisitorAction.CONTINUE; } /// <summary> /// Called when a Body node is encountered in the document. /// </summary> public int visitBodyStart(final Body body) { int paragraphCount = body.getParagraphs().getCount(); indentAndAppendLine("[Body start] Paragraphs: " + paragraphCount); mDocTraversalDepth++; return VisitorAction.CONTINUE; } /// <summary> /// Called when the visiting of a Body node is ended. /// </summary> public int visitBodyEnd(final Body body) { mDocTraversalDepth--; indentAndAppendLine("[Body end]"); return VisitorAction.CONTINUE; } /// <summary> /// Called when a Paragraph node is encountered in the document. /// </summary> public int visitParagraphStart(final Paragraph paragraph) { indentAndAppendLine("[Paragraph start]"); mDocTraversalDepth++; return VisitorAction.CONTINUE; } /// <summary> /// Called when the visiting of a Paragraph node is ended. /// </summary> public int visitParagraphEnd(final Paragraph paragraph) { mDocTraversalDepth--; indentAndAppendLine("[Paragraph end]"); return VisitorAction.CONTINUE; } /// <summary> /// Called when a Run node is encountered in the document. /// </summary> public int visitRun(final Run run) { indentAndAppendLine("[Run] \"" + run.getText() + "\""); return VisitorAction.CONTINUE; } /// <summary> /// Called when a SubDocument node is encountered in the document. /// </summary> public int visitSubDocument(final SubDocument subDocument) { indentAndAppendLine("[SubDocument]"); return VisitorAction.CONTINUE; } /// <summary> /// Append a line to the StringBuilder and indent it depending on how deep the visitor is into the document tree. /// </summary> /// <param name="text"></param> private void indentAndAppendLine(final String text) { for (int i = 0; i < mDocTraversalDepth; i++) { mBuilder.append("| "); } mBuilder.append(text + "\r\n"); } private int mDocTraversalDepth; private StringBuilder mBuilder; }
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"); // Start tracking and make some revisions. doc.startTrackRevisions("Author"); doc.getFirstSection().getBody().appendParagraph("Hello world!"); // Revisions will now show up as normal text in the output document. doc.acceptAllRevisions(); doc.save(getArtifactsDir() + "Document.AcceptedRevisions.doc");
appendChild | → inherited from CompositeNode |
public Node appendChild(Node newChild) |
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 paragraph 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 \x000c is automatically appended. \x000c is the end of section character. System.out.println(doc.getText()); // Save the document. doc.save(getArtifactsDir() + "Section.CreateFromScratch.doc");
appendDocument | |
public void appendDocument(Document srcDoc, int importFormatMode) |
srcDoc
- The document to append.importFormatMode
- A Example:
Shows how to use the AppendDocument method to combine all the documents in a folder to the end of a template document.// Lets start with a simple template and append all the documents in a folder to this document. Document baseDoc = new Document(); // Add some content to the template. DocumentBuilder builder = new DocumentBuilder(baseDoc); builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1); builder.writeln("Template Document"); builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.NORMAL); builder.writeln("Some content here"); // Gather the files which will be appended to our template document. // In this case we add the optional parameter to include the search only for files with the ".doc" extension. File srcDir = new File(getMyDir()); FilenameFilter filter = (dir, name) -> name.endsWith(".doc"); File[] files = srcDir.listFiles(filter); // The list of files may come in any order, let's sort the files by name so the documents are enumerated alphabetically. Arrays.sort(files); // Iterate through every file in the directory and append each one to the end of the template document. for (File file : files) { String fileName = file.getCanonicalPath(); // We have some encrypted test documents in our directory, Aspose.Words can open encrypted documents // but only with the correct password. Let's just skip them here for simplicity. FileFormatInfo info = FileFormatUtil.detectFileFormat(fileName); if (info.isEncrypted()) continue; Document subDoc = new Document(fileName); baseDoc.appendDocument(subDoc, ImportFormatMode.USE_DESTINATION_STYLES); } // Save the combined document to disk. baseDoc.save(path);
Example:
Shows how to append a document to the end of another document.// The document that the content will be appended to. Document dstDoc = new Document(getMyDir() + "Document.doc"); // The document to append. Document srcDoc = new Document(getMyDir() + "DocumentBuilder.doc"); // Append the source document to the destination document. // Pass format mode to retain the original formatting of the source document when importing it. dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING); // Save the document. dstDoc.save(getArtifactsDir() + "Document.AppendDocument.doc");
appendDocument | |
public void appendDocument(Document srcDoc, int importFormatMode, ImportFormatOptions importFormatOptions) |
srcDoc
- The document to append.importFormatMode
- A importFormatOptions
- Allows to specify options that affect formatting of a result document.Example:
Shows how to resolve styles behavior while append document.Document srcDoc = new Document(getMyDir() + "DocumentBuilder.ResolveStyleBehaviorWhileAppendDocument.Source.docx"); Document dstDoc = new Document(getMyDir() + "DocumentBuilder.ResolveStyleBehaviorWhileAppendDocument.Destination.docx"); ImportFormatOptions options = new ImportFormatOptions(); // Specify that if numbering clashes in source and destination documents // then a numbering from the source document will be used. options.setKeepSourceNumbering(true); dstDoc.appendDocument(srcDoc, ImportFormatMode.USE_DESTINATION_STYLES, options); dstDoc.updateListLabels();
cleanup | |
public void cleanup() throws java.lang.Exception |
Example:
Shows how to remove unused styles and lists from a document.// Create a new document Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Brand new documents have 4 styles and 0 lists by default Assert.assertEquals(doc.getStyles().getCount(), 4); Assert.assertEquals(doc.getLists().getCount(), 0); // We will add one style and one list and mark them as "used" by applying them to the builder builder.getParagraphFormat().setStyle(doc.getStyles().add(StyleType.PARAGRAPH, "My Used Style")); builder.getListFormat().setList(doc.getLists().add(ListTemplate.BULLET_DIAMONDS)); // These items were added to their respective collections Assert.assertEquals(doc.getStyles().getCount(), 5); Assert.assertEquals(doc.getLists().getCount(), 1); // doc.Cleanup() removes all unused styles and lists doc.cleanup(); // It currently has no effect because the 2 items we added plus the original 4 styles are all used Assert.assertEquals(doc.getStyles().getCount(), 5); Assert.assertEquals(doc.getLists().getCount(), 1); // These two items will be added but will not associated with any part of the document doc.getStyles().add(StyleType.PARAGRAPH, "My Unused Style"); doc.getLists().add(ListTemplate.NUMBER_ARABIC_DOT); // They also get stored in the document and are ready to be used Assert.assertEquals(doc.getStyles().getCount(), 6); Assert.assertEquals(doc.getLists().getCount(), 2); doc.cleanup(); // Since we didn't apply them anywhere, the two unused items are removed by doc.Cleanup() Assert.assertEquals(doc.getStyles().getCount(), 5); Assert.assertEquals(doc.getLists().getCount(), 1);
cleanup | |
public void cleanup(CleanupOptions options) throws java.lang.Exception |
Example:
Shows how to remove all unused styles and lists from a document.Document doc = new Document(getMyDir() + "Document.doc"); CleanupOptions cleanupOptions = new CleanupOptions(); cleanupOptions.setUnusedLists(true); cleanupOptions.setUnusedStyles(true); doc.cleanup(cleanupOptions);
compare | |
public void compare(Document document, java.lang.String author, java.util.Date dateTime) throws java.lang.Exception |
Note: Documents must not have revisions before comparison.
document
- Document to compare.author
- Initials of the author to use for revisions.dateTime
- The date and time to use for revisions.Example:
Shows how to apply the compare method to two documents and then use the results.Document doc1 = new Document(getMyDir() + "Document.Compare.1.doc"); Document doc2 = new Document(getMyDir() + "Document.Compare.2.doc"); // If either document has a revision, an exception will be thrown. if (doc1.getRevisions().getCount() == 0 && doc2.getRevisions().getCount() == 0) { doc1.compare(doc2, "authorName", new Date()); } // If doc1 and doc2 are different, doc1 now has some revisions after the comparison, which can now be viewed and processed. for (Revision r : doc1.getRevisions()) System.out.println(r.getRevisionType()); // All the revisions in doc1 are differences between doc1 and doc2, so accepting them on doc1 transforms doc1 into doc2. doc1.getRevisions().acceptAll(); // doc1, when saved, now resembles doc2. doc1.save(getArtifactsDir() + "Document.Compare.doc");
compare | |
public void compare(Document document, java.lang.String author, java.util.Date dateTime, CompareOptions options) throws java.lang.Exception |
Example:
Shows how to specify which document shall be used as a target during comparisonDocument doc1 = new Document(getMyDir() + "Document.CompareOptions.1.docx"); Document doc2 = new Document(getMyDir() + "Document.CompareOptions.2.docx"); //ComparisonTargetType with IgnoreFormatting setting determines which document has to be used as formatting source for ranges of equal text. CompareOptions compareOptions = new CompareOptions(); compareOptions.setIgnoreFormatting(true); compareOptions.setIgnoreCaseChanges(false); compareOptions.setIgnoreComments(false); compareOptions.setIgnoreTables(false); compareOptions.setIgnoreFields(false); compareOptions.setIgnoreFootnotes(false); compareOptions.setIgnoreTextboxes(false); compareOptions.setIgnoreHeadersAndFooters(false); compareOptions.setTarget(ComparisonTargetType.NEW); doc1.compare(doc2, "vderyushev", new Date(), compareOptions); doc1.save(getArtifactsDir() + "Document.CompareOptions.docx");
copyStylesFromTemplate | |
public void copyStylesFromTemplate(Document template) |
Example:
Shows how to copies styles from the template to a document via Document.Document template = new Document(getMyDir() + "Rendering.doc"); Document target = new Document(getMyDir() + "Document.docx"); target.copyStylesFromTemplate(template); target.save(getArtifactsDir() + "CopyStylesFromTemplateViaDocument.docx");
copyStylesFromTemplate | |
public void copyStylesFromTemplate(java.lang.String template) throws java.lang.Exception |
Example:
Shows how to copies styles from the template to a document via string.String templatePath = getMyDir() + "Rendering.doc"; Document target = new Document(getMyDir() + "Document.docx"); target.copyStylesFromTemplate(templatePath); target.save(getArtifactsDir() + "CopyStylesFromTemplateViaString.docx");
deepClone | |
public Document deepClone() |
Example:
Shows how to deep clone a document.Document doc = new Document(getMyDir() + "Document.doc"); Document clone = doc.deepClone();
deepClone | → inherited from Node |
public Node deepClone(boolean isCloneChildren) |
Example:
Shows how to clone composite nodes with and without their child nodes.// Create a new empty document. Document doc = new Document(); // Add some text to the first paragraph Paragraph para = doc.getFirstSection().getBody().getFirstParagraph(); para.appendChild(new Run(doc, "Some text")); // Clone the paragraph and the child nodes. Node cloneWithChildren = para.deepClone(true); // Only clone the paragraph and no child nodes. Node cloneWithoutChildren = para.deepClone(false);
ensureMinimum | |
public void ensureMinimum() |
Example:
Shows how to ensure the Document is valid (has the minimum nodes required to be valid).// Create a blank document then remove all nodes from it, the result will be a completely empty document. Document doc = new Document(); doc.removeAllChildren(); // Ensure that the document is valid. Since the document has no nodes this method will create an empty section // and add an empty paragraph to make it valid. doc.ensureMinimum();
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.
This version of Aspose.Words provides limited support for table styles as follows:
Example:
Shows how to expand the formatting from styles onto the rows and cells of the table as direct formatting.Document doc = new Document(getMyDir() + "Table.TableStyle.docx"); // Get the first cell of the first table in the document. Table table = (Table) doc.getChild(NodeType.TABLE, 0, true); Cell firstCell = table.getFirstRow().getFirstCell(); // First print the color of the cell shading. This should be empty as the current shading // is stored in the table style. double cellShadingBefore = table.getFirstRow().getRowFormat().getHeight(); System.out.println("Cell shading before style expansion: " + cellShadingBefore); // Expand table style formatting to direct formatting. doc.expandTableStylesToDirectFormatting(); // Now print the cell shading after expanding table styles. A blue background pattern color // should have been applied from the table style. double cellShadingAfter = table.getFirstRow().getRowFormat().getHeight(); System.out.println("Cell shading after style expansion: " + cellShadingAfter);
getAncestor | → inherited from Node |
public CompositeNode getAncestor(int ancestorType) |
ancestorType
- A Example:
Shows how to find out if a table contains another table or if the table itself is nested inside another table.public void calculateDepthOfNestedTables() throws Exception { Document doc = new Document(getMyDir() + "Table.NestedTables.doc"); int tableIndex = 0; for (Table table : (Iterable<Table>) doc.getChildNodes(NodeType.TABLE, true)) { // First lets find if any cells in the table have tables themselves as children. int count = getChildTableCount(table); System.out.println(MessageFormat.format("Table #{0} has {1} tables directly within its cells", tableIndex, count)); // Now let's try the other way around, lets try find if the table is nested inside another table and at what depth. int tableDepth = getNestedDepthOfTable(table); if (tableDepth > 0) { System.out.println(MessageFormat.format("Table #{0} is nested inside another table at depth of {1}", tableIndex, tableDepth)); } else { System.out.println(MessageFormat.format("Table #{0} is a non nested table (is not a child of another table)", tableIndex)); } tableIndex++; } } /** * Calculates what level a table is nested inside other tables. * * @returns An integer containing the level the table is nested at. * 0 = Table is not nested inside any other table * 1 = Table is nested within one parent table * 2 = Table is nested within two parent tables etc.. */ private static int getNestedDepthOfTable(final Table table) { int depth = 0; int type = table.getNodeType(); // The parent of the table will be a Cell, instead attempt to find a grandparent that is of type Table Node parent = table.getAncestor(table.getNodeType()); while (parent != null) { // Every time we find a table a level up we increase the depth counter and then try to find an // ancestor of type table from the parent. depth++; parent = parent.getAncestor(Table.class); } return depth; } /** * Determines if a table contains any immediate child table within its cells. * Does not recursively traverse through those tables to check for further tables. * * @returns Returns true if at least one child cell contains a table. * Returns false if no cells in the table contains a table. */ private static int getChildTableCount(final Table table) { int tableCount = 0; // Iterate through all child rows in the table for (Row row : table.getRows()) { // Iterate through all child cells in the row for (Cell cell : row.getCells()) { // Retrieve the collection of child tables of this cell TableCollection childTables = cell.getTables(); // If this cell has a table as a child then return true if (childTables.getCount() > 0) tableCount++; } } // No cell contains a table return tableCount; }
getAncestor | → inherited from Node |
public CompositeNode 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.Example:
Shows how to find out if a table contains another table or if the table itself is nested inside another table.public void calculateDepthOfNestedTables() throws Exception { Document doc = new Document(getMyDir() + "Table.NestedTables.doc"); int tableIndex = 0; for (Table table : (Iterable<Table>) doc.getChildNodes(NodeType.TABLE, true)) { // First lets find if any cells in the table have tables themselves as children. int count = getChildTableCount(table); System.out.println(MessageFormat.format("Table #{0} has {1} tables directly within its cells", tableIndex, count)); // Now let's try the other way around, lets try find if the table is nested inside another table and at what depth. int tableDepth = getNestedDepthOfTable(table); if (tableDepth > 0) { System.out.println(MessageFormat.format("Table #{0} is nested inside another table at depth of {1}", tableIndex, tableDepth)); } else { System.out.println(MessageFormat.format("Table #{0} is a non nested table (is not a child of another table)", tableIndex)); } tableIndex++; } } /** * Calculates what level a table is nested inside other tables. * * @returns An integer containing the level the table is nested at. * 0 = Table is not nested inside any other table * 1 = Table is nested within one parent table * 2 = Table is nested within two parent tables etc.. */ private static int getNestedDepthOfTable(final Table table) { int depth = 0; int type = table.getNodeType(); // The parent of the table will be a Cell, instead attempt to find a grandparent that is of type Table Node parent = table.getAncestor(table.getNodeType()); while (parent != null) { // Every time we find a table a level up we increase the depth counter and then try to find an // ancestor of type table from the parent. depth++; parent = parent.getAncestor(Table.class); } return depth; } /** * Determines if a table contains any immediate child table within its cells. * Does not recursively traverse through those tables to check for further tables. * * @returns Returns true if at least one child cell contains a table. * Returns false if no cells in the table contains a table. */ private static int getChildTableCount(final Table table) { int tableCount = 0; // Iterate through all child rows in the table for (Row row : table.getRows()) { // Iterate through all child cells in the row for (Cell cell : row.getCells()) { // Retrieve the collection of child tables of this cell TableCollection childTables = cell.getTables(); // If this cell has a table as a child then return true if (childTables.getCount() > 0) tableCount++; } } // No cell contains a table return tableCount; }
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. See remarks for more info.Example:
Shows how to test if a node is inside a field by using an XPath expression.// Let's pick a document we know has some fields in. Document doc = new Document(getMyDir() + "MailMerge.MergeImage.doc"); // Let's say we want to check if the Run below is inside a field. Run run = (Run) doc.getChild(NodeType.RUN, 5, true); // Evaluate the XPath expression. The resulting NodeList will contain all nodes found inside a field a field (between FieldStart // and FieldEnd exclusive). There can however be FieldStart and FieldEnd nodes in the list if there are nested fields // in the path. Currently does not find rare fields in which the FieldCode or FieldResult spans across multiple paragraphs. NodeList resultList = doc.selectNodes("//FieldStart/following-sibling::node()[following-sibling::FieldEnd]"); // Check if the specified run is one of the nodes that are inside the field. for (Node node : (Iterable<Node>) resultList) { if (node == run) { System.out.println("The node is found inside a field"); break; } }
Example:
Shows how to extract a specific child node from a CompositeNode by using the GetChild method and passing the NodeType and index.Paragraph paragraph = (Paragraph) doc.getChild(NodeType.PARAGRAPH, 0, true);
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. 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 shapes = doc.getChildNodes(NodeType.SHAPE, true); int imageIndex = 0; for (Shape shape : (Iterable<Shape>) shapes) { if (shape.hasImage()) { String imageFileName = MessageFormat.format("Image.ExportImages.{0}{1}", imageIndex, FileFormatUtil.imageTypeToExtension(shape.getImageData().getImageType())); shape.getImageData().save(getArtifactsDir() + imageFileName); imageIndex++; } } }
Example:
Demonstrates how to remove a specified TOC from a document.public void removeTOCFromDocument() throws Exception { // Open a document which contains a TOC. Document doc = new Document(getMyDir() + "Document.TableOfContents.doc"); // Remove the first TOC from the document. Field tocField = doc.getRange().getFields().get(0); tocField.remove(); // Save the output. doc.save(getArtifactsDir() + "Document.TableOfContentsRemoveTOC.doc"); }
getPageInfo | |
public PageInfo getPageInfo(int pageIndex) throws java.lang.Exception |
pageIndex
- The 0-based page index.Example:
Renders a page of a Word document into a BufferedImage using a specified zoom factor.Document doc = new Document(getMyDir() + "Rendering.doc"); PageInfo pageInfo = doc.getPageInfo(0); // Let's say we want the image at 50% zoom. final float myScale = 0.50f; // Let's say we want the image at this resolution. final float myResolution = 200.0f; Dimension pageSize = pageInfo.getSizeInPixels(myScale, myResolution); BufferedImage img = new BufferedImage((int) pageSize.getWidth(), (int) pageSize.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D gr = img.createGraphics(); try { // You can apply various settings to the Graphics object. gr.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); // Fill the page background. gr.setPaint(Color.black); // Render the page using the zoom. doc.renderToScale(0, gr, 0, 0, myScale); } finally { if (gr != null) { gr.dispose(); } } ImageIO.write(img, "PNG", new File(getArtifactsDir() + "Rendering.RenderToScale.png"));
getText | → inherited from CompositeNode |
public java.lang.String getText() |
The returned string includes all control and special characters as described in
Example:
Shows the difference between calling the GetText and ToString methods on a node.Document doc = new Document(); // Enter a dummy field into the document. DocumentBuilder builder = new DocumentBuilder(doc); builder.insertField("MERGEFIELD Field"); // GetText will retrieve all field codes and special characters System.out.println("GetText() Result: " + doc.getText()); // ToString will export the node to the specified format. When converted to text it will not retrieve fields code // or special characters, but will still contain some natural formatting characters such as paragraph markers etc. // This is the same as "viewing" the document as if it was opened in a text editor. System.out.println("ToString() Result: " + doc.toString(SaveFormat.TEXT));
Example:
Finds and outputs all paragraphs in a document that are bulleted or numbered.NodeCollection paras = doc.getChildNodes(NodeType.PARAGRAPH, true); for (Paragraph para : (Iterable<Paragraph>) paras) { if (para.getListFormat().isListItem()) { System.out.println(java.text.MessageFormat.format("*** A paragraph belongs to list {0}", para.getListFormat().getList().getListId())); System.out.println(para.getText()); } }
importNode | → inherited from DocumentBase |
public Node importNode(Node srcNode, boolean isImportChildren) |
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.Example:
Shows how to import node from source document to destination document.Document src = new Document(); Document dst = new Document(); // Add text to both documents src.getFirstSection().getBody().getFirstParagraph().appendChild(new Run(src, "Source document first paragraph text.")); dst.getFirstSection().getBody().getFirstParagraph().appendChild(new Run(dst, "Destination document first paragraph text.")); // If we want to add the section from doc2 to doc1, we can't just append them like this: // dst.AppendChild(src.FirstSection); // Uncommenting that line throws an exception because doc2's first section belongs to doc2, // but each node in a document must belong to the document Assert.assertNotEquals(src.getFirstSection().getDocument(), dst); // We can create a new node that belongs to the destination document Section importedSection = (Section) dst.importNode(src.getFirstSection(), true); // It has the same content but it is not the same node nor do they have the same owner Assert.assertNotEquals(src.getFirstSection(), importedSection); Assert.assertNotEquals(src.getFirstSection().getDocument(), importedSection.getDocument()); Assert.assertEquals(src.getFirstSection().getBody().getFirstParagraph().getText(), importedSection.getBody().getFirstParagraph().getText()); // Now it is ready to be placed in the document dst.appendChild(importedSection); // Our document does indeed contain both the original and imported section Assert.assertEquals("Destination document first paragraph text.\r\nSource document first paragraph text.\r\n", dst.toString(SaveFormat.TEXT));
importNode | → inherited from DocumentBase |
public Node importNode(Node srcNode, boolean isImportChildren, int importFormatMode) |
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:
Shows how to import node from source document to destination document with specific options.// Create two documents with two styles that aren't the same but have the same name Document src = new Document(); Style srcStyle = src.getStyles().add(StyleType.CHARACTER, "My style"); DocumentBuilder srcBuilder = new DocumentBuilder(src); srcBuilder.getFont().setStyle(srcStyle); srcBuilder.writeln("Source document text."); Document dst = new Document(); Style dstStyle = dst.getStyles().add(StyleType.CHARACTER, "My style"); dstStyle.getFont().setBold(true); DocumentBuilder dstBuilder = new DocumentBuilder(dst); dstBuilder.getFont().setStyle(dstStyle); srcBuilder.writeln("Destination document text."); dst.importNode(src.getFirstSection(), true, ImportFormatMode.USE_DESTINATION_STYLES); Assert.assertNull(dst.getStyles().get("My style_0")); dst.importNode(src.getFirstSection(), true, ImportFormatMode.KEEP_DIFFERENT_STYLES); Assert.assertNotNull(dst.getStyles().get("My style_0"));
indexOf | → inherited from CompositeNode |
public int indexOf(Node child) |
Example:
Shows how to get the index of a given child node from its parent.Document doc = new Document(getMyDir() + "Rendering.doc"); // Get the body of the first section in the document. Body body = doc.getFirstSection().getBody(); // Retrieve the index of the last paragraph in the body. int index = body.getChildNodes().indexOf(body.getLastParagraph());
Example:
Retrieves the index of a cell in a row.int cellIndex = row.indexOf(cell);
Example:
Retrieves the index of a row in a table.int rowIndex = table.indexOf(row);
insertAfter | → inherited from CompositeNode |
public Node insertAfter(Node newChild, Node refChild) |
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. Example:
Shows how to add, update and delete child nodes from within a CompositeNode.Document doc = new Document(); // An empty document has one paragraph by default Assert.assertEquals(doc.getFirstSection().getBody().getParagraphs().getCount(), 1); // A paragraph is a composite node because it can contain runs, which are another type of node Paragraph paragraph = doc.getFirstSection().getBody().getFirstParagraph(); Run paragraphText = new Run(doc, "Initial text. "); paragraph.appendChild(paragraphText); // We will place these 3 children into the main text of our paragraph Run run1 = new Run(doc, "Run 1. "); Run run2 = new Run(doc, "Run 2. "); Run run3 = new Run(doc, "Run 3. "); // We initialized them but not in our paragraph yet Assert.assertEquals(paragraph.getText(), "Initial text. " + (char) 12); // Insert run2 before initial paragraph text. This will be at the start of the paragraph paragraph.insertBefore(run2, paragraphText); // Insert run3 after initial paragraph text. This will be at the end of the paragraph paragraph.insertAfter(run3, paragraphText); // Insert run1 before every other child node. run2 was the start of the paragraph, now it will be run1 paragraph.prependChild(run1); Assert.assertEquals(paragraph.getText(), "Run 1. Run 2. Initial text. Run 3. " + (char) 12); Assert.assertEquals(paragraph.getChildNodes(NodeType.ANY, true).getCount(), 4); // Access the child node collection and update/delete children ((Run) paragraph.getChildNodes(NodeType.RUN, true).get(1)).setText("Updated run 2. "); paragraph.getChildNodes(NodeType.RUN, true).remove(paragraphText); Assert.assertEquals(paragraph.getText(), "Run 1. Updated run 2. Run 3. " + (char) 12); Assert.assertEquals(paragraph.getChildNodes(NodeType.ANY, true).getCount(), 3);
Example:
Shows how to replace all textboxes with images.Document doc = new Document(getMyDir() + "Shape.ReplaceTextboxesWithImages.doc"); // This gets a live collection of all shape nodes in the document. NodeCollection shapeCollection = doc.getChildNodes(NodeType.SHAPE, true); // Since we will be adding/removing nodes, it is better to copy all collection // into a fixed size array, otherwise iterator will be invalidated. Node[] shapes = shapeCollection.toArray(); for (Node node : shapes) { Shape shape = (Shape) node; // Filter out all shapes that we don't need. if (shape.getShapeType() == ShapeType.TEXT_BOX) { // Create a new shape that will replace the existing shape. Shape image = new Shape(doc, ShapeType.IMAGE); // Load the image into the new shape. image.getImageData().setImage(getImageDir() + "Hammer.wmf"); // Make new shape's position to match the old shape. image.setLeft(shape.getLeft()); image.setTop(shape.getTop()); image.setWidth(shape.getWidth()); image.setHeight(shape.getHeight()); image.setRelativeHorizontalPosition(shape.getRelativeHorizontalPosition()); image.setRelativeVerticalPosition(shape.getRelativeVerticalPosition()); image.setHorizontalAlignment(shape.getHorizontalAlignment()); image.setVerticalAlignment(shape.getVerticalAlignment()); image.setWrapType(shape.getWrapType()); image.setWrapSide(shape.getWrapSide()); // Insert new shape after the old shape and remove the old shape. shape.getParentNode().insertAfter(image, shape); shape.remove(); } } doc.save(getArtifactsDir() + "Shape.ReplaceTextboxesWithImages.doc");
insertBefore | → inherited from CompositeNode |
public Node insertBefore(Node newChild, Node refChild) |
If refChild is null, inserts newChild at the end 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 newChild is placed before this node.Example:
Shows how to add, update and delete child nodes from within a CompositeNode.Document doc = new Document(); // An empty document has one paragraph by default Assert.assertEquals(doc.getFirstSection().getBody().getParagraphs().getCount(), 1); // A paragraph is a composite node because it can contain runs, which are another type of node Paragraph paragraph = doc.getFirstSection().getBody().getFirstParagraph(); Run paragraphText = new Run(doc, "Initial text. "); paragraph.appendChild(paragraphText); // We will place these 3 children into the main text of our paragraph Run run1 = new Run(doc, "Run 1. "); Run run2 = new Run(doc, "Run 2. "); Run run3 = new Run(doc, "Run 3. "); // We initialized them but not in our paragraph yet Assert.assertEquals(paragraph.getText(), "Initial text. " + (char) 12); // Insert run2 before initial paragraph text. This will be at the start of the paragraph paragraph.insertBefore(run2, paragraphText); // Insert run3 after initial paragraph text. This will be at the end of the paragraph paragraph.insertAfter(run3, paragraphText); // Insert run1 before every other child node. run2 was the start of the paragraph, now it will be run1 paragraph.prependChild(run1); Assert.assertEquals(paragraph.getText(), "Run 1. Run 2. Initial text. Run 3. " + (char) 12); Assert.assertEquals(paragraph.getChildNodes(NodeType.ANY, true).getCount(), 4); // Access the child node collection and update/delete children ((Run) paragraph.getChildNodes(NodeType.RUN, true).get(1)).setText("Updated run 2. "); paragraph.getChildNodes(NodeType.RUN, true).remove(paragraphText); Assert.assertEquals(paragraph.getText(), "Run 1. Updated run 2. Run 3. " + (char) 12); Assert.assertEquals(paragraph.getChildNodes(NodeType.ANY, true).getCount(), 3);
iterator | → inherited from CompositeNode |
public java.util.Iterator<Node> iterator() |
Example:
Shows how to enumerate immediate children of a CompositeNode using the enumerator provided by the ChildNodes collection.NodeCollection children = paragraph.getChildNodes(); for (Node child : (Iterable<Node>) 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()); } }
joinRunsWithSameFormatting | |
public int joinRunsWithSameFormatting() |
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
Example:
Shows how to join runs in a document to reduce unneeded runs.// Let's load this particular document. It contains a lot of content that has been edited many times. // This means the document will most likely contain a large number of runs with duplicate formatting. Document doc = new Document(getMyDir() + "Rendering.doc"); // This is for illustration purposes only, remember how many run nodes we had in the original document. int runsBefore = doc.getChildNodes(NodeType.RUN, true).getCount(); // Join runs with the same formatting. This is useful to speed up processing and may also reduce redundant // tags when exporting to HTML which will reduce the output file size. int joinCount = doc.joinRunsWithSameFormatting(); // This is for illustration purposes only, see how many runs are left after joining. int runsAfter = doc.getChildNodes(NodeType.RUN, true).getCount(); System.out.println(MessageFormat.format("Number of runs before:{0}, after:{1}, joined:{2}", runsBefore, runsAfter, joinCount)); // Save the optimized document to disk. doc.save(getArtifactsDir() + "Document.JoinRunsWithSameFormatting.html");
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.previousPreOrder(doc) != null && nextNode != null) { Assert.assertEquals(curNode, nextNode.previousPreOrder(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.hasImage()) { shape.remove(); } } curNode = nextNode; }
normalizeFieldTypes | |
public void normalizeFieldTypes() |
Use this method after document changes that affect field types.
To change field type values in a specific part of the document use
Example:
Shows how to get the keep a field's type up to date with its field code.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // We'll add a date field Field field = builder.insertField("DATE", null); // The FieldDate field type corresponds to the "DATE" field so our field's type property gets automatically set to it Assert.assertEquals(field.getType(), FieldType.FIELD_DATE); Assert.assertEquals(doc.getRange().getFields().getCount(), 1); // We can manually access the content of the field we added and change it Run fieldText = (Run) doc.getFirstSection().getBody().getFirstParagraph().getChildNodes(NodeType.RUN, true).get(0); Assert.assertEquals(fieldText.getText(), "DATE"); fieldText.setText("PAGE"); // We changed the text to "PAGE" but the field's type property did not update accordingly Assert.assertEquals(fieldText.getText(), "PAGE"); Assert.assertNotEquals(field.getType(), FieldType.FIELD_PAGE); // The type of the field as well as its components is still "FieldDate" Assert.assertEquals(field.getType(), FieldType.FIELD_DATE); Assert.assertEquals(field.getStart().getFieldType(), FieldType.FIELD_DATE); Assert.assertEquals(field.getSeparator().getFieldType(), FieldType.FIELD_DATE); Assert.assertEquals(field.getEnd().getFieldType(), FieldType.FIELD_DATE); doc.normalizeFieldTypes(); // After running this method the type changes everywhere to "FieldPage", which matches the text "PAGE" Assert.assertEquals(field.getType(), FieldType.FIELD_PAGE); Assert.assertEquals(field.getStart().getFieldType(), FieldType.FIELD_PAGE); Assert.assertEquals(field.getSeparator().getFieldType(), FieldType.FIELD_PAGE); Assert.assertEquals(field.getEnd().getFieldType(), FieldType.FIELD_PAGE);
prependChild | → inherited from CompositeNode |
public Node prependChild(Node newChild) |
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:
Shows how to add, update and delete child nodes from within a CompositeNode.Document doc = new Document(); // An empty document has one paragraph by default Assert.assertEquals(doc.getFirstSection().getBody().getParagraphs().getCount(), 1); // A paragraph is a composite node because it can contain runs, which are another type of node Paragraph paragraph = doc.getFirstSection().getBody().getFirstParagraph(); Run paragraphText = new Run(doc, "Initial text. "); paragraph.appendChild(paragraphText); // We will place these 3 children into the main text of our paragraph Run run1 = new Run(doc, "Run 1. "); Run run2 = new Run(doc, "Run 2. "); Run run3 = new Run(doc, "Run 3. "); // We initialized them but not in our paragraph yet Assert.assertEquals(paragraph.getText(), "Initial text. " + (char) 12); // Insert run2 before initial paragraph text. This will be at the start of the paragraph paragraph.insertBefore(run2, paragraphText); // Insert run3 after initial paragraph text. This will be at the end of the paragraph paragraph.insertAfter(run3, paragraphText); // Insert run1 before every other child node. run2 was the start of the paragraph, now it will be run1 paragraph.prependChild(run1); Assert.assertEquals(paragraph.getText(), "Run 1. Run 2. Initial text. Run 3. " + (char) 12); Assert.assertEquals(paragraph.getChildNodes(NodeType.ANY, true).getCount(), 4); // Access the child node collection and update/delete children ((Run) paragraph.getChildNodes(NodeType.RUN, true).get(1)).setText("Updated run 2. "); paragraph.getChildNodes(NodeType.RUN, true).remove(paragraphText); Assert.assertEquals(paragraph.getText(), "Run 1. Updated run 2. Run 3. " + (char) 12); Assert.assertEquals(paragraph.getChildNodes(NodeType.ANY, true).getCount(), 3);
previousPreOrder | → inherited from Node |
public Node previousPreOrder(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.previousPreOrder(doc) != null && nextNode != null) { Assert.assertEquals(curNode, nextNode.previousPreOrder(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.hasImage()) { shape.remove(); } } curNode = nextNode; }
public void print() |
Example:
Prints the whole document to the default printer.Document doc = new Document(getMyDir() + "Document.doc"); doc.print();
public void print(java.lang.String printerName) |
printerName
- The name of the printer.Example:
Prints the whole document to a specified printer.Document doc = new Document(getMyDir() + "Document.doc"); doc.print("KONICA MINOLTA magicolor 2400W");
public void print(javax.print.attribute.AttributeSet printerSettings) |
The
The
printerSettings
- The printer settings to use.Example:
Prints a range of pages.Document doc = new Document(getMyDir() + "Rendering.doc"); AttributeSet printerSettings = new HashAttributeSet(); // Page numbers in printer settings are 1-based. printerSettings.add(new PageRanges(1, 3)); doc.print(printerSettings);
public void print(javax.print.attribute.AttributeSet printerSettings, java.lang.String documentName) |
The
The
printerSettings
- The printer settings to use.documentName
- The document name to display (for example, in a print status dialog
box or printer queue) while printing the document.Example:
Prints a range of pages along with the name of the document.Document doc = new Document(getMyDir() + "Rendering.doc"); AttributeSet printerSettings = new HashAttributeSet(); // Page numbers in printer settings are 1-based. printerSettings.add(new PageRanges(1, 3)); doc.print(printerSettings, "My Print Document.doc");
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(getArtifactsDir() + "Section.Protect.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");
remove | → inherited from Node |
public void remove() |
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; }
Example:
Shows how to delete all images from a document.// Here we get all shapes from the document node, but you can do this for any smaller // node too, for example delete shapes from a single section or a paragraph. NodeCollection shapes = doc.getChildNodes(NodeType.SHAPE, true); // We cannot delete shape nodes while we enumerate through the collection. // One solution is to add nodes that we want to delete to a temporary array and delete afterwards. ArrayList shapesToDelete = new ArrayList(); for (Shape shape : (Iterable<Shape>) shapes) { // Several shape types can have an image including image shapes and OLE objects. if (shape.hasImage()) { shapesToDelete.add(shape); } } // Now we can delete shapes. for (Shape shape : (Iterable<Shape>) shapesToDelete) { shape.remove(); }
removeAllChildren | → inherited from CompositeNode |
public void removeAllChildren() |
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 paragraph 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 \x000c is automatically appended. \x000c is the end of section character. System.out.println(doc.getText()); // Save the document. doc.save(getArtifactsDir() + "Section.CreateFromScratch.doc");
removeChild | → inherited from CompositeNode |
public Node removeChild(Node oldChild) |
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); }
removeExternalSchemaReferences | |
public void removeExternalSchemaReferences() |
Example:
Shows how to remove all external XML schema references from a document.Document doc = new Document(getMyDir() + "Document.doc"); doc.removeExternalSchemaReferences();
removeMacros | |
public void removeMacros() |
By removing all macros from a document you can ensure the document contains no macro viruses.
Example:
Shows how to remove all macros from a document.Document doc = new Document(getMyDir() + "Document.doc"); doc.removeMacros();
removeSmartTags | → inherited from CompositeNode |
public void removeSmartTags() |
Example:
Removes all smart tags from descendant nodes of the composite node.Document doc = new Document(getMyDir() + "Document.doc"); // Remove smart tags from the first paragraph in the document. doc.getFirstSection().getBody().getFirstParagraph().removeSmartTags();
Example:
Shows how to remove all smart tags from a document.Document doc = new Document(getMyDir() + "Document.doc"); doc.removeSmartTags();
renderToScale | |
public java.awt.geom.Point2D.Float renderToScale(int pageIndex, java.awt.Graphics2D graphics, float x, float y, float scale) throws java.lang.Exception |
pageIndex
- The 0-based page index.graphics
- The object where to render to.x
- The X coordinate (in world units) of the top left corner of the rendered page.y
- The Y coordinate (in world units) of the top left corner of the rendered page.scale
- The scale for rendering the page (1.0 is 100%).Example:
Renders individual pages to graphics to create one image with thumbnails of all pages.// The user opens or builds a document. Document doc = new Document(getMyDir() + "Rendering.doc"); // This defines the number of columns to display the thumbnails in. final int thumbColumns = 2; // Calculate the required number of rows for thumbnails. // We can now get the number of pages in the document. int thumbRows = doc.getPageCount() / thumbColumns; int remainder = doc.getPageCount() % thumbColumns; if (remainder > 0) thumbRows++; // Lets say I want thumbnails to be of this zoom. float scale = 0.25f; // For simplicity lets pretend all pages in the document are of the same size, // so we can use the size of the first page to calculate the size of the thumbnail. Dimension thumbSize = doc.getPageInfo(0).getSizeInPixels(scale, 96); // Calculate the size of the image that will contain all the thumbnails. int imgWidth = (int) (thumbSize.getWidth() * thumbColumns); int imgHeight = (int) (thumbSize.getHeight() * thumbRows); BufferedImage img = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_ARGB); // The user has to provides a Graphics object to draw on. // The Graphics object can be created from a bitmap, from a metafile, printer or window. Graphics2D gr = img.createGraphics(); try { gr.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); gr.setColor(Color.white); // Fill the "paper" with white, otherwise it will be transparent. gr.fillRect(0, 0, imgWidth, imgHeight); for (int pageIndex = 0; pageIndex < doc.getPageCount(); pageIndex++) { int rowIdx = pageIndex / thumbColumns; int columnIdx = pageIndex % thumbColumns; // Specify where we want the thumbnail to appear. float thumbLeft = (float) (columnIdx * thumbSize.getWidth()); float thumbTop = (float) (rowIdx * thumbSize.getHeight()); Point2D.Float size = doc.renderToScale(pageIndex, gr, thumbLeft, thumbTop, scale); gr.setColor(Color.black); // Draw the page rectangle. gr.drawRect((int) thumbLeft, (int) thumbTop, (int) size.getX(), (int) size.getY()); } ImageIO.write(img, "PNG", new File(getArtifactsDir() + "Rendering.Thumbnails.png")); } finally { if (gr != null) { gr.dispose(); } }
Example:
Renders a page of a Word document into a BufferedImage using a specified zoom factor.Document doc = new Document(getMyDir() + "Rendering.doc"); PageInfo pageInfo = doc.getPageInfo(0); // Let's say we want the image at 50% zoom. final float myScale = 0.50f; // Let's say we want the image at this resolution. final float myResolution = 200.0f; Dimension pageSize = pageInfo.getSizeInPixels(myScale, myResolution); BufferedImage img = new BufferedImage((int) pageSize.getWidth(), (int) pageSize.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D gr = img.createGraphics(); try { // You can apply various settings to the Graphics object. gr.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); // Fill the page background. gr.setPaint(Color.black); // Render the page using the zoom. doc.renderToScale(0, gr, 0, 0, myScale); } finally { if (gr != null) { gr.dispose(); } } ImageIO.write(img, "PNG", new File(getArtifactsDir() + "Rendering.RenderToScale.png"));
renderToSize | |
public float renderToSize(int pageIndex, java.awt.Graphics2D graphics, float x, float y, float width, float height) throws java.lang.Exception |
pageIndex
- The 0-based page index.graphics
- The object where to render to.x
- The X coordinate (in world units) of the top left corner of the rendered page.y
- The Y coordinate (in world units) of the top left corner of the rendered page.width
- The maximum width (in world units) that can be occupied by the rendered page.height
- The maximum height (in world units) that can be occupied by the rendered page.Example:
Render to a BufferedImage at a specified location and size.Document doc = new Document(getMyDir() + "Rendering.doc"); // Bitmap bmp = new Bitmap(700, 700); BufferedImage img = new BufferedImage(700, 700, BufferedImage.TYPE_INT_ARGB); // User has some sort of a Graphics object. In this case created from a bitmap. Graphics2D gr = img.createGraphics(); try { // The user can specify any options on the Graphics object including // transform, antialiasing, page units, etc. gr.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); // The output should be offset 0.5" from the edge and rotated. gr.translate(ConvertUtil.inchToPoint(0.5f), ConvertUtil.inchToPoint(0.5f)); gr.rotate(10.0 * Math.PI / 180.0, img.getWidth() / 2.0, img.getHeight() / 2.0); // Set pen color and draw our test rectangle. gr.setColor(Color.RED); gr.drawRect(0, 0, (int) ConvertUtil.inchToPoint(3), (int) ConvertUtil.inchToPoint(3)); // User specifies (in world coordinates) where on the Graphics to render and what size. float returnedScale = doc.renderToSize(0, gr, 0f, 0f, (float) ConvertUtil.inchToPoint(3), (float) ConvertUtil.inchToPoint(3)); // This is the calculated scale factor to fit 297mm into 3". System.out.println(MessageFormat.format("The image was rendered at {0,number,#}% zoom.", returnedScale * 100)); ImageIO.write(img, "PNG", new File(getArtifactsDir() + "Rendering.RenderToSize.png")); } finally { if (gr != null) { gr.dispose(); } }
save | |
public void save(java.io.OutputStream outputStream, SaveOptions saveOptions) throws java.lang.Exception |
stream
- Stream where to save the document.saveOptions
- Specifies the options that control how the document is saved. Can be null.
If this is null, the document will be saved in the binary DOC format.Example:
Converts just one page (third page in this example) of the document to PDF.Document doc = new Document(getMyDir() + "Rendering.doc"); OutputStream stream = new FileOutputStream(getArtifactsDir() + "Rendering.SaveToPdfStreamOnePage.pdf"); try { PdfSaveOptions options = new PdfSaveOptions(); options.setPageIndex(2); options.setPageCount(1); doc.save(stream, options); } finally { if (stream != null) { stream.close(); } }
save | |
public void save(java.io.OutputStream outputStream, int saveFormat) throws java.lang.Exception |
stream
- Stream where to save the document.saveFormat
- The format in which to save the document.Example:
Shows how to save a document to the XPS format in different ways.// Open the document Document doc = new Document(getMyDir() + "Rendering.doc"); // Save document to file in the XPS format with default options doc.save(getArtifactsDir() + "Rendering.SaveAsXps.DefaultOptions.xps"); // Save document to stream in the XPS format with default options OutputStream docStream = new FileOutputStream(getArtifactsDir() + "Rendering.SaveAsXps.FromStream.xps"); doc.save(docStream, SaveFormat.XPS); docStream.close(); // Save document to file in the XPS format with specified options // Render 3 pages starting from page 2; pages 2, 3 and 4 XpsSaveOptions xpsOptions = new XpsSaveOptions(); xpsOptions.setSaveFormat(SaveFormat.XPS); xpsOptions.setPageIndex(1); xpsOptions.setPageCount(3); // All paragraphs in the "Heading 1" style will be included in the outline but "Heading 2" and onwards won't xpsOptions.getOutlineOptions().setHeadingsOutlineLevels(1); doc.save(getArtifactsDir() + "Rendering.SaveAsXps.PartialDocument.xps", xpsOptions);
Example:
Shows how to save a document to the Jpeg format using the Save method and the ImageSaveOptions class.// Open the document Document doc = new Document(getMyDir() + "Rendering.doc"); // Save as a Jpeg image file with default options doc.save(getArtifactsDir() + "Rendering.JpegDefaultOptions.jpg"); // Save document to an ByteArrayOutputStream as a Jpeg with default options ByteArrayOutputStream docStream = new ByteArrayOutputStream(); doc.save(docStream, SaveFormat.JPEG); // Save document to a Jpeg image with specified options. // Render the third page only and set the jpeg quality to 80% // In this case we need to pass the desired SaveFormat to the ImageSaveOptions constructor // to signal what type of image to save as. ImageSaveOptions imageOptions = new ImageSaveOptions(SaveFormat.JPEG); imageOptions.setPageIndex(2); imageOptions.setPageCount(1); imageOptions.setJpegQuality(80); doc.save(getArtifactsDir() + "Rendering.JpegCustomOptions.jpg", imageOptions);
Example:
Shows how to save a document to the PDF format using the Save method and the PdfSaveOptions class.// Open the document Document doc = new Document(getMyDir() + "Rendering.doc"); // Option 1: Save document to file in the PDF format with default options doc.save(getArtifactsDir() + "Rendering.PdfDefaultOptions.pdf"); // Option 2: Save the document to stream in the PDF format with default options ByteArrayOutputStream stream = new ByteArrayOutputStream(); doc.save(stream, SaveFormat.PDF); // Option 3: Save document to the PDF format with specified options // Render the first page only and preserve form fields as usable controls and not as plain text PdfSaveOptions pdfOptions = new PdfSaveOptions(); pdfOptions.setPageIndex(0); pdfOptions.setPageCount(1); pdfOptions.setPreserveFormFields(true); doc.save(getArtifactsDir() + "Rendering.PdfCustomOptions.pdf", pdfOptions);
Example:
Shows how to save a document to a stream.Document doc = new Document(getMyDir() + "Document.doc"); ByteArrayOutputStream dstStream = new ByteArrayOutputStream(); doc.save(dstStream, SaveFormat.DOCX); // In you want to read the result into a Document object again, in Java you need to get the // data bytes and wrap into an input stream. ByteArrayInputStream srcStream = new ByteArrayInputStream(dstStream.toByteArray());
Example:
Saves a document page as a BMP image into a ByteArayOutputStream.Document doc = new Document(getMyDir() + "Rendering.doc"); ByteArrayOutputStream stream = new ByteArrayOutputStream(); doc.save(stream, SaveFormat.BMP); // The stream now contains image bytes. byte[] imageBytes = stream.toByteArray(); // Read the bytes back into an image. BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageBytes));
save | |
public SaveOutputParameters 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:
Shows how to save a document to the XPS format in different ways.// Open the document Document doc = new Document(getMyDir() + "Rendering.doc"); // Save document to file in the XPS format with default options doc.save(getArtifactsDir() + "Rendering.SaveAsXps.DefaultOptions.xps"); // Save document to stream in the XPS format with default options OutputStream docStream = new FileOutputStream(getArtifactsDir() + "Rendering.SaveAsXps.FromStream.xps"); doc.save(docStream, SaveFormat.XPS); docStream.close(); // Save document to file in the XPS format with specified options // Render 3 pages starting from page 2; pages 2, 3 and 4 XpsSaveOptions xpsOptions = new XpsSaveOptions(); xpsOptions.setSaveFormat(SaveFormat.XPS); xpsOptions.setPageIndex(1); xpsOptions.setPageCount(3); // All paragraphs in the "Heading 1" style will be included in the outline but "Heading 2" and onwards won't xpsOptions.getOutlineOptions().setHeadingsOutlineLevels(1); doc.save(getArtifactsDir() + "Rendering.SaveAsXps.PartialDocument.xps", xpsOptions);
Example:
Shows how to save a document to the Jpeg format using the Save method and the ImageSaveOptions class.// Open the document Document doc = new Document(getMyDir() + "Rendering.doc"); // Save as a Jpeg image file with default options doc.save(getArtifactsDir() + "Rendering.JpegDefaultOptions.jpg"); // Save document to an ByteArrayOutputStream as a Jpeg with default options ByteArrayOutputStream docStream = new ByteArrayOutputStream(); doc.save(docStream, SaveFormat.JPEG); // Save document to a Jpeg image with specified options. // Render the third page only and set the jpeg quality to 80% // In this case we need to pass the desired SaveFormat to the ImageSaveOptions constructor // to signal what type of image to save as. ImageSaveOptions imageOptions = new ImageSaveOptions(SaveFormat.JPEG); imageOptions.setPageIndex(2); imageOptions.setPageCount(1); imageOptions.setJpegQuality(80); doc.save(getArtifactsDir() + "Rendering.JpegCustomOptions.jpg", imageOptions);
Example:
Shows how to save a document to the PDF format using the Save method and the PdfSaveOptions class.// Open the document Document doc = new Document(getMyDir() + "Rendering.doc"); // Option 1: Save document to file in the PDF format with default options doc.save(getArtifactsDir() + "Rendering.PdfDefaultOptions.pdf"); // Option 2: Save the document to stream in the PDF format with default options ByteArrayOutputStream stream = new ByteArrayOutputStream(); doc.save(stream, SaveFormat.PDF); // Option 3: Save document to the PDF format with specified options // Render the first page only and preserve form fields as usable controls and not as plain text PdfSaveOptions pdfOptions = new PdfSaveOptions(); pdfOptions.setPageIndex(0); pdfOptions.setPageCount(1); pdfOptions.setPreserveFormFields(true); doc.save(getArtifactsDir() + "Rendering.PdfCustomOptions.pdf", pdfOptions);
Example:
Converts a whole document into a multipage TIFF file using default options.Document doc = new Document(getMyDir() + "Rendering.doc"); doc.save(getArtifactsDir() + "Rendering.SaveToTiffDefault.tiff");
Example:
Converts a whole document to PDF using default options.Document doc = new Document(getMyDir() + "Rendering.doc"); doc.save(getArtifactsDir() + "Rendering.SaveToPdfDefault.pdf");
Example:
Converts from DOC to MHTML format.Document doc = new Document(getMyDir() + "Document.doc"); doc.save(getArtifactsDir() + "Document.ConvertToMhtml.mht");
Example:
Converts a whole document from DOC to PDF using default options.Document doc = new Document(getMyDir() + "Document.doc"); doc.save(getArtifactsDir() + "Document.Doc2PdfSave.pdf");
Example:
Saves a document to a file.doc.save(getArtifactsDir() + "Document.OpenFromFile.doc");
save | |
public SaveOutputParameters save(java.lang.String fileName, SaveOptions saveOptions) 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.saveOptions
- Specifies the options that control how the document is saved. Can be null.Example:
Shows how to save a document to the XPS format in different ways.// Open the document Document doc = new Document(getMyDir() + "Rendering.doc"); // Save document to file in the XPS format with default options doc.save(getArtifactsDir() + "Rendering.SaveAsXps.DefaultOptions.xps"); // Save document to stream in the XPS format with default options OutputStream docStream = new FileOutputStream(getArtifactsDir() + "Rendering.SaveAsXps.FromStream.xps"); doc.save(docStream, SaveFormat.XPS); docStream.close(); // Save document to file in the XPS format with specified options // Render 3 pages starting from page 2; pages 2, 3 and 4 XpsSaveOptions xpsOptions = new XpsSaveOptions(); xpsOptions.setSaveFormat(SaveFormat.XPS); xpsOptions.setPageIndex(1); xpsOptions.setPageCount(3); // All paragraphs in the "Heading 1" style will be included in the outline but "Heading 2" and onwards won't xpsOptions.getOutlineOptions().setHeadingsOutlineLevels(1); doc.save(getArtifactsDir() + "Rendering.SaveAsXps.PartialDocument.xps", xpsOptions);
Example:
Shows how to save a document to the Jpeg format using the Save method and the ImageSaveOptions class.// Open the document Document doc = new Document(getMyDir() + "Rendering.doc"); // Save as a Jpeg image file with default options doc.save(getArtifactsDir() + "Rendering.JpegDefaultOptions.jpg"); // Save document to an ByteArrayOutputStream as a Jpeg with default options ByteArrayOutputStream docStream = new ByteArrayOutputStream(); doc.save(docStream, SaveFormat.JPEG); // Save document to a Jpeg image with specified options. // Render the third page only and set the jpeg quality to 80% // In this case we need to pass the desired SaveFormat to the ImageSaveOptions constructor // to signal what type of image to save as. ImageSaveOptions imageOptions = new ImageSaveOptions(SaveFormat.JPEG); imageOptions.setPageIndex(2); imageOptions.setPageCount(1); imageOptions.setJpegQuality(80); doc.save(getArtifactsDir() + "Rendering.JpegCustomOptions.jpg", imageOptions);
Example:
Shows how to save a document to the PDF format using the Save method and the PdfSaveOptions class.// Open the document Document doc = new Document(getMyDir() + "Rendering.doc"); // Option 1: Save document to file in the PDF format with default options doc.save(getArtifactsDir() + "Rendering.PdfDefaultOptions.pdf"); // Option 2: Save the document to stream in the PDF format with default options ByteArrayOutputStream stream = new ByteArrayOutputStream(); doc.save(stream, SaveFormat.PDF); // Option 3: Save document to the PDF format with specified options // Render the first page only and preserve form fields as usable controls and not as plain text PdfSaveOptions pdfOptions = new PdfSaveOptions(); pdfOptions.setPageIndex(0); pdfOptions.setPageCount(1); pdfOptions.setPreserveFormFields(true); doc.save(getArtifactsDir() + "Rendering.PdfCustomOptions.pdf", pdfOptions);
Example:
Converts every page of a DOC file into a separate scalable EMF file.Document doc = new Document(getMyDir() + "Rendering.doc"); ImageSaveOptions options = new ImageSaveOptions(SaveFormat.EMF); options.setPageCount(1); for (int i = 0; i < doc.getPageCount(); i++) { options.setPageIndex(i); doc.save(getArtifactsDir() + "Rendering.SaveToEmf." + Integer.toString(i) + ".emf", options); }
Example:
Converts a page of a Word document into a TIFF image and uses the CCITT compression.Document doc = new Document(getMyDir() + "Rendering.doc"); ImageSaveOptions options = new ImageSaveOptions(SaveFormat.TIFF); options.setTiffCompression(TiffCompression.CCITT_3); options.setPageIndex(0); options.setPageCount(1); doc.save(getArtifactsDir() + "Rendering.SaveToTiffCompression.tiff", options);
Example:
Converts a whole document to PDF with three levels in the document outline.Document doc = new Document(getMyDir() + "Rendering.doc"); PdfSaveOptions options = new PdfSaveOptions(); options.getOutlineOptions().setHeadingsOutlineLevels(3); options.getOutlineOptions().setExpandedOutlineLevels(1); doc.save(getArtifactsDir() + "Rendering.SaveToPdfWithOutline.pdf", options);
save | |
public SaveOutputParameters save(java.lang.String fileName, int saveFormat) 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.saveFormat
- A Example:
Converts from DOC to HTML format.Document doc = new Document(getMyDir() + "Document.doc"); doc.save(getArtifactsDir() + "Document.ConvertToHtml.html", SaveFormat.HTML);
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:
Shows how to select certain nodes by using an XPath expression.Document doc = new Document(getMyDir() + "Table.Document.doc"); // This expression will extract all paragraph nodes which are descendants of any table node in the document. // This will return any paragraphs which are in a table. NodeList nodeList = doc.selectNodes("//Table//Paragraph"); // Iterate through the list with an enumerator and print the contents of every paragraph in each cell of the table int index = 0; Iterator<Node> e = nodeList.iterator(); while (e.hasNext()) { Node currentNode = e.next(); System.out.println(MessageFormat.format("Table paragraph index {0}, contents: \"{1}\"", index++, currentNode.getText().trim())); } // This expression will select any paragraphs that are direct children of any body node in the document. nodeList = doc.selectNodes("//Body/Paragraph"); // We can treat the list as an array too Assert.assertEquals(nodeList.toArray().length, 4); // Use SelectSingleNode to select the first result of the same expression as above. Node node = doc.selectSingleNode("//Body/Paragraph");
Example:
Shows how to test if a node is inside a field by using an XPath expression.// Let's pick a document we know has some fields in. Document doc = new Document(getMyDir() + "MailMerge.MergeImage.doc"); // Let's say we want to check if the Run below is inside a field. Run run = (Run) doc.getChild(NodeType.RUN, 5, true); // Evaluate the XPath expression. The resulting NodeList will contain all nodes found inside a field a field (between FieldStart // and FieldEnd exclusive). There can however be FieldStart and FieldEnd nodes in the list if there are nested fields // in the path. Currently does not find rare fields in which the FieldCode or FieldResult spans across multiple paragraphs. NodeList resultList = doc.selectNodes("//FieldStart/following-sibling::node()[following-sibling::FieldEnd]"); // Check if the specified run is one of the nodes that are inside the field. for (Node node : (Iterable<Node>) resultList) { if (node == run) { System.out.println("The node is found inside a field"); break; } }
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.Example:
Shows how to select certain nodes by using an XPath expression.Document doc = new Document(getMyDir() + "Table.Document.doc"); // This expression will extract all paragraph nodes which are descendants of any table node in the document. // This will return any paragraphs which are in a table. NodeList nodeList = doc.selectNodes("//Table//Paragraph"); // Iterate through the list with an enumerator and print the contents of every paragraph in each cell of the table int index = 0; Iterator<Node> e = nodeList.iterator(); while (e.hasNext()) { Node currentNode = e.next(); System.out.println(MessageFormat.format("Table paragraph index {0}, contents: \"{1}\"", index++, currentNode.getText().trim())); } // This expression will select any paragraphs that are direct children of any body node in the document. nodeList = doc.selectNodes("//Body/Paragraph"); // We can treat the list as an array too Assert.assertEquals(nodeList.toArray().length, 4); // Use SelectSingleNode to select the first result of the same expression as above. Node node = doc.selectSingleNode("//Body/Paragraph");
startTrackRevisions | |
public void startTrackRevisions(java.lang.String author) |
If you call this method and then make some changes to the document programmatically, save the document and later open the document in MS Word you will see these changes as revisions.
Currently Aspose.Words supports tracking of node insertions and deletions only. Formatting changes are not recorded as revisions.
Automatic tracking of changes is supported both when modifying this document through node manipulations
as well as when using
This method does not change the
author
- Initials of the author to use for revisions.Example:
Shows how tracking revisions affects document editing.Document doc = new Document(); // This text will appear as normal text in the document and no revisions will be counted. doc.getFirstSection().getBody().getFirstParagraph().getRuns().add(new Run(doc, "Hello world!")); System.out.println(doc.getRevisions().getCount()); // 0 doc.startTrackRevisions("Author"); // This text will appear as a revision. // We did not specify a time while calling StartTrackRevisions(), so the date/time that's noted // on the revision will be the real time when StartTrackRevisions() executes. doc.getFirstSection().getBody().appendParagraph("Hello again!"); System.out.println(doc.getRevisions().getCount()); // 2 // Stopping the tracking of revisions makes this text appear as normal text. // Revisions are not counted when the document is changed. doc.stopTrackRevisions(); doc.getFirstSection().getBody().appendParagraph("Hello again!"); System.out.println(doc.getRevisions().getCount()); // 2 // Specifying some date/time will apply that date/time to all subsequent revisions until StopTrackRevisions() is called. // Note that placing values such as DateTime.MinValue as an argument will create revisions that do not have a date/time at all. doc.startTrackRevisions("Author", new SimpleDateFormat("yyyy/MM/DD").parse("1970/01/01")); doc.getFirstSection().getBody().appendParagraph("Hello again!"); System.out.println(doc.getRevisions().getCount()); // 4 doc.save(getArtifactsDir() + "Document.StartTrackRevisions.doc");
startTrackRevisions | |
public void startTrackRevisions(java.lang.String author, java.util.Date dateTime) |
If you call this method and then make some changes to the document programmatically, save the document and later open the document in MS Word you will see these changes as revisions.
Currently Aspose.Words supports tracking of node insertions and deletions only. Formatting changes are not recorded as revisions.
Automatic tracking of changes is supported both when modifying this document through node manipulations
as well as when using
This method does not change the
author
- Initials of the author to use for revisions.dateTime
- The date and time to use for revisions.Example:
Shows how tracking revisions affects document editing.Document doc = new Document(); // This text will appear as normal text in the document and no revisions will be counted. doc.getFirstSection().getBody().getFirstParagraph().getRuns().add(new Run(doc, "Hello world!")); System.out.println(doc.getRevisions().getCount()); // 0 doc.startTrackRevisions("Author"); // This text will appear as a revision. // We did not specify a time while calling StartTrackRevisions(), so the date/time that's noted // on the revision will be the real time when StartTrackRevisions() executes. doc.getFirstSection().getBody().appendParagraph("Hello again!"); System.out.println(doc.getRevisions().getCount()); // 2 // Stopping the tracking of revisions makes this text appear as normal text. // Revisions are not counted when the document is changed. doc.stopTrackRevisions(); doc.getFirstSection().getBody().appendParagraph("Hello again!"); System.out.println(doc.getRevisions().getCount()); // 2 // Specifying some date/time will apply that date/time to all subsequent revisions until StopTrackRevisions() is called. // Note that placing values such as DateTime.MinValue as an argument will create revisions that do not have a date/time at all. doc.startTrackRevisions("Author", new SimpleDateFormat("yyyy/MM/DD").parse("1970/01/01")); doc.getFirstSection().getBody().appendParagraph("Hello again!"); System.out.println(doc.getRevisions().getCount()); // 4 doc.save(getArtifactsDir() + "Document.StartTrackRevisions.doc");
stopTrackRevisions | |
public void stopTrackRevisions() |
Example:
Shows how tracking revisions affects document editing.Document doc = new Document(); // This text will appear as normal text in the document and no revisions will be counted. doc.getFirstSection().getBody().getFirstParagraph().getRuns().add(new Run(doc, "Hello world!")); System.out.println(doc.getRevisions().getCount()); // 0 doc.startTrackRevisions("Author"); // This text will appear as a revision. // We did not specify a time while calling StartTrackRevisions(), so the date/time that's noted // on the revision will be the real time when StartTrackRevisions() executes. doc.getFirstSection().getBody().appendParagraph("Hello again!"); System.out.println(doc.getRevisions().getCount()); // 2 // Stopping the tracking of revisions makes this text appear as normal text. // Revisions are not counted when the document is changed. doc.stopTrackRevisions(); doc.getFirstSection().getBody().appendParagraph("Hello again!"); System.out.println(doc.getRevisions().getCount()); // 2 // Specifying some date/time will apply that date/time to all subsequent revisions until StopTrackRevisions() is called. // Note that placing values such as DateTime.MinValue as an argument will create revisions that do not have a date/time at all. doc.startTrackRevisions("Author", new SimpleDateFormat("yyyy/MM/DD").parse("1970/01/01")); doc.getFirstSection().getBody().appendParagraph("Hello again!"); System.out.println(doc.getRevisions().getCount()); // 4 doc.save(getArtifactsDir() + "Document.StartTrackRevisions.doc");
toString | → inherited from Node |
public java.lang.String toString(SaveOptions saveOptions) throws java.lang.Exception |
saveOptions
- Specifies the options that control how the node is saved.Example:
Exports the content of a node to string in HTML format using custom specified options.Document doc = new Document(getMyDir() + "Document.doc"); // Extract the last paragraph in the document to convert to HTML. Node node = doc.getLastSection().getBody().getLastParagraph(); // Create an instance of HtmlSaveOptions and set a few options. HtmlSaveOptions saveOptions = new HtmlSaveOptions(); saveOptions.setExportHeadersFootersMode(ExportHeadersFootersMode.PER_SECTION); saveOptions.setExportRelativeFontSize(true); // Convert the document to HTML and return as a string. Pass the instance of HtmlSaveOptions to // to use the specified options during the conversion. String nodeAsHtml = node.toString(saveOptions);
toString | → inherited from Node |
public java.lang.String toString(int saveFormat) throws java.lang.Exception |
saveFormat
- A SaveFormat value.Example:
Shows how to extract the label of each paragraph in a list as a value or a String.Document doc = new Document(getMyDir() + "Lists.PrintOutAllLists.doc"); doc.updateListLabels(); int listParaCount = 1; for (Paragraph paragraph : (Iterable<Paragraph>) doc.getChildNodes(NodeType.PARAGRAPH, true)) { // Find if we have the paragraph list. In our document our list uses plain arabic numbers, // which start at three and ends at six. if (paragraph.getListFormat().isListItem()) { System.out.println(MessageFormat.format("Paragraph #{0}", listParaCount)); // This is the text we get when actually getting when we output this node to text format. // The list labels are not included in this text output. Trim any paragraph formatting characters. String paragraphText = paragraph.toString(SaveFormat.TEXT).trim(); System.out.println("Exported Text: " + paragraphText); ListLabel label = paragraph.getListLabel(); // This gets the position of the paragraph in current level of the list. If we have a list with multiple level then this // will tell us what position it is on that particular level. System.out.println("Numerical Id: " + label.getLabelValue()); // Combine them together to include the list label with the text in the output. System.out.println("List label combined with text: " + label.getLabelString() + " " + paragraphText); listParaCount++; } }
Example:
Shows the difference between calling the GetText and ToString methods on a node.Document doc = new Document(); // Enter a dummy field into the document. DocumentBuilder builder = new DocumentBuilder(doc); builder.insertField("MERGEFIELD Field"); // GetText will retrieve all field codes and special characters System.out.println("GetText() Result: " + doc.getText()); // ToString will export the node to the specified format. When converted to text it will not retrieve fields code // or special characters, but will still contain some natural formatting characters such as paragraph markers etc. // This is the same as "viewing" the document as if it was opened in a text editor. System.out.println("ToString() Result: " + doc.toString(SaveFormat.TEXT));
Example:
Exports the content of a node to string in HTML format using default options.Document doc = new Document(getMyDir() + "Document.doc"); // Extract the last paragraph in the document to convert to HTML. Node node = doc.getLastSection().getBody().getLastParagraph(); // When ToString is called using the SaveFormat overload then conversion is executed using default save options. // When saving to HTML using default options the following settings are set: // ExportImagesAsBase64 = true // CssStyleSheetType = CssStyleSheetType.Inline // ExportFontResources = false String nodeAsHtml = node.toString(SaveFormat.HTML);
unlinkFields | |
public void unlinkFields() throws java.lang.Exception |
Replaces all the fields in the whole document with their most recent results.
To unlink fields in a specific part of the document use
Example:
Shows how to unlink all fields in the documentDocument doc = new Document(getMyDir() + "Field.UnlinkFields.docx"); doc.unlinkFields();
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();
unprotect | |
public boolean unprotect(java.lang.String password) |
This method unprotects the document only if a correct password is specified.
Note that document protection is different from write protection.
Write protection is specified using the
password
- The password to unprotect the document with.Example:
Shows how to unprotect a document using a password.doc.unprotect("password");
updateFields | |
public void updateFields() throws java.lang.Exception |
When you open, modify and then save a document, Aspose.Words does not update fields automatically, it keeps them intact. Therefore, you would usually want to call this method before saving if you have modified the document programmatically and want to make sure the proper (calculated) field values appear in the saved document.
There is no need to update fields after executing a mail merge because mail merge is a kind of field update and automatically updates all fields in the document.
This method does not update all field types. For the detailed list of supported field types, see the Programmers Guide.
This method does not update fields that are related to the page layout algorithms (e.g. PAGE, PAGES, PAGEREF).
The page layout-related fields are updated when you render a document or call
Use the
To update fields in a specific part of the document use
Example:
Demonstrates how to insert a Table of contents (TOC) into a document using heading styles as entries.// Use a blank document Document doc = new Document(); // Create a document builder to insert content with into document. DocumentBuilder builder = new DocumentBuilder(doc); // Insert a table of contents at the beginning of the document. builder.insertTableOfContents("\\o \"1-3\" \\h \\z \\u"); // Start the actual document content on the second page. builder.insertBreak(BreakType.PAGE_BREAK); // Build a document with complex structure by applying different heading styles thus creating TOC entries. builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1); builder.writeln("Heading 1"); builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2); builder.writeln("Heading 1.1"); builder.writeln("Heading 1.2"); builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1); builder.writeln("Heading 2"); builder.writeln("Heading 3"); builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2); builder.writeln("Heading 3.1"); builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_3); builder.writeln("Heading 3.1.1"); builder.writeln("Heading 3.1.2"); builder.writeln("Heading 3.1.3"); builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2); builder.writeln("Heading 3.2"); builder.writeln("Heading 3.3"); // Call the method below to update the TOC. doc.updateFields();
Example:
Shows how to update all fields before rendering a document.Document doc = new Document(getMyDir() + "Rendering.doc"); // This updates all fields in the document. doc.updateFields(); doc.save(getArtifactsDir() + "Rendering.UpdateFields.pdf");
Example:
Shows how to update all fields in a document.Document doc = new Document(getMyDir() + "Document.doc"); doc.updateFields();
updateListLabels | |
public void updateListLabels() throws java.lang.Exception |
This method updates list label properties such as
Also, this method is sometimes implicitly called when updating fields in the document. This is required because some fields that may reference list numbers (such as TOC or REF) need them be up-to-date.
Example:
Shows how to extract the label of each paragraph in a list as a value or a String.Document doc = new Document(getMyDir() + "Lists.PrintOutAllLists.doc"); doc.updateListLabels(); int listParaCount = 1; for (Paragraph paragraph : (Iterable<Paragraph>) doc.getChildNodes(NodeType.PARAGRAPH, true)) { // Find if we have the paragraph list. In our document our list uses plain arabic numbers, // which start at three and ends at six. if (paragraph.getListFormat().isListItem()) { System.out.println(MessageFormat.format("Paragraph #{0}", listParaCount)); // This is the text we get when actually getting when we output this node to text format. // The list labels are not included in this text output. Trim any paragraph formatting characters. String paragraphText = paragraph.toString(SaveFormat.TEXT).trim(); System.out.println("Exported Text: " + paragraphText); ListLabel label = paragraph.getListLabel(); // This gets the position of the paragraph in current level of the list. If we have a list with multiple level then this // will tell us what position it is on that particular level. System.out.println("Numerical Id: " + label.getLabelValue()); // Combine them together to include the list label with the text in the output. System.out.println("List label combined with text: " + label.getLabelString() + " " + paragraphText); listParaCount++; } }
updatePageLayout | |
public void updatePageLayout() throws java.lang.Exception |
This method formats a document into pages and updates the page number related fields in the document such as PAGE, PAGES, PAGEREF and REF. The up-to-date page layout information is required for a correct rendering of the document to fixed-page formats.
This method is automatically invoked when you first convert a document to PDF, XPS, image or print it.
However, if you modify the document after rendering and then attempt to render it again - Aspose.Words will not
update the page layout automatically. In this case you should call
Example:
Shows when to request page layout of the document to be recalculated.Document doc = new Document(getMyDir() + "Rendering.doc"); // Saving a document to PDF or to image or printing for the first time will automatically // layout document pages and this information will be cached inside the document. doc.save(getArtifactsDir() + "Rendering.UpdatePageLayout1.pdf"); // Modify the document in any way. doc.getStyles().get("Normal").getFont().setSize(6); doc.getSections().get(0).getPageSetup().setOrientation(com.aspose.words.Orientation.LANDSCAPE); // In the current version of Aspose.Words, modifying the document does not automatically rebuild // the cached page layout. If you want to save to PDF or render a modified document again, // you need to manually request page layout to be updated. doc.updatePageLayout(); doc.save(getArtifactsDir() + "Rendering.UpdatePageLayout2.pdf");
updateTableLayout | |
public void updateTableLayout() |
You do not normally need to call this method as cell and table widths are maintained automatically. You can call this method before exporting to PDF (or any other fixed-page format), only in rare cases where you confirmed that tables appear incorrectly laid out in the output document. Calling this method might help to correct the output.
Example:
Shows how to update the layout of tables in a document.Document doc = new Document(getMyDir() + "Document.doc"); // Normally this method is not necessary to call, as cell and table widths are maintained automatically. // This method may need to be called when exporting to PDF in rare cases when the table layout appears // incorrectly in the rendered output. doc.updateTableLayout();
updateThumbnail | |
public void updateThumbnail() throws java.lang.Exception |
Example:
Shows how to update a document's thumbnail.Document doc = new Document(); // Update document's thumbnail the default way. doc.updateThumbnail(); // Review/change thumbnail options and then update document's thumbnail. ThumbnailGeneratingOptions tgo = new ThumbnailGeneratingOptions(); System.out.println(MessageFormat.format("Thumbnail size: {0}", tgo.getThumbnailSize())); tgo.setGenerateFromFirstPage(true); doc.updateThumbnail(tgo);
updateThumbnail | |
public void updateThumbnail(ThumbnailGeneratingOptions options) throws java.lang.Exception |
options
- The generating options to use.Example:
Shows how to update a document's thumbnail.Document doc = new Document(); // Update document's thumbnail the default way. doc.updateThumbnail(); // Review/change thumbnail options and then update document's thumbnail. ThumbnailGeneratingOptions tgo = new ThumbnailGeneratingOptions(); System.out.println(MessageFormat.format("Thumbnail size: {0}", tgo.getThumbnailSize())); tgo.setGenerateFromFirstPage(true); doc.updateThumbnail(tgo);
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.
Use the
When you use an evaluation version, the evaluation watermark will also be included in the word count.
Example:
Shows how to update all list labels in a document.Document doc = new Document(getMyDir() + "Document.doc"); // Some work should be done here that changes the document's content. // Update the word, character and paragraph count of the document. doc.updateWordCount(); // Display the updated document properties. System.out.println(MessageFormat.format("Characters: {0}", doc.getBuiltInDocumentProperties().getCharacters())); System.out.println(MessageFormat.format("Words: {0}", doc.getBuiltInDocumentProperties().getWords())); System.out.println(MessageFormat.format("Paragraphs: {0}", doc.getBuiltInDocumentProperties().getParagraphs()));
updateWordCount | |
public void updateWordCount(boolean updateLinesCount) throws java.lang.Exception |
updateLinesCount
- True if number of lines in the document shall be calculated.Example:
Shows how to keep track of the word count.// Create an empty document Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.writeln("This is the first line."); builder.writeln("This is the second line."); builder.writeln("These three lines contain eighteen words in total."); // The fields that keep track of how many lines and words a document has are not automatically updated // An empty document has one paragraph by default, which contains one empty line Assert.assertEquals(doc.getBuiltInDocumentProperties().getWords(), 0); Assert.assertEquals(doc.getBuiltInDocumentProperties().getLines(), 1); // To update them we have to use this method // The default constructor updates just the word count doc.updateWordCount(); Assert.assertEquals(doc.getBuiltInDocumentProperties().getWords(), 18); Assert.assertEquals(doc.getBuiltInDocumentProperties().getLines(), 1); // If we want to update the line count as well, we have to use this overload doc.updateWordCount(true); Assert.assertEquals(doc.getBuiltInDocumentProperties().getWords(), 18); Assert.assertEquals(doc.getBuiltInDocumentProperties().getLines(), 3);