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:
Shows how to execute a mail merge with data from a DataTable.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.insertField(" MERGEFIELD CustomerName "); builder.insertParagraph(); builder.insertField(" MERGEFIELD Address "); // This example creates a table, but you would normally load table from a database DataTable table = new DataTable("Test"); table.getColumns().add("CustomerName"); table.getColumns().add("Address"); table.getRows().add("Thomas Hardy", "120 Hanover Sq., London"); table.getRows().add("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.docx"); // Create a copy of our document to perform another mail merge doc = new Document(); builder = new DocumentBuilder(doc); builder.insertField(" MERGEFIELD CustomerName "); builder.insertParagraph(); builder.insertField(" MERGEFIELD Address "); // We can also source values for a mail merge from a single row in the table doc.getMailMerge().execute(table.getRows().get(1)); doc.save(getArtifactsDir() + "MailMerge.ExecuteDataTable.OneRow.docx");
Constructor Summary |
---|
Document()
Creates a blank Word document. |
Document(java.lang.StringfileName)
Opens an existing document from a file. Automatically detects the file format. |
Document(java.lang.StringfileName, LoadOptions loadOptions)
Opens an existing document from a file. Allows to specify additional options such as an encryption password. |
Document(java.io.InputStreamstream)
Opens an existing document from a stream. Automatically detects the file format. |
Document(java.io.InputStreamstream, 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.Stringvalue) | |
Gets or sets the full path of the template attached to the document. | ||
boolean | getAutomaticallyUpdateStyles() | |
void | setAutomaticallyUpdateStyles(booleanvalue) | |
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() | |
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() | |
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() | |
Gets the number of immediate children of this node.
|
||
CustomDocumentProperties | getCustomDocumentProperties() | |
Returns a collection that represents all the custom document properties of the document.
|
||
int | getCustomNodeId() | |
void | setCustomNodeId(intvalue) | |
Specifies custom node identifier. | ||
CustomXmlPartCollection | getCustomXmlParts() | |
void | ||
Gets or sets the collection of Custom XML Data Storage Parts. | ||
double | getDefaultTabStop() | |
void | setDefaultTabStop(doublevalue) | |
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() | |
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() | |
Gets the first child of the node.
|
||
Section | getFirstSection() | |
Gets the first section in the document.
|
||
FontInfoCollection | getFontInfos() | |
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.
|
||
Aspose.Words.Framesets.Frameset | getFrameset() | |
Returns a |
||
GlossaryDocument | getGlossaryDocument() | |
void | ||
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 | getGrammarChecked() | |
void | setGrammarChecked(booleanvalue) | |
Returns true if the document has been checked for grammar. | ||
boolean | hasChildNodes() | |
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() | |
Returns true as this node can have child nodes.
|
||
Node | getLastChild() | |
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() | |
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 | ||
Gets or sets the object that contains all of the mail merge information for a document. | ||
Node | getNextSibling() | |
Gets the node immediately following this node.
|
||
INodeChangingCallback | getNodeChangingCallback() | |
void | ||
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 | ||
Gets or sets the collection of custom parts (arbitrary content) that are linked to the OOXML package using "unknown relationships". | ||
java.awt.Color | getPageColor() | |
void | setPageColor(java.awt.Colorvalue) | |
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() | |
Gets the immediate parent of this node.
|
||
Node | getPreviousSibling() | |
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() | |
Returns a Range object that represents the portion of a document that is contained in this node.
|
||
boolean | getRemovePersonalInformation() | |
void | setRemovePersonalInformation(booleanvalue) | |
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() | |
void | ||
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(intvalue) | |
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(booleanvalue) | |
Specifies whether to turn on the gray shading on form fields. | ||
boolean | getShowGrammaticalErrors() | |
void | setShowGrammaticalErrors(booleanvalue) | |
Specifies whether to display grammar errors in this document. | ||
boolean | getShowSpellingErrors() | |
void | setShowSpellingErrors(booleanvalue) | |
Specifies whether to display spelling errors in this document. | ||
boolean | getSpellingChecked() | |
void | setSpellingChecked(booleanvalue) | |
Returns true if the document has been checked for spelling. | ||
StyleCollection | getStyles() | |
Returns a collection of styles defined in the document.
|
||
Theme | getTheme() | |
Gets the |
||
boolean | getTrackRevisions() | |
void | setTrackRevisions(booleanvalue) | |
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() | |
void | setVbaProject(VbaProject value) | |
Gets or sets a |
||
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() | |
void | ||
Called during various document processing procedures when an issue is detected that might result in data or formatting fidelity loss. | ||
Watermark | getWatermark() | |
Provides access to the document watermark.
|
||
TaskPaneCollection | getWebExtensionTaskPanes() | |
Returns a collection that represents a list of task pane add-ins.
|
||
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) | |
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) | |
Creates a duplicate of the 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.
|
||
Document | extractPages(int index, int count) | |
Returns the |
||
CompositeNode | getAncestor(int ancestorType) | |
Gets the first ancestor of the specified |
||
CompositeNode | getAncestor(java.lang.Class ancestorType) | |
Gets the first ancestor of the specified object type.
|
||
Node | getChild(int nodeType, int index, boolean isDeep) | |
Returns an Nth child node that matches the specified type.
|
||
NodeCollection | getChildNodes(int nodeType, boolean isDeep) | |
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() | |
Gets the text of this node and of all its children.
|
||
Node | importNode(Node srcNode, boolean isImportChildren) | |
Imports a node from another document to the current document. |
||
Node | importNode(Node srcNode, boolean isImportChildren, int importFormatMode) | |
Imports a node from another document to the current document with an option to control formatting. |
||
int | indexOf(Node child) | |
Returns the index of the specified child node in the child node array.
|
||
Node | insertAfter(Node newChild, Node refChild) | |
Inserts the specified node immediately after the specified reference node.
|
||
Node | insertBefore(Node newChild, Node refChild) | |
Inserts the specified node immediately before the specified reference node.
|
||
java.util.Iterator<Node> | iterator() | |
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) | |
Gets next node according to the pre-order tree traversal algorithm.
|
||
void | normalizeFieldTypes() | |
Changes field type values |
||
Node | prependChild(Node newChild) | |
Adds the specified node to the beginning of the list of child nodes for this node.
|
||
Node | previousPreOrder(Node rootNode) | |
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() | |
Deprecated. Reserved for internal use. |
||
void | removeAllChildren() | |
Removes all the child nodes of the current node.
|
||
Node | removeChild(Node oldChild) | |
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() | |
Removes all |
||
java.awt.Dimension | 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) | |
Renders a document page into a |
||
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) | |
Selects a list of nodes matching the XPath expression.
|
||
Node | selectSingleNode(java.lang.String xpath) | |
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) | |
Exports the content of the node into a string using the specified save options.
|
||
java.lang.String | toString(int saveFormat) | |
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() | |
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 |
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 create and load documents.// There are two ways of creating a Document object using Aspose.Words. // 1 - Create a blank document: Document doc = new Document(); // New Document objects by default come with the minimal set of nodes // required to begin adding content such as text and shapes: a Section, a Body, and a Paragraph. doc.getFirstSection().getBody().getFirstParagraph().appendChild(new Run(doc, "Hello world!")); // 2 - Load a document that exists in the local file system: doc = new Document(getMyDir() + "Document.docx"); // Loaded documents will have contents that we can access and edit. Assert.assertEquals("Hello World!", doc.getFirstSection().getBody().getFirstParagraph().getText().trim()); // Some operations that need to occur during loading, such as using a password to decrypt a document, // can be done by passing a LoadOptions object when loading the document. doc = new Document(getMyDir() + "Encrypted.docx", new LoadOptions("docPassword")); Assert.assertEquals("Test encrypted document.", doc.getFirstSection().getBody().getFirstParagraph().getText().trim());
Example:
Shows how to format a run of text using its font property.Document doc = new Document(); Run run = new Run(doc, "Hello world!"); Font font = run.getFont(); font.setName("Courier New"); font.setSize(36.0); font.setHighlightColor(Color.YELLOW); doc.getFirstSection().getBody().getFirstParagraph().appendChild(run); doc.save(getArtifactsDir() + "Font.CreateFormattedRun.docx");
public Document(java.lang.String fileName) throws java.lang.Exception
fileName
- File name of the document to open.Example:
Shows how to open a document and convert it to .PDF.Document doc = new Document(getMyDir() + "Document.docx"); doc.save(getArtifactsDir() + "Document.ConvertToPdf.pdf");
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:
Shows how to create and load documents.// There are two ways of creating a Document object using Aspose.Words. // 1 - Create a blank document: Document doc = new Document(); // New Document objects by default come with the minimal set of nodes // required to begin adding content such as text and shapes: a Section, a Body, and a Paragraph. doc.getFirstSection().getBody().getFirstParagraph().appendChild(new Run(doc, "Hello world!")); // 2 - Load a document that exists in the local file system: doc = new Document(getMyDir() + "Document.docx"); // Loaded documents will have contents that we can access and edit. Assert.assertEquals("Hello World!", doc.getFirstSection().getBody().getFirstParagraph().getText().trim()); // Some operations that need to occur during loading, such as using a password to decrypt a document, // can be done by passing a LoadOptions object when loading the document. doc = new Document(getMyDir() + "Encrypted.docx", new LoadOptions("docPassword")); Assert.assertEquals("Test encrypted document.", doc.getFirstSection().getBody().getFirstParagraph().getText().trim());
Example:
Shows how to load an encrypted Microsoft Word document.Document doc; // Aspose.Words throw an exception if we try to open an encrypted document without its password. Assert.assertThrows(IncorrectPasswordException.class, () -> new Document(getMyDir() + "Encrypted.docx")); // When loading such a document, the password is passed to the document's constructor using a LoadOptions object. LoadOptions options = new LoadOptions("docPassword"); // There are two ways of loading an encrypted document with a LoadOptions object. // 1 - Load the document from the local file system by filename: doc = new Document(getMyDir() + "Encrypted.docx", options); // 2 - Load the document from a stream: InputStream stream = new FileInputStream(getMyDir() + "Encrypted.docx"); try { doc = new Document(stream, options); if (stream != null) stream.close(); }
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:
Shows how to retrieve a document from a URL and saves it to disk in a different format.// This is the URL address pointing to where to find the document URL url = new URL("https://omextemplates.content.office.net/support/templates/en-us/tf16402488.dotx"); // 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 and save doc.save(getArtifactsDir() + "Document.OpenDocumentFromWeb.docx");
Example:
Shows how to load a document using a stream.InputStream stream = new FileInputStream(getMyDir() + "Document.docx"); try { Document doc = new Document(stream); Assert.assertEquals("Hello World!", doc.getFirstSection().getBody().getText().trim()); } finally { if (stream != null) stream.close(); }
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.URL url = new URL("https://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); doc.save(getArtifactsDir() + "Document.InsertHtmlFromWebPage.docx");
Example:
Shows how to open an HTML document with images from a stream using a base URI.InputStream stream = new FileInputStream(getMyDir() + "Document.html"); try /*JAVA: was using*/ { // Pass the URI of the base folder while loading it // so that 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); // Verify that the first shape of the document contains a valid image. Shape shape = (Shape) doc.getChild(NodeType.SHAPE, 0, true); Assert.assertTrue(shape.isImage()); Assert.assertNotNull(shape.getImageData().getImageBytes()); Assert.assertEquals(32.0, ConvertUtil.pointToPixel(shape.getWidth()), 0.01); Assert.assertEquals(32.0, ConvertUtil.pointToPixel(shape.getHeight()), 0.01); } finally { if (stream != null) stream.close(); }
Example:
Shows how to load an encrypted Microsoft Word document.Document doc; // Aspose.Words throw an exception if we try to open an encrypted document without its password. Assert.assertThrows(IncorrectPasswordException.class, () -> new Document(getMyDir() + "Encrypted.docx")); // When loading such a document, the password is passed to the document's constructor using a LoadOptions object. LoadOptions options = new LoadOptions("docPassword"); // There are two ways of loading an encrypted document with a LoadOptions object. // 1 - Load the document from the local file system by filename: doc = new Document(getMyDir() + "Encrypted.docx", options); // 2 - Load the document from a stream: InputStream stream = new FileInputStream(getMyDir() + "Encrypted.docx"); try { doc = new Document(stream, options); if (stream != null) stream.close(); }
public java.lang.String getAttachedTemplate() / public void setAttachedTemplate(java.lang.String value)
Empty string means the document is attached to the Normal template.
Example:
Shows how to set a default template for documents that do not have attached templates.Document doc = new Document(); // Enable automatic style updating, but do not attach a template document. doc.setAutomaticallyUpdateStyles(true); Assert.assertEquals("", doc.getAttachedTemplate()); // Since there is no template document, the document had nowhere to track style changes. // Use a SaveOptions object to automatically set a template // if a document that we are saving does not have one. SaveOptions options = SaveOptions.createSaveOptions("Document.DefaultTemplate.docx"); options.setDefaultTemplate(getMyDir() + "Business brochure.dotx"); doc.save(getArtifactsDir() + "Document.DefaultTemplate.docx", options);
public boolean getAutomaticallyUpdateStyles() / public void setAutomaticallyUpdateStyles(boolean value)
Example:
Shows how to attach a template to a document.Document doc = new Document(); // Microsoft Word documents by default come with an attached template called "Normal.dotm". // There is no default template for blank Aspose.Words documents. Assert.assertEquals("", doc.getAttachedTemplate()); // Attach a template, then set the flag to apply style changes // within the template to styles in our document. doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx"); doc.setAutomaticallyUpdateStyles(true); doc.save(getArtifactsDir() + "Document.AutomaticallyUpdateStyles.docx");
Example:
Shows how to set a default template for documents that do not have attached templates.Document doc = new Document(); // Enable automatic style updating, but do not attach a template document. doc.setAutomaticallyUpdateStyles(true); Assert.assertEquals("", doc.getAttachedTemplate()); // Since there is no template document, the document had nowhere to track style changes. // Use a SaveOptions object to automatically set a template // if a document that we are saving does not have one. SaveOptions options = SaveOptions.createSaveOptions("Document.DefaultTemplate.docx"); options.setDefaultTemplate(getMyDir() + "Business brochure.dotx"); doc.save(getArtifactsDir() + "Document.DefaultTemplate.docx", options);
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 a background shape for every page of a document.Document doc = new Document(); Assert.assertNull(doc.getBackgroundShape()); // The only shape type that we can use as a background is a rectangle. Shape shapeRectangle = new Shape(doc, ShapeType.RECTANGLE); // There are two ways of using this shape as a page background. // 1 - A flat color: shapeRectangle.setFillColor(Color.BLUE); doc.setBackgroundShape(shapeRectangle); doc.save(getArtifactsDir() + "DocumentBase.BackgroundShape.FlatColor.docx"); // 2 - An image: shapeRectangle = new Shape(doc, ShapeType.RECTANGLE); shapeRectangle.getImageData().setImage(getImageDir() + "Transparent background logo.png"); // Adjust the image's appearance to make it more suitable as a watermark. shapeRectangle.getImageData().setContrast(0.2); shapeRectangle.getImageData().setBrightness(0.7); doc.setBackgroundShape(shapeRectangle); Assert.assertTrue(doc.getBackgroundShape().hasImage()); // Microsoft Word does not support shapes with images as backgrounds, // but we can still see these backgrounds in other save formats such as .pdf. doc.save(getArtifactsDir() + "DocumentBase.BackgroundShape.Image.pdf");
public BuiltInDocumentProperties getBuiltInDocumentProperties()
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 traverse through a composite node's collection of child nodes.Document doc = new Document(); // Add two runs and one shape as child nodes to the first paragraph of this document. Paragraph paragraph = (Paragraph) doc.getChild(NodeType.PARAGRAPH, 0, true); paragraph.appendChild(new Run(doc, "Hello world! ")); Shape shape = new Shape(doc, ShapeType.RECTANGLE); shape.setWidth(200.0); shape.setHeight(200.0); // Note that the 'CustomNodeId' is not saved to an output file and exists only during the node lifetime. shape.setCustomNodeId(100); shape.setWrapType(WrapType.INLINE); paragraph.appendChild(shape); paragraph.appendChild(new Run(doc, "Hello again!")); // Iterate through the paragraph's collection of immediate children, // and print any runs or shapes that we find within. NodeCollection children = paragraph.getChildNodes(); Assert.assertEquals(3, paragraph.getChildNodes().getCount()); for (Node child : (Iterable<Node>) children) switch (child.getNodeType()) { case NodeType.RUN: System.out.println("Run contents:"); System.out.println("\t\"{child.GetText().Trim()}\""); break; case NodeType.SHAPE: Shape childShape = (Shape) child; System.out.println("Shape:"); System.out.println("\t{childShape.ShapeType}, {childShape.Width}x{childShape.Height}"); }
public CompatibilityOptions getCompatibilityOptions()
public int getCompliance()
If you created a new blank document or load non OOXML document
returns the
Example:
Shows how to read a loaded document's Open Office XML compliance version.// The compliance version varies between documents created by different versions of Microsoft Word. Document doc = new Document(getMyDir() + "Document.doc"); Assert.assertEquals(doc.getCompliance(), OoxmlCompliance.ECMA_376_2006); doc = new Document(getMyDir() + "Document.docx"); Assert.assertEquals(doc.getCompliance(), OoxmlCompliance.ISO_29500_2008_TRANSITIONAL);
public int getCount()
Example:
Shows how to add, update and delete child nodes in a CompositeNode's collection of children.Document doc = new Document(); // An empty document, by default, has one paragraph. Assert.assertEquals(1, doc.getFirstSection().getBody().getParagraphs().getCount()); // Composite nodes such as our paragraph can contain other composite and inline nodes as children. Paragraph paragraph = doc.getFirstSection().getBody().getFirstParagraph(); Run paragraphText = new Run(doc, "Initial text. "); paragraph.appendChild(paragraphText); // Create three more run nodes. Run run1 = new Run(doc, "Run 1. "); Run run2 = new Run(doc, "Run 2. "); Run run3 = new Run(doc, "Run 3. "); // The document body will not display these runs until we insert them into a composite node // that itself is a part of the document's node tree, as we did with the first run. // We can determine where the text contents of nodes that we insert // appears in the document by specifying an insertion location relative to another node in the paragraph. Assert.assertEquals("Initial text.", paragraph.getText().trim()); // Insert the second run into the paragraph in front of the initial run. paragraph.insertBefore(run2, paragraphText); Assert.assertEquals("Run 2. Initial text.", paragraph.getText().trim()); // Insert the third run after the initial run. paragraph.insertAfter(run3, paragraphText); Assert.assertEquals("Run 2. Initial text. Run 3.", paragraph.getText().trim()); // Insert the first run to the start of the paragraph's child nodes collection. paragraph.prependChild(run1); Assert.assertEquals("Run 1. Run 2. Initial text. Run 3.", paragraph.getText().trim()); Assert.assertEquals(4, paragraph.getChildNodes(NodeType.ANY, true).getCount()); // We can modify the contents of the run by editing and deleting existing child nodes. ((Run) paragraph.getChildNodes(NodeType.RUN, true).get(1)).setText("Updated run 2. "); paragraph.getChildNodes(NodeType.RUN, true).remove(paragraphText); Assert.assertEquals("Run 1. Updated run 2. Run 3.", paragraph.getText().trim()); Assert.assertEquals(3, paragraph.getChildNodes(NodeType.ANY, true).getCount());
public CustomDocumentProperties getCustomDocumentProperties()
public int getCustomNodeId() / public void setCustomNodeId(int value)
Default is zero.
This identifier can be set and used arbitrarily. For example, as a key to get external data.
Important note, specified value is not saved to an output file and exists only during the node lifetime.
Example:
Shows how to traverse through a composite node's collection of child nodes.Document doc = new Document(); // Add two runs and one shape as child nodes to the first paragraph of this document. Paragraph paragraph = (Paragraph) doc.getChild(NodeType.PARAGRAPH, 0, true); paragraph.appendChild(new Run(doc, "Hello world! ")); Shape shape = new Shape(doc, ShapeType.RECTANGLE); shape.setWidth(200.0); shape.setHeight(200.0); // Note that the 'CustomNodeId' is not saved to an output file and exists only during the node lifetime. shape.setCustomNodeId(100); shape.setWrapType(WrapType.INLINE); paragraph.appendChild(shape); paragraph.appendChild(new Run(doc, "Hello again!")); // Iterate through the paragraph's collection of immediate children, // and print any runs or shapes that we find within. NodeCollection children = paragraph.getChildNodes(); Assert.assertEquals(3, paragraph.getChildNodes().getCount()); for (Node child : (Iterable<Node>) children) switch (child.getNodeType()) { case NodeType.RUN: System.out.println("Run contents:"); System.out.println("\t\"{child.GetText().Trim()}\""); break; case NodeType.SHAPE: Shape childShape = (Shape) child; System.out.println("Shape:"); System.out.println("\t{childShape.ShapeType}, {childShape.Width}x{childShape.Height}"); }
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 a structured document tag with custom XML data.Document doc = new Document(); // Construct an XML part that contains data and add it to the document's collection. // If we enable the "Developer" tab in Microsoft Word, // we can find elements from this collection in the "XML Mapping Pane", along with a few default elements. String xmlPartId = UUID.randomUUID().toString(); String xmlPartContent = "<root><text>Hello, World!</text></root>"; CustomXmlPart xmlPart = doc.getCustomXmlParts().add(xmlPartId, xmlPartContent); Assert.assertEquals(xmlPart.getData(), xmlPartContent.getBytes()); Assert.assertEquals(xmlPart.getId(), xmlPartId); // Below are two ways to refer to XML parts. // 1 - By an index in the custom XML part collection: Assert.assertEquals(xmlPart, doc.getCustomXmlParts().get(0)); // 2 - By GUID: Assert.assertEquals(xmlPart, doc.getCustomXmlParts().getById(xmlPartId)); // Add an XML schema association. xmlPart.getSchemas().add("http://www.w3.org/2001/XMLSchema"); // Clone a part, and then insert it into the collection. CustomXmlPart xmlPartClone = xmlPart.deepClone(); xmlPartClone.setId(UUID.randomUUID().toString()); doc.getCustomXmlParts().add(xmlPartClone); Assert.assertEquals(doc.getCustomXmlParts().getCount(), 2); // Iterate through the collection 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++; } // Use the "RemoveAt" method to remove the cloned part by index. doc.getCustomXmlParts().removeAt(1); Assert.assertEquals(doc.getCustomXmlParts().getCount(), 1); // Clone the XML parts collection, and then use the "Clear" method to remove all its elements at once. CustomXmlPartCollection customXmlParts = doc.getCustomXmlParts().deepClone(); customXmlParts.clear(); // Create a structured document tag that will display our part's contents and insert it into the document body. StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PLAIN_TEXT, MarkupLevel.BLOCK); tag.getXmlMapping().setMapping(xmlPart, "/root[1]/text[1]", ""); doc.getFirstSection().getBody().appendChild(tag); doc.save(getArtifactsDir() + "StructuredDocumentTag.CustomXml.docx");
public double getDefaultTabStop() / public void setDefaultTabStop(double value)
Example:
Shows how to set a custom interval for tab stop positions.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Set tab stops to appear every 72 points (1 inch). builder.getDocument().setDefaultTabStop(72.0); // Each tab character snaps the text after it to the next closest tab stop position. builder.writeln("Hello" + ControlChar.TAB + "World!"); builder.writeln("Hello" + ControlChar.TAB_CHAR + "World!");
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 and display information about each signature in a document.Document doc = new Document(getMyDir() + "Digitally signed.docx"); for (DigitalSignature signature : doc.getDigitalSignatures()) { System.out.println("*** Signature Found ***"); System.out.println("Is valid: " + signature.isValid()); // This property is available in MS Word documents only System.out.println("Reason for signing: " + signature.getComments()); System.out.println("Signature type: " + 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(); }
Example:
Shows how to sign documents with X.509 certificates.// Verify that a document is not signed. Assert.assertFalse(FileFormatUtil.detectFileFormat(getMyDir() + "Document.docx").hasDigitalSignature()); // Create a CertificateHolder object from a PKCS12 file, which we will use to sign the document. CertificateHolder certificateHolder = CertificateHolder.create(getMyDir() + "morzal.pfx", "aw", null); SignOptions signOptions = new SignOptions(); signOptions.setSignTime(new Date()); // There are two ways of saving a signed copy of a document to the local file system: // 1 - Designate a document by a local system filename and save a signed copy at a location specified by another filename. DigitalSignatureUtil.sign(getMyDir() + "Document.docx", getArtifactsDir() + "Document.DigitalSignature.docx", certificateHolder, signOptions); Assert.assertTrue(FileFormatUtil.detectFileFormat(getArtifactsDir() + "Document.DigitalSignature.docx").hasDigitalSignature()); // 2 - Take a document from a stream, and save a signed copy to another stream. InputStream inDoc = new FileInputStream(getMyDir() + "Document.docx"); try { OutputStream outDoc = new FileOutputStream(getArtifactsDir() + "Document.DigitalSignature.docx"); try { DigitalSignatureUtil.sign(inDoc, outDoc, certificateHolder); } finally { if (outDoc != null) outDoc.close(); } } finally { if (inDoc != null) inDoc.close(); } Assert.assertTrue(FileFormatUtil.detectFileFormat(getArtifactsDir() + "Document.DigitalSignature.docx").hasDigitalSignature()); // Please verify that all of the document's digital signatures are valid and check their details. Document signedDoc = new Document(getArtifactsDir() + "Document.DigitalSignature.docx"); DigitalSignatureCollection digitalSignatureCollection = signedDoc.getDigitalSignatures(); Assert.assertTrue(digitalSignatureCollection.isValid()); Assert.assertEquals(1, digitalSignatureCollection.getCount()); Assert.assertEquals(DigitalSignatureType.XML_DSIG, digitalSignatureCollection.get(0).getSignatureType()); Assert.assertEquals("CN=Morzal.Me", signedDoc.getDigitalSignatures().get(0).getIssuerName()); Assert.assertEquals("CN=Morzal.Me", signedDoc.getDigitalSignatures().get(0).getSubjectName());
public DocumentBase getDocument()
public EndnoteOptions getEndnoteOptions()
Example:
Shows how to select a different place where the document collects and displays its endnotes.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // An endnote is a way to attach a reference or a side comment to text // that does not interfere with the main body text's flow. // Inserting an endnote adds a small superscript reference symbol // at the main body text where we insert the endnote. // Each endnote also creates an entry at the end of the document, consisting of a symbol // that matches the reference symbol in the main body text. // The reference text that we pass to the document builder's "InsertEndnote" method. builder.write("Hello world!"); builder.insertFootnote(FootnoteType.ENDNOTE, "Endnote contents."); builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE); builder.write("This is the second section."); // We can use the "Position" property to determine where the document will place all its endnotes. // If we set the value of the "Position" property to "EndnotePosition.EndOfDocument", // every footnote will show up in a collection at the end of the document. This is the default value. // If we set the value of the "Position" property to "EndnotePosition.EndOfSection", // every footnote will show up in a collection at the end of the section whose text contains the endnote's reference mark. doc.getEndnoteOptions().setPosition(endnotePosition); doc.save(getArtifactsDir() + "InlineStory.PositionEndnote.docx");
Example:
Shows how to set a number at which the document begins the footnote/endnote count.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Footnotes and endnotes are a way to attach a reference or a side comment to text // that does not interfere with the main body text's flow. // Inserting a footnote/endnote adds a small superscript reference symbol // at the main body text where we insert the footnote/endnote. // Each footnote/endnote also creates an entry, which consists of a symbol // that matches the reference symbol in the main body text. // The reference text that we pass to the document builder's "InsertEndnote" method. // Footnote entries, by default, show up at the bottom of each page that contains // their reference symbols, and endnotes show up at the end of the document. builder.write("Text 1. "); builder.insertFootnote(FootnoteType.FOOTNOTE, "Footnote 1."); builder.write("Text 2. "); builder.insertFootnote(FootnoteType.FOOTNOTE, "Footnote 2."); builder.write("Text 3. "); builder.insertFootnote(FootnoteType.FOOTNOTE, "Footnote 3."); builder.insertParagraph(); builder.write("Text 1. "); builder.insertFootnote(FootnoteType.ENDNOTE, "Endnote 1."); builder.write("Text 2. "); builder.insertFootnote(FootnoteType.ENDNOTE, "Endnote 2."); builder.write("Text 3. "); builder.insertFootnote(FootnoteType.ENDNOTE, "Endnote 3."); // By default, the reference symbol for each footnote and endnote is its index // among all the document's footnotes/endnotes. Each document maintains separate counts // for footnotes and for endnotes, which both begin at 1. Assert.assertEquals(1, doc.getFootnoteOptions().getStartNumber()); Assert.assertEquals(1, doc.getEndnoteOptions().getStartNumber()); // We can use the "StartNumber" property to get the document to // begin a footnote or endnote count at a different number. doc.getEndnoteOptions().setNumberStyle(NumberStyle.ARABIC); doc.getEndnoteOptions().setStartNumber(50); doc.save(getArtifactsDir() + "InlineStory.StartNumber.docx");
Example:
Shows how to change the number style of footnote/endnote reference marks.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Footnotes and endnotes are a way to attach a reference or a side comment to text // that does not interfere with the main body text's flow. // Inserting a footnote/endnote adds a small superscript reference symbol // at the main body text where we insert the footnote/endnote. // Each footnote/endnote also creates an entry, which consists of a symbol that matches the reference // symbol in the main body text. The reference text that we pass to the document builder's "InsertEndnote" method. // Footnote entries, by default, show up at the bottom of each page that contains // their reference symbols, and endnotes show up at the end of the document. builder.write("Text 1. "); builder.insertFootnote(FootnoteType.FOOTNOTE, "Footnote 1."); builder.write("Text 2. "); builder.insertFootnote(FootnoteType.FOOTNOTE, "Footnote 2."); builder.write("Text 3. "); builder.insertFootnote(FootnoteType.FOOTNOTE, "Footnote 3.", "Custom footnote reference mark"); builder.insertParagraph(); builder.write("Text 1. "); builder.insertFootnote(FootnoteType.ENDNOTE, "Endnote 1."); builder.write("Text 2. "); builder.insertFootnote(FootnoteType.ENDNOTE, "Endnote 2."); builder.write("Text 3. "); builder.insertFootnote(FootnoteType.ENDNOTE, "Endnote 3.", "Custom endnote reference mark"); // By default, the reference symbol for each footnote and endnote is its index // among all the document's footnotes/endnotes. Each document maintains separate counts // for footnotes and for endnotes. By default, footnotes display their numbers using Arabic numerals, // and endnotes display their numbers in lowercase Roman numerals. Assert.assertEquals(NumberStyle.ARABIC, doc.getFootnoteOptions().getNumberStyle()); Assert.assertEquals(NumberStyle.LOWERCASE_ROMAN, doc.getEndnoteOptions().getNumberStyle()); // We can use the "NumberStyle" property to apply custom numbering styles to footnotes and endnotes. // This will not affect footnotes/endnotes with custom reference marks. doc.getFootnoteOptions().setNumberStyle(NumberStyle.UPPERCASE_ROMAN); doc.getEndnoteOptions().setNumberStyle(NumberStyle.UPPERCASE_LETTER); doc.save(getArtifactsDir() + "InlineStory.RefMarkNumberStyle.docx");
Example:
Shows how to restart footnote/endnote numbering at certain places in the document.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Footnotes and endnotes are a way to attach a reference or a side comment to text // that does not interfere with the main body text's flow. // Inserting a footnote/endnote adds a small superscript reference symbol // at the main body text where we insert the footnote/endnote. // Each footnote/endnote also creates an entry, which consists of a symbol that matches the reference // symbol in the main body text. The reference text that we pass to the document builder's "InsertEndnote" method. // Footnote entries, by default, show up at the bottom of each page that contains // their reference symbols, and endnotes show up at the end of the document. builder.write("Text 1. "); builder.insertFootnote(FootnoteType.FOOTNOTE, "Footnote 1."); builder.write("Text 2. "); builder.insertFootnote(FootnoteType.FOOTNOTE, "Footnote 2."); builder.insertBreak(BreakType.PAGE_BREAK); builder.write("Text 3. "); builder.insertFootnote(FootnoteType.FOOTNOTE, "Footnote 3."); builder.write("Text 4. "); builder.insertFootnote(FootnoteType.FOOTNOTE, "Footnote 4."); builder.insertBreak(BreakType.PAGE_BREAK); builder.write("Text 1. "); builder.insertFootnote(FootnoteType.ENDNOTE, "Endnote 1."); builder.write("Text 2. "); builder.insertFootnote(FootnoteType.ENDNOTE, "Endnote 2."); builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE); builder.write("Text 3. "); builder.insertFootnote(FootnoteType.ENDNOTE, "Endnote 3."); builder.write("Text 4. "); builder.insertFootnote(FootnoteType.ENDNOTE, "Endnote 4."); // By default, the reference symbol for each footnote and endnote is its index // among all the document's footnotes/endnotes. Each document maintains separate counts // for footnotes and endnotes and does not restart these counts at any point. Assert.assertEquals(doc.getFootnoteOptions().getRestartRule(), FootnoteNumberingRule.DEFAULT); Assert.assertEquals(FootnoteNumberingRule.DEFAULT, FootnoteNumberingRule.CONTINUOUS); // We can use the "RestartRule" property to get the document to restart // the footnote/endnote counts at a new page or section. doc.getFootnoteOptions().setRestartRule(FootnoteNumberingRule.RESTART_PAGE); doc.getEndnoteOptions().setRestartRule(FootnoteNumberingRule.RESTART_SECTION); doc.save(getArtifactsDir() + "InlineStory.NumberingRule.docx");
public FieldOptions getFieldOptions()
public Node getFirstChild()
Example:
Shows how to use a node's NextSibling property to enumerate through its immediate children.Document doc = new Document(getMyDir() + "Paragraphs.docx"); for (Node node = doc.getFirstSection().getBody().getFirstChild(); node != null; node = node.getNextSibling()) { System.out.println(Node.nodeTypeToString(node.getNodeType())); }
Example:
Shows how to traverse a composite node's tree of child nodes.Document doc = new Document(getMyDir() + "Paragraphs.docx"); // Any node that can contain child nodes, such as the document itself, is composite. Assert.assertTrue(doc.isComposite()); // Invoke the recursive function that will go through and print all the child nodes of a composite node. traverseAllNodes(doc, 0); } /// <summary> /// Recursively traverses a node tree while printing the type of each node /// with an indent depending on depth as well as the contents of all inline nodes. /// </summary> @Test(enabled = false) public void traverseAllNodes(CompositeNode parentNode, int depth) { for (Node childNode = parentNode.getFirstChild(); childNode != null; childNode = childNode.getNextSibling()) { System.out.println(MessageFormat.format("{0}{1}", String.format(" ", depth), Node.nodeTypeToString(childNode.getNodeType()))); // Recurse into the node if it is a composite node. Otherwise, print its contents if it is an inline node. if (childNode.isComposite()) { System.out.println(); traverseAllNodes((CompositeNode) childNode, depth + 1); } else if (childNode instanceof Inline) { System.out.println(" - \"{childNode.GetText().Trim()}\""); } else { System.out.println(); } } }
public Section getFirstSection()
null
if there are no sections.
Example:
Shows how to replace text in a document's footer.Document doc = new Document(getMyDir() + "Footer.docx"); HeaderFooterCollection headersFooters = doc.getFirstSection().getHeadersFooters(); HeaderFooter footer = headersFooters.getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY); FindReplaceOptions options = new FindReplaceOptions(); options.setMatchCase(false); options.setFindWholeWordsOnly(false); int currentYear = Calendar.YEAR; footer.getRange().replace("(C) 2006 Aspose Pty Ltd.", MessageFormat.format("Copyright (C) {0} by Aspose Pty Ltd.", currentYear), options); doc.save(getArtifactsDir() + "HeaderFooter.ReplaceText.docx");
Example:
Shows how to create a new section with a document builder.Document doc = new Document(); // A blank document contains one section by default, // which contains child nodes that we can edit. Assert.assertEquals(1, doc.getSections().getCount()); // Use a document builder to add text to the first section. DocumentBuilder builder = new DocumentBuilder(doc); builder.writeln("Hello world!"); // Create a second section by inserting a section break. builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE); Assert.assertEquals(2, doc.getSections().getCount()); // Each section has its own page setup settings. // We can split the text in the second section into two columns. // This will not affect the text in the first section. doc.getLastSection().getPageSetup().getTextColumns().setCount(2); builder.writeln("Column 1."); builder.insertBreak(BreakType.COLUMN_BREAK); builder.writeln("Column 2."); Assert.assertEquals(1, doc.getFirstSection().getPageSetup().getTextColumns().getCount()); Assert.assertEquals(2, doc.getLastSection().getPageSetup().getTextColumns().getCount()); doc.save(getArtifactsDir() + "Section.Create.docx");
Example:
Shows how to iterate through the children of a composite node.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.write("Section 1"); builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY); builder.write("Primary header"); builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY); builder.write("Primary footer"); Section section = doc.getFirstSection(); // A Section is a composite node and can contain child nodes, // but only if those child nodes are of a "Body" or "HeaderFooter" node type. for (Node node : section) { switch (node.getNodeType()) { case NodeType.BODY: { Body body = (Body) node; System.out.println("Body:"); System.out.println("\t\"{body.GetText().Trim()}\""); break; } case NodeType.HEADER_FOOTER: { HeaderFooter headerFooter = (HeaderFooter) node; System.out.println("HeaderFooter type: {headerFooter.HeaderFooterType}:"); System.out.println("\t\"{headerFooter.GetText().Trim()}\""); break; } default: { throw new Exception("Unexpected node type in a section."); } } }
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 print the details of what fonts are present in a document.Document doc = new Document(getMyDir() + "Embedded font.docx"); FontInfoCollection allFonts = doc.getFontInfos(); // Print all the used and unused fonts in the document. for (int i = 0; i < allFonts.getCount(); i++) { System.out.println("Font index #{i}"); System.out.println("\tName: {allFonts[i].Name}"); System.out.println("\tIs {(allFonts[i].IsTrueType ? "); }
Example:
Shows how to save a document with embedded TrueType fonts.Document doc = new Document(getMyDir() + "Document.docx"); FontInfoCollection fontInfos = doc.getFontInfos(); fontInfos.setEmbedTrueTypeFonts(embedAllFonts); fontInfos.setEmbedSystemFonts(embedAllFonts); fontInfos.setSaveSubsetFonts(embedAllFonts); doc.save(getArtifactsDir() + "Font.FontInfoCollection.docx"); if (embedAllFonts) Assert.assertTrue(new File(getArtifactsDir() + "Font.FontInfoCollection.docx").length() > 25000); else Assert.assertTrue(new File(getArtifactsDir() + "Font.FontInfoCollection.docx").length() <= 15000);
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 set font substitution rules.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.getFont().setName("Arial"); builder.writeln("Hello world!"); builder.getFont().setName("Amethysta"); builder.writeln("The quick brown fox jumps over the lazy dog."); FontSourceBase[] fontSources = FontSettings.getDefaultInstance().getFontsSources(); // The default font sources contain the first font that the document uses. Assert.assertEquals(1, fontSources.length); Assert.assertTrue(IterableUtils.matchesAny(fontSources[0].getAvailableFonts(), f -> f.getFullFontName().contains("Arial"))); // The second font, "Amethysta", is unavailable. Assert.assertFalse(IterableUtils.matchesAny(fontSources[0].getAvailableFonts(), f -> f.getFullFontName().contains("Amethysta"))); // We can configure a font substitution table which determines // which fonts Aspose.Words will use as substitutes for unavailable fonts. // Set two substitution fonts for "Amethysta": "Arvo", and "Courier New". // If the first substitute is unavailable, Aspose.Words attempts to use the second substitute, and so on. doc.setFontSettings(new FontSettings()); doc.getFontSettings().getSubstitutionSettings().getTableSubstitution().setSubstitutes( "Amethysta", "Arvo", "Courier New"); // "Amethysta" is unavailable, and the substitution rule states that the first font to use as a substitute is "Arvo". Assert.assertFalse(IterableUtils.matchesAny(fontSources[0].getAvailableFonts(), f -> f.getFullFontName().contains("Arvo"))); // "Arvo" is also unavailable, but "Courier New" is. Assert.assertTrue(IterableUtils.matchesAny(fontSources[0].getAvailableFonts(), f -> f.getFullFontName().contains("Courier New"))); // The output document will display the text that uses the "Amethysta" font formatted with "Courier New". doc.save(getArtifactsDir() + "FontSettings.TableSubstitution.pdf");
public FootnoteOptions getFootnoteOptions()
Example:
Shows how to select a different place where the document collects and displays its footnotes.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // A footnote is a way to attach a reference or a side comment to text // that does not interfere with the main body text's flow. // Inserting a footnote adds a small superscript reference symbol // at the main body text where we insert the footnote. // Each footnote also creates an entry at the bottom of the page, consisting of a symbol // that matches the reference symbol in the main body text. // The reference text that we pass to the document builder's "InsertFootnote" method. builder.write("Hello world!"); builder.insertFootnote(FootnoteType.FOOTNOTE, "Footnote contents."); // We can use the "Position" property to determine where the document will place all its footnotes. // If we set the value of the "Position" property to "FootnotePosition.BottomOfPage", // every footnote will show up at the bottom of the page that contains its reference mark. This is the default value. // If we set the value of the "Position" property to "FootnotePosition.BeneathText", // every footnote will show up at the end of the page's text that contains its reference mark. doc.getFootnoteOptions().setPosition(footnotePosition); doc.save(getArtifactsDir() + "InlineStory.PositionFootnote.docx");
Example:
Shows how to set a number at which the document begins the footnote/endnote count.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Footnotes and endnotes are a way to attach a reference or a side comment to text // that does not interfere with the main body text's flow. // Inserting a footnote/endnote adds a small superscript reference symbol // at the main body text where we insert the footnote/endnote. // Each footnote/endnote also creates an entry, which consists of a symbol // that matches the reference symbol in the main body text. // The reference text that we pass to the document builder's "InsertEndnote" method. // Footnote entries, by default, show up at the bottom of each page that contains // their reference symbols, and endnotes show up at the end of the document. builder.write("Text 1. "); builder.insertFootnote(FootnoteType.FOOTNOTE, "Footnote 1."); builder.write("Text 2. "); builder.insertFootnote(FootnoteType.FOOTNOTE, "Footnote 2."); builder.write("Text 3. "); builder.insertFootnote(FootnoteType.FOOTNOTE, "Footnote 3."); builder.insertParagraph(); builder.write("Text 1. "); builder.insertFootnote(FootnoteType.ENDNOTE, "Endnote 1."); builder.write("Text 2. "); builder.insertFootnote(FootnoteType.ENDNOTE, "Endnote 2."); builder.write("Text 3. "); builder.insertFootnote(FootnoteType.ENDNOTE, "Endnote 3."); // By default, the reference symbol for each footnote and endnote is its index // among all the document's footnotes/endnotes. Each document maintains separate counts // for footnotes and for endnotes, which both begin at 1. Assert.assertEquals(1, doc.getFootnoteOptions().getStartNumber()); Assert.assertEquals(1, doc.getEndnoteOptions().getStartNumber()); // We can use the "StartNumber" property to get the document to // begin a footnote or endnote count at a different number. doc.getEndnoteOptions().setNumberStyle(NumberStyle.ARABIC); doc.getEndnoteOptions().setStartNumber(50); doc.save(getArtifactsDir() + "InlineStory.StartNumber.docx");
Example:
Shows how to change the number style of footnote/endnote reference marks.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Footnotes and endnotes are a way to attach a reference or a side comment to text // that does not interfere with the main body text's flow. // Inserting a footnote/endnote adds a small superscript reference symbol // at the main body text where we insert the footnote/endnote. // Each footnote/endnote also creates an entry, which consists of a symbol that matches the reference // symbol in the main body text. The reference text that we pass to the document builder's "InsertEndnote" method. // Footnote entries, by default, show up at the bottom of each page that contains // their reference symbols, and endnotes show up at the end of the document. builder.write("Text 1. "); builder.insertFootnote(FootnoteType.FOOTNOTE, "Footnote 1."); builder.write("Text 2. "); builder.insertFootnote(FootnoteType.FOOTNOTE, "Footnote 2."); builder.write("Text 3. "); builder.insertFootnote(FootnoteType.FOOTNOTE, "Footnote 3.", "Custom footnote reference mark"); builder.insertParagraph(); builder.write("Text 1. "); builder.insertFootnote(FootnoteType.ENDNOTE, "Endnote 1."); builder.write("Text 2. "); builder.insertFootnote(FootnoteType.ENDNOTE, "Endnote 2."); builder.write("Text 3. "); builder.insertFootnote(FootnoteType.ENDNOTE, "Endnote 3.", "Custom endnote reference mark"); // By default, the reference symbol for each footnote and endnote is its index // among all the document's footnotes/endnotes. Each document maintains separate counts // for footnotes and for endnotes. By default, footnotes display their numbers using Arabic numerals, // and endnotes display their numbers in lowercase Roman numerals. Assert.assertEquals(NumberStyle.ARABIC, doc.getFootnoteOptions().getNumberStyle()); Assert.assertEquals(NumberStyle.LOWERCASE_ROMAN, doc.getEndnoteOptions().getNumberStyle()); // We can use the "NumberStyle" property to apply custom numbering styles to footnotes and endnotes. // This will not affect footnotes/endnotes with custom reference marks. doc.getFootnoteOptions().setNumberStyle(NumberStyle.UPPERCASE_ROMAN); doc.getEndnoteOptions().setNumberStyle(NumberStyle.UPPERCASE_LETTER); doc.save(getArtifactsDir() + "InlineStory.RefMarkNumberStyle.docx");
Example:
Shows how to restart footnote/endnote numbering at certain places in the document.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Footnotes and endnotes are a way to attach a reference or a side comment to text // that does not interfere with the main body text's flow. // Inserting a footnote/endnote adds a small superscript reference symbol // at the main body text where we insert the footnote/endnote. // Each footnote/endnote also creates an entry, which consists of a symbol that matches the reference // symbol in the main body text. The reference text that we pass to the document builder's "InsertEndnote" method. // Footnote entries, by default, show up at the bottom of each page that contains // their reference symbols, and endnotes show up at the end of the document. builder.write("Text 1. "); builder.insertFootnote(FootnoteType.FOOTNOTE, "Footnote 1."); builder.write("Text 2. "); builder.insertFootnote(FootnoteType.FOOTNOTE, "Footnote 2."); builder.insertBreak(BreakType.PAGE_BREAK); builder.write("Text 3. "); builder.insertFootnote(FootnoteType.FOOTNOTE, "Footnote 3."); builder.write("Text 4. "); builder.insertFootnote(FootnoteType.FOOTNOTE, "Footnote 4."); builder.insertBreak(BreakType.PAGE_BREAK); builder.write("Text 1. "); builder.insertFootnote(FootnoteType.ENDNOTE, "Endnote 1."); builder.write("Text 2. "); builder.insertFootnote(FootnoteType.ENDNOTE, "Endnote 2."); builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE); builder.write("Text 3. "); builder.insertFootnote(FootnoteType.ENDNOTE, "Endnote 3."); builder.write("Text 4. "); builder.insertFootnote(FootnoteType.ENDNOTE, "Endnote 4."); // By default, the reference symbol for each footnote and endnote is its index // among all the document's footnotes/endnotes. Each document maintains separate counts // for footnotes and endnotes and does not restart these counts at any point. Assert.assertEquals(doc.getFootnoteOptions().getRestartRule(), FootnoteNumberingRule.DEFAULT); Assert.assertEquals(FootnoteNumberingRule.DEFAULT, FootnoteNumberingRule.CONTINUOUS); // We can use the "RestartRule" property to get the document to restart // the footnote/endnote counts at a new page or section. doc.getFootnoteOptions().setRestartRule(FootnoteNumberingRule.RESTART_PAGE); doc.getEndnoteOptions().setRestartRule(FootnoteNumberingRule.RESTART_SECTION); doc.save(getArtifactsDir() + "InlineStory.NumberingRule.docx");
public Aspose.Words.Framesets.Frameset getFrameset()
Example:
Shows how to access frames on-page.// Document contains several frames with links to other documents. Document doc = new Document(getMyDir() + "Frameset.docx"); // We can check the default URL (a web page URL or local document) or if the frame is an external resource. Assert.assertEquals("https://file-examples-com.github.io/uploads/2017/02/file-sample_100kB.docx", doc.getFrameset().getChildFramesets().get(0).getChildFramesets().get(0).getFrameDefaultUrl()); Assert.assertTrue(doc.getFrameset().getChildFramesets().get(0).getChildFramesets().get(0).isFrameLinkToFile()); Assert.assertEquals("Document.docx", doc.getFrameset().getChildFramesets().get(1).getFrameDefaultUrl()); Assert.assertFalse(doc.getFrameset().getChildFramesets().get(1).isFrameLinkToFile()); // Change properties for one of our frames. doc.getFrameset().getChildFramesets().get(0).getChildFramesets().get(0).setFrameDefaultUrl("https://github.com/aspose-words/Aspose.Words-for-.NET/blob/master/Examples/Data/Absolute%20position%20tab.docx"); doc.getFrameset().getChildFramesets().get(0).getChildFramesets().get(0).isFrameLinkToFile(false);
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.// A document's glossary document stores building blocks. Document doc = new Document(); GlossaryDocument glossaryDoc = new GlossaryDocument(); doc.setGlossaryDocument(glossaryDoc); // Create a building block, name it, and then add it to the glossary document. BuildingBlock block = new BuildingBlock(glossaryDoc); block.setName("Custom Block"); glossaryDoc.appendChild(block); // All new building block GUIDs have the same zero value by default, and we can give them a new unique value. Assert.assertEquals(block.getGuid().toString(), "00000000-0000-0000-0000-000000000000"); block.setGuid(UUID.randomUUID()); // The following properties categorize building blocks // in the menu we can access in Microsoft Word via "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); // Before we can add this building block to our document, we will need to give it some contents, // which we will do using a document visitor. This visitor will also set a category, gallery, and behavior. BuildingBlockVisitor visitor = new BuildingBlockVisitor(glossaryDoc); block.accept(visitor); // We can access the block that we just made from the glossary document. BuildingBlock customBlock = glossaryDoc.getBuildingBlock(BuildingBlockGallery.QUICK_PARTS, "My custom building blocks", "Custom Block"); // The block itself is a section that contains the text. Assert.assertEquals(MessageFormat.format("Text inside {0}\f", customBlock.getName()), customBlock.getFirstSection().getBody().getFirstParagraph().getText()); Assert.assertEquals(customBlock.getFirstSection(), customBlock.getLastSection()); // Now, we can insert it into the document as a new section. doc.appendChild(doc.importNode(customBlock.getFirstSection(), true)); // We can also find it in Microsoft Word's Building Blocks Organizer and place it manually. doc.save(getArtifactsDir() + "BuildingBlocks.CreateAndInsert.dotx"); } /// <summary> /// Sets up a visited building block to be inserted into the document as a quick part and adds text to its contents. /// </summary> public static class BuildingBlockVisitor extends DocumentVisitor { public BuildingBlockVisitor(final GlossaryDocument ownerGlossaryDoc) { mBuilder = new StringBuilder(); mGlossaryDoc = ownerGlossaryDoc; } public int visitBuildingBlockStart(final BuildingBlock block) { // Configure the building block as a quick part, and add properties used by Building Blocks Organizer. 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); // Add a section with text. // Inserting the block into the document will append this section with its child nodes at the location. Section section = new Section(mGlossaryDoc); block.appendChild(section); block.getFirstSection().ensureMinimum(); 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 final StringBuilder mBuilder; private final GlossaryDocument mGlossaryDoc; }
public boolean getGrammarChecked() / public void setGrammarChecked(boolean value)
Example:
Shows how to set spelling or grammar verifying.Document doc = new Document(); // The string with spelling errors. doc.getFirstSection().getBody().getFirstParagraph().getRuns().add(new Run(doc, "The speeling in this documentz is all broked.")); // Spelling/Grammar check start if we set properties to false. // We can see all errors in Microsoft Word via Review -> Spelling & Grammar. // Note that Microsoft Word does not start grammar/spell check automatically for DOC and RTF document format. doc.setSpellingChecked(checkSpellingGrammar); doc.setGrammarChecked(checkSpellingGrammar); doc.save(getArtifactsDir() + "Document.SpellingOrGrammar.docx");
public boolean hasChildNodes()
Example:
Shows how to combine the rows from two tables into one.Document doc = new Document(getMyDir() + "Tables.docx"); // Below are two ways of getting a table from a document. // 1 - From the "Tables" collection of a Body node: Table firstTable = doc.getFirstSection().getBody().getTables().get(0); // 2 - Using the "GetChild" method: Table secondTable = (Table) doc.getChild(NodeType.TABLE, 1, true); // Append all rows from the current table to the next. while (secondTable.hasChildNodes()) firstTable.getRows().add(secondTable.getFirstRow()); // Remove the empty table container. secondTable.remove(); doc.save(getArtifactsDir() + "Table.CombineTables.docx");
public boolean hasMacros()
Example:
Shows how to use MACROBUTTON fields to allow us to run a document's macros by clicking.Document doc = new Document(getMyDir() + "Macro.docm"); DocumentBuilder builder = new DocumentBuilder(doc); Assert.assertTrue(doc.hasMacros()); // Insert a MACROBUTTON field, and reference one of the document's macros by name in the MacroName property. FieldMacroButton field = (FieldMacroButton) builder.insertField(FieldType.FIELD_MACRO_BUTTON, true); field.setMacroName("MyMacro"); field.setDisplayText("Double click to run macro: " + field.getMacroName()); Assert.assertEquals(" MACROBUTTON MyMacro Double click to run macro: MyMacro", field.getFieldCode()); // Use the property to reference "ViewZoom200", a macro that ships with Microsoft Word. // We can find all other macros via View -> Macros (dropdown) -> View Macros. // In that menu, select "Word Commands" from the "Macros in:" drop down. // If our document contains a custom macro with the same name as a stock macro, // our macro will be the one that the MACROBUTTON field runs. builder.insertParagraph(); 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");
public boolean hasRevisions()
Example:
Shows how to work with revisions in a document.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()); // To register our edits as revisions, we need to declare an author, and then start tracking them. doc.startTrackRevisions("John Doe", new Date()); builder.write("This is revision #1. "); Assert.assertTrue(doc.hasRevisions()); Assert.assertEquals(1, doc.getRevisions().getCount()); // This flag corresponds to the "Review" -> "Tracking" -> "Track Changes" option in Microsoft Word. // The "StartTrackRevisions" method does not affect its value, // and the document is tracking revisions programmatically despite it having a value of "false". // If we open this document using Microsoft Word, it will not be tracking revisions. Assert.assertFalse(doc.getTrackRevisions()); // We have added text using the document builder, so the first revision is an insertion-type revision. Revision revision = doc.getRevisions().get(0); Assert.assertEquals("John Doe", revision.getAuthor()); Assert.assertEquals("This is revision #1. ", revision.getParentNode().getText()); Assert.assertEquals(RevisionType.INSERTION, revision.getRevisionType()); Assert.assertEquals(revision.getDateTime().getDate(), new Date().getDate()); Assert.assertEquals(doc.getRevisions().getGroups().get(0), revision.getGroup()); // Remove a run to create a deletion-type revision. doc.getFirstSection().getBody().getFirstParagraph().getRuns().get(0).remove(); // Adding a new revision places it at the beginning of the revision collection. Assert.assertEquals(RevisionType.DELETION, doc.getRevisions().get(0).getRevisionType()); Assert.assertEquals(2, doc.getRevisions().getCount()); // Insert revisions show up in the document body even before we accept/reject the revision. // Rejecting the revision will remove its nodes from the body. Conversely, nodes that make up delete revisions // also linger in the document until we accept the revision. Assert.assertEquals("This does not count as a revision. This is revision #1.", doc.getText().trim()); // Accepting the delete revision will remove its parent node from the paragraph text // and then remove the collection's revision itself. doc.getRevisions().get(0).accept(); Assert.assertEquals(1, doc.getRevisions().getCount()); Assert.assertEquals("This is revision #1.", doc.getText().trim()); builder.writeln(""); builder.write("This is revision #2."); // Now move the node to create a moving revision type. Node node = doc.getFirstSection().getBody().getParagraphs().get(1); Node endNode = doc.getFirstSection().getBody().getParagraphs().get(1).getNextSibling(); Node referenceNode = doc.getFirstSection().getBody().getParagraphs().get(0); while (node != endNode) { Node nextNode = node.getNextSibling(); doc.getFirstSection().getBody().insertBefore(node, referenceNode); node = nextNode; } Assert.assertEquals(RevisionType.MOVING, doc.getRevisions().get(0).getRevisionType()); Assert.assertEquals(8, doc.getRevisions().getCount()); Assert.assertEquals("This is revision #2.\rThis is revision #1. \rThis is revision #2.", doc.getText().trim()); // The moving revision is now at index 1. Reject the revision to discard its contents. doc.getRevisions().get(1).reject(); Assert.assertEquals(6, doc.getRevisions().getCount()); Assert.assertEquals("This is revision #1. \rThis is revision #2.", doc.getText().trim());
public HyphenationOptions getHyphenationOptions()
Example:
Shows how to configure automatic hyphenation.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.getFont().setSize(24.0); 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); doc.getHyphenationOptions().setHyphenateCaps(true); doc.save(getArtifactsDir() + "Document.HyphenationOptions.docx");
public boolean isComposite()
Example:
Shows how to traverse a composite node's tree of child nodes.Document doc = new Document(getMyDir() + "Paragraphs.docx"); // Any node that can contain child nodes, such as the document itself, is composite. Assert.assertTrue(doc.isComposite()); // Invoke the recursive function that will go through and print all the child nodes of a composite node. traverseAllNodes(doc, 0); } /// <summary> /// Recursively traverses a node tree while printing the type of each node /// with an indent depending on depth as well as the contents of all inline nodes. /// </summary> @Test(enabled = false) public void traverseAllNodes(CompositeNode parentNode, int depth) { for (Node childNode = parentNode.getFirstChild(); childNode != null; childNode = childNode.getNextSibling()) { System.out.println(MessageFormat.format("{0}{1}", String.format(" ", depth), Node.nodeTypeToString(childNode.getNodeType()))); // Recurse into the node if it is a composite node. Otherwise, print its contents if it is an inline node. if (childNode.isComposite()) { System.out.println(); traverseAllNodes((CompositeNode) childNode, depth + 1); } else if (childNode instanceof Inline) { System.out.println(" - \"{childNode.GetText().Trim()}\""); } else { System.out.println(); } } }
public Node getLastChild()
Example:
Shows how to use of methods of Node and CompositeNode to remove a section before the last section in the document.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.writeln("Section 1 text."); builder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS); builder.writeln("Section 2 text."); // Both sections are siblings of each other. Section lastSection = (Section) doc.getLastChild(); Section firstSection = (Section) lastSection.getPreviousSibling(); // Remove a section based on its sibling relationship with another section. if (lastSection.getPreviousSibling() != null) doc.removeChild(firstSection); // The section we removed was the first one, leaving the document with only the second. Assert.assertEquals("Section 2 text.", doc.getText().trim());
public Section getLastSection()
null
if there are no sections.
Example:
Shows how to create a new section with a document builder.Document doc = new Document(); // A blank document contains one section by default, // which contains child nodes that we can edit. Assert.assertEquals(1, doc.getSections().getCount()); // Use a document builder to add text to the first section. DocumentBuilder builder = new DocumentBuilder(doc); builder.writeln("Hello world!"); // Create a second section by inserting a section break. builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE); Assert.assertEquals(2, doc.getSections().getCount()); // Each section has its own page setup settings. // We can split the text in the second section into two columns. // This will not affect the text in the first section. doc.getLastSection().getPageSetup().getTextColumns().setCount(2); builder.writeln("Column 1."); builder.insertBreak(BreakType.COLUMN_BREAK); builder.writeln("Column 2."); Assert.assertEquals(1, doc.getFirstSection().getPageSetup().getTextColumns().getCount()); Assert.assertEquals(2, doc.getLastSection().getPageSetup().getTextColumns().getCount()); doc.save(getArtifactsDir() + "Section.Create.docx");
public LayoutOptions getLayoutOptions()
Example:
Shows how to hide text in a rendered output document.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert hidden text, then specify whether we wish to omit it from a rendered document. builder.writeln("This text is not hidden."); builder.getFont().setHidden(true); builder.writeln("This text is hidden."); doc.getLayoutOptions().setShowHiddenText(showHiddenText); doc.save(getArtifactsDir() + "Document.LayoutOptionsHiddenText.pdf");
Example:
Shows how to show paragraph marks in a rendered output document.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Add some paragraphs, then enable paragraph marks to show the ends of paragraphs // with a pilcrow (¶) symbol when we render the document. builder.writeln("Hello world!"); builder.writeln("Hello again!"); doc.getLayoutOptions().setShowParagraphMarks(showParagraphMarks); doc.save(getArtifactsDir() + "Document.LayoutOptionsParagraphMarks.pdf");
Example:
Shows how to alter the appearance of revisions in a rendered output document.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert a revision, then change the color of all revisions to green. builder.writeln("This is not a revision."); doc.startTrackRevisions("John Doe", new Date()); builder.writeln("This is a revision."); doc.stopTrackRevisions(); builder.writeln("This is not a revision."); // Remove the bar that appears to the left of every revised line. doc.getLayoutOptions().getRevisionOptions().setInsertedTextColor(RevisionColor.BRIGHT_GREEN); doc.getLayoutOptions().getRevisionOptions().setShowRevisionBars(false); doc.save(getArtifactsDir() + "Document.LayoutOptionsRevisions.pdf");
public ListCollection getLists()
For more information see the description of the
Example:
Shows how to work with list levels.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Assert.assertFalse(builder.getListFormat().isListItem()); // A list allows us to organize and decorate sets of paragraphs with prefix symbols and indents. // We can create nested lists by increasing the indent level. // We can begin and end a list by using a document builder's "ListFormat" property. // Each paragraph that we add between a list's start and the end will become an item in the list. // Below are two types of lists that we can create using a document builder. // 1 - A numbered list: // Numbered lists create a logical order for their paragraphs by numbering each item. builder.getListFormat().setList(doc.getLists().add(ListTemplate.NUMBER_DEFAULT)); Assert.assertTrue(builder.getListFormat().isListItem()); // By setting the "ListLevelNumber" property, we can increase the list level // to begin a self-contained sub-list at the current list item. // The Microsoft Word list template called "NumberDefault" uses numbers to create list levels for the first list level. // Deeper list levels use letters and lowercase Roman numerals. for (int i = 0; i < 9; i++) { builder.getListFormat().setListLevelNumber(i); builder.writeln("Level " + i); } // 2 - A bulleted list: // This list will apply an indent and a bullet symbol ("�") before each paragraph. // Deeper levels of this list will use different symbols, such as "�" and "?". builder.getListFormat().setList(doc.getLists().add(ListTemplate.BULLET_DEFAULT)); for (int i = 0; i < 9; i++) { builder.getListFormat().setListLevelNumber(i); builder.writeln("Level " + i); } // We can disable list formatting to not format any subsequent paragraphs as lists by un-setting the "List" flag. builder.getListFormat().setList(null); Assert.assertFalse(builder.getListFormat().isListItem()); doc.save(getArtifactsDir() + "Lists.SpecifyListLevel.docx");
public MailMerge getMailMerge()
Example:
Shows how to execute a mail merge with data from a DataTable.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.insertField(" MERGEFIELD CustomerName "); builder.insertParagraph(); builder.insertField(" MERGEFIELD Address "); // This example creates a table, but you would normally load table from a database DataTable table = new DataTable("Test"); table.getColumns().add("CustomerName"); table.getColumns().add("Address"); table.getRows().add("Thomas Hardy", "120 Hanover Sq., London"); table.getRows().add("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.docx"); // Create a copy of our document to perform another mail merge doc = new Document(); builder = new DocumentBuilder(doc); builder.insertField(" MERGEFIELD CustomerName "); builder.insertParagraph(); builder.insertField(" MERGEFIELD Address "); // We can also source values for a mail merge from a single row in the table doc.getMailMerge().execute(table.getRows().get(1)); doc.save(getArtifactsDir() + "MailMerge.ExecuteDataTable.OneRow.docx");
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.
public Node getNextSibling()
Example:
Shows how to use a node's NextSibling property to enumerate through its immediate children.Document doc = new Document(getMyDir() + "Paragraphs.docx"); for (Node node = doc.getFirstSection().getBody().getFirstChild(); node != null; node = node.getNextSibling()) { System.out.println(Node.nodeTypeToString(node.getNodeType())); }
Example:
Shows how to traverse a composite node's tree of child nodes.Document doc = new Document(getMyDir() + "Paragraphs.docx"); // Any node that can contain child nodes, such as the document itself, is composite. Assert.assertTrue(doc.isComposite()); // Invoke the recursive function that will go through and print all the child nodes of a composite node. traverseAllNodes(doc, 0); } /// <summary> /// Recursively traverses a node tree while printing the type of each node /// with an indent depending on depth as well as the contents of all inline nodes. /// </summary> @Test(enabled = false) public void traverseAllNodes(CompositeNode parentNode, int depth) { for (Node childNode = parentNode.getFirstChild(); childNode != null; childNode = childNode.getNextSibling()) { System.out.println(MessageFormat.format("{0}{1}", String.format(" ", depth), Node.nodeTypeToString(childNode.getNodeType()))); // Recurse into the node if it is a composite node. Otherwise, print its contents if it is an inline node. if (childNode.isComposite()) { System.out.println(); traverseAllNodes((CompositeNode) childNode, depth + 1); } else if (childNode instanceof Inline) { System.out.println(" - \"{childNode.GetText().Trim()}\""); } else { System.out.println(); } } }
public INodeChangingCallback getNodeChangingCallback() / public void setNodeChangingCallback(INodeChangingCallback value)
Example:
Shows how customize node changing with a callback.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Set the node changing callback to custom implementation, // then add/remove nodes to get it to generate a log. HandleNodeChangingFontChanger callback = new HandleNodeChangingFontChanger(); doc.setNodeChangingCallback(callback); builder.writeln("Hello world!"); builder.writeln("Hello again!"); builder.insertField(" HYPERLINK \"https://www.google.com/\" "); builder.insertShape(ShapeType.RECTANGLE, 300.0, 300.0); doc.getRange().getFields().get(0).remove(); System.out.println(callback.getLog()); /// <summary> /// Logs the date and time of each node insertion and removal. /// Sets a custom font name/size for the text contents of Run nodes. /// </summary> public static class HandleNodeChangingFontChanger implements INodeChangingCallback { public void nodeInserted(NodeChangingArgs args) { mLog.append(MessageFormat.format("\tType:\t{0}", args.getNode().getNodeType())); mLog.append(MessageFormat.format("\tHash:\t{0}", args.getNode().hashCode())); if (args.getNode().getNodeType() == NodeType.RUN) { Font font = ((Run) args.getNode()).getFont(); mLog.append(MessageFormat.format("\tFont:\tChanged from \"{0}\" {1}pt", font.getName(), font.getSize())); font.setSize(24.0); font.setName("Arial"); mLog.append(MessageFormat.format(" to \"{0}\" {1}pt", font.getName(), font.getSize())); mLog.append(MessageFormat.format("\tContents:\n\t\t\"{0}\"", args.getNode().getText())); } } public void nodeInserting(NodeChangingArgs args) { mLog.append(MessageFormat.format("\n{0}\tNode insertion:", new Date())); } public void nodeRemoved(NodeChangingArgs args) { mLog.append(MessageFormat.format("\tType:\t{0}", args.getNode().getNodeType())); mLog.append(MessageFormat.format("\tHash code:\t{0}", args.getNode().hashCode())); } public void nodeRemoving(NodeChangingArgs args) { mLog.append(MessageFormat.format("\n{0}\tNode removal:", new Date())); } public String getLog() { return mLog.toString(); } private final StringBuilder mLog = new StringBuilder(); }
public int getNodeType()
Example:
Shows how to traverse a composite node's tree of child nodes.Document doc = new Document(getMyDir() + "Paragraphs.docx"); // Any node that can contain child nodes, such as the document itself, is composite. Assert.assertTrue(doc.isComposite()); // Invoke the recursive function that will go through and print all the child nodes of a composite node. traverseAllNodes(doc, 0); } /// <summary> /// Recursively traverses a node tree while printing the type of each node /// with an indent depending on depth as well as the contents of all inline nodes. /// </summary> @Test(enabled = false) public void traverseAllNodes(CompositeNode parentNode, int depth) { for (Node childNode = parentNode.getFirstChild(); childNode != null; childNode = childNode.getNextSibling()) { System.out.println(MessageFormat.format("{0}{1}", String.format(" ", depth), Node.nodeTypeToString(childNode.getNodeType()))); // Recurse into the node if it is a composite node. Otherwise, print its contents if it is an inline node. if (childNode.isComposite()) { System.out.println(); traverseAllNodes((CompositeNode) childNode, depth + 1); } else if (childNode instanceof Inline) { System.out.println(" - \"{childNode.GetText().Trim()}\""); } else { System.out.println(); } } }
public java.lang.String getOriginalFileName()
Returns null if the document was loaded from a stream or created blank.
Example:
Shows how to retrieve details of a document's load operation.Document doc = new Document(getMyDir() + "Document.docx"); Assert.assertEquals(getMyDir() + "Document.docx", doc.getOriginalFileName()); Assert.assertEquals(LoadFormat.DOCX, doc.getOriginalLoadFormat());
Example:
Shows how to use the FileFormatUtil methods to detect the format of a document.// Load a document from a file that is missing a file extension, and then detect its file format. FileInputStream docStream = new FileInputStream(getMyDir() + "Word document with missing file extension"); FileFormatInfo info = FileFormatUtil.detectFileFormat(docStream); /*LoadFormat*/ int loadFormat = info.getLoadFormat(); Assert.assertEquals(LoadFormat.DOC, loadFormat); // Below are two methods of converting a LoadFormat to its corresponding SaveFormat. // 1 - Get the file extension string for the LoadFormat, then get the corresponding SaveFormat from that string: String fileExtension = FileFormatUtil.loadFormatToExtension(loadFormat); /*SaveFormat*/ int saveFormat = FileFormatUtil.extensionToSaveFormat(fileExtension); // 2 - Convert the LoadFormat directly to its SaveFormat: saveFormat = FileFormatUtil.loadFormatToSaveFormat(loadFormat); // Load a document from the stream, and then save it to the automatically detected file extension. Document doc = new Document(docStream); Assert.assertEquals(".doc", FileFormatUtil.saveFormatToExtension(saveFormat)); doc.save(getArtifactsDir() + "File.SaveToDetectedFileFormat" + FileFormatUtil.saveFormatToExtension(saveFormat));
public int getOriginalLoadFormat()
If you created a new blank document, returns the
Example:
Shows how to retrieve details of a document's load operation.Document doc = new Document(getMyDir() + "Document.docx"); Assert.assertEquals(getMyDir() + "Document.docx", doc.getOriginalFileName()); Assert.assertEquals(LoadFormat.DOCX, doc.getOriginalLoadFormat());
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 access a document's arbitrary custom parts collection.Document doc = new Document(getMyDir() + "Custom parts OOXML package.docx"); Assert.assertEquals(2, doc.getPackageCustomParts().getCount()); // Clone the second part, then add the clone to the collection. CustomPart clonedPart = doc.getPackageCustomParts().get(1).deepClone(); doc.getPackageCustomParts().add(clonedPart); Assert.assertEquals(3, doc.getPackageCustomParts().getCount()); // Enumerate over the collection and print every part. Iterator<CustomPart> enumerator = doc.getPackageCustomParts().iterator(); int index = 0; while (enumerator.hasNext()) { 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++; } // We can remove elements from this collection individually, or all at once. doc.getPackageCustomParts().removeAt(2); Assert.assertEquals(2, doc.getPackageCustomParts().getCount()); doc.getPackageCustomParts().clear(); Assert.assertEquals(0, doc.getPackageCustomParts().getCount());
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 background color for all pages of a document.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.writeln("Hello world!"); doc.setPageColor(Color.lightGray); doc.save(getArtifactsDir() + "DocumentBase.SetPageColor.docx");
public int getPageCount()
Example:
Shows how to count the number of pages in the document.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.write("Page 1"); builder.insertBreak(BreakType.PAGE_BREAK); builder.write("Page 2"); builder.insertBreak(BreakType.PAGE_BREAK); builder.write("Page 3"); // Verify the expected page count of the document. Assert.assertEquals(3, doc.getPageCount()); // Getting the PageCount property invoked the document's page layout to calculate the value. // This operation will not need to be re-done when rendering the document to a fixed page save format, // such as .pdf. So you can save some time, especially with more complex documents. doc.save(getArtifactsDir() + "Document.GetPageCount.pdf");
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 a node's parent node.Document doc = new Document(); Paragraph para = doc.getFirstSection().getBody().getFirstParagraph(); // Append a child Run node to the document's first paragraph. Run run = new Run(doc, "Hello world!"); para.appendChild(run); // The paragraph is the parent node of the run node. We can trace this lineage // all the way to the document node, which is the root of the document's node tree. Assert.assertEquals(para, run.getParentNode()); Assert.assertEquals(doc.getFirstSection().getBody(), para.getParentNode()); Assert.assertEquals(doc.getFirstSection(), doc.getFirstSection().getBody().getParentNode()); Assert.assertEquals(doc, doc.getFirstSection().getParentNode());
Example:
Shows how to create a node and set its owning document.Document doc = new Document(); Paragraph para = new Paragraph(doc); para.appendChild(new Run(doc, "Hello world!")); // We have not yet appended this paragraph as a child to any composite node. Assert.assertNull(para.getParentNode()); // If a node is an appropriate child node type of another composite node, // we can attach it as a child only if both nodes have the same owner document. // The owner document is the document we passed to the node's constructor. // We have not attached this paragraph to the document, so the document does not contain its text. Assert.assertEquals(para.getDocument(), doc); Assert.assertEquals("", doc.getText().trim()); // Since the document owns this paragraph, we can apply one of its styles to the paragraph's contents. para.getParagraphFormat().setStyleName("Heading 1"); // Add this node to the document, and then verify its contents. doc.getFirstSection().getBody().appendChild(para); Assert.assertEquals(doc.getFirstSection().getBody(), para.getParentNode()); Assert.assertEquals("Hello world!", doc.getText().trim());
public Node getPreviousSibling()
Example:
Shows how to use of methods of Node and CompositeNode to remove a section before the last section in the document.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.writeln("Section 1 text."); builder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS); builder.writeln("Section 2 text."); // Both sections are siblings of each other. Section lastSection = (Section) doc.getLastChild(); Section firstSection = (Section) lastSection.getPreviousSibling(); // Remove a section based on its sibling relationship with another section. if (lastSection.getPreviousSibling() != null) doc.removeChild(firstSection); // The section we removed was the first one, leaving the document with only the second. Assert.assertEquals("Section 2 text.", doc.getText().trim());
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 protect and unprotect a document.Document doc = new Document(); doc.protect(ProtectionType.READ_ONLY, "password"); Assert.assertEquals(ProtectionType.READ_ONLY, doc.getProtectionType()); // If we open this document with Microsoft Word intending to edit it, // we will need to apply the password to get through the protection. doc.save(getArtifactsDir() + "Document.Protect.docx"); // Note that the protection only applies to Microsoft Word users opening our document. // We have not encrypted the document in any way, and we do not need the password to open and edit it programmatically. Document protectedDoc = new Document(getArtifactsDir() + "Document.Protect.docx"); Assert.assertEquals(ProtectionType.READ_ONLY, protectedDoc.getProtectionType()); DocumentBuilder builder = new DocumentBuilder(protectedDoc); builder.writeln("Text added to a protected document."); // There are two ways of removing protection from a document. // 1 - With no password: doc.unprotect(); Assert.assertEquals(ProtectionType.NO_PROTECTION, doc.getProtectionType()); doc.protect(ProtectionType.READ_ONLY, "NewPassword"); Assert.assertEquals(ProtectionType.READ_ONLY, doc.getProtectionType()); doc.unprotect("WrongPassword"); Assert.assertEquals(ProtectionType.READ_ONLY, doc.getProtectionType()); // 2 - With the correct password: doc.unprotect("NewPassword"); Assert.assertEquals(ProtectionType.NO_PROTECTION, doc.getProtectionType());
public Range getRange()
Example:
Shows how to delete all the nodes from a range.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Add text to the first section in the document, and then add another section. builder.write("Section 1. "); builder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS); builder.write("Section 2."); Assert.assertEquals("Section 1. \fSection 2.", doc.getText().trim()); // Remove the first section entirely by removing all the nodes // within its range, including the section itself. doc.getSections().get(0).getRange().delete(); Assert.assertEquals(1, doc.getSections().getCount()); Assert.assertEquals("Section 2.", doc.getText().trim());
public boolean getRemovePersonalInformation() / public void setRemovePersonalInformation(boolean value)
Example:
Shows how to enable the removal of personal information during a manual save.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert some content with personal information. doc.getBuiltInDocumentProperties().setAuthor("John Doe"); doc.getBuiltInDocumentProperties().setCompany("Placeholder Inc."); doc.startTrackRevisions(doc.getBuiltInDocumentProperties().getAuthor(), new Date()); builder.write("Hello world!"); doc.stopTrackRevisions(); // This flag is equivalent to File -> Options -> Trust Center -> Trust Center Settings... -> // Privacy Options -> "Remove personal information from file properties on save" in Microsoft Word. doc.setRemovePersonalInformation(saveWithoutPersonalInfo); // This option will not take effect during a save operation made using Aspose.Words. // Personal data will be removed from our document with the flag set when we save it manually using Microsoft Word. doc.save(getArtifactsDir() + "Document.RemovePersonalInformation.docx"); doc = new Document(getArtifactsDir() + "Document.RemovePersonalInformation.docx"); Assert.assertEquals(saveWithoutPersonalInfo, doc.getRemovePersonalInformation()); Assert.assertEquals("John Doe", doc.getBuiltInDocumentProperties().getAuthor()); Assert.assertEquals("Placeholder Inc.", doc.getBuiltInDocumentProperties().getCompany()); Assert.assertEquals("John Doe", doc.getRevisions().get(0).getAuthor());
public IResourceLoadingCallback getResourceLoadingCallback() / public void setResourceLoadingCallback(IResourceLoadingCallback value)
Example:
Shows how to customize the process of loading external resources into a document.Document doc = new Document(); doc.setResourceLoadingCallback(new ImageNameHandler()); DocumentBuilder builder = new DocumentBuilder(doc); // Images usually are inserted using a URI, or a byte array. // Every instance of a resource load will call our callback's ResourceLoading method. builder.insertImage("Google logo"); builder.insertImage("Aspose logo"); builder.insertImage("Watermark"); Assert.assertEquals(3, doc.getChildNodes(NodeType.SHAPE, true).getCount()); doc.save(getArtifactsDir() + "DocumentBase.ResourceLoadingCallback.docx"); /// <summary> /// Allows us to load images into a document using predefined shorthands, as opposed to URIs. /// This will separate image loading logic from the rest of the document construction. /// </summary> private static class ImageNameHandler implements IResourceLoadingCallback { public int resourceLoading(final ResourceLoadingArgs args) throws URISyntaxException, IOException { if (args.getResourceType() == ResourceType.IMAGE) { // If this callback encounters one of the image shorthands while loading an image, // it will apply unique logic for each defined shorthand instead of treating it as a URI. 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; } if ("Watermark".equals(args.getOriginalUri())) { InputStream imageStream = new FileInputStream(getImageDir() + "Transparent background logo.png"); args.setData(DocumentHelper.getBytesFromStream(imageStream)); return ResourceLoadingAction.USER_PROVIDED; } } return ResourceLoadingAction.DEFAULT; } }
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 work with revisions in a document.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()); // To register our edits as revisions, we need to declare an author, and then start tracking them. doc.startTrackRevisions("John Doe", new Date()); builder.write("This is revision #1. "); Assert.assertTrue(doc.hasRevisions()); Assert.assertEquals(1, doc.getRevisions().getCount()); // This flag corresponds to the "Review" -> "Tracking" -> "Track Changes" option in Microsoft Word. // The "StartTrackRevisions" method does not affect its value, // and the document is tracking revisions programmatically despite it having a value of "false". // If we open this document using Microsoft Word, it will not be tracking revisions. Assert.assertFalse(doc.getTrackRevisions()); // We have added text using the document builder, so the first revision is an insertion-type revision. Revision revision = doc.getRevisions().get(0); Assert.assertEquals("John Doe", revision.getAuthor()); Assert.assertEquals("This is revision #1. ", revision.getParentNode().getText()); Assert.assertEquals(RevisionType.INSERTION, revision.getRevisionType()); Assert.assertEquals(revision.getDateTime().getDate(), new Date().getDate()); Assert.assertEquals(doc.getRevisions().getGroups().get(0), revision.getGroup()); // Remove a run to create a deletion-type revision. doc.getFirstSection().getBody().getFirstParagraph().getRuns().get(0).remove(); // Adding a new revision places it at the beginning of the revision collection. Assert.assertEquals(RevisionType.DELETION, doc.getRevisions().get(0).getRevisionType()); Assert.assertEquals(2, doc.getRevisions().getCount()); // Insert revisions show up in the document body even before we accept/reject the revision. // Rejecting the revision will remove its nodes from the body. Conversely, nodes that make up delete revisions // also linger in the document until we accept the revision. Assert.assertEquals("This does not count as a revision. This is revision #1.", doc.getText().trim()); // Accepting the delete revision will remove its parent node from the paragraph text // and then remove the collection's revision itself. doc.getRevisions().get(0).accept(); Assert.assertEquals(1, doc.getRevisions().getCount()); Assert.assertEquals("This is revision #1.", doc.getText().trim()); builder.writeln(""); builder.write("This is revision #2."); // Now move the node to create a moving revision type. Node node = doc.getFirstSection().getBody().getParagraphs().get(1); Node endNode = doc.getFirstSection().getBody().getParagraphs().get(1).getNextSibling(); Node referenceNode = doc.getFirstSection().getBody().getParagraphs().get(0); while (node != endNode) { Node nextNode = node.getNextSibling(); doc.getFirstSection().getBody().insertBefore(node, referenceNode); node = nextNode; } Assert.assertEquals(RevisionType.MOVING, doc.getRevisions().get(0).getRevisionType()); Assert.assertEquals(8, doc.getRevisions().getCount()); Assert.assertEquals("This is revision #2.\rThis is revision #1. \rThis is revision #2.", doc.getText().trim()); // The moving revision is now at index 1. Reject the revision to discard its contents. doc.getRevisions().get(1).reject(); Assert.assertEquals(6, doc.getRevisions().getCount()); Assert.assertEquals("This is revision #1. \rThis is revision #2.", doc.getText().trim());
public int getRevisionsView() / public void setRevisionsView(int value)
Example:
Shows how to switch between the revised and the original view of a document.Document doc = new Document(getMyDir() + "Revisions at list levels.docx"); doc.updateListLabels(); ParagraphCollection paragraphs = doc.getFirstSection().getBody().getParagraphs(); Assert.assertEquals("1.", paragraphs.get(0).getListLabel().getLabelString()); Assert.assertEquals("a.", paragraphs.get(1).getListLabel().getLabelString()); Assert.assertEquals("", paragraphs.get(2).getListLabel().getLabelString()); // View the document object as if all the revisions are accepted. Currently supports list labels. doc.setRevisionsView(RevisionsView.FINAL); Assert.assertEquals("", paragraphs.get(0).getListLabel().getLabelString()); Assert.assertEquals("1.", paragraphs.get(1).getListLabel().getLabelString()); Assert.assertEquals("a.", paragraphs.get(2).getListLabel().getLabelString());
public SectionCollection getSections()
Example:
Shows how to add and remove sections in a document.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.write("Section 1"); builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE); builder.write("Section 2"); Assert.assertEquals("Section 1\fSection 2", doc.getText().trim()); // Delete the first section from the document. doc.getSections().removeAt(0); Assert.assertEquals("Section 2", doc.getText().trim()); // Append a copy of what is now the first section to the end of the document. int lastSectionIdx = doc.getSections().getCount() - 1; Section newSection = doc.getSections().get(lastSectionIdx).deepClone(); doc.getSections().add(newSection); Assert.assertEquals("Section 2\fSection 2", doc.getText().trim());
Example:
Shows how to specify how a new section separates itself from the previous.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.writeln("This text is in section 1."); // Section break types determine how a new section separates itself from the previous section. // Below are five types of section breaks. // 1 - Starts the next section on a new page: builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE); builder.writeln("This text is in section 2."); Assert.assertEquals(SectionStart.NEW_PAGE, doc.getSections().get(1).getPageSetup().getSectionStart()); // 2 - Starts the next section on the current page: builder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS); builder.writeln("This text is in section 3."); Assert.assertEquals(SectionStart.CONTINUOUS, doc.getSections().get(2).getPageSetup().getSectionStart()); // 3 - Starts the next section on a new even page: builder.insertBreak(BreakType.SECTION_BREAK_EVEN_PAGE); builder.writeln("This text is in section 4."); Assert.assertEquals(SectionStart.EVEN_PAGE, doc.getSections().get(3).getPageSetup().getSectionStart()); // 4 - Starts the next section on a new odd page: builder.insertBreak(BreakType.SECTION_BREAK_ODD_PAGE); builder.writeln("This text is in section 5."); Assert.assertEquals(SectionStart.ODD_PAGE, doc.getSections().get(4).getPageSetup().getSectionStart()); // 5 - Starts the next section on a new column: TextColumnCollection columns = builder.getPageSetup().getTextColumns(); columns.setCount(2); builder.insertBreak(BreakType.SECTION_BREAK_NEW_COLUMN); builder.writeln("This text is in section 6."); Assert.assertEquals(SectionStart.NEW_COLUMN, doc.getSections().get(5).getPageSetup().getSectionStart()); doc.save(getArtifactsDir() + "PageSetup.SetSectionStart.docx");
public boolean getShadeFormData() / public void setShadeFormData(boolean value)
Example:
Shows how to apply gray shading to form fields.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.write("Hello world! "); builder.insertTextInput("My form field", TextFormFieldType.REGULAR, "", "Text contents of form field, which are shaded in grey by default.", 0); // We can turn the grey shading off, so the bookmarked text will blend in with the other text. doc.setShadeFormData(useGreyShading); doc.save(getArtifactsDir() + "Document.ShadeFormData.docx");
public boolean getShowGrammaticalErrors() / public void setShowGrammaticalErrors(boolean value)
Example:
Shows how to show/hide errors in the document.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert two sentences with mistakes that would be picked up // by the spelling and grammar checkers in Microsoft Word. builder.writeln("There is a speling error in this sentence."); builder.writeln("Their is a grammatical error in this sentence."); // If these options are enabled, then spelling errors will be underlined // in the output document by a jagged red line, and a double blue line will highlight grammatical mistakes. doc.setShowGrammaticalErrors(showErrors); doc.setShowSpellingErrors(showErrors); doc.save(getArtifactsDir() + "Document.SpellingAndGrammarErrors.docx");
public boolean getShowSpellingErrors() / public void setShowSpellingErrors(boolean value)
Example:
Shows how to show/hide errors in the document.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert two sentences with mistakes that would be picked up // by the spelling and grammar checkers in Microsoft Word. builder.writeln("There is a speling error in this sentence."); builder.writeln("Their is a grammatical error in this sentence."); // If these options are enabled, then spelling errors will be underlined // in the output document by a jagged red line, and a double blue line will highlight grammatical mistakes. doc.setShowGrammaticalErrors(showErrors); doc.setShowSpellingErrors(showErrors); doc.save(getArtifactsDir() + "Document.SpellingAndGrammarErrors.docx");
public boolean getSpellingChecked() / public void setSpellingChecked(boolean value)
Example:
Shows how to set spelling or grammar verifying.Document doc = new Document(); // The string with spelling errors. doc.getFirstSection().getBody().getFirstParagraph().getRuns().add(new Run(doc, "The speeling in this documentz is all broked.")); // Spelling/Grammar check start if we set properties to false. // We can see all errors in Microsoft Word via Review -> Spelling & Grammar. // Note that Microsoft Word does not start grammar/spell check automatically for DOC and RTF document format. doc.setSpellingChecked(checkSpellingGrammar); doc.setGrammarChecked(checkSpellingGrammar); doc.save(getArtifactsDir() + "Document.SpellingOrGrammar.docx");
public StyleCollection getStyles()
For more information see the description of the
Example:
Shows how to access a document's style collection.Document doc = new Document(); Assert.assertEquals(4, doc.getStyles().getCount()); // Enumerate and list all the styles that a document created using Aspose.Words contains by default. Iterator<Style> stylesEnum = doc.getStyles().iterator(); while (stylesEnum.hasNext()) { Style curStyle = stylesEnum.next(); System.out.println(MessageFormat.format("Style name:\t\"{0}\", of type \"{1}\"", curStyle.getName(), curStyle.getType())); System.out.println(MessageFormat.format("\tSubsequent style:\t{0}", curStyle.getNextParagraphStyleName())); System.out.println(MessageFormat.format("\tIs heading:\t\t\t{0}", curStyle.isHeading())); System.out.println(MessageFormat.format("\tIs QuickStyle:\t\t{0}", curStyle.isQuickStyle())); Assert.assertEquals(curStyle.getDocument(), doc); }
Example:
Shows how to create and use a paragraph style with list formatting.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Create a custom paragraph style. Style style = doc.getStyles().add(StyleType.PARAGRAPH, "MyStyle1"); style.getFont().setSize(24.0); style.getFont().setName("Verdana"); style.getParagraphFormat().setSpaceAfter(12.0); // 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 document builder's current paragraph, and then add some text. builder.getParagraphFormat().setStyle(style); builder.writeln("Hello World: MyStyle1, bulleted list."); // Change the document builder's style to one that has no list formatting and write another paragraph. builder.getParagraphFormat().setStyle(doc.getStyles().get("Normal")); builder.writeln("Hello World: Normal."); builder.getDocument().save(getArtifactsDir() + "Styles.ParagraphStyleBulletedList.docx");
public Theme getTheme()
Example:
Shows how to set custom colors and fonts for themes.Document doc = new Document(getMyDir() + "Theme colors.docx"); // The "Theme" object gives us access to the document theme, a source of default fonts and colors. Theme theme = doc.getTheme(); // Some styles, such as "Heading 1" and "Subtitle", will inherit these fonts. theme.getMajorFonts().setLatin("Courier New"); theme.getMinorFonts().setLatin("Agency FB"); // Other languages may also have their custom fonts in this theme. Assert.assertEquals(theme.getMajorFonts().getComplexScript(), ""); Assert.assertEquals(theme.getMajorFonts().getEastAsian(), ""); Assert.assertEquals(theme.getMinorFonts().getComplexScript(), ""); Assert.assertEquals(theme.getMinorFonts().getEastAsian(), ""); // The "Colors" property contains the color palette from Microsoft Word, // which appears when changing shading or font color. // Apply custom colors to the color palette so we have easy access to them in Microsoft Word // when we, for example, change the font color via "Home" -> "Font" -> "Font Color", // or insert a shape, and then set a color for it via "Shape Format" -> "Shape Styles". ThemeColors colors = theme.getColors(); colors.setDark1(Color.BLUE); colors.setLight1(Color.GREEN); colors.setDark2(Color.MAGENTA); colors.setLight2(Color.BLACK); colors.setAccent1(Color.RED); colors.setAccent2(Color.PINK); colors.setAccent3(Color.YELLOW); colors.setAccent4(Color.orange); colors.setAccent5(Color.cyan); colors.setAccent6(Color.darkGray); // Apply custom colors to hyperlinks in their clicked and un-clicked states. colors.setHyperlink(Color.WHITE); colors.setFollowedHyperlink(Color.lightGray); doc.save(getArtifactsDir() + "Themes.CustomColorsAndFonts.docx");
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 work with revisions in a document.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()); // To register our edits as revisions, we need to declare an author, and then start tracking them. doc.startTrackRevisions("John Doe", new Date()); builder.write("This is revision #1. "); Assert.assertTrue(doc.hasRevisions()); Assert.assertEquals(1, doc.getRevisions().getCount()); // This flag corresponds to the "Review" -> "Tracking" -> "Track Changes" option in Microsoft Word. // The "StartTrackRevisions" method does not affect its value, // and the document is tracking revisions programmatically despite it having a value of "false". // If we open this document using Microsoft Word, it will not be tracking revisions. Assert.assertFalse(doc.getTrackRevisions()); // We have added text using the document builder, so the first revision is an insertion-type revision. Revision revision = doc.getRevisions().get(0); Assert.assertEquals("John Doe", revision.getAuthor()); Assert.assertEquals("This is revision #1. ", revision.getParentNode().getText()); Assert.assertEquals(RevisionType.INSERTION, revision.getRevisionType()); Assert.assertEquals(revision.getDateTime().getDate(), new Date().getDate()); Assert.assertEquals(doc.getRevisions().getGroups().get(0), revision.getGroup()); // Remove a run to create a deletion-type revision. doc.getFirstSection().getBody().getFirstParagraph().getRuns().get(0).remove(); // Adding a new revision places it at the beginning of the revision collection. Assert.assertEquals(RevisionType.DELETION, doc.getRevisions().get(0).getRevisionType()); Assert.assertEquals(2, doc.getRevisions().getCount()); // Insert revisions show up in the document body even before we accept/reject the revision. // Rejecting the revision will remove its nodes from the body. Conversely, nodes that make up delete revisions // also linger in the document until we accept the revision. Assert.assertEquals("This does not count as a revision. This is revision #1.", doc.getText().trim()); // Accepting the delete revision will remove its parent node from the paragraph text // and then remove the collection's revision itself. doc.getRevisions().get(0).accept(); Assert.assertEquals(1, doc.getRevisions().getCount()); Assert.assertEquals("This is revision #1.", doc.getText().trim()); builder.writeln(""); builder.write("This is revision #2."); // Now move the node to create a moving revision type. Node node = doc.getFirstSection().getBody().getParagraphs().get(1); Node endNode = doc.getFirstSection().getBody().getParagraphs().get(1).getNextSibling(); Node referenceNode = doc.getFirstSection().getBody().getParagraphs().get(0); while (node != endNode) { Node nextNode = node.getNextSibling(); doc.getFirstSection().getBody().insertBefore(node, referenceNode); node = nextNode; } Assert.assertEquals(RevisionType.MOVING, doc.getRevisions().get(0).getRevisionType()); Assert.assertEquals(8, doc.getRevisions().getCount()); Assert.assertEquals("This is revision #2.\rThis is revision #1. \rThis is revision #2.", doc.getText().trim()); // The moving revision is now at index 1. Reject the revision to discard its contents. doc.getRevisions().get(1).reject(); Assert.assertEquals(6, doc.getRevisions().getCount()); Assert.assertEquals("This is revision #1. \rThis is revision #2.", doc.getText().trim());
public VariableCollection getVariables()
Example:
Shows how to work with a document's variable collection.Document doc = new Document(); VariableCollection variables = doc.getVariables(); // Every document has a collection of key/value pair variables, which we can add items to. variables.add("Home address", "123 Main St."); variables.add("City", "London"); variables.add("Bedrooms", "3"); Assert.assertEquals(3, variables.getCount()); // We can display the values of variables in the document body using DOCVARIABLE fields. DocumentBuilder builder = new DocumentBuilder(doc); FieldDocVariable field = (FieldDocVariable) builder.insertField(FieldType.FIELD_DOC_VARIABLE, true); field.setVariableName("Home address"); field.update(); Assert.assertEquals("123 Main St.", field.getResult()); // Assigning values to existing keys will update them. variables.add("Home address", "456 Queen St."); // We will then have to update DOCVARIABLE fields to ensure they display an up-to-date value. Assert.assertEquals("123 Main St.", field.getResult()); field.update(); Assert.assertEquals("456 Queen St.", field.getResult()); // Verify that the document variables with a certain name or value exist. Assert.assertTrue(variables.contains("City")); Assert.assertTrue(IterableUtils.matchesAny(variables, s -> s.getValue() == "London")); // The collection of variables automatically sorts variables alphabetically by name. Assert.assertEquals(0, variables.indexOfKey("Bedrooms")); Assert.assertEquals(1, variables.indexOfKey("City")); Assert.assertEquals(2, variables.indexOfKey("Home address")); // Below are three ways of removing document variables from a collection. // 1 - By name: variables.remove("City"); Assert.assertFalse(variables.contains("City")); // 2 - By index: variables.removeAt(1); Assert.assertFalse(variables.contains("Home address")); // 3 - Clear the whole collection at once: variables.clear(); Assert.assertEquals(variables.getCount(), 0);
public VbaProject getVbaProject() / public void setVbaProject(VbaProject value)
Example:
Shows how to access a document's VBA project information.Document doc = new Document(getMyDir() + "VBA project.docm"); // A VBA project contains a collection of VBA modules. VbaProject vbaProject = doc.getVbaProject(); ? MessageFormat.format("Project name: {0} signed; Project code page: {1}; Modules count: {2}\n", vbaProject.getName(), vbaProject.getCodePage(), vbaProject.getModules().getCount()) : MessageFormat.format("Project name: {0} not signed; Project code page: {1}; Modules count: {2}\n", vbaProject.getName(), vbaProject.getCodePage(), vbaProject.getModules().getCount())); VbaModuleCollection vbaModules = doc.getVbaProject().getModules(); Assert.assertEquals(vbaModules.getCount(), 3); for (VbaModule module : vbaModules) { System.out.println(MessageFormat.format("Module name: {0};\nModule code:\n{1}\n", module.getName(), module.getSourceCode())); } // Set new source code for VBA module. You can access VBA modules in the collection either by index or by name. vbaModules.get(0).setSourceCode("Your VBA code..."); vbaModules.get("Module1").setSourceCode("Your VBA code..."); // Remove a module from the collection. vbaModules.remove(vbaModules.get(2));
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 work with the versions count feature of older Microsoft Word documents.Document doc = new Document(getMyDir() + "Versions.doc"); // We can read this property of a document, but we cannot preserve it while saving. Assert.assertEquals(4, doc.getVersionsCount()); doc.save(getArtifactsDir() + "Document.VersionsCount.doc"); doc = new Document(getArtifactsDir() + "Document.VersionsCount.doc"); Assert.assertEquals(0, doc.getVersionsCount());
public ViewOptions getViewOptions()
Example:
Shows how to set a custom zoom factor, which older versions of Microsoft Word will apply to a document upon loading.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.writeln("Hello world!"); doc.getViewOptions().setViewType(ViewType.PAGE_LAYOUT); doc.getViewOptions().setZoomPercent(50); Assert.assertEquals(ZoomType.CUSTOM, doc.getViewOptions().getZoomType()); Assert.assertEquals(ZoomType.NONE, doc.getViewOptions().getZoomType()); doc.save(getArtifactsDir() + "ViewOptions.SetZoomPercentage.doc");
Example:
Shows how to set a custom zoom type, which older versions of Microsoft Word will apply to a document upon loading.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.writeln("Hello world!"); // Set the "ZoomType" property to "ZoomType.PageWidth" to get Microsoft Word // to automatically zoom the document to fit the width of the page. // Set the "ZoomType" property to "ZoomType.FullPage" to get Microsoft Word // to automatically zoom the document to make the entire first page visible. // Set the "ZoomType" property to "ZoomType.TextFit" to get Microsoft Word // to automatically zoom the document to fit the inner text margins of the first page. doc.getViewOptions().setZoomType(zoomType); doc.save(getArtifactsDir() + "ViewOptions.SetZoomType.doc");
public IWarningCallback getWarningCallback() / public void setWarningCallback(IWarningCallback value)
Example:
Shows how to use the IWarningCallback interface to monitor font substitution warnings.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.getFont().setName("Times New Roman"); builder.writeln("Hello world!"); FontSubstitutionWarningCollector callback = new FontSubstitutionWarningCollector(); doc.setWarningCallback(callback); // Store the current collection of font sources, which will be the default font source for every document // for which we do not specify a different font source. FontSourceBase[] originalFontSources = FontSettings.getDefaultInstance().getFontsSources(); // For testing purposes, we will set Aspose.Words to look for fonts only in a folder that does not exist. FontSettings.getDefaultInstance().setFontsFolder("", false); // When rendering the document, there will be no place to find the "Times New Roman" font. // This will cause a font substitution warning, which our callback will detect. doc.save(getArtifactsDir() + "FontSettings.SubstitutionWarning.pdf"); FontSettings.getDefaultInstance().setFontsSources(originalFontSources); Assert.assertTrue(callback.FontSubstitutionWarnings.get(0).getDescription() .equals("Font 'Times New Roman' has not been found. Using 'Fanwood' font instead. Reason: first available font.")); } private static class FontSubstitutionWarningCollector implements IWarningCallback { /// <summary> /// Called every time a warning occurs during loading/saving. /// </summary> public void warning(WarningInfo info) { if (info.getWarningType() == WarningType.FONT_SUBSTITUTION) FontSubstitutionWarnings.warning(info); } public WarningInfoCollection FontSubstitutionWarnings = new WarningInfoCollection(); }
Example:
Shows how to set the property for finding the closest match for a missing font from the available font sources.@Test public void enableFontSubstitution() throws Exception { // Open a document that contains text formatted with a font that does not exist in any of our font sources. Document doc = new Document(getMyDir() + "Missing font.docx"); // Assign a callback for handling font substitution warnings. HandleDocumentSubstitutionWarnings substitutionWarningHandler = new HandleDocumentSubstitutionWarnings(); doc.setWarningCallback(substitutionWarningHandler); // Set a default font name and enable font substitution. FontSettings fontSettings = new FontSettings(); fontSettings.getSubstitutionSettings().getDefaultFontSubstitution().setDefaultFontName("Arial"); fontSettings.getSubstitutionSettings().getFontInfoSubstitution().setEnabled(true); // We will get a font substitution warning if we save a document with a missing font. doc.setFontSettings(fontSettings); doc.save(getArtifactsDir() + "FontSettings.EnableFontSubstitution.pdf"); Iterator<WarningInfo> warnings = substitutionWarningHandler.FontWarnings.iterator(); while (warnings.hasNext()) System.out.println(warnings.next().getDescription()); // We can also verify warnings in the collection and clear them. Assert.assertEquals(WarningSource.LAYOUT, substitutionWarningHandler.FontWarnings.get(0).getSource()); Assert.assertEquals("Font '28 Days Later' has not been found. Using 'Calibri' font instead. Reason: alternative name from document.", substitutionWarningHandler.FontWarnings.get(0).getDescription()); substitutionWarningHandler.FontWarnings.clear(); Assert.assertTrue(substitutionWarningHandler.FontWarnings.getCount() == 0); } public static class HandleDocumentSubstitutionWarnings implements IWarningCallback { /// <summary> /// Called every time a warning occurs during loading/saving. /// </summary> public void warning(WarningInfo info) { if (info.getWarningType() == WarningType.FONT_SUBSTITUTION) FontWarnings.warning(info); } public WarningInfoCollection FontWarnings = new WarningInfoCollection(); }
public Watermark getWatermark()
public TaskPaneCollection getWebExtensionTaskPanes()
public WriteProtection getWriteProtection()
Example:
Shows how to protect a document with a password.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.writeln("Hello world! This document is protected."); // Enter a password up to 15 characters in length, and then verify the document's protection status. doc.getWriteProtection().setPassword("MyPassword"); doc.getWriteProtection().setReadOnlyRecommended(true); Assert.assertTrue(doc.getWriteProtection().isWriteProtected()); Assert.assertTrue(doc.getWriteProtection().validatePassword("MyPassword")); // Protection does not prevent the document from being edited programmatically, nor does it encrypt the contents. doc.save(getArtifactsDir() + "Document.WriteProtection.docx"); doc = new Document(getArtifactsDir() + "Document.WriteProtection.docx"); Assert.assertTrue(doc.getWriteProtection().isWriteProtected()); builder = new DocumentBuilder(doc); builder.moveToDocumentEnd(); builder.writeln("Writing text in a protected document."); Assert.assertEquals("Hello world! This document is protected." + "\rWriting text in a protected document.", doc.getText().trim());
public boolean accept(DocumentVisitor visitor) throws java.lang.Exception
Enumerates over this node and all of its children. Each node calls a corresponding method on DocumentVisitor.
For more info see the Visitor design pattern.
visitor
- The visitor that will visit the nodes.Example:
Shows how to use a document visitor to print a document's node structure.Document doc = new Document(getMyDir() + "DocumentVisitor-compatible features.docx"); DocStructurePrinter visitor = new DocStructurePrinter(); // When we get a composite node to accept a document visitor, the visitor visits the accepting node, // and then traverses all the node's children in a depth-first manner. // The visitor can read and modify each visited node. doc.accept(visitor); System.out.println(visitor.getText()); /// <summary> /// Traverses a node's tree of child nodes. /// Creates a map of this tree in the form of a string. /// </summary> public static class DocStructurePrinter extends DocumentVisitor { public DocStructurePrinter() { mAcceptingNodeChildTree = new StringBuilder(); } public String getText() { return mAcceptingNodeChildTree.toString(); } /// <summary> /// Called when a Document node is encountered. /// </summary> public int visitDocumentStart(Document doc) { int childNodeCount = doc.getChildNodes(NodeType.ANY, true).getCount(); indentAndAppendLine("[Document start] Child nodes: " + childNodeCount); mDocTraversalDepth++; // Allow the visitor to continue visiting other nodes. return VisitorAction.CONTINUE; } /// <summary> /// Called after all the child nodes of a Document node have been visited. /// </summary> public int visitDocumentEnd(Document doc) { 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 after all the child nodes of a Section node have been visited. /// </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 after all the child nodes of a Body node have been visited. /// </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 after all the child nodes of a Paragraph node have been visited. /// </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++) { mAcceptingNodeChildTree.append("| "); } mAcceptingNodeChildTree.append(text + "\r\n"); } private int mDocTraversalDepth; private final StringBuilder mAcceptingNodeChildTree; }
public void acceptAllRevisions() throws java.lang.Exception
Example:
Shows how to accept all tracking changes in the document.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Edit the document while tracking changes to create a few revisions. doc.startTrackRevisions("John Doe"); builder.write("Hello world! "); builder.write("Hello again! "); builder.write("This is another revision."); doc.stopTrackRevisions(); Assert.assertEquals(3, doc.getRevisions().getCount()); // We can iterate through every revision and accept/reject it as a part of our document. // If we know we wish to accept every revision, we can do it more straightforwardly so by calling this method. doc.acceptAllRevisions(); Assert.assertEquals(0, doc.getRevisions().getCount()); Assert.assertEquals("Hello world! Hello again! This is another revision.", doc.getText().trim());
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:
Shows how to construct an Aspose.Words document by hand.Document doc = new Document(); // A blank document contains one section, one body and one paragraph. // Call the "RemoveAllChildren" method to remove all those nodes, // and end up with a document node with no children. doc.removeAllChildren(); // This document now has no composite child nodes that we can add content to. // If we wish to edit it, we will need to repopulate its node collection. // First, create a new section, and then append it as a child to the root document node. Section section = new Section(doc); doc.appendChild(section); // Set some page setup properties for the section. section.getPageSetup().setSectionStart(SectionStart.NEW_PAGE); section.getPageSetup().setPaperSize(PaperSize.LETTER); // A section needs a body, which will contain and display all its contents // on the page between the section's header and footer. Body body = new Body(doc); section.appendChild(body); // Create a paragraph, set some formatting properties, and then append it as a child to the body. Paragraph para = new Paragraph(doc); para.getParagraphFormat().setStyleName("Heading 1"); para.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER); body.appendChild(para); // Finally, add some content to do the document. Create a run, // set its appearance and contents, and then append it as a child to the paragraph. Run run = new Run(doc); run.setText("Hello World!"); run.getFont().setColor(Color.RED); para.appendChild(run); Assert.assertEquals("Hello World!", doc.getText().trim()); doc.save(getArtifactsDir() + "Section.CreateManually.docx");
public void appendDocument(Document srcDoc, int importFormatMode)
srcDoc
- The document to append.importFormatMode
- A Example:
Shows how to append all the documents in a folder to the end of a template document.Document dstDoc = new Document(); DocumentBuilder builder = new DocumentBuilder(dstDoc); builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1); builder.writeln("Template Document"); builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.NORMAL); builder.writeln("Some content here"); // Append all unencrypted documents with the .doc extension // from our local file system directory to the base document. for (File fileName : new File(getMyDir()).listFiles((f, name) -> name.endsWith(".doc"))) { FileFormatInfo info = FileFormatUtil.detectFileFormat(fileName.getPath()); if (info.isEncrypted()) continue; Document srcDoc = new Document(fileName.getPath()); dstDoc.appendDocument(srcDoc, ImportFormatMode.USE_DESTINATION_STYLES); } dstDoc.save(getArtifactsDir() + "Document.AppendAllDocumentsInFolder.doc");
Example:
Shows how to append a document to the end of another document.Document srcDoc = new Document(); srcDoc.getFirstSection().getBody().appendParagraph("Source document text. "); Document dstDoc = new Document(); dstDoc.getFirstSection().getBody().appendParagraph("Destination document text. "); // Append the source document to the destination document while preserving its formatting, // then save the source document to the local file system. dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING); dstDoc.save(getArtifactsDir() + "Document.AppendDocument.docx");
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 manage list style clashes while appending a document.// Load a document with text in a custom style and clone it. Document srcDoc = new Document(getMyDir() + "Custom list numbering.docx"); Document dstDoc = srcDoc.deepClone(); // We now have two documents, each with an identical style named "CustomStyle". // Change the text color for one of the styles to set it apart from the other. dstDoc.getStyles().get("CustomStyle").getFont().setColor(Color.RED); // If there is a clash of list styles, apply the list format of the source document. // Set the "KeepSourceNumbering" property to "false" to not import any list numbers into the destination document. // Set the "KeepSourceNumbering" property to "true" import all clashing // list style numbering with the same appearance that it had in the source document. ImportFormatOptions options = new ImportFormatOptions(); options.setKeepSourceNumbering(keepSourceNumbering); // Joining two documents that have different styles that share the same name causes a style clash. // We can specify an import format mode while appending documents to resolve this clash. dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_DIFFERENT_STYLES, options); dstDoc.updateListLabels(); dstDoc.save(getArtifactsDir() + "DocumentBuilder.AppendDocumentAndResolveStyles.docx");
Example:
Shows how to manage list style clashes while inserting a document.Document dstDoc = new Document(); DocumentBuilder builder = new DocumentBuilder(dstDoc); builder.insertBreak(BreakType.PARAGRAPH_BREAK); dstDoc.getLists().add(ListTemplate.NUMBER_DEFAULT); List list = dstDoc.getLists().get(0); builder.getListFormat().setList(list); for (int i = 1; i <= 15; i++) builder.write(MessageFormat.format("List Item {0}\n", i)); Document attachDoc = (Document)dstDoc.deepClone(true); // If there is a clash of list styles, apply the list format of the source document. // Set the "KeepSourceNumbering" property to "false" to not import any list numbers into the destination document. // Set the "KeepSourceNumbering" property to "true" import all clashing // list style numbering with the same appearance that it had in the source document. ImportFormatOptions importOptions = new ImportFormatOptions(); importOptions.setKeepSourceNumbering(keepSourceNumbering); builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE); builder.insertDocument(attachDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING, importOptions); dstDoc.save(getArtifactsDir() + "DocumentBuilder.InsertDocumentAndResolveStyles.docx");
Example:
Shows how to manage list style clashes while appending a clone of a document to itself.Document srcDoc = new Document(getMyDir() + "List item.docx"); Document dstDoc = new Document(getMyDir() + "List item.docx"); // If there is a clash of list styles, apply the list format of the source document. // Set the "KeepSourceNumbering" property to "false" to not import any list numbers into the destination document. // Set the "KeepSourceNumbering" property to "true" import all clashing // list style numbering with the same appearance that it had in the source document. DocumentBuilder builder = new DocumentBuilder(dstDoc); builder.moveToDocumentEnd(); builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE); ImportFormatOptions options = new ImportFormatOptions(); options.setKeepSourceNumbering(keepSourceNumbering); builder.insertDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING, options); dstDoc.updateListLabels();
public void cleanup() throws java.lang.Exception
Example:
Shows how to remove unused custom styles from a document.Document doc = new Document(); doc.getStyles().add(StyleType.LIST, "MyListStyle1"); doc.getStyles().add(StyleType.LIST, "MyListStyle2"); doc.getStyles().add(StyleType.CHARACTER, "MyParagraphStyle1"); doc.getStyles().add(StyleType.CHARACTER, "MyParagraphStyle2"); // Combined with the built-in styles, the document now has eight styles. // A custom style counts as "used" while applied to some part of the document, // which means that the four styles we added are currently unused. Assert.assertEquals(8, doc.getStyles().getCount()); // Apply a custom character style, and then a custom list style. Doing so will mark the styles as "used". DocumentBuilder builder = new DocumentBuilder(doc); builder.getFont().setStyle(doc.getStyles().get("MyParagraphStyle1")); builder.writeln("Hello world!"); List list = doc.getLists().add(doc.getStyles().get("MyListStyle1")); builder.getListFormat().setList(list); builder.writeln("Item 1"); builder.writeln("Item 2"); doc.cleanup(); Assert.assertEquals(6, doc.getStyles().getCount()); // Removing every node that a custom style is applied to marks it as "unused" again. // Run the Cleanup method again to remove them. doc.getFirstSection().getBody().removeAllChildren(); doc.cleanup(); Assert.assertEquals(4, doc.getStyles().getCount());
public void cleanup(CleanupOptions options) throws java.lang.Exception
Example:
Shows how to remove all unused custom styles from a document.Document doc = new Document(); doc.getStyles().add(StyleType.LIST, "MyListStyle1"); doc.getStyles().add(StyleType.LIST, "MyListStyle2"); doc.getStyles().add(StyleType.CHARACTER, "MyParagraphStyle1"); doc.getStyles().add(StyleType.CHARACTER, "MyParagraphStyle2"); // Combined with the built-in styles, the document now has eight styles. // A custom style is marked as "used" while there is any text within the document // formatted in that style. This means that the 4 styles we added are currently unused. Assert.assertEquals(8, doc.getStyles().getCount()); // Apply a custom character style, and then a custom list style. Doing so will mark them as "used". DocumentBuilder builder = new DocumentBuilder(doc); builder.getFont().setStyle(doc.getStyles().get("MyParagraphStyle1")); builder.writeln("Hello world!"); List list = doc.getLists().add(doc.getStyles().get("MyListStyle1")); builder.getListFormat().setList(list); builder.writeln("Item 1"); builder.writeln("Item 2"); // Now, there is one unused character style and one unused list style. // The Cleanup() method, when configured with a CleanupOptions object, can target unused styles and remove them. CleanupOptions cleanupOptions = new CleanupOptions(); cleanupOptions.setUnusedLists(true); cleanupOptions.setUnusedStyles(true); cleanupOptions.setUnusedBuiltinStyles(true); doc.cleanup(cleanupOptions); Assert.assertEquals(4, doc.getStyles().getCount()); // Removing every node that a custom style is applied to marks it as "unused" again. // Rerun the Cleanup method to remove them. doc.getFirstSection().getBody().removeAllChildren(); doc.cleanup(cleanupOptions); Assert.assertEquals(2, doc.getStyles().getCount());
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 compare documents.Document docOriginal = new Document(); DocumentBuilder builder = new DocumentBuilder(docOriginal); builder.writeln("This is the original document."); Document docEdited = new Document(); builder = new DocumentBuilder(docEdited); builder.writeln("This is the edited document."); // Comparing documents with revisions will throw an exception. if (docOriginal.getRevisions().getCount() == 0 && docEdited.getRevisions().getCount() == 0) docOriginal.compare(docEdited, "authorName", new Date()); // After the comparison, the original document will gain a new revision // for every element that is different in the edited document. System.out.println("Revision type: {r.RevisionType}, on a node of type \"{r.ParentNode.NodeType}\""); System.out.println("\tChanged text: \"{r.ParentNode.GetText()}\""); } // Accepting these revisions will transform the original document into the edited document. docOriginal.getRevisions().acceptAll(); Assert.assertEquals(docOriginal.getText(), docEdited.getText());
public void compare(Document document, java.lang.String author, java.util.Date dateTime, CompareOptions options) throws java.lang.Exception
Example:
Shows how to filter specific types of document elements when making a comparison.// Create the original document and populate it with various kinds of elements. Document docOriginal = new Document(); DocumentBuilder builder = new DocumentBuilder(docOriginal); // Paragraph text referenced with an endnote: builder.writeln("Hello world! This is the first paragraph."); builder.insertFootnote(FootnoteType.ENDNOTE, "Original endnote text."); // Table: builder.startTable(); builder.insertCell(); builder.write("Original cell 1 text"); builder.insertCell(); builder.write("Original cell 2 text"); builder.endTable(); // Textbox: Shape textBox = builder.insertShape(ShapeType.TEXT_BOX, 150.0, 20.0); builder.moveTo(textBox.getFirstParagraph()); builder.write("Original textbox contents"); // DATE field: builder.moveTo(docOriginal.getFirstSection().getBody().appendParagraph("")); builder.insertField(" DATE "); // Comment: Comment newComment = new Comment(docOriginal, "John Doe", "J.D.", new Date()); newComment.setText("Original comment."); builder.getCurrentParagraph().appendChild(newComment); // Header: builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY); builder.writeln("Original header contents."); // Create a clone of our document and perform a quick edit on each of the cloned document's elements. Document docEdited = (Document) docOriginal.deepClone(true); Paragraph firstParagraph = docEdited.getFirstSection().getBody().getFirstParagraph(); firstParagraph.getRuns().get(0).setText("hello world! this is the first paragraph, after editing."); firstParagraph.getParagraphFormat().setStyle(docEdited.getStyles().getByStyleIdentifier(StyleIdentifier.HEADING_1)); ((Footnote) docEdited.getChild(NodeType.FOOTNOTE, 0, true)).getFirstParagraph().getRuns().get(1).setText("Edited endnote text."); ((Table) docEdited.getChild(NodeType.TABLE, 0, true)).getFirstRow().getCells().get(1).getFirstParagraph().getRuns().get(0).setText("Edited Cell 2 contents"); ((Shape) docEdited.getChild(NodeType.SHAPE, 0, true)).getFirstParagraph().getRuns().get(0).setText("Edited textbox contents"); ((FieldDate) docEdited.getRange().getFields().get(0)).setUseLunarCalendar(true); ((Comment) docEdited.getChild(NodeType.COMMENT, 0, true)).getFirstParagraph().getRuns().get(0).setText("Edited comment."); docEdited.getFirstSection().getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_PRIMARY).getFirstParagraph().getRuns().get(0).setText("Edited header contents."); // Comparing documents creates a revision for every edit in the edited document. // A CompareOptions object has a series of flags that can suppress revisions // on each respective type of element, effectively ignoring their change. CompareOptions compareOptions = new CompareOptions(); compareOptions.setIgnoreFormatting(false); 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); docOriginal.compare(docEdited, "John Doe", new Date(), compareOptions); docOriginal.save(getArtifactsDir() + "Document.CompareOptions.docx");
public void copyStylesFromTemplate(Document template)
Example:
Shows how to copy styles from one document to another.// Create a document, and then add styles that we will copy to another document. Document template = new Document(); Style style = template.getStyles().add(StyleType.PARAGRAPH, "TemplateStyle1"); style.getFont().setName("Times New Roman"); style.getFont().setColor(Color.WHITE); style = template.getStyles().add(StyleType.PARAGRAPH, "TemplateStyle2"); style.getFont().setName("Arial"); style.getFont().setColor(Color.RED); style = template.getStyles().add(StyleType.PARAGRAPH, "TemplateStyle3"); style.getFont().setName("Courier New"); style.getFont().setColor(Color.BLUE); Assert.assertEquals(7, template.getStyles().getCount()); // Create a document which we will copy the styles to. Document target = new Document(); // Create a style with the same name as a style from the template document and add it to the target document. style = target.getStyles().add(StyleType.PARAGRAPH, "TemplateStyle3"); style.getFont().setName("Calibri"); style.getFont().setColor(Color.ORANGE); Assert.assertEquals(5, target.getStyles().getCount()); // There are two ways of calling the method to copy all the styles from one document to another. // 1 - Passing the template document object: target.copyStylesFromTemplate(template); // Copying styles adds all styles from the template document to the target // and overwrites existing styles with the same name. Assert.assertEquals(7, target.getStyles().getCount()); Assert.assertEquals("Courier New", target.getStyles().get("TemplateStyle3").getFont().getName()); Assert.assertEquals(Color.BLUE.getRGB(), target.getStyles().get("TemplateStyle3").getFont().getColor().getRGB()); // 2 - Passing the local system filename of a template document: target.copyStylesFromTemplate(getMyDir() + "Rendering.docx"); Assert.assertEquals(21, target.getStyles().getCount());
Example:
Shows how to copies styles from the template to a document via Document.Document template = new Document(getMyDir() + "Rendering.docx"); Document target = new Document(getMyDir() + "Document.docx"); target.copyStylesFromTemplate(template);
public void copyStylesFromTemplate(java.lang.String template) throws java.lang.Exception
Example:
Shows how to copy styles from one document to another.// Create a document, and then add styles that we will copy to another document. Document template = new Document(); Style style = template.getStyles().add(StyleType.PARAGRAPH, "TemplateStyle1"); style.getFont().setName("Times New Roman"); style.getFont().setColor(Color.WHITE); style = template.getStyles().add(StyleType.PARAGRAPH, "TemplateStyle2"); style.getFont().setName("Arial"); style.getFont().setColor(Color.RED); style = template.getStyles().add(StyleType.PARAGRAPH, "TemplateStyle3"); style.getFont().setName("Courier New"); style.getFont().setColor(Color.BLUE); Assert.assertEquals(7, template.getStyles().getCount()); // Create a document which we will copy the styles to. Document target = new Document(); // Create a style with the same name as a style from the template document and add it to the target document. style = target.getStyles().add(StyleType.PARAGRAPH, "TemplateStyle3"); style.getFont().setName("Calibri"); style.getFont().setColor(Color.ORANGE); Assert.assertEquals(5, target.getStyles().getCount()); // There are two ways of calling the method to copy all the styles from one document to another. // 1 - Passing the template document object: target.copyStylesFromTemplate(template); // Copying styles adds all styles from the template document to the target // and overwrites existing styles with the same name. Assert.assertEquals(7, target.getStyles().getCount()); Assert.assertEquals("Courier New", target.getStyles().get("TemplateStyle3").getFont().getName()); Assert.assertEquals(Color.BLUE.getRGB(), target.getStyles().get("TemplateStyle3").getFont().getColor().getRGB()); // 2 - Passing the local system filename of a template document: target.copyStylesFromTemplate(getMyDir() + "Rendering.docx"); Assert.assertEquals(21, target.getStyles().getCount());
public Document deepClone()
Example:
Shows how to deep clone a document.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.write("Hello world!"); // Cloning will produce a new document with the same contents as the original, // but with a unique copy of each of the original document's nodes. Document clone = doc.deepClone(); Assert.assertEquals(doc.getFirstSection().getBody().getFirstParagraph().getRuns().get(0).getText(), clone.getFirstSection().getBody().getFirstParagraph().getRuns().get(0).getText()); Assert.assertNotEquals(doc.getFirstSection().getBody().getFirstParagraph().getRuns().get(0).hashCode(), clone.getFirstSection().getBody().getFirstParagraph().getRuns().get(0).hashCode());
public Node deepClone(boolean isCloneChildren)
This method serves as a copy constructor for nodes. The cloned node has no parent, but belongs to the same document as the original node.
This method always performs a deep copy of the node. The isCloneChildren parameter specifies whether to perform copy all child nodes as well.
isCloneChildren
- True to recursively clone the subtree under the specified node;
false to clone only the node itself.Example:
Shows how to clone a composite node.Document doc = new Document(); Paragraph para = doc.getFirstSection().getBody().getFirstParagraph(); para.appendChild(new Run(doc, "Hello world!")); // Below are two ways of cloning a composite node. // 1 - Create a clone of a node, and create a clone of each of its child nodes as well. Node cloneWithChildren = para.deepClone(true); Assert.assertTrue(((CompositeNode) cloneWithChildren).hasChildNodes()); Assert.assertEquals("Hello world!", cloneWithChildren.getText().trim()); // 2 - Create a clone of a node just by itself without any children. Node cloneWithoutChildren = para.deepClone(false); Assert.assertFalse(((CompositeNode) cloneWithoutChildren).hasChildNodes()); Assert.assertEquals("", cloneWithoutChildren.getText().trim());
public void ensureMinimum()
Example:
Shows how to ensure that a document contains the minimal set of nodes required for editing its contents.// A newly created document contains one child Section, which includes one child Body and one child Paragraph. // We can edit the document body's contents by adding nodes such as Runs or inline Shapes to that paragraph. Document doc = new Document(); NodeCollection nodes = doc.getChildNodes(NodeType.ANY, true); Assert.assertEquals(NodeType.SECTION, nodes.get(0).getNodeType()); Assert.assertEquals(doc, nodes.get(0).getParentNode()); Assert.assertEquals(NodeType.BODY, nodes.get(1).getNodeType()); Assert.assertEquals(nodes.get(0), nodes.get(1).getParentNode()); Assert.assertEquals(NodeType.PARAGRAPH, nodes.get(2).getNodeType()); Assert.assertEquals(nodes.get(1), nodes.get(2).getParentNode()); // This is the minimal set of nodes that we need to be able to edit the document. // We will no longer be able to edit the document if we remove any of them. doc.removeAllChildren(); Assert.assertEquals(0, doc.getChildNodes(NodeType.ANY, true).getCount()); // Call this method to make sure that the document has at least those three nodes so we can edit it again. doc.ensureMinimum(); Assert.assertEquals(NodeType.SECTION, nodes.get(0).getNodeType()); Assert.assertEquals(NodeType.BODY, nodes.get(1).getNodeType()); Assert.assertEquals(NodeType.PARAGRAPH, nodes.get(2).getNodeType()); ((Paragraph) nodes.get(2)).getRuns().add(new Run(doc, "Hello world!"));
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 apply the properties of a table's style directly to the table's elements.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Table table = builder.startTable(); builder.insertCell(); builder.write("Hello world!"); builder.endTable(); TableStyle tableStyle = (TableStyle) doc.getStyles().add(StyleType.TABLE, "MyTableStyle1"); tableStyle.setRowStripe(3); tableStyle.setCellSpacing(5.0); tableStyle.getShading().setBackgroundPatternColor(Color.WHITE); tableStyle.getBorders().setColor(Color.BLUE); tableStyle.getBorders().setLineStyle(LineStyle.DOT_DASH); table.setStyle(tableStyle); // This method concerns table style properties such as the ones we set above. doc.expandTableStylesToDirectFormatting(); doc.save(getArtifactsDir() + "Document.TableStyleToDirectFormatting.docx");
public Document extractPages(int index, int count) throws java.lang.Exception
index
- The zero-based index of the first page to extract.count
- Number of pages to be extracted.Example:
Shows how to get specified range of pages from the document.Document doc = new Document(getMyDir() + "Layout entities.docx"); doc = doc.extractPages(0, 2); doc.save(getArtifactsDir() + "Document.ExtractPages.docx");
public CompositeNode getAncestor(int ancestorType)
ancestorType
- A Example:
Shows how to find out if a tables are nested.Document doc = new Document(getMyDir() + "Nested tables.docx"); NodeCollection tables = doc.getChildNodes(NodeType.TABLE, true); for (int i = 0; i < tables.getCount(); i++) { Table table = (Table) tables.get(i); // Find out if any cells in the table have other tables as children. int count = getChildTableCount(table); System.out.print(MessageFormat.format("Table #{0} has {1} tables directly within its cells", i, count)); // Find out if the table is nested inside another table, and, if so, 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}", i, tableDepth)); else System.out.println(MessageFormat.format("Table #{0} is a non nested table (is not a child of another table)", i)); } } /** * 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; Node parent = table.getAncestor(table.getNodeType()); while (parent != null) { 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 childTableCount = 0; for (Row row : table.getRows()) { for (Cell cell : row.getCells()) { TableCollection childTables = cell.getTables(); if (childTables.getCount() > 0) childTableCount++; } } return childTableCount; }
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 tables are nested.Document doc = new Document(getMyDir() + "Nested tables.docx"); NodeCollection tables = doc.getChildNodes(NodeType.TABLE, true); for (int i = 0; i < tables.getCount(); i++) { Table table = (Table) tables.get(i); // Find out if any cells in the table have other tables as children. int count = getChildTableCount(table); System.out.print(MessageFormat.format("Table #{0} has {1} tables directly within its cells", i, count)); // Find out if the table is nested inside another table, and, if so, 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}", i, tableDepth)); else System.out.println(MessageFormat.format("Table #{0} is a non nested table (is not a child of another table)", i)); } } /** * 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; Node parent = table.getAncestor(table.getNodeType()); while (parent != null) { 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 childTableCount = 0; for (Row row : table.getRows()) { for (Cell cell : row.getCells()) { TableCollection childTables = cell.getTables(); if (childTables.getCount() > 0) childTableCount++; } } return childTableCount; }
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 traverse through a composite node's collection of child nodes.Document doc = new Document(); // Add two runs and one shape as child nodes to the first paragraph of this document. Paragraph paragraph = (Paragraph) doc.getChild(NodeType.PARAGRAPH, 0, true); paragraph.appendChild(new Run(doc, "Hello world! ")); Shape shape = new Shape(doc, ShapeType.RECTANGLE); shape.setWidth(200.0); shape.setHeight(200.0); // Note that the 'CustomNodeId' is not saved to an output file and exists only during the node lifetime. shape.setCustomNodeId(100); shape.setWrapType(WrapType.INLINE); paragraph.appendChild(shape); paragraph.appendChild(new Run(doc, "Hello again!")); // Iterate through the paragraph's collection of immediate children, // and print any runs or shapes that we find within. NodeCollection children = paragraph.getChildNodes(); Assert.assertEquals(3, paragraph.getChildNodes().getCount()); for (Node child : (Iterable<Node>) children) switch (child.getNodeType()) { case NodeType.RUN: System.out.println("Run contents:"); System.out.println("\t\"{child.GetText().Trim()}\""); break; case NodeType.SHAPE: Shape childShape = (Shape) child; System.out.println("Shape:"); System.out.println("\t{childShape.ShapeType}, {childShape.Width}x{childShape.Height}"); }
Example:
Shows how to apply the properties of a table's style directly to the table's elements.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Table table = builder.startTable(); builder.insertCell(); builder.write("Hello world!"); builder.endTable(); TableStyle tableStyle = (TableStyle) doc.getStyles().add(StyleType.TABLE, "MyTableStyle1"); tableStyle.setRowStripe(3); tableStyle.setCellSpacing(5.0); tableStyle.getShading().setBackgroundPatternColor(Color.WHITE); tableStyle.getBorders().setColor(Color.BLUE); tableStyle.getBorders().setLineStyle(LineStyle.DOT_DASH); table.setStyle(tableStyle); // This method concerns table style properties such as the ones we set above. doc.expandTableStylesToDirectFormatting(); doc.save(getArtifactsDir() + "Document.TableStyleToDirectFormatting.docx");
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 add, update and delete child nodes in a CompositeNode's collection of children.Document doc = new Document(); // An empty document, by default, has one paragraph. Assert.assertEquals(1, doc.getFirstSection().getBody().getParagraphs().getCount()); // Composite nodes such as our paragraph can contain other composite and inline nodes as children. Paragraph paragraph = doc.getFirstSection().getBody().getFirstParagraph(); Run paragraphText = new Run(doc, "Initial text. "); paragraph.appendChild(paragraphText); // Create three more run nodes. Run run1 = new Run(doc, "Run 1. "); Run run2 = new Run(doc, "Run 2. "); Run run3 = new Run(doc, "Run 3. "); // The document body will not display these runs until we insert them into a composite node // that itself is a part of the document's node tree, as we did with the first run. // We can determine where the text contents of nodes that we insert // appears in the document by specifying an insertion location relative to another node in the paragraph. Assert.assertEquals("Initial text.", paragraph.getText().trim()); // Insert the second run into the paragraph in front of the initial run. paragraph.insertBefore(run2, paragraphText); Assert.assertEquals("Run 2. Initial text.", paragraph.getText().trim()); // Insert the third run after the initial run. paragraph.insertAfter(run3, paragraphText); Assert.assertEquals("Run 2. Initial text. Run 3.", paragraph.getText().trim()); // Insert the first run to the start of the paragraph's child nodes collection. paragraph.prependChild(run1); Assert.assertEquals("Run 1. Run 2. Initial text. Run 3.", paragraph.getText().trim()); Assert.assertEquals(4, paragraph.getChildNodes(NodeType.ANY, true).getCount()); // We can modify the contents of the run by editing and deleting existing child nodes. ((Run) paragraph.getChildNodes(NodeType.RUN, true).get(1)).setText("Updated run 2. "); paragraph.getChildNodes(NodeType.RUN, true).remove(paragraphText); Assert.assertEquals("Run 1. Updated run 2. Run 3.", paragraph.getText().trim()); Assert.assertEquals(3, paragraph.getChildNodes(NodeType.ANY, true).getCount());
Example:
Shows how to extract images from a document, and save them to the local file system as individual files.Document doc = new Document(getMyDir() + "Images.docx"); // Get the collection of shapes from the document, // and save the image data of every shape with an image as a file to the local file system. NodeCollection shapes = doc.getChildNodes(NodeType.SHAPE, true); int imageIndex = 0; for (Shape shape : (Iterable<Shape>) shapes) { if (shape.hasImage()) { // The image data of shapes may contain images of many possible image formats. // We can determine a file extension for each image automatically, based on its format. String imageFileName = MessageFormat.format("File.ExtractImages.{0}{1}", imageIndex, FileFormatUtil.imageTypeToExtension(shape.getImageData().getImageType())); shape.getImageData().save(getArtifactsDir() + imageFileName); imageIndex++; } }
Example:
Shows how to print all of a document's comments and their replies.Document doc = new Document(getMyDir() + "Comments.docx"); NodeCollection comments = doc.getChildNodes(NodeType.COMMENT, true); // If a comment has no ancestor, it is a "top-level" comment as opposed to a reply-type comment. // Print all top-level comments along with any replies they may have. for (Comment comment : (Iterable<Comment>) comments) { if (comment.getAncestor() == null) { System.out.println("Top-level comment:"); System.out.println("\t\"{comment.GetText().Trim()}\", by {comment.Author}"); System.out.println("Has {comment.Replies.Count} replies"); for (Comment commentReply : comment.getReplies()) { System.out.println("\t\"{commentReply.GetText().Trim()}\", by {commentReply.Author}"); } System.out.println(); } }
public PageInfo getPageInfo(int pageIndex) throws java.lang.Exception
pageIndex
- The 0-based page index.public java.lang.String getText()
The returned string includes all control and special characters as described in
Example:
Shows how to output all paragraphs in a document that are list items.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.getListFormat().applyNumberDefault(); builder.writeln("Numbered list item 1"); builder.writeln("Numbered list item 2"); builder.writeln("Numbered list item 3"); builder.getListFormat().removeNumbers(); builder.getListFormat().applyBulletDefault(); builder.writeln("Bulleted list item 1"); builder.writeln("Bulleted list item 2"); builder.writeln("Bulleted list item 3"); builder.getListFormat().removeNumbers(); 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()); } }
Example:
Shows the difference between calling the GetText and ToString methods on a node.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.insertField("MERGEFIELD Field"); // GetText will retrieve the visible text as well as field codes and special characters. Assert.assertEquals("\u0013MERGEFIELD Field\u0014«Field»\u0015\f", doc.getText()); // ToString will give us the document's appearance if saved to a passed save format. Assert.assertEquals("«Field»\r\n", doc.toString(SaveFormat.TEXT));
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 a node from one document to another.Document srcDoc = new Document(); Document dstDoc = new Document(); srcDoc.getFirstSection().getBody().getFirstParagraph().appendChild( new Run(srcDoc, "Source document first paragraph text.")); dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild( new Run(dstDoc, "Destination document first paragraph text.")); // Every node has a parent document, which is the document that contains the node. // Inserting a node into a document that the node does not belong to will throw an exception. Assert.assertNotEquals(dstDoc, srcDoc.getFirstSection().getDocument()); Assert.assertThrows(IllegalArgumentException.class, () -> dstDoc.appendChild(srcDoc.getFirstSection())); // Use the ImportNode method to create a copy of a node, which will have the document // that called the ImportNode method set as its new owner document. Section importedSection = (Section) dstDoc.importNode(srcDoc.getFirstSection(), true); Assert.assertEquals(dstDoc, importedSection.getDocument()); // We can now insert the node into the document. dstDoc.appendChild(importedSection); Assert.assertEquals("Destination document first paragraph text.\r\nSource document first paragraph text.\r\n", dstDoc.toString(SaveFormat.TEXT));
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 and add a character style to each document. // Configure the styles to have the same name, but different text formatting. Document srcDoc = new Document(); Style srcStyle = srcDoc.getStyles().add(StyleType.CHARACTER, "My style"); srcStyle.getFont().setName("Courier New"); DocumentBuilder srcBuilder = new DocumentBuilder(srcDoc); srcBuilder.getFont().setStyle(srcStyle); srcBuilder.writeln("Source document text."); Document dstDoc = new Document(); Style dstStyle = dstDoc.getStyles().add(StyleType.CHARACTER, "My style"); dstStyle.getFont().setName("Calibri"); DocumentBuilder dstBuilder = new DocumentBuilder(dstDoc); dstBuilder.getFont().setStyle(dstStyle); dstBuilder.writeln("Destination document text."); // Import the Section from the destination document into the source document, causing a style name collision. // If we use destination styles, then the imported source text with the same style name // as destination text will adopt the destination style. Section importedSection = (Section) dstDoc.importNode(srcDoc.getFirstSection(), true, ImportFormatMode.USE_DESTINATION_STYLES); Assert.assertEquals(dstStyle.getFont().getName(), importedSection.getBody().getFirstParagraph().getRuns().get(0).getFont().getName()); Assert.assertEquals(dstStyle.getName(), importedSection.getBody().getFirstParagraph().getRuns().get(0).getFont().getStyleName()); // If we use ImportFormatMode.KeepDifferentStyles, the source style is preserved, // and the naming clash resolves by adding a suffix. dstDoc.importNode(srcDoc.getFirstSection(), true, ImportFormatMode.KEEP_DIFFERENT_STYLES); Assert.assertEquals(dstStyle.getFont().getName(), dstDoc.getStyles().get("My style").getFont().getName()); Assert.assertEquals(srcStyle.getFont().getName(), dstDoc.getStyles().get("My style_0").getFont().getName());
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.docx"); Body body = doc.getFirstSection().getBody(); // Retrieve the index of the last paragraph in the body of the first section. Assert.assertEquals(24, body.getChildNodes().indexOf(body.getLastParagraph()));
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 in a CompositeNode's collection of children.Document doc = new Document(); // An empty document, by default, has one paragraph. Assert.assertEquals(1, doc.getFirstSection().getBody().getParagraphs().getCount()); // Composite nodes such as our paragraph can contain other composite and inline nodes as children. Paragraph paragraph = doc.getFirstSection().getBody().getFirstParagraph(); Run paragraphText = new Run(doc, "Initial text. "); paragraph.appendChild(paragraphText); // Create three more run nodes. Run run1 = new Run(doc, "Run 1. "); Run run2 = new Run(doc, "Run 2. "); Run run3 = new Run(doc, "Run 3. "); // The document body will not display these runs until we insert them into a composite node // that itself is a part of the document's node tree, as we did with the first run. // We can determine where the text contents of nodes that we insert // appears in the document by specifying an insertion location relative to another node in the paragraph. Assert.assertEquals("Initial text.", paragraph.getText().trim()); // Insert the second run into the paragraph in front of the initial run. paragraph.insertBefore(run2, paragraphText); Assert.assertEquals("Run 2. Initial text.", paragraph.getText().trim()); // Insert the third run after the initial run. paragraph.insertAfter(run3, paragraphText); Assert.assertEquals("Run 2. Initial text. Run 3.", paragraph.getText().trim()); // Insert the first run to the start of the paragraph's child nodes collection. paragraph.prependChild(run1); Assert.assertEquals("Run 1. Run 2. Initial text. Run 3.", paragraph.getText().trim()); Assert.assertEquals(4, paragraph.getChildNodes(NodeType.ANY, true).getCount()); // We can modify the contents of the run by editing and deleting existing child nodes. ((Run) paragraph.getChildNodes(NodeType.RUN, true).get(1)).setText("Updated run 2. "); paragraph.getChildNodes(NodeType.RUN, true).remove(paragraphText); Assert.assertEquals("Run 1. Updated run 2. Run 3.", paragraph.getText().trim()); Assert.assertEquals(3, paragraph.getChildNodes(NodeType.ANY, true).getCount());
Example:
Shows how to replace all textbox shapes with image shapes.Document doc = new Document(getMyDir() + "Textboxes in drawing canvas.docx"); List<Shape> shapeList = Arrays.stream(doc.getChildNodes(NodeType.SHAPE, true).toArray()) .filter(Shape.class::isInstance) .map(Shape.class::cast) .collect(Collectors.toList()); Assert.assertEquals(3, IterableUtils.countMatches(shapeList, s -> s.getShapeType() == ShapeType.TEXT_BOX)); Assert.assertEquals(1, IterableUtils.countMatches(shapeList, s -> s.getShapeType() == ShapeType.IMAGE)); for (Shape shape : shapeList) { if (((shape.getShapeType()) == (ShapeType.TEXT_BOX))) { Shape replacementShape = new Shape(doc, ShapeType.IMAGE); replacementShape.getImageData().setImage(getImageDir() + "Logo.jpg"); replacementShape.setLeft(shape.getLeft()); replacementShape.setTop(shape.getTop()); replacementShape.setWidth(shape.getWidth()); replacementShape.setHeight(shape.getHeight()); replacementShape.setRelativeHorizontalPosition(shape.getRelativeHorizontalPosition()); replacementShape.setRelativeVerticalPosition(shape.getRelativeVerticalPosition()); replacementShape.setHorizontalAlignment(shape.getHorizontalAlignment()); replacementShape.setVerticalAlignment(shape.getVerticalAlignment()); replacementShape.setWrapType(shape.getWrapType()); replacementShape.setWrapSide(shape.getWrapSide()); shape.getParentNode().insertAfter(replacementShape, shape); shape.remove(); } } shapeList = Arrays.stream(doc.getChildNodes(NodeType.SHAPE, true).toArray()) .filter(Shape.class::isInstance) .map(Shape.class::cast) .collect(Collectors.toList()); Assert.assertEquals(0, IterableUtils.countMatches(shapeList, s -> s.getShapeType() == ShapeType.TEXT_BOX)); Assert.assertEquals(4, IterableUtils.countMatches(shapeList, s -> s.getShapeType() == ShapeType.IMAGE)); doc.save(getArtifactsDir() + "Shape.ReplaceTextboxesWithImages.docx");
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 in a CompositeNode's collection of children.Document doc = new Document(); // An empty document, by default, has one paragraph. Assert.assertEquals(1, doc.getFirstSection().getBody().getParagraphs().getCount()); // Composite nodes such as our paragraph can contain other composite and inline nodes as children. Paragraph paragraph = doc.getFirstSection().getBody().getFirstParagraph(); Run paragraphText = new Run(doc, "Initial text. "); paragraph.appendChild(paragraphText); // Create three more run nodes. Run run1 = new Run(doc, "Run 1. "); Run run2 = new Run(doc, "Run 2. "); Run run3 = new Run(doc, "Run 3. "); // The document body will not display these runs until we insert them into a composite node // that itself is a part of the document's node tree, as we did with the first run. // We can determine where the text contents of nodes that we insert // appears in the document by specifying an insertion location relative to another node in the paragraph. Assert.assertEquals("Initial text.", paragraph.getText().trim()); // Insert the second run into the paragraph in front of the initial run. paragraph.insertBefore(run2, paragraphText); Assert.assertEquals("Run 2. Initial text.", paragraph.getText().trim()); // Insert the third run after the initial run. paragraph.insertAfter(run3, paragraphText); Assert.assertEquals("Run 2. Initial text. Run 3.", paragraph.getText().trim()); // Insert the first run to the start of the paragraph's child nodes collection. paragraph.prependChild(run1); Assert.assertEquals("Run 1. Run 2. Initial text. Run 3.", paragraph.getText().trim()); Assert.assertEquals(4, paragraph.getChildNodes(NodeType.ANY, true).getCount()); // We can modify the contents of the run by editing and deleting existing child nodes. ((Run) paragraph.getChildNodes(NodeType.RUN, true).get(1)).setText("Updated run 2. "); paragraph.getChildNodes(NodeType.RUN, true).remove(paragraphText); Assert.assertEquals("Run 1. Updated run 2. Run 3.", paragraph.getText().trim()); Assert.assertEquals(3, paragraph.getChildNodes(NodeType.ANY, true).getCount());
public java.util.Iterator<Node> iterator()
Example:
Shows how to traverse through a composite node's collection of child nodes.Document doc = new Document(); // Add two runs and one shape as child nodes to the first paragraph of this document. Paragraph paragraph = (Paragraph) doc.getChild(NodeType.PARAGRAPH, 0, true); paragraph.appendChild(new Run(doc, "Hello world! ")); Shape shape = new Shape(doc, ShapeType.RECTANGLE); shape.setWidth(200.0); shape.setHeight(200.0); // Note that the 'CustomNodeId' is not saved to an output file and exists only during the node lifetime. shape.setCustomNodeId(100); shape.setWrapType(WrapType.INLINE); paragraph.appendChild(shape); paragraph.appendChild(new Run(doc, "Hello again!")); // Iterate through the paragraph's collection of immediate children, // and print any runs or shapes that we find within. NodeCollection children = paragraph.getChildNodes(); Assert.assertEquals(3, paragraph.getChildNodes().getCount()); for (Node child : (Iterable<Node>) children) switch (child.getNodeType()) { case NodeType.RUN: System.out.println("Run contents:"); System.out.println("\t\"{child.GetText().Trim()}\""); break; case NodeType.SHAPE: Shape childShape = (Shape) child; System.out.println("Shape:"); System.out.println("\t{childShape.ShapeType}, {childShape.Width}x{childShape.Height}"); }
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.// Open a document that contains adjacent runs of text with identical formatting, // which commonly occurs if we edit the same paragraph multiple times in Microsoft Word. Document doc = new Document(getMyDir() + "Rendering.docx"); // If any number of these runs are adjacent with identical formatting, // then the document may be simplified. Assert.assertEquals(317, doc.getChildNodes(NodeType.RUN, true).getCount()); // Combine such runs with this method and verify the number of run joins that will take place. Assert.assertEquals(121, doc.joinRunsWithSameFormatting()); // The number of joins and the number of runs we have after the join // should add up the number of runs we had initially. Assert.assertEquals(196, doc.getChildNodes(NodeType.RUN, true).getCount());
public Node nextPreOrder(Node rootNode)
rootNode
- The top node (limit) of traversal.Example:
Shows how to traverse the document's node tree using the pre-order traversal algorithm, and delete any encountered shape with an image.Document doc = new Document(getMyDir() + "Images.docx"); ArrayList<Shape> shapes = (ArrayList<Shape>) IterableUtils.toList(doc.getChildNodes(NodeType.SHAPE, true)); Assert.assertEquals(9, IterableUtils.countMatches(shapes, s -> { try { return s.hasImage(); } catch (Exception e) { e.printStackTrace(); } return false; })); 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) curNode).hasImage()) curNode.remove(); curNode = nextNode; } shapes = (ArrayList<Shape>) IterableUtils.toList(doc.getChildNodes(NodeType.SHAPE, true)); Assert.assertEquals(0, IterableUtils.countMatches(shapes, s -> { try { return s.hasImage(); } catch (Exception e) { e.printStackTrace(); } return false; }));
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); Field field = builder.insertField("DATE", null); // Aspose.Words automatically detects field types based on field codes. Assert.assertEquals(FieldType.FIELD_DATE, field.getType()); // Manually change the raw text of the field, which determines the field code. Run fieldText = (Run) doc.getFirstSection().getBody().getFirstParagraph().getChildNodes(NodeType.RUN, true).get(0); // Changing the field code has changed this field to one of a different type, // but the field's type properties still display the old type. Assert.assertEquals("PAGE", field.getFieldCode()); Assert.assertEquals(FieldType.FIELD_DATE, field.getType()); Assert.assertEquals(FieldType.FIELD_DATE, field.getStart().getFieldType()); Assert.assertEquals(FieldType.FIELD_DATE, field.getSeparator().getFieldType()); Assert.assertEquals(FieldType.FIELD_DATE, field.getEnd().getFieldType()); // Update those properties with this method to display current value. doc.normalizeFieldTypes(); Assert.assertEquals(FieldType.FIELD_PAGE, field.getType()); Assert.assertEquals(FieldType.FIELD_PAGE, field.getStart().getFieldType()); Assert.assertEquals(FieldType.FIELD_PAGE, field.getSeparator().getFieldType()); Assert.assertEquals(FieldType.FIELD_PAGE, field.getEnd().getFieldType());
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 in a CompositeNode's collection of children.Document doc = new Document(); // An empty document, by default, has one paragraph. Assert.assertEquals(1, doc.getFirstSection().getBody().getParagraphs().getCount()); // Composite nodes such as our paragraph can contain other composite and inline nodes as children. Paragraph paragraph = doc.getFirstSection().getBody().getFirstParagraph(); Run paragraphText = new Run(doc, "Initial text. "); paragraph.appendChild(paragraphText); // Create three more run nodes. Run run1 = new Run(doc, "Run 1. "); Run run2 = new Run(doc, "Run 2. "); Run run3 = new Run(doc, "Run 3. "); // The document body will not display these runs until we insert them into a composite node // that itself is a part of the document's node tree, as we did with the first run. // We can determine where the text contents of nodes that we insert // appears in the document by specifying an insertion location relative to another node in the paragraph. Assert.assertEquals("Initial text.", paragraph.getText().trim()); // Insert the second run into the paragraph in front of the initial run. paragraph.insertBefore(run2, paragraphText); Assert.assertEquals("Run 2. Initial text.", paragraph.getText().trim()); // Insert the third run after the initial run. paragraph.insertAfter(run3, paragraphText); Assert.assertEquals("Run 2. Initial text. Run 3.", paragraph.getText().trim()); // Insert the first run to the start of the paragraph's child nodes collection. paragraph.prependChild(run1); Assert.assertEquals("Run 1. Run 2. Initial text. Run 3.", paragraph.getText().trim()); Assert.assertEquals(4, paragraph.getChildNodes(NodeType.ANY, true).getCount()); // We can modify the contents of the run by editing and deleting existing child nodes. ((Run) paragraph.getChildNodes(NodeType.RUN, true).get(1)).setText("Updated run 2. "); paragraph.getChildNodes(NodeType.RUN, true).remove(paragraphText); Assert.assertEquals("Run 1. Updated run 2. Run 3.", paragraph.getText().trim()); Assert.assertEquals(3, paragraph.getChildNodes(NodeType.ANY, true).getCount());
public Node previousPreOrder(Node rootNode)
rootNode
- The top node (limit) of traversal.Example:
Shows how to traverse the document's node tree using the pre-order traversal algorithm, and delete any encountered shape with an image.Document doc = new Document(getMyDir() + "Images.docx"); ArrayList<Shape> shapes = (ArrayList<Shape>) IterableUtils.toList(doc.getChildNodes(NodeType.SHAPE, true)); Assert.assertEquals(9, IterableUtils.countMatches(shapes, s -> { try { return s.hasImage(); } catch (Exception e) { e.printStackTrace(); } return false; })); 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) curNode).hasImage()) curNode.remove(); curNode = nextNode; } shapes = (ArrayList<Shape>) IterableUtils.toList(doc.getChildNodes(NodeType.SHAPE, true)); Assert.assertEquals(0, IterableUtils.countMatches(shapes, s -> { try { return s.hasImage(); } catch (Exception e) { e.printStackTrace(); } return false; }));
public void print()
public void print(java.lang.String printerName)
printerName
- The name of the printer.public void print(javax.print.attribute.AttributeSet printerSettings)
The
The
printerSettings
- The printer settings to use.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.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:
Shows how to turn off protection for a section.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.writeln("Section 1. Hello world!"); builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE); builder.writeln("Section 2. Hello again!"); builder.write("Please enter text here: "); builder.insertTextInput("TextInput1", TextFormFieldType.REGULAR, "", "Placeholder text", 0); // Apply write protection to every section in the document. doc.protect(ProtectionType.ALLOW_ONLY_FORM_FIELDS); // Turn off write protection for the first section. doc.getSections().get(0).setProtectedForForms(false); // In this output document, we will be able to edit the first section freely, // and we will only be able to edit the contents of the form field in the second section. doc.save(getArtifactsDir() + "Section.Protect.docx");
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 and unprotect a document.Document doc = new Document(); doc.protect(ProtectionType.READ_ONLY, "password"); Assert.assertEquals(ProtectionType.READ_ONLY, doc.getProtectionType()); // If we open this document with Microsoft Word intending to edit it, // we will need to apply the password to get through the protection. doc.save(getArtifactsDir() + "Document.Protect.docx"); // Note that the protection only applies to Microsoft Word users opening our document. // We have not encrypted the document in any way, and we do not need the password to open and edit it programmatically. Document protectedDoc = new Document(getArtifactsDir() + "Document.Protect.docx"); Assert.assertEquals(ProtectionType.READ_ONLY, protectedDoc.getProtectionType()); DocumentBuilder builder = new DocumentBuilder(protectedDoc); builder.writeln("Text added to a protected document."); // There are two ways of removing protection from a document. // 1 - With no password: doc.unprotect(); Assert.assertEquals(ProtectionType.NO_PROTECTION, doc.getProtectionType()); doc.protect(ProtectionType.READ_ONLY, "NewPassword"); Assert.assertEquals(ProtectionType.READ_ONLY, doc.getProtectionType()); doc.unprotect("WrongPassword"); Assert.assertEquals(ProtectionType.READ_ONLY, doc.getProtectionType()); // 2 - With the correct password: doc.unprotect("NewPassword"); Assert.assertEquals(ProtectionType.NO_PROTECTION, doc.getProtectionType());
@Deprecated public void remove()
public void removeAllChildren()
Example:
Shows how to construct an Aspose.Words document by hand.Document doc = new Document(); // A blank document contains one section, one body and one paragraph. // Call the "RemoveAllChildren" method to remove all those nodes, // and end up with a document node with no children. doc.removeAllChildren(); // This document now has no composite child nodes that we can add content to. // If we wish to edit it, we will need to repopulate its node collection. // First, create a new section, and then append it as a child to the root document node. Section section = new Section(doc); doc.appendChild(section); // Set some page setup properties for the section. section.getPageSetup().setSectionStart(SectionStart.NEW_PAGE); section.getPageSetup().setPaperSize(PaperSize.LETTER); // A section needs a body, which will contain and display all its contents // on the page between the section's header and footer. Body body = new Body(doc); section.appendChild(body); // Create a paragraph, set some formatting properties, and then append it as a child to the body. Paragraph para = new Paragraph(doc); para.getParagraphFormat().setStyleName("Heading 1"); para.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER); body.appendChild(para); // Finally, add some content to do the document. Create a run, // set its appearance and contents, and then append it as a child to the paragraph. Run run = new Run(doc); run.setText("Hello World!"); run.getFont().setColor(Color.RED); para.appendChild(run); Assert.assertEquals("Hello World!", doc.getText().trim()); doc.save(getArtifactsDir() + "Section.CreateManually.docx");
public Node removeChild(Node oldChild)
The parent of oldChild is set to null after the node is removed.
oldChild
- The node to remove.Example:
Shows how to use of methods of Node and CompositeNode to remove a section before the last section in the document.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.writeln("Section 1 text."); builder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS); builder.writeln("Section 2 text."); // Both sections are siblings of each other. Section lastSection = (Section) doc.getLastChild(); Section firstSection = (Section) lastSection.getPreviousSibling(); // Remove a section based on its sibling relationship with another section. if (lastSection.getPreviousSibling() != null) doc.removeChild(firstSection); // The section we removed was the first one, leaving the document with only the second. Assert.assertEquals("Section 2 text.", doc.getText().trim());
public void removeExternalSchemaReferences()
Example:
Shows how to remove all external XML schema references from a document.Document doc = new Document(getMyDir() + "External XML schema.docx"); doc.removeExternalSchemaReferences();
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() + "Macro.docm"); Assert.assertTrue(doc.hasMacros()); Assert.assertEquals("Project", doc.getVbaProject().getName()); // Remove the document's VBA project, along with all its macros. doc.removeMacros(); Assert.assertFalse(doc.hasMacros()); Assert.assertNull(doc.getVbaProject());
public void removeSmartTags()
Example:
Shows how to create smart tags.Document doc = new Document(); // A smart tag appears in a document with Microsoft Word recognizes a part of its text as some form of data, // such as a name, date, or address, and converts it to a hyperlink that displays a purple dotted underline. SmartTag smartTag = new SmartTag(doc); // Smart tags are composite nodes that contain their recognized text in its entirety. // Add contents to this smart tag manually. smartTag.appendChild(new Run(doc, "May 29, 2019")); // Microsoft Word may recognize the above contents as being a date. // Smart tags use the "Element" property to reflect the type of data they contain. smartTag.setElement("date"); // Some smart tag types process their contents further into custom XML properties. smartTag.getProperties().add(new CustomXmlProperty("Day", "", "29")); smartTag.getProperties().add(new CustomXmlProperty("Month", "", "5")); smartTag.getProperties().add(new CustomXmlProperty("Year", "", "2019")); // Set the smart tag's URI to the default value. smartTag.setUri("urn:schemas-microsoft-com:office:smarttags"); doc.getFirstSection().getBody().getFirstParagraph().appendChild(smartTag); doc.getFirstSection().getBody().getFirstParagraph().appendChild(new Run(doc, " is a date. ")); // Create another smart tag for a stock ticker. smartTag = new SmartTag(doc); smartTag.setElement("stockticker"); smartTag.setUri("urn:schemas-microsoft-com:office:smarttags"); smartTag.appendChild(new Run(doc, "MSFT")); doc.getFirstSection().getBody().getFirstParagraph().appendChild(smartTag); doc.getFirstSection().getBody().getFirstParagraph().appendChild(new Run(doc, " is a stock ticker.")); // Print all the smart tags in our document using a document visitor. doc.accept(new SmartTagPrinter()); // Older versions of Microsoft Word support smart tags. doc.save(getArtifactsDir() + "SmartTag.Create.doc"); // Use the "RemoveSmartTags" method to remove all smart tags from a document. Assert.assertEquals(2, doc.getChildNodes(NodeType.SMART_TAG, true).getCount()); doc.removeSmartTags(); Assert.assertEquals(0, doc.getChildNodes(NodeType.SMART_TAG, true).getCount()); /// <summary> /// Prints visited smart tags and their contents. /// </summary> private static class SmartTagPrinter extends DocumentVisitor { /// <summary> /// Called when a SmartTag node is encountered in the document. /// </summary> public /*override*/ /*VisitorAction*/int visitSmartTagStart(SmartTag smartTag) { System.out.println("Smart tag type: {smartTag.Element}"); return VisitorAction.CONTINUE; } /// <summary> /// Called when the visiting of a SmartTag node is ended. /// </summary> public /*override*/ /*VisitorAction*/int visitSmartTagEnd(SmartTag smartTag) { System.out.println("\tContents: \"{smartTag.ToString(SaveFormat.Text)}\""); if (smartTag.getProperties().getCount() == 0) { System.out.println("\tContains no properties"); } else { System.out.println("\tProperties: "); String[] properties = new String[smartTag.getProperties().getCount()]; int index = 0; for (CustomXmlProperty cxp : smartTag.getProperties()) properties[index++] = MessageFormat.format("\"{0}\" = \"{1}\"", cxp.getName(), cxp.getValue()); System.out.println(StringUtils.join(properties, ", ")); } return VisitorAction.CONTINUE; } }
Example:
Removes all smart tags from descendant nodes of a composite node.Document doc = new Document(getMyDir() + "Smart tags.doc"); Assert.assertEquals(8, doc.getChildNodes(NodeType.SMART_TAG, true).getCount()); doc.removeSmartTags(); Assert.assertEquals(0, doc.getChildNodes(NodeType.SMART_TAG, true).getCount());
public java.awt.Dimension 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:
Shows how to the individual pages of a document to graphics to create one image with thumbnails of all pages.Document doc = new Document(getMyDir() + "Rendering.docx"); // Calculate the number of rows and columns that we will fill with thumbnails. final int thumbColumns = 2; int thumbRows = doc.getPageCount() / thumbColumns; int remainder = doc.getPageCount() % thumbColumns; if (remainder > 0) thumbRows++; // Scale the thumbnails relative to the size of the first page. float scale = 0.25f; 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); Graphics2D gr = img.createGraphics(); try { gr.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); gr.setColor(Color.white); // Fill the background, which is transparent by default, in white. 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); // Render a page as a thumbnail, and then frame it in a rectangle of the same size. 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(); } }
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:
Shows how to render a document to a bitmap at a specified location and size.Document doc = new Document(getMyDir() + "Rendering.docx"); 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); // Offset the output 0.5" from the edge. gr.translate(ConvertUtil.inchToPoint(0.5f), ConvertUtil.inchToPoint(0.5f)); // Rotate the output by 10 degrees. gr.rotate(10.0 * Math.PI / 180.0, img.getWidth() / 2.0, img.getHeight() / 2.0); gr.setColor(Color.RED); // Draw a 3"x3" rectangle. gr.drawRect(0, 0, (int) ConvertUtil.inchToPoint(3), (int) ConvertUtil.inchToPoint(3)); // Draw the first page of our document with the same dimensions and transformation as the rectangle. // The rectangle will frame the first page. float returnedScale = doc.renderToSize(0, gr, 0f, 0f, (float) ConvertUtil.inchToPoint(3), (float) ConvertUtil.inchToPoint(3)); ImageIO.write(img, "PNG", new File(getArtifactsDir() + "Rendering.RenderToSize.png")); } finally { if (gr != null) { gr.dispose(); } }
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.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 a stream.Document doc = new Document(getMyDir() + "Document.docx"); ByteArrayOutputStream dstStream = new ByteArrayOutputStream(); try { doc.save(dstStream, SaveFormat.DOCX); byte[] dataBytes = dstStream.toByteArray(); ByteArrayInputStream byteStream = new ByteArrayInputStream(dataBytes); // Verify that the stream contains the document. Assert.assertEquals("Hello World!", new Document(byteStream).getFirstSection().getBody().getText().trim()); } finally { if (dstStream != null) dstStream.close(); }
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 open a document and convert it to .PDF.Document doc = new Document(getMyDir() + "Document.docx"); doc.save(getArtifactsDir() + "Document.ConvertToPdf.pdf");
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 configure compression while saving a document as a JPEG.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.insertImage(getImageDir() + "Logo.jpg"); // Create an "ImageSaveOptions" object which we can pass to the document's "Save" method // to modify the way in which that method renders the document into an image. ImageSaveOptions imageOptions = new ImageSaveOptions(SaveFormat.JPEG); // Set the "JpegQuality" property to "10" to use stronger compression when rendering the document. // This will reduce the file size of the document, but the image will display more prominent compression artifacts. imageOptions.setJpegQuality(10); doc.save(getArtifactsDir() + "ImageSaveOptions.JpegQuality.HighCompression.jpg", imageOptions); Assert.assertTrue(new File(getArtifactsDir() + "ImageSaveOptions.JpegQuality.HighCompression.jpg").length() <= 20000); // Set the "JpegQuality" property to "100" to use weaker compression when rending the document. // This will improve the quality of the image at the cost of an increased file size. imageOptions.setJpegQuality(100); doc.save(getArtifactsDir() + "ImageSaveOptions.JpegQuality.HighQuality.jpg", imageOptions); Assert.assertTrue(new File(getArtifactsDir() + "ImageSaveOptions.JpegQuality.HighQuality.jpg").length() < 60000);
Example:
Shows how to render one page from a document to a JPEG image.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.writeln("Page 1."); builder.insertBreak(BreakType.PAGE_BREAK); builder.writeln("Page 2."); builder.insertImage(getImageDir() + "Logo.jpg"); builder.insertBreak(BreakType.PAGE_BREAK); builder.writeln("Page 3."); // Create an "ImageSaveOptions" object which we can pass to the document's "Save" method // to modify the way in which that method renders the document into an image. ImageSaveOptions options = new ImageSaveOptions(SaveFormat.JPEG); // Set the "PageSet" to "1" to select the second page via // the zero-based index to start rendering the document from. options.setPageSet(new PageSet(1)); // When we save the document to the JPEG format, Aspose.Words only renders one page. // This image will contain one page starting from page two, // which will just be the second page of the original document. doc.save(getArtifactsDir() + "ImageSaveOptions.OnePage.jpg", options);
Example:
Shows how to render every page of a document to a separate TIFF image.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.writeln("Page 1."); builder.insertBreak(BreakType.PAGE_BREAK); builder.writeln("Page 2."); builder.insertImage(getImageDir() + "Logo.jpg"); builder.insertBreak(BreakType.PAGE_BREAK); builder.writeln("Page 3."); // Create an "ImageSaveOptions" object which we can pass to the document's "Save" method // to modify the way in which that method renders the document into an image. ImageSaveOptions options = new ImageSaveOptions(SaveFormat.TIFF); for (int i = 0; i < doc.getPageCount(); i++) { // Set the "PageSet" property to the number of the first page from // which to start rendering the document from. options.setPageSet(new PageSet(i)); doc.save(getArtifactsDir() + MessageFormat.format("ImageSaveOptions.PageByPage.{0}.tiff", i + 1), options); }
Example:
Shows how to improve the quality of a rendered document with SaveOptions.Document doc = new Document(getMyDir() + "Rendering.docx"); DocumentBuilder builder = new DocumentBuilder(doc); builder.getFont().setSize(60.0); builder.writeln("Some text."); SaveOptions options = new ImageSaveOptions(SaveFormat.JPEG); doc.save(getArtifactsDir() + "Document.ImageSaveOptions.Default.jpg", options); options.setUseAntiAliasing(true); options.setUseHighQualityRendering(true); doc.save(getArtifactsDir() + "Document.ImageSaveOptions.HighQuality.jpg", options);
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:
Shows how to convert from DOCX to HTML format.Document doc = new Document(getMyDir() + "Document.docx"); doc.save(getArtifactsDir() + "Document.ConvertToHtml.html", SaveFormat.HTML);
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() + "Tables.docx"); // This expression will extract all paragraph nodes, // which are descendants of any table node in the document. 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. 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"); Assert.assertEquals(Paragraph.class, node.getClass());
Example:
Shows how to use an XPath expression to test whether a node is inside a field.Document doc = new Document(getMyDir() + "Mail merge destination - Northwind employees.docx"); // The NodeList that results from this XPath expression will contain all nodes we find inside a field. // However, FieldStart and FieldEnd nodes can be on 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. System.out.println("Contents of the first Run node that's part of a field: {resultList.First(n => n.NodeType == NodeType.Run).GetText().Trim()}");
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() + "Tables.docx"); // This expression will extract all paragraph nodes, // which are descendants of any table node in the document. 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. 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"); Assert.assertEquals(Paragraph.class, node.getClass());
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 to track revisions while editing a document.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Editing a document usually does not count as a revision until we begin tracking them. builder.write("Hello world! "); Assert.assertEquals(0, doc.getRevisions().getCount()); Assert.assertFalse(doc.getFirstSection().getBody().getParagraphs().get(0).getRuns().get(0).isInsertRevision()); doc.startTrackRevisions("John Doe"); builder.write("Hello again! "); Assert.assertEquals(1, doc.getRevisions().getCount()); Assert.assertTrue(doc.getFirstSection().getBody().getParagraphs().get(0).getRuns().get(1).isInsertRevision()); Assert.assertEquals("John Doe", doc.getRevisions().get(0).getAuthor()); // Stop tracking revisions to not count any future edits as revisions. doc.stopTrackRevisions(); builder.write("Hello again! "); Assert.assertEquals(1, doc.getRevisions().getCount()); Assert.assertFalse(doc.getFirstSection().getBody().getParagraphs().get(0).getRuns().get(2).isInsertRevision()); // Creating revisions gives them a date and time of the operation. // We can disable this by passing DateTime.MinValue when we start tracking revisions. doc.startTrackRevisions("John Doe", new Date()); builder.write("Hello again! "); Assert.assertEquals(2, doc.getRevisions().getCount()); Assert.assertEquals("John Doe", doc.getRevisions().get(1).getAuthor()); Assert.assertEquals(new Date(), doc.getRevisions().get(1).getDateTime()); // We can accept/reject these revisions programmatically // by calling methods such as Document.AcceptAllRevisions, or each revision's Accept method. // In Microsoft Word, we can process them manually via "Review" -> "Changes". doc.save(getArtifactsDir() + "Document.StartTrackRevisions.docx");
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 to track revisions while editing a document.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Editing a document usually does not count as a revision until we begin tracking them. builder.write("Hello world! "); Assert.assertEquals(0, doc.getRevisions().getCount()); Assert.assertFalse(doc.getFirstSection().getBody().getParagraphs().get(0).getRuns().get(0).isInsertRevision()); doc.startTrackRevisions("John Doe"); builder.write("Hello again! "); Assert.assertEquals(1, doc.getRevisions().getCount()); Assert.assertTrue(doc.getFirstSection().getBody().getParagraphs().get(0).getRuns().get(1).isInsertRevision()); Assert.assertEquals("John Doe", doc.getRevisions().get(0).getAuthor()); // Stop tracking revisions to not count any future edits as revisions. doc.stopTrackRevisions(); builder.write("Hello again! "); Assert.assertEquals(1, doc.getRevisions().getCount()); Assert.assertFalse(doc.getFirstSection().getBody().getParagraphs().get(0).getRuns().get(2).isInsertRevision()); // Creating revisions gives them a date and time of the operation. // We can disable this by passing DateTime.MinValue when we start tracking revisions. doc.startTrackRevisions("John Doe", new Date()); builder.write("Hello again! "); Assert.assertEquals(2, doc.getRevisions().getCount()); Assert.assertEquals("John Doe", doc.getRevisions().get(1).getAuthor()); Assert.assertEquals(new Date(), doc.getRevisions().get(1).getDateTime()); // We can accept/reject these revisions programmatically // by calling methods such as Document.AcceptAllRevisions, or each revision's Accept method. // In Microsoft Word, we can process them manually via "Review" -> "Changes". doc.save(getArtifactsDir() + "Document.StartTrackRevisions.docx");
public void stopTrackRevisions()
Example:
Shows how to track revisions while editing a document.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Editing a document usually does not count as a revision until we begin tracking them. builder.write("Hello world! "); Assert.assertEquals(0, doc.getRevisions().getCount()); Assert.assertFalse(doc.getFirstSection().getBody().getParagraphs().get(0).getRuns().get(0).isInsertRevision()); doc.startTrackRevisions("John Doe"); builder.write("Hello again! "); Assert.assertEquals(1, doc.getRevisions().getCount()); Assert.assertTrue(doc.getFirstSection().getBody().getParagraphs().get(0).getRuns().get(1).isInsertRevision()); Assert.assertEquals("John Doe", doc.getRevisions().get(0).getAuthor()); // Stop tracking revisions to not count any future edits as revisions. doc.stopTrackRevisions(); builder.write("Hello again! "); Assert.assertEquals(1, doc.getRevisions().getCount()); Assert.assertFalse(doc.getFirstSection().getBody().getParagraphs().get(0).getRuns().get(2).isInsertRevision()); // Creating revisions gives them a date and time of the operation. // We can disable this by passing DateTime.MinValue when we start tracking revisions. doc.startTrackRevisions("John Doe", new Date()); builder.write("Hello again! "); Assert.assertEquals(2, doc.getRevisions().getCount()); Assert.assertEquals("John Doe", doc.getRevisions().get(1).getAuthor()); Assert.assertEquals(new Date(), doc.getRevisions().get(1).getDateTime()); // We can accept/reject these revisions programmatically // by calling methods such as Document.AcceptAllRevisions, or each revision's Accept method. // In Microsoft Word, we can process them manually via "Review" -> "Changes". doc.save(getArtifactsDir() + "Document.StartTrackRevisions.docx");
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.Document doc = new Document(getMyDir() + "Document.docx"); Node node = doc.getLastSection().getBody().getLastParagraph(); // When we call the ToString method using the html SaveFormat overload, // it converts the node's contents to their raw html representation. Assert.assertEquals("<p style=\"margin-top:0pt; margin-bottom:8pt; line-height:108%; font-size:12pt\">" + "<span style=\"font-family:'Times New Roman'\">Hello World!</span>" + "</p>", node.toString(SaveFormat.HTML)); // We can also modify the result of this conversion using a SaveOptions object. HtmlSaveOptions saveOptions = new HtmlSaveOptions(); saveOptions.setExportRelativeFontSize(true); Assert.assertEquals("<p style=\"margin-top:0pt; margin-bottom:8pt; line-height:108%\">" + "<span style=\"font-family:'Times New Roman'\">Hello World!</span>" + "</p>", node.toString(saveOptions));
public java.lang.String toString(int saveFormat) throws java.lang.Exception
saveFormat
- A SaveFormat value.Example:
Shows how to extract the list labels of all paragraphs that are list items.Document doc = new Document(getMyDir() + "Rendering.docx"); 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("List item paragraph #{0}", listParaCount)); // This is the text we get when getting when we output this node to text format. // This text output will omit list labels. 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 the current level of the list. If we have a list with multiple levels, // this will tell us what position it is on that level. System.out.println("\tNumerical Id: {label.LabelValue}"); // Combine them together to include the list label with the text in the output. System.out.println("\tList label combined with text: {label.LabelString} {paragraphText}"); }
Example:
Exports the content of a node to String in HTML format.Document doc = new Document(getMyDir() + "Document.docx"); Node node = doc.getLastSection().getBody().getLastParagraph(); // When we call the ToString method using the html SaveFormat overload, // it converts the node's contents to their raw html representation. Assert.assertEquals("<p style=\"margin-top:0pt; margin-bottom:8pt; line-height:108%; font-size:12pt\">" + "<span style=\"font-family:'Times New Roman'\">Hello World!</span>" + "</p>", node.toString(SaveFormat.HTML)); // We can also modify the result of this conversion using a SaveOptions object. HtmlSaveOptions saveOptions = new HtmlSaveOptions(); saveOptions.setExportRelativeFontSize(true); Assert.assertEquals("<p style=\"margin-top:0pt; margin-bottom:8pt; line-height:108%\">" + "<span style=\"font-family:'Times New Roman'\">Hello World!</span>" + "</p>", node.toString(saveOptions));
Example:
Shows the difference between calling the GetText and ToString methods on a node.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.insertField("MERGEFIELD Field"); // GetText will retrieve the visible text as well as field codes and special characters. Assert.assertEquals("\u0013MERGEFIELD Field\u0014«Field»\u0015\f", doc.getText()); // ToString will give us the document's appearance if saved to a passed save format. Assert.assertEquals("«Field»\r\n", doc.toString(SaveFormat.TEXT));
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 document.Document doc = new Document(getMyDir() + "Linked fields.docx"); doc.unlinkFields();
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 protect and unprotect a document.Document doc = new Document(); doc.protect(ProtectionType.READ_ONLY, "password"); Assert.assertEquals(ProtectionType.READ_ONLY, doc.getProtectionType()); // If we open this document with Microsoft Word intending to edit it, // we will need to apply the password to get through the protection. doc.save(getArtifactsDir() + "Document.Protect.docx"); // Note that the protection only applies to Microsoft Word users opening our document. // We have not encrypted the document in any way, and we do not need the password to open and edit it programmatically. Document protectedDoc = new Document(getArtifactsDir() + "Document.Protect.docx"); Assert.assertEquals(ProtectionType.READ_ONLY, protectedDoc.getProtectionType()); DocumentBuilder builder = new DocumentBuilder(protectedDoc); builder.writeln("Text added to a protected document."); // There are two ways of removing protection from a document. // 1 - With no password: doc.unprotect(); Assert.assertEquals(ProtectionType.NO_PROTECTION, doc.getProtectionType()); doc.protect(ProtectionType.READ_ONLY, "NewPassword"); Assert.assertEquals(ProtectionType.READ_ONLY, doc.getProtectionType()); doc.unprotect("WrongPassword"); Assert.assertEquals(ProtectionType.READ_ONLY, doc.getProtectionType()); // 2 - With the correct password: doc.unprotect("NewPassword"); Assert.assertEquals(ProtectionType.NO_PROTECTION, doc.getProtectionType());
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 protect and unprotect a document.Document doc = new Document(); doc.protect(ProtectionType.READ_ONLY, "password"); Assert.assertEquals(ProtectionType.READ_ONLY, doc.getProtectionType()); // If we open this document with Microsoft Word intending to edit it, // we will need to apply the password to get through the protection. doc.save(getArtifactsDir() + "Document.Protect.docx"); // Note that the protection only applies to Microsoft Word users opening our document. // We have not encrypted the document in any way, and we do not need the password to open and edit it programmatically. Document protectedDoc = new Document(getArtifactsDir() + "Document.Protect.docx"); Assert.assertEquals(ProtectionType.READ_ONLY, protectedDoc.getProtectionType()); DocumentBuilder builder = new DocumentBuilder(protectedDoc); builder.writeln("Text added to a protected document."); // There are two ways of removing protection from a document. // 1 - With no password: doc.unprotect(); Assert.assertEquals(ProtectionType.NO_PROTECTION, doc.getProtectionType()); doc.protect(ProtectionType.READ_ONLY, "NewPassword"); Assert.assertEquals(ProtectionType.READ_ONLY, doc.getProtectionType()); doc.unprotect("WrongPassword"); Assert.assertEquals(ProtectionType.READ_ONLY, doc.getProtectionType()); // 2 - With the correct password: doc.unprotect("NewPassword"); Assert.assertEquals(ProtectionType.NO_PROTECTION, doc.getProtectionType());
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:
Shows how to insert a Table of contents (TOC) into a document using heading styles as entries.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert a table of contents for the first page of the document. // Configure the table to pick up paragraphs with headings of levels 1 to 3. // Also, set its entries to be hyperlinks that will take us // to the location of the heading when left-clicked in Microsoft Word. builder.insertTableOfContents("\\o \"1-3\" \\h \\z \\u"); builder.insertBreak(BreakType.PAGE_BREAK); // Populate the table of contents by adding paragraphs with heading styles. // Each such heading with a level between 1 and 3 will create an entry in the table. 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_4); builder.writeln("Heading 3.1.3.1"); builder.writeln("Heading 3.1.3.2"); builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2); builder.writeln("Heading 3.2"); builder.writeln("Heading 3.3"); // A table of contents is a field of a type that needs to be updated to show an up-to-date result. doc.updateFields(); doc.save(getArtifactsDir() + "DocumentBuilder.InsertToc.docx");
Example:
Shows how to set user details, and display them using fields.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Create a UserInformation object and set it as the data source for fields that display user information. UserInformation userInformation = new UserInformation(); userInformation.setName("John Doe"); userInformation.setInitials("J. D."); userInformation.setAddress("123 Main Street"); doc.getFieldOptions().setCurrentUser(userInformation); // Insert USERNAME, USERINITIALS, and USERADDRESS fields, which display values of // the respective properties of the UserInformation object that we have created above. Assert.assertEquals(userInformation.getName(), builder.insertField(" USERNAME ").getResult()); Assert.assertEquals(userInformation.getInitials(), builder.insertField(" USERINITIALS ").getResult()); Assert.assertEquals(userInformation.getAddress(), builder.insertField(" USERADDRESS ").getResult()); // The field options object also has a static default user that fields from all documents can refer to. UserInformation.getDefaultUser().setName("Default User"); UserInformation.getDefaultUser().setInitials("D. U."); UserInformation.getDefaultUser().setAddress("One Microsoft Way"); doc.getFieldOptions().setCurrentUser(UserInformation.getDefaultUser()); Assert.assertEquals("Default User", builder.insertField(" USERNAME ").getResult()); Assert.assertEquals("D. U.", builder.insertField(" USERINITIALS ").getResult()); Assert.assertEquals("One Microsoft Way", builder.insertField(" USERADDRESS ").getResult()); doc.updateFields(); doc.save(getArtifactsDir() + "FieldOptions.CurrentUser.docx");
Example:
Shows to use the QUOTE field.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert a QUOTE field, which will display the value of its Text property. FieldQuote field = (FieldQuote) builder.insertField(FieldType.FIELD_QUOTE, true); field.setText("\"Quoted text\""); Assert.assertEquals(" QUOTE \"\\\"Quoted text\\\"\"", field.getFieldCode()); // Insert a QUOTE field and nest a DATE field inside it. // DATE fields update their value to the current date every time we open the document using Microsoft Word. // Nesting the DATE field inside the QUOTE field like this will freeze its value // to the date when we created the document. builder.write("\nDocument creation date: "); field = (FieldQuote) builder.insertField(FieldType.FIELD_QUOTE, true); builder.moveTo(field.getSeparator()); builder.insertField(FieldType.FIELD_DATE, true); Assert.assertEquals(" QUOTE \u0013 DATE \u0014" + LocalDate.now().format(DateTimeFormatter.ofPattern("M/d/YYYY")) + "\u0015", field.getFieldCode()); // Update all the fields to display their correct results. doc.updateFields(); Assert.assertEquals("\"Quoted text\"", doc.getRange().getFields().get(0).getResult()); doc.save(getArtifactsDir() + "Field.QUOTE.docx");
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 list labels of all paragraphs that are list items.Document doc = new Document(getMyDir() + "Rendering.docx"); 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("List item paragraph #{0}", listParaCount)); // This is the text we get when getting when we output this node to text format. // This text output will omit list labels. 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 the current level of the list. If we have a list with multiple levels, // this will tell us what position it is on that level. System.out.println("\tNumerical Id: {label.LabelValue}"); // Combine them together to include the list label with the text in the output. System.out.println("\tList label combined with text: {label.LabelString} {paragraphText}"); }
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 recalculate the page layout of the document.Document doc = new Document(getMyDir() + "Rendering.docx"); // Saving a document to PDF, to an image, or printing for the first time will automatically // cache the layout of the document within its pages. doc.save(getArtifactsDir() + "Document.UpdatePageLayout.1.pdf"); // Modify the document in some way. doc.getStyles().get("Normal").getFont().setSize(6.0); 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 we wish for the cached layout // to stay up to date, we will need to update it manually. doc.updatePageLayout(); doc.save(getArtifactsDir() + "Document.UpdatePageLayout.2.pdf");
@Deprecated public void updateTableLayout()
public void updateThumbnail() throws java.lang.Exception
Example:
Shows how to update a document's thumbnail.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.writeln("Hello world!"); builder.insertImage(getImageDir() + "Logo.jpg"); // There are two ways of setting a thumbnail image when saving a document to .epub. // 1 - Use the document's first page: doc.updateThumbnail(); doc.save(getArtifactsDir() + "Document.UpdateThumbnail.FirstPage.epub"); // 2 - Use the first image found in the document: ThumbnailGeneratingOptions options = new ThumbnailGeneratingOptions(); options.setThumbnailSize(new Dimension(400, 400)); options.setGenerateFromFirstPage(false); doc.updateThumbnail(options); doc.save(getArtifactsDir() + "Document.UpdateThumbnail.FirstImage.epub");
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(); DocumentBuilder builder = new DocumentBuilder(doc); builder.writeln("Hello world!"); builder.insertImage(getImageDir() + "Logo.jpg"); // There are two ways of setting a thumbnail image when saving a document to .epub. // 1 - Use the document's first page: doc.updateThumbnail(); doc.save(getArtifactsDir() + "Document.UpdateThumbnail.FirstPage.epub"); // 2 - Use the first image found in the document: ThumbnailGeneratingOptions options = new ThumbnailGeneratingOptions(); options.setThumbnailSize(new Dimension(400, 400)); options.setGenerateFromFirstPage(false); doc.updateThumbnail(options); doc.save(getArtifactsDir() + "Document.UpdateThumbnail.FirstImage.epub");
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(); DocumentBuilder builder = new DocumentBuilder(doc); builder.writeln("Lorem ipsum dolor sit amet, consectetur adipiscing elit, " + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."); builder.write("Ut enim ad minim veniam, " + "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."); // Aspose.Words does not track document metrics like these in real time. Assert.assertEquals(0, doc.getBuiltInDocumentProperties().getCharacters()); Assert.assertEquals(0, doc.getBuiltInDocumentProperties().getWords()); Assert.assertEquals(1, doc.getBuiltInDocumentProperties().getParagraphs()); Assert.assertEquals(1, doc.getBuiltInDocumentProperties().getLines()); // To get accurate values for three of these properties, we will need to update them manually. doc.updateWordCount(); Assert.assertEquals(196, doc.getBuiltInDocumentProperties().getCharacters()); Assert.assertEquals(36, doc.getBuiltInDocumentProperties().getWords()); Assert.assertEquals(2, doc.getBuiltInDocumentProperties().getParagraphs()); // For the line count, we will need to call a specific overload of the updating method. Assert.assertEquals(1, doc.getBuiltInDocumentProperties().getLines()); doc.updateWordCount(true); Assert.assertEquals(4, doc.getBuiltInDocumentProperties().getLines());
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 update all list labels in a document.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.writeln("Lorem ipsum dolor sit amet, consectetur adipiscing elit, " + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."); builder.write("Ut enim ad minim veniam, " + "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."); // Aspose.Words does not track document metrics like these in real time. Assert.assertEquals(0, doc.getBuiltInDocumentProperties().getCharacters()); Assert.assertEquals(0, doc.getBuiltInDocumentProperties().getWords()); Assert.assertEquals(1, doc.getBuiltInDocumentProperties().getParagraphs()); Assert.assertEquals(1, doc.getBuiltInDocumentProperties().getLines()); // To get accurate values for three of these properties, we will need to update them manually. doc.updateWordCount(); Assert.assertEquals(196, doc.getBuiltInDocumentProperties().getCharacters()); Assert.assertEquals(36, doc.getBuiltInDocumentProperties().getWords()); Assert.assertEquals(2, doc.getBuiltInDocumentProperties().getParagraphs()); // For the line count, we will need to call a specific overload of the updating method. Assert.assertEquals(1, doc.getBuiltInDocumentProperties().getLines()); doc.updateWordCount(true); Assert.assertEquals(4, doc.getBuiltInDocumentProperties().getLines());