BuiltInDocumentProperties
Inheritance: java.lang.Object, com.aspose.words.DocumentPropertyCollection
public class BuiltInDocumentProperties extends DocumentPropertyCollection
A collection of built-in document properties.
To learn more, visit the Work with Document Properties documentation article.
Remarks:
Provides access to DocumentProperty objects by their names (using an indexer) and via a set of typed properties that return values of appropriate types.
The names of the properties are case-insensitive.
The properties in the collection are sorted alphabetically by name.
Methods
Method | Description |
---|---|
clear() | Removes all properties from the collection. |
contains(String name) | Returns true if a property with the specified name exists in the collection. |
get(int index) | Returns a DocumentProperty object by index. |
get(String name) | Returns a DocumentProperty object. |
getAuthor() | Gets the name of the document’s author. |
getBytes() | Represents an estimate of the number of bytes in the document. |
getCategory() | Gets the category of the document. |
getCharacters() | Represents an estimate of the number of characters in the document. |
getCharactersWithSpaces() | Represents an estimate of the number of characters (including spaces) in the document. |
getComments() | Gets the document comments. |
getCompany() | Gets the company property. |
getContentStatus() | Gets the content status of the document. |
getContentType() | Gets the content type of the document. |
getCount() | Gets number of items in the collection. |
getCreatedTime() | Gets date of the document creation in UTC. |
getHeadingPairs() | Specifies document headings and their names. |
getHyperlinkBase() | Specifies the base string used for evaluating relative hyperlinks in this document. |
getHyperlinksChanged() | Indicates whether hyperlinks in a document were changed. |
getKeywords() | Gets the document keywords. |
getLastPrinted() | Gets the date when the document was last printed in UTC. |
getLastSavedBy() | Gets the name of the last author. |
getLastSavedTime() | Gets the time of the last save in UTC. |
getLines() | Represents an estimate of the number of lines in the document. |
getLinksUpToDate() | Indicates whether hyperlinks in a document are up-to-date. |
getManager() | Gets the manager property. |
getNameOfApplication() | Gets the name of the application. |
getPages() | Represents an estimate of the number of pages in the document. |
getParagraphs() | Represents an estimate of the number of paragraphs in the document. |
getRevisionNumber() | Gets the document revision number. |
getScaleCrop() | Indicates whether document thumbnail is cropped or scaled to fit the display. |
getSecurity() | Specifies the security level of a document as a numeric value. |
getSharedDocument() | Indicates whether the document is a shared document. |
getSubject() | Gets the subject of the document. |
getTemplate() | Gets the informational name of the document template. |
getThumbnail() | Gets or sets the thumbnail of the document. |
getTitle() | Gets the title of the document. |
getTitlesOfParts() | Each string in the array specifies the name of a part in the document. |
getTotalEditingTime() | Gets the total editing time in minutes. |
getVersion() | Represents the version number of the application that created the document. |
getWords() | Represents an estimate of the number of words in the document. |
indexOf(String name) | Gets the index of a property by name. |
iterator() | Returns an iterator object that can be used to iterate over all items in the collection. |
remove(String name) | Removes a property with the specified name from the collection. |
removeAt(int index) | Removes a property at the specified index. |
setAuthor(String value) | Sets the name of the document’s author. |
setBytes(int value) | Represents an estimate of the number of bytes in the document. |
setCategory(String value) | Sets the category of the document. |
setCharacters(int value) | Represents an estimate of the number of characters in the document. |
setCharactersWithSpaces(int value) | Represents an estimate of the number of characters (including spaces) in the document. |
setComments(String value) | Sets the document comments. |
setCompany(String value) | Sets the company property. |
setContentStatus(String value) | Sets the content status of the document. |
setContentType(String value) | Sets the content type of the document. |
setCreatedTime(Date value) | Sets date of the document creation in UTC. |
setHeadingPairs(Object[] value) | Specifies document headings and their names. |
setHyperlinkBase(String value) | Specifies the base string used for evaluating relative hyperlinks in this document. |
setKeywords(String value) | Sets the document keywords. |
setLastPrinted(Date value) | Sets the date when the document was last printed in UTC. |
setLastSavedBy(String value) | Sets the name of the last author. |
setLastSavedTime(Date value) | Sets the time of the last save in UTC. |
setLines(int value) | Represents an estimate of the number of lines in the document. |
setLinksUpToDate(boolean value) | Indicates whether hyperlinks in a document are up-to-date. |
setManager(String value) | Sets the manager property. |
setNameOfApplication(String value) | Sets the name of the application. |
setPages(int value) | Represents an estimate of the number of pages in the document. |
setParagraphs(int value) | Represents an estimate of the number of paragraphs in the document. |
setRevisionNumber(int value) | Sets the document revision number. |
setSecurity(int value) | Specifies the security level of a document as a numeric value. |
setSubject(String value) | Sets the subject of the document. |
setTemplate(String value) | Sets the informational name of the document template. |
setThumbnail(byte[] value) | Gets or sets the thumbnail of the document. |
setTitle(String value) | Sets the title of the document. |
setTitlesOfParts(String[] value) | Each string in the array specifies the name of a part in the document. |
setTotalEditingTime(int value) | Sets the total editing time in minutes. |
setVersion(int value) | Represents the version number of the application that created the document. |
setWords(int value) | Represents an estimate of the number of words in the document. |
clear()
public void clear()
Removes all properties from the collection.
Examples:
Shows how to work with a document’s custom properties.
Document doc = new Document();
CustomDocumentProperties properties = doc.getCustomDocumentProperties();
Assert.assertEquals(0, properties.getCount());
// Custom document properties are key-value pairs that we can add to the document.
properties.add("Authorized", true);
properties.add("Authorized By", "John Doe");
properties.add("Authorized Date", new Date());
properties.add("Authorized Revision", doc.getBuiltInDocumentProperties().getRevisionNumber());
properties.add("Authorized Amount", 123.45);
// The collection sorts the custom properties in alphabetic order.
Assert.assertEquals(1, properties.indexOf("Authorized Amount"));
Assert.assertEquals(5, properties.getCount());
// Print every custom property in the document.
Iterator enumerator = properties.iterator();
while (enumerator.hasNext()) {
DocumentProperty property = enumerator.next();
System.out.println(MessageFormat.format("Name: \"{0}\"\n\tType: \"{1}\"\n\tValue: \"{2}\"", property.getName(), property.getType(), property.getValue()));
}
// Display the value of a custom property using a DOCPROPERTY field.
DocumentBuilder builder = new DocumentBuilder(doc);
FieldDocProperty field = (FieldDocProperty) builder.insertField(" DOCPROPERTY \"Authorized By\"");
field.update();
Assert.assertEquals("John Doe", field.getResult());
// We can find these custom properties in Microsoft Word via "File" -> "Properties" > "Advanced Properties" > "Custom".
doc.save(getArtifactsDir() + "DocumentProperties.DocumentPropertyCollection.docx");
// Below are three ways or removing custom properties from a document.
// 1 - Remove by index:
properties.removeAt(1);
Assert.assertFalse(properties.contains("Authorized Amount"));
Assert.assertEquals(4, properties.getCount());
// 2 - Remove by name:
properties.remove("Authorized Revision");
Assert.assertFalse(properties.contains("Authorized Revision"));
Assert.assertEquals(3, properties.getCount());
// 3 - Empty the entire collection at once:
properties.clear();
Assert.assertEquals(0, properties.getCount());
contains(String name)
public boolean contains(String name)
Returns true if a property with the specified name exists in the collection.
Examples:
Shows how to work with a document’s custom properties.
Document doc = new Document();
CustomDocumentProperties properties = doc.getCustomDocumentProperties();
Assert.assertEquals(0, properties.getCount());
// Custom document properties are key-value pairs that we can add to the document.
properties.add("Authorized", true);
properties.add("Authorized By", "John Doe");
properties.add("Authorized Date", new Date());
properties.add("Authorized Revision", doc.getBuiltInDocumentProperties().getRevisionNumber());
properties.add("Authorized Amount", 123.45);
// The collection sorts the custom properties in alphabetic order.
Assert.assertEquals(1, properties.indexOf("Authorized Amount"));
Assert.assertEquals(5, properties.getCount());
// Print every custom property in the document.
Iterator enumerator = properties.iterator();
while (enumerator.hasNext()) {
DocumentProperty property = enumerator.next();
System.out.println(MessageFormat.format("Name: \"{0}\"\n\tType: \"{1}\"\n\tValue: \"{2}\"", property.getName(), property.getType(), property.getValue()));
}
// Display the value of a custom property using a DOCPROPERTY field.
DocumentBuilder builder = new DocumentBuilder(doc);
FieldDocProperty field = (FieldDocProperty) builder.insertField(" DOCPROPERTY \"Authorized By\"");
field.update();
Assert.assertEquals("John Doe", field.getResult());
// We can find these custom properties in Microsoft Word via "File" -> "Properties" > "Advanced Properties" > "Custom".
doc.save(getArtifactsDir() + "DocumentProperties.DocumentPropertyCollection.docx");
// Below are three ways or removing custom properties from a document.
// 1 - Remove by index:
properties.removeAt(1);
Assert.assertFalse(properties.contains("Authorized Amount"));
Assert.assertEquals(4, properties.getCount());
// 2 - Remove by name:
properties.remove("Authorized Revision");
Assert.assertFalse(properties.contains("Authorized Revision"));
Assert.assertEquals(3, properties.getCount());
// 3 - Empty the entire collection at once:
properties.clear();
Assert.assertEquals(0, properties.getCount());
Parameters:
Parameter | Type | Description |
---|---|---|
name | java.lang.String | The case-insensitive name of the property. |
Returns: boolean - true if the property exists in the collection; false otherwise.
get(int index)
public DocumentProperty get(int index)
Returns a DocumentProperty object by index.
Remarks:
Note: In Java this method is slow because iterates over all nodes.
Examples:
Shows how to work with custom document properties.
Document doc = new Document(getMyDir() + "Properties.docx");
// Every document contains a collection of custom properties, which, like the built-in properties, are key-value pairs.
// The document has a fixed list of built-in properties. The user creates all of the custom properties.
Assert.assertEquals("Value of custom document property", doc.getCustomDocumentProperties().get("CustomProperty").toString());
doc.getCustomDocumentProperties().add("CustomProperty2", "Value of custom document property #2");
System.out.println("Custom Properties:");
for (DocumentProperty customDocumentProperty : doc.getCustomDocumentProperties()) {
System.out.println(customDocumentProperty.getName());
System.out.println(MessageFormat.format("\tType:\t{0}", customDocumentProperty.getType()));
System.out.println(MessageFormat.format("\tValue:\t\"{0}\"", customDocumentProperty.getValue()));
}
Parameters:
Parameter | Type | Description |
---|---|---|
index | int | Zero-based index of the DocumentProperty to retrieve. |
Returns: DocumentProperty - A DocumentProperty object by index.
get(String name)
public DocumentProperty get(String name)
Returns a DocumentProperty object. Returns a DocumentProperty object by the name of the property.
Remarks:
The string names of the properties correspond to the names of the typed properties available from BuiltInDocumentProperties.
If you request a property that is not present in the document, but the name of the property is recognized as a valid built-in name, a new DocumentProperty is created, added to the collection and returned. The newly created property is assigned a default value (empty string, zero, false or DateTime.MinValue depending on the type of the built-in property).
If you request a property that is not present in the document and the name is not recognized as a built-in name, a null is returned.
Examples:
Shows how to work with custom document properties.
Document doc = new Document(getMyDir() + "Properties.docx");
// Every document contains a collection of custom properties, which, like the built-in properties, are key-value pairs.
// The document has a fixed list of built-in properties. The user creates all of the custom properties.
Assert.assertEquals("Value of custom document property", doc.getCustomDocumentProperties().get("CustomProperty").toString());
doc.getCustomDocumentProperties().add("CustomProperty2", "Value of custom document property #2");
System.out.println("Custom Properties:");
for (DocumentProperty customDocumentProperty : doc.getCustomDocumentProperties()) {
System.out.println(customDocumentProperty.getName());
System.out.println(MessageFormat.format("\tType:\t{0}", customDocumentProperty.getType()));
System.out.println(MessageFormat.format("\tValue:\t\"{0}\"", customDocumentProperty.getValue()));
}
Parameters:
Parameter | Type | Description |
---|---|---|
name | java.lang.String | The case-insensitive name of the property to retrieve. |
Returns: DocumentProperty - The corresponding DocumentProperty value.
getAuthor()
public String getAuthor()
Gets the name of the document’s author.
Examples:
Shows how to work with built-in document properties in the “Description” category.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// Below are four built-in document properties that have fields that can display their values in the document body.
// 1 - "Author" property, which we can display using an AUTHOR field:
properties.setAuthor("John Doe");
builder.write("Author:\t");
builder.insertField(FieldType.FIELD_AUTHOR, true);
// 2 - "Title" property, which we can display using a TITLE field:
properties.setTitle("John's Document");
builder.write("\nDoc title:\t");
builder.insertField(FieldType.FIELD_TITLE, true);
// 3 - "Subject" property, which we can display using a SUBJECT field:
properties.setSubject("My subject");
builder.write("\nSubject:\t");
builder.insertField(FieldType.FIELD_SUBJECT, true);
// 4 - "Comments" property, which we can display using a COMMENTS field:
properties.setComments(MessageFormat.format("This is {0}''s document about {1}", properties.getAuthor(), properties.getSubject()));
builder.write("\nComments:\t\"");
builder.insertField(FieldType.FIELD_COMMENTS, true);
builder.write("\"");
// The "Category" built-in property does not have a field that can display its value.
properties.setCategory("My category");
// We can set multiple keywords for a document by separating the string value of the "Keywords" property with semicolons.
properties.setKeywords("Tag 1; Tag 2; Tag 3");
// We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details".
// The "Author" built-in property is in the "Origin" group, and the others are in the "Description" group.
doc.save(getArtifactsDir() + "DocumentProperties.Description.docx");
Returns: java.lang.String - The name of the document’s author.
getBytes()
public int getBytes()
Represents an estimate of the number of bytes in the document.
Remarks:
Microsoft Word does not always set this property.
Aspose.Words does not update this property.
Examples:
Shows how to work with document properties in the “Content” category.
public void content() throws Exception {
Document doc = new Document(getMyDir() + "Paragraphs.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// By using built in properties,
// we can treat document statistics such as word/page/character counts as metadata that can be glanced at without opening the document
// These properties are accessed by right clicking the file in Windows Explorer and navigating to Properties > Details > Content
// If we want to display this data inside the document, we can use fields such as NUMPAGES, NUMWORDS, NUMCHARS etc.
// Also, these values can also be viewed in Microsoft Word by navigating File > Properties > Advanced Properties > Statistics
// Page count: The PageCount property shows the page count in real time and its value can be assigned to the Pages property
// The "Pages" property stores the page count of the document.
Assert.assertEquals(6, properties.getPages());
// The "Words", "Characters", and "CharactersWithSpaces" built-in properties also display various document statistics,
// but we need to call the "UpdateWordCount" method on the whole document before we can expect them to contain accurate values.
doc.updateWordCount();
Assert.assertEquals(1035, properties.getWords());
Assert.assertEquals(6026, properties.getCharacters());
Assert.assertEquals(7041, properties.getCharactersWithSpaces());
// Count the number of lines in the document, and then assign the result to the "Lines" built-in property.
LineCounter lineCounter = new LineCounter(doc);
properties.setLines(lineCounter.getLineCount());
Assert.assertEquals(142, properties.getLines());
// Assign the number of Paragraph nodes in the document to the "Paragraphs" built-in property.
properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount());
Assert.assertEquals(29, properties.getParagraphs());
// Get an estimate of the file size of our document via the "Bytes" built-in property.
Assert.assertEquals(20310, properties.getBytes());
// Set a different template for our document, and then update the "Template" built-in property manually to reflect this change.
doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx");
Assert.assertEquals("Normal", properties.getTemplate());
properties.setTemplate(doc.getAttachedTemplate());
// "ContentStatus" is a descriptive built-in property.
properties.setContentStatus("Draft");
// Upon saving, the "ContentType" built-in property will contain the MIME type of the output save format.
Assert.assertEquals("", properties.getContentType());
// If the document contains links, and they are all up to date, we can set the "LinksUpToDate" property to "true".
Assert.assertFalse(properties.getLinksUpToDate());
doc.save(getArtifactsDir() + "DocumentProperties.Content.docx");
}
///
/// Counts the lines in a document.
/// Traverses the document's layout entities tree upon construction,
/// counting entities of the "Line" type that also contain real text.
///
private static class LineCounter {
public LineCounter(Document doc) throws Exception {
mLayoutEnumerator = new LayoutEnumerator(doc);
countLines();
}
public int getLineCount() {
return mLineCount;
}
private void countLines() throws Exception {
do {
if (mLayoutEnumerator.getType() == LayoutEntityType.LINE) {
mScanningLineForRealText = true;
}
if (mLayoutEnumerator.moveFirstChild()) {
if (mScanningLineForRealText && mLayoutEnumerator.getKind().startsWith("TEXT")) {
mLineCount++;
mScanningLineForRealText = false;
}
countLines();
mLayoutEnumerator.moveParent();
}
} while (mLayoutEnumerator.moveNext());
}
private final LayoutEnumerator mLayoutEnumerator;
private int mLineCount;
private boolean mScanningLineForRealText;
}
Returns: int - The corresponding int value.
getCategory()
public String getCategory()
Gets the category of the document.
Examples:
Shows how to work with built-in document properties in the “Description” category.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// Below are four built-in document properties that have fields that can display their values in the document body.
// 1 - "Author" property, which we can display using an AUTHOR field:
properties.setAuthor("John Doe");
builder.write("Author:\t");
builder.insertField(FieldType.FIELD_AUTHOR, true);
// 2 - "Title" property, which we can display using a TITLE field:
properties.setTitle("John's Document");
builder.write("\nDoc title:\t");
builder.insertField(FieldType.FIELD_TITLE, true);
// 3 - "Subject" property, which we can display using a SUBJECT field:
properties.setSubject("My subject");
builder.write("\nSubject:\t");
builder.insertField(FieldType.FIELD_SUBJECT, true);
// 4 - "Comments" property, which we can display using a COMMENTS field:
properties.setComments(MessageFormat.format("This is {0}''s document about {1}", properties.getAuthor(), properties.getSubject()));
builder.write("\nComments:\t\"");
builder.insertField(FieldType.FIELD_COMMENTS, true);
builder.write("\"");
// The "Category" built-in property does not have a field that can display its value.
properties.setCategory("My category");
// We can set multiple keywords for a document by separating the string value of the "Keywords" property with semicolons.
properties.setKeywords("Tag 1; Tag 2; Tag 3");
// We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details".
// The "Author" built-in property is in the "Origin" group, and the others are in the "Description" group.
doc.save(getArtifactsDir() + "DocumentProperties.Description.docx");
Returns: java.lang.String - The category of the document.
getCharacters()
public int getCharacters()
Represents an estimate of the number of characters in the document.
Remarks:
Aspose.Words updates this property when you call Document.updateWordCount().
Examples:
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());
Shows how to work with document properties in the “Content” category.
public void content() throws Exception {
Document doc = new Document(getMyDir() + "Paragraphs.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// By using built in properties,
// we can treat document statistics such as word/page/character counts as metadata that can be glanced at without opening the document
// These properties are accessed by right clicking the file in Windows Explorer and navigating to Properties > Details > Content
// If we want to display this data inside the document, we can use fields such as NUMPAGES, NUMWORDS, NUMCHARS etc.
// Also, these values can also be viewed in Microsoft Word by navigating File > Properties > Advanced Properties > Statistics
// Page count: The PageCount property shows the page count in real time and its value can be assigned to the Pages property
// The "Pages" property stores the page count of the document.
Assert.assertEquals(6, properties.getPages());
// The "Words", "Characters", and "CharactersWithSpaces" built-in properties also display various document statistics,
// but we need to call the "UpdateWordCount" method on the whole document before we can expect them to contain accurate values.
doc.updateWordCount();
Assert.assertEquals(1035, properties.getWords());
Assert.assertEquals(6026, properties.getCharacters());
Assert.assertEquals(7041, properties.getCharactersWithSpaces());
// Count the number of lines in the document, and then assign the result to the "Lines" built-in property.
LineCounter lineCounter = new LineCounter(doc);
properties.setLines(lineCounter.getLineCount());
Assert.assertEquals(142, properties.getLines());
// Assign the number of Paragraph nodes in the document to the "Paragraphs" built-in property.
properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount());
Assert.assertEquals(29, properties.getParagraphs());
// Get an estimate of the file size of our document via the "Bytes" built-in property.
Assert.assertEquals(20310, properties.getBytes());
// Set a different template for our document, and then update the "Template" built-in property manually to reflect this change.
doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx");
Assert.assertEquals("Normal", properties.getTemplate());
properties.setTemplate(doc.getAttachedTemplate());
// "ContentStatus" is a descriptive built-in property.
properties.setContentStatus("Draft");
// Upon saving, the "ContentType" built-in property will contain the MIME type of the output save format.
Assert.assertEquals("", properties.getContentType());
// If the document contains links, and they are all up to date, we can set the "LinksUpToDate" property to "true".
Assert.assertFalse(properties.getLinksUpToDate());
doc.save(getArtifactsDir() + "DocumentProperties.Content.docx");
}
///
/// Counts the lines in a document.
/// Traverses the document's layout entities tree upon construction,
/// counting entities of the "Line" type that also contain real text.
///
private static class LineCounter {
public LineCounter(Document doc) throws Exception {
mLayoutEnumerator = new LayoutEnumerator(doc);
countLines();
}
public int getLineCount() {
return mLineCount;
}
private void countLines() throws Exception {
do {
if (mLayoutEnumerator.getType() == LayoutEntityType.LINE) {
mScanningLineForRealText = true;
}
if (mLayoutEnumerator.moveFirstChild()) {
if (mScanningLineForRealText && mLayoutEnumerator.getKind().startsWith("TEXT")) {
mLineCount++;
mScanningLineForRealText = false;
}
countLines();
mLayoutEnumerator.moveParent();
}
} while (mLayoutEnumerator.moveNext());
}
private final LayoutEnumerator mLayoutEnumerator;
private int mLineCount;
private boolean mScanningLineForRealText;
}
Returns: int - The corresponding int value.
getCharactersWithSpaces()
public int getCharactersWithSpaces()
Represents an estimate of the number of characters (including spaces) in the document.
Remarks:
Aspose.Words updates this property when you call Document.updateWordCount().
Examples:
Shows how to work with document properties in the “Content” category.
public void content() throws Exception {
Document doc = new Document(getMyDir() + "Paragraphs.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// By using built in properties,
// we can treat document statistics such as word/page/character counts as metadata that can be glanced at without opening the document
// These properties are accessed by right clicking the file in Windows Explorer and navigating to Properties > Details > Content
// If we want to display this data inside the document, we can use fields such as NUMPAGES, NUMWORDS, NUMCHARS etc.
// Also, these values can also be viewed in Microsoft Word by navigating File > Properties > Advanced Properties > Statistics
// Page count: The PageCount property shows the page count in real time and its value can be assigned to the Pages property
// The "Pages" property stores the page count of the document.
Assert.assertEquals(6, properties.getPages());
// The "Words", "Characters", and "CharactersWithSpaces" built-in properties also display various document statistics,
// but we need to call the "UpdateWordCount" method on the whole document before we can expect them to contain accurate values.
doc.updateWordCount();
Assert.assertEquals(1035, properties.getWords());
Assert.assertEquals(6026, properties.getCharacters());
Assert.assertEquals(7041, properties.getCharactersWithSpaces());
// Count the number of lines in the document, and then assign the result to the "Lines" built-in property.
LineCounter lineCounter = new LineCounter(doc);
properties.setLines(lineCounter.getLineCount());
Assert.assertEquals(142, properties.getLines());
// Assign the number of Paragraph nodes in the document to the "Paragraphs" built-in property.
properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount());
Assert.assertEquals(29, properties.getParagraphs());
// Get an estimate of the file size of our document via the "Bytes" built-in property.
Assert.assertEquals(20310, properties.getBytes());
// Set a different template for our document, and then update the "Template" built-in property manually to reflect this change.
doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx");
Assert.assertEquals("Normal", properties.getTemplate());
properties.setTemplate(doc.getAttachedTemplate());
// "ContentStatus" is a descriptive built-in property.
properties.setContentStatus("Draft");
// Upon saving, the "ContentType" built-in property will contain the MIME type of the output save format.
Assert.assertEquals("", properties.getContentType());
// If the document contains links, and they are all up to date, we can set the "LinksUpToDate" property to "true".
Assert.assertFalse(properties.getLinksUpToDate());
doc.save(getArtifactsDir() + "DocumentProperties.Content.docx");
}
///
/// Counts the lines in a document.
/// Traverses the document's layout entities tree upon construction,
/// counting entities of the "Line" type that also contain real text.
///
private static class LineCounter {
public LineCounter(Document doc) throws Exception {
mLayoutEnumerator = new LayoutEnumerator(doc);
countLines();
}
public int getLineCount() {
return mLineCount;
}
private void countLines() throws Exception {
do {
if (mLayoutEnumerator.getType() == LayoutEntityType.LINE) {
mScanningLineForRealText = true;
}
if (mLayoutEnumerator.moveFirstChild()) {
if (mScanningLineForRealText && mLayoutEnumerator.getKind().startsWith("TEXT")) {
mLineCount++;
mScanningLineForRealText = false;
}
countLines();
mLayoutEnumerator.moveParent();
}
} while (mLayoutEnumerator.moveNext());
}
private final LayoutEnumerator mLayoutEnumerator;
private int mLineCount;
private boolean mScanningLineForRealText;
}
Returns: int - The corresponding int value.
getComments()
public String getComments()
Gets the document comments.
Examples:
Shows how to work with built-in document properties in the “Description” category.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// Below are four built-in document properties that have fields that can display their values in the document body.
// 1 - "Author" property, which we can display using an AUTHOR field:
properties.setAuthor("John Doe");
builder.write("Author:\t");
builder.insertField(FieldType.FIELD_AUTHOR, true);
// 2 - "Title" property, which we can display using a TITLE field:
properties.setTitle("John's Document");
builder.write("\nDoc title:\t");
builder.insertField(FieldType.FIELD_TITLE, true);
// 3 - "Subject" property, which we can display using a SUBJECT field:
properties.setSubject("My subject");
builder.write("\nSubject:\t");
builder.insertField(FieldType.FIELD_SUBJECT, true);
// 4 - "Comments" property, which we can display using a COMMENTS field:
properties.setComments(MessageFormat.format("This is {0}''s document about {1}", properties.getAuthor(), properties.getSubject()));
builder.write("\nComments:\t\"");
builder.insertField(FieldType.FIELD_COMMENTS, true);
builder.write("\"");
// The "Category" built-in property does not have a field that can display its value.
properties.setCategory("My category");
// We can set multiple keywords for a document by separating the string value of the "Keywords" property with semicolons.
properties.setKeywords("Tag 1; Tag 2; Tag 3");
// We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details".
// The "Author" built-in property is in the "Origin" group, and the others are in the "Description" group.
doc.save(getArtifactsDir() + "DocumentProperties.Description.docx");
Returns: java.lang.String - The document comments.
getCompany()
public String getCompany()
Gets the company property.
Examples:
Shows how to work with document properties in the “Origin” category.
// Open a document that we have created and edited using Microsoft Word.
Document doc = new Document(getMyDir() + "Properties.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// The following built-in properties contain information regarding the creation and editing of this document.
// We can right-click this document in Windows Explorer and find
// these properties via "Properties" -> "Details" -> "Origin" category.
// Fields such as PRINTDATE and EDITTIME can display these values in the document body.
System.out.println(MessageFormat.format("Created using {0}, on {1}", properties.getNameOfApplication(), properties.getCreatedTime()));
System.out.println(MessageFormat.format("Minutes spent editing: {0}", properties.getTotalEditingTime()));
System.out.println(MessageFormat.format("Date/time last printed: {0}", properties.getLastPrinted()));
System.out.println(MessageFormat.format("Template document: {0}", properties.getTemplate()));
// We can also change the values of built-in properties.
properties.setCompany("Doe Ltd.");
properties.setManager("Jane Doe");
properties.setVersion(5);
properties.setRevisionNumber(properties.getRevisionNumber() + 1);
// Microsoft Word updates the following properties automatically when we save the document.
// To use these properties with Aspose.Words, we will need to set values for them manually.
properties.setLastSavedBy("John Doe");
properties.setLastSavedTime(new Date());
// We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details" -> "Origin".
doc.save(getArtifactsDir() + "DocumentProperties.Origin.docx");
Returns: java.lang.String - The company property.
getContentStatus()
public String getContentStatus()
Gets the content status of the document.
Examples:
Shows how to work with document properties in the “Content” category.
public void content() throws Exception {
Document doc = new Document(getMyDir() + "Paragraphs.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// By using built in properties,
// we can treat document statistics such as word/page/character counts as metadata that can be glanced at without opening the document
// These properties are accessed by right clicking the file in Windows Explorer and navigating to Properties > Details > Content
// If we want to display this data inside the document, we can use fields such as NUMPAGES, NUMWORDS, NUMCHARS etc.
// Also, these values can also be viewed in Microsoft Word by navigating File > Properties > Advanced Properties > Statistics
// Page count: The PageCount property shows the page count in real time and its value can be assigned to the Pages property
// The "Pages" property stores the page count of the document.
Assert.assertEquals(6, properties.getPages());
// The "Words", "Characters", and "CharactersWithSpaces" built-in properties also display various document statistics,
// but we need to call the "UpdateWordCount" method on the whole document before we can expect them to contain accurate values.
doc.updateWordCount();
Assert.assertEquals(1035, properties.getWords());
Assert.assertEquals(6026, properties.getCharacters());
Assert.assertEquals(7041, properties.getCharactersWithSpaces());
// Count the number of lines in the document, and then assign the result to the "Lines" built-in property.
LineCounter lineCounter = new LineCounter(doc);
properties.setLines(lineCounter.getLineCount());
Assert.assertEquals(142, properties.getLines());
// Assign the number of Paragraph nodes in the document to the "Paragraphs" built-in property.
properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount());
Assert.assertEquals(29, properties.getParagraphs());
// Get an estimate of the file size of our document via the "Bytes" built-in property.
Assert.assertEquals(20310, properties.getBytes());
// Set a different template for our document, and then update the "Template" built-in property manually to reflect this change.
doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx");
Assert.assertEquals("Normal", properties.getTemplate());
properties.setTemplate(doc.getAttachedTemplate());
// "ContentStatus" is a descriptive built-in property.
properties.setContentStatus("Draft");
// Upon saving, the "ContentType" built-in property will contain the MIME type of the output save format.
Assert.assertEquals("", properties.getContentType());
// If the document contains links, and they are all up to date, we can set the "LinksUpToDate" property to "true".
Assert.assertFalse(properties.getLinksUpToDate());
doc.save(getArtifactsDir() + "DocumentProperties.Content.docx");
}
///
/// Counts the lines in a document.
/// Traverses the document's layout entities tree upon construction,
/// counting entities of the "Line" type that also contain real text.
///
private static class LineCounter {
public LineCounter(Document doc) throws Exception {
mLayoutEnumerator = new LayoutEnumerator(doc);
countLines();
}
public int getLineCount() {
return mLineCount;
}
private void countLines() throws Exception {
do {
if (mLayoutEnumerator.getType() == LayoutEntityType.LINE) {
mScanningLineForRealText = true;
}
if (mLayoutEnumerator.moveFirstChild()) {
if (mScanningLineForRealText && mLayoutEnumerator.getKind().startsWith("TEXT")) {
mLineCount++;
mScanningLineForRealText = false;
}
countLines();
mLayoutEnumerator.moveParent();
}
} while (mLayoutEnumerator.moveNext());
}
private final LayoutEnumerator mLayoutEnumerator;
private int mLineCount;
private boolean mScanningLineForRealText;
}
Returns: java.lang.String - The content status of the document.
getContentType()
public String getContentType()
Gets the content type of the document.
Examples:
Shows how to work with document properties in the “Content” category.
public void content() throws Exception {
Document doc = new Document(getMyDir() + "Paragraphs.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// By using built in properties,
// we can treat document statistics such as word/page/character counts as metadata that can be glanced at without opening the document
// These properties are accessed by right clicking the file in Windows Explorer and navigating to Properties > Details > Content
// If we want to display this data inside the document, we can use fields such as NUMPAGES, NUMWORDS, NUMCHARS etc.
// Also, these values can also be viewed in Microsoft Word by navigating File > Properties > Advanced Properties > Statistics
// Page count: The PageCount property shows the page count in real time and its value can be assigned to the Pages property
// The "Pages" property stores the page count of the document.
Assert.assertEquals(6, properties.getPages());
// The "Words", "Characters", and "CharactersWithSpaces" built-in properties also display various document statistics,
// but we need to call the "UpdateWordCount" method on the whole document before we can expect them to contain accurate values.
doc.updateWordCount();
Assert.assertEquals(1035, properties.getWords());
Assert.assertEquals(6026, properties.getCharacters());
Assert.assertEquals(7041, properties.getCharactersWithSpaces());
// Count the number of lines in the document, and then assign the result to the "Lines" built-in property.
LineCounter lineCounter = new LineCounter(doc);
properties.setLines(lineCounter.getLineCount());
Assert.assertEquals(142, properties.getLines());
// Assign the number of Paragraph nodes in the document to the "Paragraphs" built-in property.
properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount());
Assert.assertEquals(29, properties.getParagraphs());
// Get an estimate of the file size of our document via the "Bytes" built-in property.
Assert.assertEquals(20310, properties.getBytes());
// Set a different template for our document, and then update the "Template" built-in property manually to reflect this change.
doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx");
Assert.assertEquals("Normal", properties.getTemplate());
properties.setTemplate(doc.getAttachedTemplate());
// "ContentStatus" is a descriptive built-in property.
properties.setContentStatus("Draft");
// Upon saving, the "ContentType" built-in property will contain the MIME type of the output save format.
Assert.assertEquals("", properties.getContentType());
// If the document contains links, and they are all up to date, we can set the "LinksUpToDate" property to "true".
Assert.assertFalse(properties.getLinksUpToDate());
doc.save(getArtifactsDir() + "DocumentProperties.Content.docx");
}
///
/// Counts the lines in a document.
/// Traverses the document's layout entities tree upon construction,
/// counting entities of the "Line" type that also contain real text.
///
private static class LineCounter {
public LineCounter(Document doc) throws Exception {
mLayoutEnumerator = new LayoutEnumerator(doc);
countLines();
}
public int getLineCount() {
return mLineCount;
}
private void countLines() throws Exception {
do {
if (mLayoutEnumerator.getType() == LayoutEntityType.LINE) {
mScanningLineForRealText = true;
}
if (mLayoutEnumerator.moveFirstChild()) {
if (mScanningLineForRealText && mLayoutEnumerator.getKind().startsWith("TEXT")) {
mLineCount++;
mScanningLineForRealText = false;
}
countLines();
mLayoutEnumerator.moveParent();
}
} while (mLayoutEnumerator.moveNext());
}
private final LayoutEnumerator mLayoutEnumerator;
private int mLineCount;
private boolean mScanningLineForRealText;
}
Returns: java.lang.String - The content type of the document.
getCount()
public int getCount()
Gets number of items in the collection.
Examples:
Shows how to work with custom document properties.
Document doc = new Document(getMyDir() + "Properties.docx");
// Every document contains a collection of custom properties, which, like the built-in properties, are key-value pairs.
// The document has a fixed list of built-in properties. The user creates all of the custom properties.
Assert.assertEquals("Value of custom document property", doc.getCustomDocumentProperties().get("CustomProperty").toString());
doc.getCustomDocumentProperties().add("CustomProperty2", "Value of custom document property #2");
System.out.println("Custom Properties:");
for (DocumentProperty customDocumentProperty : doc.getCustomDocumentProperties()) {
System.out.println(customDocumentProperty.getName());
System.out.println(MessageFormat.format("\tType:\t{0}", customDocumentProperty.getType()));
System.out.println(MessageFormat.format("\tValue:\t\"{0}\"", customDocumentProperty.getValue()));
}
Returns: int - Number of items in the collection.
getCreatedTime()
public Date getCreatedTime()
Gets date of the document creation in UTC.
Remarks:
For documents originated from RTF format this property returns local time of the author’s machine at the moment of document creation.
Aspose.Words does not update this property.
Examples:
Shows how to work with document properties in the “Origin” category.
// Open a document that we have created and edited using Microsoft Word.
Document doc = new Document(getMyDir() + "Properties.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// The following built-in properties contain information regarding the creation and editing of this document.
// We can right-click this document in Windows Explorer and find
// these properties via "Properties" -> "Details" -> "Origin" category.
// Fields such as PRINTDATE and EDITTIME can display these values in the document body.
System.out.println(MessageFormat.format("Created using {0}, on {1}", properties.getNameOfApplication(), properties.getCreatedTime()));
System.out.println(MessageFormat.format("Minutes spent editing: {0}", properties.getTotalEditingTime()));
System.out.println(MessageFormat.format("Date/time last printed: {0}", properties.getLastPrinted()));
System.out.println(MessageFormat.format("Template document: {0}", properties.getTemplate()));
// We can also change the values of built-in properties.
properties.setCompany("Doe Ltd.");
properties.setManager("Jane Doe");
properties.setVersion(5);
properties.setRevisionNumber(properties.getRevisionNumber() + 1);
// Microsoft Word updates the following properties automatically when we save the document.
// To use these properties with Aspose.Words, we will need to set values for them manually.
properties.setLastSavedBy("John Doe");
properties.setLastSavedTime(new Date());
// We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details" -> "Origin".
doc.save(getArtifactsDir() + "DocumentProperties.Origin.docx");
Returns: java.util.Date - Date of the document creation in UTC.
getHeadingPairs()
public Object[] getHeadingPairs()
Specifies document headings and their names.
Remarks:
Every heading pair occupies two elements in this array.
The first element of the pair is a java.lang.String and specifies the heading name. The second element of the pair is an int and specifies the count of document parts for this heading in the getTitlesOfParts() / setTitlesOfParts(java.lang.String[]) property.
The total sum of counts for all heading pairs in this property must be equal to the number of elements in the getTitlesOfParts() / setTitlesOfParts(java.lang.String[]) property.
Aspose.Words does not update this property.
Examples:
Shows the relationship between “HeadingPairs” and “TitlesOfParts” properties.
Document doc = new Document(getMyDir() + "Heading pairs and titles of parts.docx");
// We can find the combined values of these collections via
// "File" -> "Properties" -> "Advanced Properties" -> "Contents" tab.
// The HeadingPairs property is a collection of pairs that
// determines how many document parts a heading spans across.
Object[] headingPairs = doc.getBuiltInDocumentProperties().getHeadingPairs();
// The TitlesOfParts property contains the names of parts that belong to the above headings.
String[] titlesOfParts = doc.getBuiltInDocumentProperties().getTitlesOfParts();
int headingPairsIndex = 0;
int titlesOfPartsIndex = 0;
while (headingPairsIndex < headingPairs.length) {
System.out.println(MessageFormat.format("Parts for {0}:", headingPairs[headingPairsIndex++]));
int partsCount = (int) headingPairs[headingPairsIndex++];
for (int i = 0; i < partsCount; i++)
System.out.println(MessageFormat.format("\t\"{0}\"", titlesOfParts[titlesOfPartsIndex++]));
}
Returns: java.lang.Object[] - The corresponding java.lang.Object[] value.
getHyperlinkBase()
public String getHyperlinkBase()
Specifies the base string used for evaluating relative hyperlinks in this document.
Remarks:
Aspose.Words does not use this property.
Examples:
Shows how to store the base part of a hyperlink in the document’s properties.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a relative hyperlink to a document in the local file system named "Document.docx".
// Clicking on the link in Microsoft Word will open the designated document, if it is available.
builder.insertHyperlink("Relative hyperlink", "Document.docx", false);
// This link is relative. If there is no "Document.docx" in the same folder
// as the document that contains this link, the link will be broken.
Assert.assertFalse(new File(getArtifactsDir() + "Document.docx").exists());
doc.save(getArtifactsDir() + "DocumentProperties.HyperlinkBase.BrokenLink.docx");
// The document we are trying to link to is in a different directory to the one we are planning to save the document in.
// We could fix links like this by putting an absolute filename in each one.
// Alternatively, we could provide a base link that every hyperlink with a relative filename
// will prepend to its link when we click on it.
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
properties.setHyperlinkBase(getMyDir());
Assert.assertTrue(new File(properties.getHyperlinkBase() + ((FieldHyperlink) doc.getRange().getFields().get(0)).getAddress()).exists());
doc.save(getArtifactsDir() + "DocumentProperties.HyperlinkBase.WorkingLink.docx");
Returns: java.lang.String - The corresponding java.lang.String value.
getHyperlinksChanged()
public boolean getHyperlinksChanged()
Indicates whether hyperlinks in a document were changed.
Remarks:
Aspose.Words does not update this property.
Examples:
Shows how to get extended properties.
Document doc = new Document(getMyDir() + "Extended properties.docx");
Assert.assertTrue(doc.getBuiltInDocumentProperties().getScaleCrop());
Assert.assertTrue(doc.getBuiltInDocumentProperties().getSharedDocument());
Assert.assertTrue(doc.getBuiltInDocumentProperties().getHyperlinksChanged());
Returns: boolean - The corresponding boolean value.
getKeywords()
public String getKeywords()
Gets the document keywords.
Examples:
Shows how to work with built-in document properties in the “Description” category.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// Below are four built-in document properties that have fields that can display their values in the document body.
// 1 - "Author" property, which we can display using an AUTHOR field:
properties.setAuthor("John Doe");
builder.write("Author:\t");
builder.insertField(FieldType.FIELD_AUTHOR, true);
// 2 - "Title" property, which we can display using a TITLE field:
properties.setTitle("John's Document");
builder.write("\nDoc title:\t");
builder.insertField(FieldType.FIELD_TITLE, true);
// 3 - "Subject" property, which we can display using a SUBJECT field:
properties.setSubject("My subject");
builder.write("\nSubject:\t");
builder.insertField(FieldType.FIELD_SUBJECT, true);
// 4 - "Comments" property, which we can display using a COMMENTS field:
properties.setComments(MessageFormat.format("This is {0}''s document about {1}", properties.getAuthor(), properties.getSubject()));
builder.write("\nComments:\t\"");
builder.insertField(FieldType.FIELD_COMMENTS, true);
builder.write("\"");
// The "Category" built-in property does not have a field that can display its value.
properties.setCategory("My category");
// We can set multiple keywords for a document by separating the string value of the "Keywords" property with semicolons.
properties.setKeywords("Tag 1; Tag 2; Tag 3");
// We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details".
// The "Author" built-in property is in the "Origin" group, and the others are in the "Description" group.
doc.save(getArtifactsDir() + "DocumentProperties.Description.docx");
Returns: java.lang.String - The document keywords.
getLastPrinted()
public Date getLastPrinted()
Gets the date when the document was last printed in UTC.
Remarks:
For documents originated from RTF format this property returns the local time of last print operation.
If the document was never printed, this property will return DateTime.MinValue.
Aspose.Words does not update this property.
Examples:
Shows how to work with document properties in the “Origin” category.
// Open a document that we have created and edited using Microsoft Word.
Document doc = new Document(getMyDir() + "Properties.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// The following built-in properties contain information regarding the creation and editing of this document.
// We can right-click this document in Windows Explorer and find
// these properties via "Properties" -> "Details" -> "Origin" category.
// Fields such as PRINTDATE and EDITTIME can display these values in the document body.
System.out.println(MessageFormat.format("Created using {0}, on {1}", properties.getNameOfApplication(), properties.getCreatedTime()));
System.out.println(MessageFormat.format("Minutes spent editing: {0}", properties.getTotalEditingTime()));
System.out.println(MessageFormat.format("Date/time last printed: {0}", properties.getLastPrinted()));
System.out.println(MessageFormat.format("Template document: {0}", properties.getTemplate()));
// We can also change the values of built-in properties.
properties.setCompany("Doe Ltd.");
properties.setManager("Jane Doe");
properties.setVersion(5);
properties.setRevisionNumber(properties.getRevisionNumber() + 1);
// Microsoft Word updates the following properties automatically when we save the document.
// To use these properties with Aspose.Words, we will need to set values for them manually.
properties.setLastSavedBy("John Doe");
properties.setLastSavedTime(new Date());
// We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details" -> "Origin".
doc.save(getArtifactsDir() + "DocumentProperties.Origin.docx");
Returns: java.util.Date - The date when the document was last printed in UTC.
getLastSavedBy()
public String getLastSavedBy()
Gets the name of the last author.
Remarks:
Aspose.Words does not update this property.
Examples:
Shows how to work with document properties in the “Origin” category.
// Open a document that we have created and edited using Microsoft Word.
Document doc = new Document(getMyDir() + "Properties.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// The following built-in properties contain information regarding the creation and editing of this document.
// We can right-click this document in Windows Explorer and find
// these properties via "Properties" -> "Details" -> "Origin" category.
// Fields such as PRINTDATE and EDITTIME can display these values in the document body.
System.out.println(MessageFormat.format("Created using {0}, on {1}", properties.getNameOfApplication(), properties.getCreatedTime()));
System.out.println(MessageFormat.format("Minutes spent editing: {0}", properties.getTotalEditingTime()));
System.out.println(MessageFormat.format("Date/time last printed: {0}", properties.getLastPrinted()));
System.out.println(MessageFormat.format("Template document: {0}", properties.getTemplate()));
// We can also change the values of built-in properties.
properties.setCompany("Doe Ltd.");
properties.setManager("Jane Doe");
properties.setVersion(5);
properties.setRevisionNumber(properties.getRevisionNumber() + 1);
// Microsoft Word updates the following properties automatically when we save the document.
// To use these properties with Aspose.Words, we will need to set values for them manually.
properties.setLastSavedBy("John Doe");
properties.setLastSavedTime(new Date());
// We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details" -> "Origin".
doc.save(getArtifactsDir() + "DocumentProperties.Origin.docx");
Returns: java.lang.String - The name of the last author.
getLastSavedTime()
public Date getLastSavedTime()
Gets the time of the last save in UTC.
Remarks:
For documents originated from RTF format this property returns the local time of last save operation.
Aspose.Words does not update this property.
Examples:
Shows how to work with document properties in the “Origin” category.
// Open a document that we have created and edited using Microsoft Word.
Document doc = new Document(getMyDir() + "Properties.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// The following built-in properties contain information regarding the creation and editing of this document.
// We can right-click this document in Windows Explorer and find
// these properties via "Properties" -> "Details" -> "Origin" category.
// Fields such as PRINTDATE and EDITTIME can display these values in the document body.
System.out.println(MessageFormat.format("Created using {0}, on {1}", properties.getNameOfApplication(), properties.getCreatedTime()));
System.out.println(MessageFormat.format("Minutes spent editing: {0}", properties.getTotalEditingTime()));
System.out.println(MessageFormat.format("Date/time last printed: {0}", properties.getLastPrinted()));
System.out.println(MessageFormat.format("Template document: {0}", properties.getTemplate()));
// We can also change the values of built-in properties.
properties.setCompany("Doe Ltd.");
properties.setManager("Jane Doe");
properties.setVersion(5);
properties.setRevisionNumber(properties.getRevisionNumber() + 1);
// Microsoft Word updates the following properties automatically when we save the document.
// To use these properties with Aspose.Words, we will need to set values for them manually.
properties.setLastSavedBy("John Doe");
properties.setLastSavedTime(new Date());
// We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details" -> "Origin".
doc.save(getArtifactsDir() + "DocumentProperties.Origin.docx");
Shows how to use the SAVEDATE field to display the date/time of the document’s most recent save operation performed using Microsoft Word.
Document doc = new Document(getMyDir() + "Document.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToDocumentEnd();
builder.writeln(" Date this document was last saved:");
// We can use the SAVEDATE field to display the last save operation's date and time on the document.
// The save operation that these fields refer to is the manual save in an application like Microsoft Word,
// not the document's Save method.
// Below are three different calendar types according to which the SAVEDATE field can display the date/time.
// 1 - Islamic Lunar Calendar:
builder.write("According to the Lunar Calendar - ");
FieldSaveDate field = (FieldSaveDate) builder.insertField(FieldType.FIELD_SAVE_DATE, true);
field.setUseLunarCalendar(true);
Assert.assertEquals(" SAVEDATE \\h", field.getFieldCode());
// 2 - Umm al-Qura calendar:
builder.write("\nAccording to the Umm al-Qura calendar - ");
field = (FieldSaveDate) builder.insertField(FieldType.FIELD_SAVE_DATE, true);
field.setUseUmAlQuraCalendar(true);
Assert.assertEquals(" SAVEDATE \\u", field.getFieldCode());
// 3 - Indian National calendar:
builder.write("\nAccording to the Indian National calendar - ");
field = (FieldSaveDate) builder.insertField(FieldType.FIELD_SAVE_DATE, true);
field.setUseSakaEraCalendar(true);
Assert.assertEquals(" SAVEDATE \\s", field.getFieldCode());
// The SAVEDATE fields draw their date/time values from the LastSavedTime built-in property.
// The document's Save method will not update this value, but we can still update it manually.
doc.getBuiltInDocumentProperties().setLastSavedTime(new Date());
doc.updateFields();
doc.save(getArtifactsDir() + "Field.SAVEDATE.docx");
Returns: java.util.Date - The time of the last save in UTC.
getLines()
public int getLines()
Represents an estimate of the number of lines in the document.
Remarks:
Aspose.Words updates this property when you call Document.updateWordCount(boolean).
Examples:
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());
Shows how to work with document properties in the “Content” category.
public void content() throws Exception {
Document doc = new Document(getMyDir() + "Paragraphs.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// By using built in properties,
// we can treat document statistics such as word/page/character counts as metadata that can be glanced at without opening the document
// These properties are accessed by right clicking the file in Windows Explorer and navigating to Properties > Details > Content
// If we want to display this data inside the document, we can use fields such as NUMPAGES, NUMWORDS, NUMCHARS etc.
// Also, these values can also be viewed in Microsoft Word by navigating File > Properties > Advanced Properties > Statistics
// Page count: The PageCount property shows the page count in real time and its value can be assigned to the Pages property
// The "Pages" property stores the page count of the document.
Assert.assertEquals(6, properties.getPages());
// The "Words", "Characters", and "CharactersWithSpaces" built-in properties also display various document statistics,
// but we need to call the "UpdateWordCount" method on the whole document before we can expect them to contain accurate values.
doc.updateWordCount();
Assert.assertEquals(1035, properties.getWords());
Assert.assertEquals(6026, properties.getCharacters());
Assert.assertEquals(7041, properties.getCharactersWithSpaces());
// Count the number of lines in the document, and then assign the result to the "Lines" built-in property.
LineCounter lineCounter = new LineCounter(doc);
properties.setLines(lineCounter.getLineCount());
Assert.assertEquals(142, properties.getLines());
// Assign the number of Paragraph nodes in the document to the "Paragraphs" built-in property.
properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount());
Assert.assertEquals(29, properties.getParagraphs());
// Get an estimate of the file size of our document via the "Bytes" built-in property.
Assert.assertEquals(20310, properties.getBytes());
// Set a different template for our document, and then update the "Template" built-in property manually to reflect this change.
doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx");
Assert.assertEquals("Normal", properties.getTemplate());
properties.setTemplate(doc.getAttachedTemplate());
// "ContentStatus" is a descriptive built-in property.
properties.setContentStatus("Draft");
// Upon saving, the "ContentType" built-in property will contain the MIME type of the output save format.
Assert.assertEquals("", properties.getContentType());
// If the document contains links, and they are all up to date, we can set the "LinksUpToDate" property to "true".
Assert.assertFalse(properties.getLinksUpToDate());
doc.save(getArtifactsDir() + "DocumentProperties.Content.docx");
}
///
/// Counts the lines in a document.
/// Traverses the document's layout entities tree upon construction,
/// counting entities of the "Line" type that also contain real text.
///
private static class LineCounter {
public LineCounter(Document doc) throws Exception {
mLayoutEnumerator = new LayoutEnumerator(doc);
countLines();
}
public int getLineCount() {
return mLineCount;
}
private void countLines() throws Exception {
do {
if (mLayoutEnumerator.getType() == LayoutEntityType.LINE) {
mScanningLineForRealText = true;
}
if (mLayoutEnumerator.moveFirstChild()) {
if (mScanningLineForRealText && mLayoutEnumerator.getKind().startsWith("TEXT")) {
mLineCount++;
mScanningLineForRealText = false;
}
countLines();
mLayoutEnumerator.moveParent();
}
} while (mLayoutEnumerator.moveNext());
}
private final LayoutEnumerator mLayoutEnumerator;
private int mLineCount;
private boolean mScanningLineForRealText;
}
Returns: int - The corresponding int value.
getLinksUpToDate()
public boolean getLinksUpToDate()
Indicates whether hyperlinks in a document are up-to-date.
Remarks:
Aspose.Words does not update this property.
Examples:
Shows how to work with document properties in the “Content” category.
public void content() throws Exception {
Document doc = new Document(getMyDir() + "Paragraphs.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// By using built in properties,
// we can treat document statistics such as word/page/character counts as metadata that can be glanced at without opening the document
// These properties are accessed by right clicking the file in Windows Explorer and navigating to Properties > Details > Content
// If we want to display this data inside the document, we can use fields such as NUMPAGES, NUMWORDS, NUMCHARS etc.
// Also, these values can also be viewed in Microsoft Word by navigating File > Properties > Advanced Properties > Statistics
// Page count: The PageCount property shows the page count in real time and its value can be assigned to the Pages property
// The "Pages" property stores the page count of the document.
Assert.assertEquals(6, properties.getPages());
// The "Words", "Characters", and "CharactersWithSpaces" built-in properties also display various document statistics,
// but we need to call the "UpdateWordCount" method on the whole document before we can expect them to contain accurate values.
doc.updateWordCount();
Assert.assertEquals(1035, properties.getWords());
Assert.assertEquals(6026, properties.getCharacters());
Assert.assertEquals(7041, properties.getCharactersWithSpaces());
// Count the number of lines in the document, and then assign the result to the "Lines" built-in property.
LineCounter lineCounter = new LineCounter(doc);
properties.setLines(lineCounter.getLineCount());
Assert.assertEquals(142, properties.getLines());
// Assign the number of Paragraph nodes in the document to the "Paragraphs" built-in property.
properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount());
Assert.assertEquals(29, properties.getParagraphs());
// Get an estimate of the file size of our document via the "Bytes" built-in property.
Assert.assertEquals(20310, properties.getBytes());
// Set a different template for our document, and then update the "Template" built-in property manually to reflect this change.
doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx");
Assert.assertEquals("Normal", properties.getTemplate());
properties.setTemplate(doc.getAttachedTemplate());
// "ContentStatus" is a descriptive built-in property.
properties.setContentStatus("Draft");
// Upon saving, the "ContentType" built-in property will contain the MIME type of the output save format.
Assert.assertEquals("", properties.getContentType());
// If the document contains links, and they are all up to date, we can set the "LinksUpToDate" property to "true".
Assert.assertFalse(properties.getLinksUpToDate());
doc.save(getArtifactsDir() + "DocumentProperties.Content.docx");
}
///
/// Counts the lines in a document.
/// Traverses the document's layout entities tree upon construction,
/// counting entities of the "Line" type that also contain real text.
///
private static class LineCounter {
public LineCounter(Document doc) throws Exception {
mLayoutEnumerator = new LayoutEnumerator(doc);
countLines();
}
public int getLineCount() {
return mLineCount;
}
private void countLines() throws Exception {
do {
if (mLayoutEnumerator.getType() == LayoutEntityType.LINE) {
mScanningLineForRealText = true;
}
if (mLayoutEnumerator.moveFirstChild()) {
if (mScanningLineForRealText && mLayoutEnumerator.getKind().startsWith("TEXT")) {
mLineCount++;
mScanningLineForRealText = false;
}
countLines();
mLayoutEnumerator.moveParent();
}
} while (mLayoutEnumerator.moveNext());
}
private final LayoutEnumerator mLayoutEnumerator;
private int mLineCount;
private boolean mScanningLineForRealText;
}
Returns: boolean - The corresponding boolean value.
getManager()
public String getManager()
Gets the manager property.
Examples:
Shows how to work with document properties in the “Origin” category.
// Open a document that we have created and edited using Microsoft Word.
Document doc = new Document(getMyDir() + "Properties.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// The following built-in properties contain information regarding the creation and editing of this document.
// We can right-click this document in Windows Explorer and find
// these properties via "Properties" -> "Details" -> "Origin" category.
// Fields such as PRINTDATE and EDITTIME can display these values in the document body.
System.out.println(MessageFormat.format("Created using {0}, on {1}", properties.getNameOfApplication(), properties.getCreatedTime()));
System.out.println(MessageFormat.format("Minutes spent editing: {0}", properties.getTotalEditingTime()));
System.out.println(MessageFormat.format("Date/time last printed: {0}", properties.getLastPrinted()));
System.out.println(MessageFormat.format("Template document: {0}", properties.getTemplate()));
// We can also change the values of built-in properties.
properties.setCompany("Doe Ltd.");
properties.setManager("Jane Doe");
properties.setVersion(5);
properties.setRevisionNumber(properties.getRevisionNumber() + 1);
// Microsoft Word updates the following properties automatically when we save the document.
// To use these properties with Aspose.Words, we will need to set values for them manually.
properties.setLastSavedBy("John Doe");
properties.setLastSavedTime(new Date());
// We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details" -> "Origin".
doc.save(getArtifactsDir() + "DocumentProperties.Origin.docx");
Returns: java.lang.String - The manager property.
getNameOfApplication()
public String getNameOfApplication()
Gets the name of the application.
Examples:
Shows how to work with document properties in the “Origin” category.
// Open a document that we have created and edited using Microsoft Word.
Document doc = new Document(getMyDir() + "Properties.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// The following built-in properties contain information regarding the creation and editing of this document.
// We can right-click this document in Windows Explorer and find
// these properties via "Properties" -> "Details" -> "Origin" category.
// Fields such as PRINTDATE and EDITTIME can display these values in the document body.
System.out.println(MessageFormat.format("Created using {0}, on {1}", properties.getNameOfApplication(), properties.getCreatedTime()));
System.out.println(MessageFormat.format("Minutes spent editing: {0}", properties.getTotalEditingTime()));
System.out.println(MessageFormat.format("Date/time last printed: {0}", properties.getLastPrinted()));
System.out.println(MessageFormat.format("Template document: {0}", properties.getTemplate()));
// We can also change the values of built-in properties.
properties.setCompany("Doe Ltd.");
properties.setManager("Jane Doe");
properties.setVersion(5);
properties.setRevisionNumber(properties.getRevisionNumber() + 1);
// Microsoft Word updates the following properties automatically when we save the document.
// To use these properties with Aspose.Words, we will need to set values for them manually.
properties.setLastSavedBy("John Doe");
properties.setLastSavedTime(new Date());
// We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details" -> "Origin".
doc.save(getArtifactsDir() + "DocumentProperties.Origin.docx");
Returns: java.lang.String - The name of the application.
getPages()
public int getPages()
Represents an estimate of the number of pages in the document.
Remarks:
Aspose.Words updates this property when you call Document.updatePageLayout().
Examples:
Shows how to work with document properties in the “Content” category.
public void content() throws Exception {
Document doc = new Document(getMyDir() + "Paragraphs.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// By using built in properties,
// we can treat document statistics such as word/page/character counts as metadata that can be glanced at without opening the document
// These properties are accessed by right clicking the file in Windows Explorer and navigating to Properties > Details > Content
// If we want to display this data inside the document, we can use fields such as NUMPAGES, NUMWORDS, NUMCHARS etc.
// Also, these values can also be viewed in Microsoft Word by navigating File > Properties > Advanced Properties > Statistics
// Page count: The PageCount property shows the page count in real time and its value can be assigned to the Pages property
// The "Pages" property stores the page count of the document.
Assert.assertEquals(6, properties.getPages());
// The "Words", "Characters", and "CharactersWithSpaces" built-in properties also display various document statistics,
// but we need to call the "UpdateWordCount" method on the whole document before we can expect them to contain accurate values.
doc.updateWordCount();
Assert.assertEquals(1035, properties.getWords());
Assert.assertEquals(6026, properties.getCharacters());
Assert.assertEquals(7041, properties.getCharactersWithSpaces());
// Count the number of lines in the document, and then assign the result to the "Lines" built-in property.
LineCounter lineCounter = new LineCounter(doc);
properties.setLines(lineCounter.getLineCount());
Assert.assertEquals(142, properties.getLines());
// Assign the number of Paragraph nodes in the document to the "Paragraphs" built-in property.
properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount());
Assert.assertEquals(29, properties.getParagraphs());
// Get an estimate of the file size of our document via the "Bytes" built-in property.
Assert.assertEquals(20310, properties.getBytes());
// Set a different template for our document, and then update the "Template" built-in property manually to reflect this change.
doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx");
Assert.assertEquals("Normal", properties.getTemplate());
properties.setTemplate(doc.getAttachedTemplate());
// "ContentStatus" is a descriptive built-in property.
properties.setContentStatus("Draft");
// Upon saving, the "ContentType" built-in property will contain the MIME type of the output save format.
Assert.assertEquals("", properties.getContentType());
// If the document contains links, and they are all up to date, we can set the "LinksUpToDate" property to "true".
Assert.assertFalse(properties.getLinksUpToDate());
doc.save(getArtifactsDir() + "DocumentProperties.Content.docx");
}
///
/// Counts the lines in a document.
/// Traverses the document's layout entities tree upon construction,
/// counting entities of the "Line" type that also contain real text.
///
private static class LineCounter {
public LineCounter(Document doc) throws Exception {
mLayoutEnumerator = new LayoutEnumerator(doc);
countLines();
}
public int getLineCount() {
return mLineCount;
}
private void countLines() throws Exception {
do {
if (mLayoutEnumerator.getType() == LayoutEntityType.LINE) {
mScanningLineForRealText = true;
}
if (mLayoutEnumerator.moveFirstChild()) {
if (mScanningLineForRealText && mLayoutEnumerator.getKind().startsWith("TEXT")) {
mLineCount++;
mScanningLineForRealText = false;
}
countLines();
mLayoutEnumerator.moveParent();
}
} while (mLayoutEnumerator.moveNext());
}
private final LayoutEnumerator mLayoutEnumerator;
private int mLineCount;
private boolean mScanningLineForRealText;
}
Returns: int - The corresponding int value.
getParagraphs()
public int getParagraphs()
Represents an estimate of the number of paragraphs in the document.
Remarks:
Aspose.Words updates this property when you call Document.updateWordCount().
Examples:
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());
Shows how to work with document properties in the “Content” category.
public void content() throws Exception {
Document doc = new Document(getMyDir() + "Paragraphs.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// By using built in properties,
// we can treat document statistics such as word/page/character counts as metadata that can be glanced at without opening the document
// These properties are accessed by right clicking the file in Windows Explorer and navigating to Properties > Details > Content
// If we want to display this data inside the document, we can use fields such as NUMPAGES, NUMWORDS, NUMCHARS etc.
// Also, these values can also be viewed in Microsoft Word by navigating File > Properties > Advanced Properties > Statistics
// Page count: The PageCount property shows the page count in real time and its value can be assigned to the Pages property
// The "Pages" property stores the page count of the document.
Assert.assertEquals(6, properties.getPages());
// The "Words", "Characters", and "CharactersWithSpaces" built-in properties also display various document statistics,
// but we need to call the "UpdateWordCount" method on the whole document before we can expect them to contain accurate values.
doc.updateWordCount();
Assert.assertEquals(1035, properties.getWords());
Assert.assertEquals(6026, properties.getCharacters());
Assert.assertEquals(7041, properties.getCharactersWithSpaces());
// Count the number of lines in the document, and then assign the result to the "Lines" built-in property.
LineCounter lineCounter = new LineCounter(doc);
properties.setLines(lineCounter.getLineCount());
Assert.assertEquals(142, properties.getLines());
// Assign the number of Paragraph nodes in the document to the "Paragraphs" built-in property.
properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount());
Assert.assertEquals(29, properties.getParagraphs());
// Get an estimate of the file size of our document via the "Bytes" built-in property.
Assert.assertEquals(20310, properties.getBytes());
// Set a different template for our document, and then update the "Template" built-in property manually to reflect this change.
doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx");
Assert.assertEquals("Normal", properties.getTemplate());
properties.setTemplate(doc.getAttachedTemplate());
// "ContentStatus" is a descriptive built-in property.
properties.setContentStatus("Draft");
// Upon saving, the "ContentType" built-in property will contain the MIME type of the output save format.
Assert.assertEquals("", properties.getContentType());
// If the document contains links, and they are all up to date, we can set the "LinksUpToDate" property to "true".
Assert.assertFalse(properties.getLinksUpToDate());
doc.save(getArtifactsDir() + "DocumentProperties.Content.docx");
}
///
/// Counts the lines in a document.
/// Traverses the document's layout entities tree upon construction,
/// counting entities of the "Line" type that also contain real text.
///
private static class LineCounter {
public LineCounter(Document doc) throws Exception {
mLayoutEnumerator = new LayoutEnumerator(doc);
countLines();
}
public int getLineCount() {
return mLineCount;
}
private void countLines() throws Exception {
do {
if (mLayoutEnumerator.getType() == LayoutEntityType.LINE) {
mScanningLineForRealText = true;
}
if (mLayoutEnumerator.moveFirstChild()) {
if (mScanningLineForRealText && mLayoutEnumerator.getKind().startsWith("TEXT")) {
mLineCount++;
mScanningLineForRealText = false;
}
countLines();
mLayoutEnumerator.moveParent();
}
} while (mLayoutEnumerator.moveNext());
}
private final LayoutEnumerator mLayoutEnumerator;
private int mLineCount;
private boolean mScanningLineForRealText;
}
Returns: int - The corresponding int value.
getRevisionNumber()
public int getRevisionNumber()
Gets the document revision number.
Remarks:
Aspose.Words does not update this property.
Examples:
Shows how to work with REVNUM fields.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("Current revision #");
// Insert a REVNUM field, which displays the document's current revision number property.
FieldRevNum field = (FieldRevNum) builder.insertField(FieldType.FIELD_REVISION_NUM, true);
Assert.assertEquals(" REVNUM ", field.getFieldCode());
Assert.assertEquals("1", field.getResult());
Assert.assertEquals(1, doc.getBuiltInDocumentProperties().getRevisionNumber());
// This property counts how many times a document has been saved in Microsoft Word,
// and is unrelated to tracked revisions. We can find it by right clicking the document in Windows Explorer
// via Properties -> Details. We can update this property manually.
doc.getBuiltInDocumentProperties().setRevisionNumber(doc.getBuiltInDocumentProperties().getRevisionNumber() + 1);
field.update();
Assert.assertEquals("2", field.getResult());
Shows how to work with document properties in the “Origin” category.
// Open a document that we have created and edited using Microsoft Word.
Document doc = new Document(getMyDir() + "Properties.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// The following built-in properties contain information regarding the creation and editing of this document.
// We can right-click this document in Windows Explorer and find
// these properties via "Properties" -> "Details" -> "Origin" category.
// Fields such as PRINTDATE and EDITTIME can display these values in the document body.
System.out.println(MessageFormat.format("Created using {0}, on {1}", properties.getNameOfApplication(), properties.getCreatedTime()));
System.out.println(MessageFormat.format("Minutes spent editing: {0}", properties.getTotalEditingTime()));
System.out.println(MessageFormat.format("Date/time last printed: {0}", properties.getLastPrinted()));
System.out.println(MessageFormat.format("Template document: {0}", properties.getTemplate()));
// We can also change the values of built-in properties.
properties.setCompany("Doe Ltd.");
properties.setManager("Jane Doe");
properties.setVersion(5);
properties.setRevisionNumber(properties.getRevisionNumber() + 1);
// Microsoft Word updates the following properties automatically when we save the document.
// To use these properties with Aspose.Words, we will need to set values for them manually.
properties.setLastSavedBy("John Doe");
properties.setLastSavedTime(new Date());
// We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details" -> "Origin".
doc.save(getArtifactsDir() + "DocumentProperties.Origin.docx");
Returns: int - The document revision number.
getScaleCrop()
public boolean getScaleCrop()
Indicates whether document thumbnail is cropped or scaled to fit the display.
Remarks:
Aspose.Words does not update this property.
Examples:
Shows how to get extended properties.
Document doc = new Document(getMyDir() + "Extended properties.docx");
Assert.assertTrue(doc.getBuiltInDocumentProperties().getScaleCrop());
Assert.assertTrue(doc.getBuiltInDocumentProperties().getSharedDocument());
Assert.assertTrue(doc.getBuiltInDocumentProperties().getHyperlinksChanged());
Returns: boolean - The corresponding boolean value.
getSecurity()
public int getSecurity()
Specifies the security level of a document as a numeric value.
Remarks:
Use this property for informational purposes only because Microsoft Word does not always set this property. This property is available in DOC and OOXML documents only.
To protect or unprotect a document use the M:Aspose.Words.Document.Protect(Aspose.Words.ProtectionType,System.String) and Document.unprotect() methods.
Aspose.Words updates this property to a correct value before saving a document.
Examples:
Shows how to use document properties to display the security level of a document.
Document doc = new Document();
Assert.assertEquals(DocumentSecurity.NONE, doc.getBuiltInDocumentProperties().getSecurity());
// If we configure a document to be read-only, it will display this status using the "Security" built-in property.
doc.getWriteProtection().setReadOnlyRecommended(true);
doc.save(getArtifactsDir() + "DocumentProperties.Security.ReadOnlyRecommended.docx");
Assert.assertEquals(DocumentSecurity.READ_ONLY_RECOMMENDED,
new Document(getArtifactsDir() + "DocumentProperties.Security.ReadOnlyRecommended.docx").getBuiltInDocumentProperties().getSecurity());
// Write-protect a document, and then verify its security level.
doc = new Document();
Assert.assertFalse(doc.getWriteProtection().isWriteProtected());
doc.getWriteProtection().setPassword("MyPassword");
Assert.assertTrue(doc.getWriteProtection().validatePassword("MyPassword"));
Assert.assertTrue(doc.getWriteProtection().isWriteProtected());
doc.save(getArtifactsDir() + "DocumentProperties.Security.ReadOnlyEnforced.docx");
Assert.assertEquals(DocumentSecurity.READ_ONLY_ENFORCED,
new Document(getArtifactsDir() + "DocumentProperties.Security.ReadOnlyEnforced.docx").getBuiltInDocumentProperties().getSecurity());
// "Security" is a descriptive property. We can edit its value manually.
doc = new Document();
doc.protect(ProtectionType.ALLOW_ONLY_COMMENTS, "MyPassword");
doc.getBuiltInDocumentProperties().setSecurity(DocumentSecurity.READ_ONLY_EXCEPT_ANNOTATIONS);
doc.save(getArtifactsDir() + "DocumentProperties.Security.ReadOnlyExceptAnnotations.docx");
Assert.assertEquals(DocumentSecurity.READ_ONLY_EXCEPT_ANNOTATIONS,
new Document(getArtifactsDir() + "DocumentProperties.Security.ReadOnlyExceptAnnotations.docx").getBuiltInDocumentProperties().getSecurity());
Returns: int - The corresponding int value. The returned value is a bitwise combination of DocumentSecurity constants.
getSharedDocument()
public boolean getSharedDocument()
Indicates whether the document is a shared document.
Remarks:
Aspose.Words does not update this property.
Examples:
Shows how to get extended properties.
Document doc = new Document(getMyDir() + "Extended properties.docx");
Assert.assertTrue(doc.getBuiltInDocumentProperties().getScaleCrop());
Assert.assertTrue(doc.getBuiltInDocumentProperties().getSharedDocument());
Assert.assertTrue(doc.getBuiltInDocumentProperties().getHyperlinksChanged());
Returns: boolean - The corresponding boolean value.
getSubject()
public String getSubject()
Gets the subject of the document.
Examples:
Shows how to work with built-in document properties in the “Description” category.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// Below are four built-in document properties that have fields that can display their values in the document body.
// 1 - "Author" property, which we can display using an AUTHOR field:
properties.setAuthor("John Doe");
builder.write("Author:\t");
builder.insertField(FieldType.FIELD_AUTHOR, true);
// 2 - "Title" property, which we can display using a TITLE field:
properties.setTitle("John's Document");
builder.write("\nDoc title:\t");
builder.insertField(FieldType.FIELD_TITLE, true);
// 3 - "Subject" property, which we can display using a SUBJECT field:
properties.setSubject("My subject");
builder.write("\nSubject:\t");
builder.insertField(FieldType.FIELD_SUBJECT, true);
// 4 - "Comments" property, which we can display using a COMMENTS field:
properties.setComments(MessageFormat.format("This is {0}''s document about {1}", properties.getAuthor(), properties.getSubject()));
builder.write("\nComments:\t\"");
builder.insertField(FieldType.FIELD_COMMENTS, true);
builder.write("\"");
// The "Category" built-in property does not have a field that can display its value.
properties.setCategory("My category");
// We can set multiple keywords for a document by separating the string value of the "Keywords" property with semicolons.
properties.setKeywords("Tag 1; Tag 2; Tag 3");
// We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details".
// The "Author" built-in property is in the "Origin" group, and the others are in the "Description" group.
doc.save(getArtifactsDir() + "DocumentProperties.Description.docx");
Returns: java.lang.String - The subject of the document.
getTemplate()
public String getTemplate()
Gets the informational name of the document template.
Remarks:
In Microsoft Word, this property is for informational purposes only and usually contains only the file name of the template without the path.
Empty string means the document is attached to the Normal template.
To get or set the actual name of the attached template, use the Document.getAttachedTemplate() / Document.setAttachedTemplate(java.lang.String) property.
Examples:
Shows how to work with document properties in the “Origin” category.
// Open a document that we have created and edited using Microsoft Word.
Document doc = new Document(getMyDir() + "Properties.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// The following built-in properties contain information regarding the creation and editing of this document.
// We can right-click this document in Windows Explorer and find
// these properties via "Properties" -> "Details" -> "Origin" category.
// Fields such as PRINTDATE and EDITTIME can display these values in the document body.
System.out.println(MessageFormat.format("Created using {0}, on {1}", properties.getNameOfApplication(), properties.getCreatedTime()));
System.out.println(MessageFormat.format("Minutes spent editing: {0}", properties.getTotalEditingTime()));
System.out.println(MessageFormat.format("Date/time last printed: {0}", properties.getLastPrinted()));
System.out.println(MessageFormat.format("Template document: {0}", properties.getTemplate()));
// We can also change the values of built-in properties.
properties.setCompany("Doe Ltd.");
properties.setManager("Jane Doe");
properties.setVersion(5);
properties.setRevisionNumber(properties.getRevisionNumber() + 1);
// Microsoft Word updates the following properties automatically when we save the document.
// To use these properties with Aspose.Words, we will need to set values for them manually.
properties.setLastSavedBy("John Doe");
properties.setLastSavedTime(new Date());
// We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details" -> "Origin".
doc.save(getArtifactsDir() + "DocumentProperties.Origin.docx");
Returns: java.lang.String - The informational name of the document template.
getThumbnail()
public byte[] getThumbnail()
Gets or sets the thumbnail of the document.
Returns: byte[] - The corresponding byte[] value.
getTitle()
public String getTitle()
Gets the title of the document.
Examples:
Shows how to work with built-in document properties in the “Description” category.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// Below are four built-in document properties that have fields that can display their values in the document body.
// 1 - "Author" property, which we can display using an AUTHOR field:
properties.setAuthor("John Doe");
builder.write("Author:\t");
builder.insertField(FieldType.FIELD_AUTHOR, true);
// 2 - "Title" property, which we can display using a TITLE field:
properties.setTitle("John's Document");
builder.write("\nDoc title:\t");
builder.insertField(FieldType.FIELD_TITLE, true);
// 3 - "Subject" property, which we can display using a SUBJECT field:
properties.setSubject("My subject");
builder.write("\nSubject:\t");
builder.insertField(FieldType.FIELD_SUBJECT, true);
// 4 - "Comments" property, which we can display using a COMMENTS field:
properties.setComments(MessageFormat.format("This is {0}''s document about {1}", properties.getAuthor(), properties.getSubject()));
builder.write("\nComments:\t\"");
builder.insertField(FieldType.FIELD_COMMENTS, true);
builder.write("\"");
// The "Category" built-in property does not have a field that can display its value.
properties.setCategory("My category");
// We can set multiple keywords for a document by separating the string value of the "Keywords" property with semicolons.
properties.setKeywords("Tag 1; Tag 2; Tag 3");
// We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details".
// The "Author" built-in property is in the "Origin" group, and the others are in the "Description" group.
doc.save(getArtifactsDir() + "DocumentProperties.Description.docx");
Returns: java.lang.String - The title of the document.
getTitlesOfParts()
public String[] getTitlesOfParts()
Each string in the array specifies the name of a part in the document.
Remarks:
Aspose.Words does not update this property.
Examples:
Shows the relationship between “HeadingPairs” and “TitlesOfParts” properties.
Document doc = new Document(getMyDir() + "Heading pairs and titles of parts.docx");
// We can find the combined values of these collections via
// "File" -> "Properties" -> "Advanced Properties" -> "Contents" tab.
// The HeadingPairs property is a collection of pairs that
// determines how many document parts a heading spans across.
Object[] headingPairs = doc.getBuiltInDocumentProperties().getHeadingPairs();
// The TitlesOfParts property contains the names of parts that belong to the above headings.
String[] titlesOfParts = doc.getBuiltInDocumentProperties().getTitlesOfParts();
int headingPairsIndex = 0;
int titlesOfPartsIndex = 0;
while (headingPairsIndex < headingPairs.length) {
System.out.println(MessageFormat.format("Parts for {0}:", headingPairs[headingPairsIndex++]));
int partsCount = (int) headingPairs[headingPairsIndex++];
for (int i = 0; i < partsCount; i++)
System.out.println(MessageFormat.format("\t\"{0}\"", titlesOfParts[titlesOfPartsIndex++]));
}
Returns: java.lang.String[] - The corresponding java.lang.String[] value.
getTotalEditingTime()
public int getTotalEditingTime()
Gets the total editing time in minutes.
Examples:
Shows how to work with document properties in the “Origin” category.
// Open a document that we have created and edited using Microsoft Word.
Document doc = new Document(getMyDir() + "Properties.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// The following built-in properties contain information regarding the creation and editing of this document.
// We can right-click this document in Windows Explorer and find
// these properties via "Properties" -> "Details" -> "Origin" category.
// Fields such as PRINTDATE and EDITTIME can display these values in the document body.
System.out.println(MessageFormat.format("Created using {0}, on {1}", properties.getNameOfApplication(), properties.getCreatedTime()));
System.out.println(MessageFormat.format("Minutes spent editing: {0}", properties.getTotalEditingTime()));
System.out.println(MessageFormat.format("Date/time last printed: {0}", properties.getLastPrinted()));
System.out.println(MessageFormat.format("Template document: {0}", properties.getTemplate()));
// We can also change the values of built-in properties.
properties.setCompany("Doe Ltd.");
properties.setManager("Jane Doe");
properties.setVersion(5);
properties.setRevisionNumber(properties.getRevisionNumber() + 1);
// Microsoft Word updates the following properties automatically when we save the document.
// To use these properties with Aspose.Words, we will need to set values for them manually.
properties.setLastSavedBy("John Doe");
properties.setLastSavedTime(new Date());
// We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details" -> "Origin".
doc.save(getArtifactsDir() + "DocumentProperties.Origin.docx");
Returns: int - The total editing time in minutes.
getVersion()
public int getVersion()
Represents the version number of the application that created the document.
Remarks:
When a document was created by Microsoft Word, then high 16 bit represent the major version and low 16 bit represent the build number.
Examples:
Shows how to work with document properties in the “Origin” category.
// Open a document that we have created and edited using Microsoft Word.
Document doc = new Document(getMyDir() + "Properties.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// The following built-in properties contain information regarding the creation and editing of this document.
// We can right-click this document in Windows Explorer and find
// these properties via "Properties" -> "Details" -> "Origin" category.
// Fields such as PRINTDATE and EDITTIME can display these values in the document body.
System.out.println(MessageFormat.format("Created using {0}, on {1}", properties.getNameOfApplication(), properties.getCreatedTime()));
System.out.println(MessageFormat.format("Minutes spent editing: {0}", properties.getTotalEditingTime()));
System.out.println(MessageFormat.format("Date/time last printed: {0}", properties.getLastPrinted()));
System.out.println(MessageFormat.format("Template document: {0}", properties.getTemplate()));
// We can also change the values of built-in properties.
properties.setCompany("Doe Ltd.");
properties.setManager("Jane Doe");
properties.setVersion(5);
properties.setRevisionNumber(properties.getRevisionNumber() + 1);
// Microsoft Word updates the following properties automatically when we save the document.
// To use these properties with Aspose.Words, we will need to set values for them manually.
properties.setLastSavedBy("John Doe");
properties.setLastSavedTime(new Date());
// We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details" -> "Origin".
doc.save(getArtifactsDir() + "DocumentProperties.Origin.docx");
Returns: int - The corresponding int value.
getWords()
public int getWords()
Represents an estimate of the number of words in the document.
Remarks:
Aspose.Words updates this property when you call Document.updateWordCount().
Examples:
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());
Shows how to work with document properties in the “Content” category.
public void content() throws Exception {
Document doc = new Document(getMyDir() + "Paragraphs.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// By using built in properties,
// we can treat document statistics such as word/page/character counts as metadata that can be glanced at without opening the document
// These properties are accessed by right clicking the file in Windows Explorer and navigating to Properties > Details > Content
// If we want to display this data inside the document, we can use fields such as NUMPAGES, NUMWORDS, NUMCHARS etc.
// Also, these values can also be viewed in Microsoft Word by navigating File > Properties > Advanced Properties > Statistics
// Page count: The PageCount property shows the page count in real time and its value can be assigned to the Pages property
// The "Pages" property stores the page count of the document.
Assert.assertEquals(6, properties.getPages());
// The "Words", "Characters", and "CharactersWithSpaces" built-in properties also display various document statistics,
// but we need to call the "UpdateWordCount" method on the whole document before we can expect them to contain accurate values.
doc.updateWordCount();
Assert.assertEquals(1035, properties.getWords());
Assert.assertEquals(6026, properties.getCharacters());
Assert.assertEquals(7041, properties.getCharactersWithSpaces());
// Count the number of lines in the document, and then assign the result to the "Lines" built-in property.
LineCounter lineCounter = new LineCounter(doc);
properties.setLines(lineCounter.getLineCount());
Assert.assertEquals(142, properties.getLines());
// Assign the number of Paragraph nodes in the document to the "Paragraphs" built-in property.
properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount());
Assert.assertEquals(29, properties.getParagraphs());
// Get an estimate of the file size of our document via the "Bytes" built-in property.
Assert.assertEquals(20310, properties.getBytes());
// Set a different template for our document, and then update the "Template" built-in property manually to reflect this change.
doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx");
Assert.assertEquals("Normal", properties.getTemplate());
properties.setTemplate(doc.getAttachedTemplate());
// "ContentStatus" is a descriptive built-in property.
properties.setContentStatus("Draft");
// Upon saving, the "ContentType" built-in property will contain the MIME type of the output save format.
Assert.assertEquals("", properties.getContentType());
// If the document contains links, and they are all up to date, we can set the "LinksUpToDate" property to "true".
Assert.assertFalse(properties.getLinksUpToDate());
doc.save(getArtifactsDir() + "DocumentProperties.Content.docx");
}
///
/// Counts the lines in a document.
/// Traverses the document's layout entities tree upon construction,
/// counting entities of the "Line" type that also contain real text.
///
private static class LineCounter {
public LineCounter(Document doc) throws Exception {
mLayoutEnumerator = new LayoutEnumerator(doc);
countLines();
}
public int getLineCount() {
return mLineCount;
}
private void countLines() throws Exception {
do {
if (mLayoutEnumerator.getType() == LayoutEntityType.LINE) {
mScanningLineForRealText = true;
}
if (mLayoutEnumerator.moveFirstChild()) {
if (mScanningLineForRealText && mLayoutEnumerator.getKind().startsWith("TEXT")) {
mLineCount++;
mScanningLineForRealText = false;
}
countLines();
mLayoutEnumerator.moveParent();
}
} while (mLayoutEnumerator.moveNext());
}
private final LayoutEnumerator mLayoutEnumerator;
private int mLineCount;
private boolean mScanningLineForRealText;
}
Returns: int - The corresponding int value.
indexOf(String name)
public int indexOf(String name)
Gets the index of a property by name.
Remarks:
Note: In Java this method is slow because iterates over all nodes.
Examples:
Shows how to work with a document’s custom properties.
Document doc = new Document();
CustomDocumentProperties properties = doc.getCustomDocumentProperties();
Assert.assertEquals(0, properties.getCount());
// Custom document properties are key-value pairs that we can add to the document.
properties.add("Authorized", true);
properties.add("Authorized By", "John Doe");
properties.add("Authorized Date", new Date());
properties.add("Authorized Revision", doc.getBuiltInDocumentProperties().getRevisionNumber());
properties.add("Authorized Amount", 123.45);
// The collection sorts the custom properties in alphabetic order.
Assert.assertEquals(1, properties.indexOf("Authorized Amount"));
Assert.assertEquals(5, properties.getCount());
// Print every custom property in the document.
Iterator enumerator = properties.iterator();
while (enumerator.hasNext()) {
DocumentProperty property = enumerator.next();
System.out.println(MessageFormat.format("Name: \"{0}\"\n\tType: \"{1}\"\n\tValue: \"{2}\"", property.getName(), property.getType(), property.getValue()));
}
// Display the value of a custom property using a DOCPROPERTY field.
DocumentBuilder builder = new DocumentBuilder(doc);
FieldDocProperty field = (FieldDocProperty) builder.insertField(" DOCPROPERTY \"Authorized By\"");
field.update();
Assert.assertEquals("John Doe", field.getResult());
// We can find these custom properties in Microsoft Word via "File" -> "Properties" > "Advanced Properties" > "Custom".
doc.save(getArtifactsDir() + "DocumentProperties.DocumentPropertyCollection.docx");
// Below are three ways or removing custom properties from a document.
// 1 - Remove by index:
properties.removeAt(1);
Assert.assertFalse(properties.contains("Authorized Amount"));
Assert.assertEquals(4, properties.getCount());
// 2 - Remove by name:
properties.remove("Authorized Revision");
Assert.assertFalse(properties.contains("Authorized Revision"));
Assert.assertEquals(3, properties.getCount());
// 3 - Empty the entire collection at once:
properties.clear();
Assert.assertEquals(0, properties.getCount());
Parameters:
Parameter | Type | Description |
---|---|---|
name | java.lang.String | The case-insensitive name of the property. |
Returns: int - The zero based index. Negative value if not found.
iterator()
public Iterator iterator()
Returns an iterator object that can be used to iterate over all items in the collection.
Examples:
Shows how to work with a document’s custom properties.
Document doc = new Document();
CustomDocumentProperties properties = doc.getCustomDocumentProperties();
Assert.assertEquals(0, properties.getCount());
// Custom document properties are key-value pairs that we can add to the document.
properties.add("Authorized", true);
properties.add("Authorized By", "John Doe");
properties.add("Authorized Date", new Date());
properties.add("Authorized Revision", doc.getBuiltInDocumentProperties().getRevisionNumber());
properties.add("Authorized Amount", 123.45);
// The collection sorts the custom properties in alphabetic order.
Assert.assertEquals(1, properties.indexOf("Authorized Amount"));
Assert.assertEquals(5, properties.getCount());
// Print every custom property in the document.
Iterator enumerator = properties.iterator();
while (enumerator.hasNext()) {
DocumentProperty property = enumerator.next();
System.out.println(MessageFormat.format("Name: \"{0}\"\n\tType: \"{1}\"\n\tValue: \"{2}\"", property.getName(), property.getType(), property.getValue()));
}
// Display the value of a custom property using a DOCPROPERTY field.
DocumentBuilder builder = new DocumentBuilder(doc);
FieldDocProperty field = (FieldDocProperty) builder.insertField(" DOCPROPERTY \"Authorized By\"");
field.update();
Assert.assertEquals("John Doe", field.getResult());
// We can find these custom properties in Microsoft Word via "File" -> "Properties" > "Advanced Properties" > "Custom".
doc.save(getArtifactsDir() + "DocumentProperties.DocumentPropertyCollection.docx");
// Below are three ways or removing custom properties from a document.
// 1 - Remove by index:
properties.removeAt(1);
Assert.assertFalse(properties.contains("Authorized Amount"));
Assert.assertEquals(4, properties.getCount());
// 2 - Remove by name:
properties.remove("Authorized Revision");
Assert.assertFalse(properties.contains("Authorized Revision"));
Assert.assertEquals(3, properties.getCount());
// 3 - Empty the entire collection at once:
properties.clear();
Assert.assertEquals(0, properties.getCount());
Returns: java.util.Iterator
remove(String name)
public void remove(String name)
Removes a property with the specified name from the collection.
Examples:
Shows how to work with a document’s custom properties.
Document doc = new Document();
CustomDocumentProperties properties = doc.getCustomDocumentProperties();
Assert.assertEquals(0, properties.getCount());
// Custom document properties are key-value pairs that we can add to the document.
properties.add("Authorized", true);
properties.add("Authorized By", "John Doe");
properties.add("Authorized Date", new Date());
properties.add("Authorized Revision", doc.getBuiltInDocumentProperties().getRevisionNumber());
properties.add("Authorized Amount", 123.45);
// The collection sorts the custom properties in alphabetic order.
Assert.assertEquals(1, properties.indexOf("Authorized Amount"));
Assert.assertEquals(5, properties.getCount());
// Print every custom property in the document.
Iterator enumerator = properties.iterator();
while (enumerator.hasNext()) {
DocumentProperty property = enumerator.next();
System.out.println(MessageFormat.format("Name: \"{0}\"\n\tType: \"{1}\"\n\tValue: \"{2}\"", property.getName(), property.getType(), property.getValue()));
}
// Display the value of a custom property using a DOCPROPERTY field.
DocumentBuilder builder = new DocumentBuilder(doc);
FieldDocProperty field = (FieldDocProperty) builder.insertField(" DOCPROPERTY \"Authorized By\"");
field.update();
Assert.assertEquals("John Doe", field.getResult());
// We can find these custom properties in Microsoft Word via "File" -> "Properties" > "Advanced Properties" > "Custom".
doc.save(getArtifactsDir() + "DocumentProperties.DocumentPropertyCollection.docx");
// Below are three ways or removing custom properties from a document.
// 1 - Remove by index:
properties.removeAt(1);
Assert.assertFalse(properties.contains("Authorized Amount"));
Assert.assertEquals(4, properties.getCount());
// 2 - Remove by name:
properties.remove("Authorized Revision");
Assert.assertFalse(properties.contains("Authorized Revision"));
Assert.assertEquals(3, properties.getCount());
// 3 - Empty the entire collection at once:
properties.clear();
Assert.assertEquals(0, properties.getCount());
Parameters:
Parameter | Type | Description |
---|---|---|
name | java.lang.String | The case-insensitive name of the property. |
removeAt(int index)
public void removeAt(int index)
Removes a property at the specified index.
Remarks:
Note: In Java this method is slow because iterates over all nodes.
Examples:
Shows how to work with a document’s custom properties.
Document doc = new Document();
CustomDocumentProperties properties = doc.getCustomDocumentProperties();
Assert.assertEquals(0, properties.getCount());
// Custom document properties are key-value pairs that we can add to the document.
properties.add("Authorized", true);
properties.add("Authorized By", "John Doe");
properties.add("Authorized Date", new Date());
properties.add("Authorized Revision", doc.getBuiltInDocumentProperties().getRevisionNumber());
properties.add("Authorized Amount", 123.45);
// The collection sorts the custom properties in alphabetic order.
Assert.assertEquals(1, properties.indexOf("Authorized Amount"));
Assert.assertEquals(5, properties.getCount());
// Print every custom property in the document.
Iterator enumerator = properties.iterator();
while (enumerator.hasNext()) {
DocumentProperty property = enumerator.next();
System.out.println(MessageFormat.format("Name: \"{0}\"\n\tType: \"{1}\"\n\tValue: \"{2}\"", property.getName(), property.getType(), property.getValue()));
}
// Display the value of a custom property using a DOCPROPERTY field.
DocumentBuilder builder = new DocumentBuilder(doc);
FieldDocProperty field = (FieldDocProperty) builder.insertField(" DOCPROPERTY \"Authorized By\"");
field.update();
Assert.assertEquals("John Doe", field.getResult());
// We can find these custom properties in Microsoft Word via "File" -> "Properties" > "Advanced Properties" > "Custom".
doc.save(getArtifactsDir() + "DocumentProperties.DocumentPropertyCollection.docx");
// Below are three ways or removing custom properties from a document.
// 1 - Remove by index:
properties.removeAt(1);
Assert.assertFalse(properties.contains("Authorized Amount"));
Assert.assertEquals(4, properties.getCount());
// 2 - Remove by name:
properties.remove("Authorized Revision");
Assert.assertFalse(properties.contains("Authorized Revision"));
Assert.assertEquals(3, properties.getCount());
// 3 - Empty the entire collection at once:
properties.clear();
Assert.assertEquals(0, properties.getCount());
Parameters:
Parameter | Type | Description |
---|---|---|
index | int | The zero based index. |
setAuthor(String value)
public void setAuthor(String value)
Sets the name of the document’s author.
Examples:
Shows how to work with built-in document properties in the “Description” category.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// Below are four built-in document properties that have fields that can display their values in the document body.
// 1 - "Author" property, which we can display using an AUTHOR field:
properties.setAuthor("John Doe");
builder.write("Author:\t");
builder.insertField(FieldType.FIELD_AUTHOR, true);
// 2 - "Title" property, which we can display using a TITLE field:
properties.setTitle("John's Document");
builder.write("\nDoc title:\t");
builder.insertField(FieldType.FIELD_TITLE, true);
// 3 - "Subject" property, which we can display using a SUBJECT field:
properties.setSubject("My subject");
builder.write("\nSubject:\t");
builder.insertField(FieldType.FIELD_SUBJECT, true);
// 4 - "Comments" property, which we can display using a COMMENTS field:
properties.setComments(MessageFormat.format("This is {0}''s document about {1}", properties.getAuthor(), properties.getSubject()));
builder.write("\nComments:\t\"");
builder.insertField(FieldType.FIELD_COMMENTS, true);
builder.write("\"");
// The "Category" built-in property does not have a field that can display its value.
properties.setCategory("My category");
// We can set multiple keywords for a document by separating the string value of the "Keywords" property with semicolons.
properties.setKeywords("Tag 1; Tag 2; Tag 3");
// We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details".
// The "Author" built-in property is in the "Origin" group, and the others are in the "Description" group.
doc.save(getArtifactsDir() + "DocumentProperties.Description.docx");
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.String | The name of the document’s author. |
setBytes(int value)
public void setBytes(int value)
Represents an estimate of the number of bytes in the document.
Remarks:
Microsoft Word does not always set this property.
Aspose.Words does not update this property.
Examples:
Shows how to work with document properties in the “Content” category.
public void content() throws Exception {
Document doc = new Document(getMyDir() + "Paragraphs.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// By using built in properties,
// we can treat document statistics such as word/page/character counts as metadata that can be glanced at without opening the document
// These properties are accessed by right clicking the file in Windows Explorer and navigating to Properties > Details > Content
// If we want to display this data inside the document, we can use fields such as NUMPAGES, NUMWORDS, NUMCHARS etc.
// Also, these values can also be viewed in Microsoft Word by navigating File > Properties > Advanced Properties > Statistics
// Page count: The PageCount property shows the page count in real time and its value can be assigned to the Pages property
// The "Pages" property stores the page count of the document.
Assert.assertEquals(6, properties.getPages());
// The "Words", "Characters", and "CharactersWithSpaces" built-in properties also display various document statistics,
// but we need to call the "UpdateWordCount" method on the whole document before we can expect them to contain accurate values.
doc.updateWordCount();
Assert.assertEquals(1035, properties.getWords());
Assert.assertEquals(6026, properties.getCharacters());
Assert.assertEquals(7041, properties.getCharactersWithSpaces());
// Count the number of lines in the document, and then assign the result to the "Lines" built-in property.
LineCounter lineCounter = new LineCounter(doc);
properties.setLines(lineCounter.getLineCount());
Assert.assertEquals(142, properties.getLines());
// Assign the number of Paragraph nodes in the document to the "Paragraphs" built-in property.
properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount());
Assert.assertEquals(29, properties.getParagraphs());
// Get an estimate of the file size of our document via the "Bytes" built-in property.
Assert.assertEquals(20310, properties.getBytes());
// Set a different template for our document, and then update the "Template" built-in property manually to reflect this change.
doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx");
Assert.assertEquals("Normal", properties.getTemplate());
properties.setTemplate(doc.getAttachedTemplate());
// "ContentStatus" is a descriptive built-in property.
properties.setContentStatus("Draft");
// Upon saving, the "ContentType" built-in property will contain the MIME type of the output save format.
Assert.assertEquals("", properties.getContentType());
// If the document contains links, and they are all up to date, we can set the "LinksUpToDate" property to "true".
Assert.assertFalse(properties.getLinksUpToDate());
doc.save(getArtifactsDir() + "DocumentProperties.Content.docx");
}
///
/// Counts the lines in a document.
/// Traverses the document's layout entities tree upon construction,
/// counting entities of the "Line" type that also contain real text.
///
private static class LineCounter {
public LineCounter(Document doc) throws Exception {
mLayoutEnumerator = new LayoutEnumerator(doc);
countLines();
}
public int getLineCount() {
return mLineCount;
}
private void countLines() throws Exception {
do {
if (mLayoutEnumerator.getType() == LayoutEntityType.LINE) {
mScanningLineForRealText = true;
}
if (mLayoutEnumerator.moveFirstChild()) {
if (mScanningLineForRealText && mLayoutEnumerator.getKind().startsWith("TEXT")) {
mLineCount++;
mScanningLineForRealText = false;
}
countLines();
mLayoutEnumerator.moveParent();
}
} while (mLayoutEnumerator.moveNext());
}
private final LayoutEnumerator mLayoutEnumerator;
private int mLineCount;
private boolean mScanningLineForRealText;
}
Parameters:
Parameter | Type | Description |
---|---|---|
value | int | The corresponding int value. |
setCategory(String value)
public void setCategory(String value)
Sets the category of the document.
Examples:
Shows how to work with built-in document properties in the “Description” category.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// Below are four built-in document properties that have fields that can display their values in the document body.
// 1 - "Author" property, which we can display using an AUTHOR field:
properties.setAuthor("John Doe");
builder.write("Author:\t");
builder.insertField(FieldType.FIELD_AUTHOR, true);
// 2 - "Title" property, which we can display using a TITLE field:
properties.setTitle("John's Document");
builder.write("\nDoc title:\t");
builder.insertField(FieldType.FIELD_TITLE, true);
// 3 - "Subject" property, which we can display using a SUBJECT field:
properties.setSubject("My subject");
builder.write("\nSubject:\t");
builder.insertField(FieldType.FIELD_SUBJECT, true);
// 4 - "Comments" property, which we can display using a COMMENTS field:
properties.setComments(MessageFormat.format("This is {0}''s document about {1}", properties.getAuthor(), properties.getSubject()));
builder.write("\nComments:\t\"");
builder.insertField(FieldType.FIELD_COMMENTS, true);
builder.write("\"");
// The "Category" built-in property does not have a field that can display its value.
properties.setCategory("My category");
// We can set multiple keywords for a document by separating the string value of the "Keywords" property with semicolons.
properties.setKeywords("Tag 1; Tag 2; Tag 3");
// We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details".
// The "Author" built-in property is in the "Origin" group, and the others are in the "Description" group.
doc.save(getArtifactsDir() + "DocumentProperties.Description.docx");
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.String | The category of the document. |
setCharacters(int value)
public void setCharacters(int value)
Represents an estimate of the number of characters in the document.
Remarks:
Aspose.Words updates this property when you call Document.updateWordCount().
Examples:
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());
Shows how to work with document properties in the “Content” category.
public void content() throws Exception {
Document doc = new Document(getMyDir() + "Paragraphs.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// By using built in properties,
// we can treat document statistics such as word/page/character counts as metadata that can be glanced at without opening the document
// These properties are accessed by right clicking the file in Windows Explorer and navigating to Properties > Details > Content
// If we want to display this data inside the document, we can use fields such as NUMPAGES, NUMWORDS, NUMCHARS etc.
// Also, these values can also be viewed in Microsoft Word by navigating File > Properties > Advanced Properties > Statistics
// Page count: The PageCount property shows the page count in real time and its value can be assigned to the Pages property
// The "Pages" property stores the page count of the document.
Assert.assertEquals(6, properties.getPages());
// The "Words", "Characters", and "CharactersWithSpaces" built-in properties also display various document statistics,
// but we need to call the "UpdateWordCount" method on the whole document before we can expect them to contain accurate values.
doc.updateWordCount();
Assert.assertEquals(1035, properties.getWords());
Assert.assertEquals(6026, properties.getCharacters());
Assert.assertEquals(7041, properties.getCharactersWithSpaces());
// Count the number of lines in the document, and then assign the result to the "Lines" built-in property.
LineCounter lineCounter = new LineCounter(doc);
properties.setLines(lineCounter.getLineCount());
Assert.assertEquals(142, properties.getLines());
// Assign the number of Paragraph nodes in the document to the "Paragraphs" built-in property.
properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount());
Assert.assertEquals(29, properties.getParagraphs());
// Get an estimate of the file size of our document via the "Bytes" built-in property.
Assert.assertEquals(20310, properties.getBytes());
// Set a different template for our document, and then update the "Template" built-in property manually to reflect this change.
doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx");
Assert.assertEquals("Normal", properties.getTemplate());
properties.setTemplate(doc.getAttachedTemplate());
// "ContentStatus" is a descriptive built-in property.
properties.setContentStatus("Draft");
// Upon saving, the "ContentType" built-in property will contain the MIME type of the output save format.
Assert.assertEquals("", properties.getContentType());
// If the document contains links, and they are all up to date, we can set the "LinksUpToDate" property to "true".
Assert.assertFalse(properties.getLinksUpToDate());
doc.save(getArtifactsDir() + "DocumentProperties.Content.docx");
}
///
/// Counts the lines in a document.
/// Traverses the document's layout entities tree upon construction,
/// counting entities of the "Line" type that also contain real text.
///
private static class LineCounter {
public LineCounter(Document doc) throws Exception {
mLayoutEnumerator = new LayoutEnumerator(doc);
countLines();
}
public int getLineCount() {
return mLineCount;
}
private void countLines() throws Exception {
do {
if (mLayoutEnumerator.getType() == LayoutEntityType.LINE) {
mScanningLineForRealText = true;
}
if (mLayoutEnumerator.moveFirstChild()) {
if (mScanningLineForRealText && mLayoutEnumerator.getKind().startsWith("TEXT")) {
mLineCount++;
mScanningLineForRealText = false;
}
countLines();
mLayoutEnumerator.moveParent();
}
} while (mLayoutEnumerator.moveNext());
}
private final LayoutEnumerator mLayoutEnumerator;
private int mLineCount;
private boolean mScanningLineForRealText;
}
Parameters:
Parameter | Type | Description |
---|---|---|
value | int | The corresponding int value. |
setCharactersWithSpaces(int value)
public void setCharactersWithSpaces(int value)
Represents an estimate of the number of characters (including spaces) in the document.
Remarks:
Aspose.Words updates this property when you call Document.updateWordCount().
Examples:
Shows how to work with document properties in the “Content” category.
public void content() throws Exception {
Document doc = new Document(getMyDir() + "Paragraphs.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// By using built in properties,
// we can treat document statistics such as word/page/character counts as metadata that can be glanced at without opening the document
// These properties are accessed by right clicking the file in Windows Explorer and navigating to Properties > Details > Content
// If we want to display this data inside the document, we can use fields such as NUMPAGES, NUMWORDS, NUMCHARS etc.
// Also, these values can also be viewed in Microsoft Word by navigating File > Properties > Advanced Properties > Statistics
// Page count: The PageCount property shows the page count in real time and its value can be assigned to the Pages property
// The "Pages" property stores the page count of the document.
Assert.assertEquals(6, properties.getPages());
// The "Words", "Characters", and "CharactersWithSpaces" built-in properties also display various document statistics,
// but we need to call the "UpdateWordCount" method on the whole document before we can expect them to contain accurate values.
doc.updateWordCount();
Assert.assertEquals(1035, properties.getWords());
Assert.assertEquals(6026, properties.getCharacters());
Assert.assertEquals(7041, properties.getCharactersWithSpaces());
// Count the number of lines in the document, and then assign the result to the "Lines" built-in property.
LineCounter lineCounter = new LineCounter(doc);
properties.setLines(lineCounter.getLineCount());
Assert.assertEquals(142, properties.getLines());
// Assign the number of Paragraph nodes in the document to the "Paragraphs" built-in property.
properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount());
Assert.assertEquals(29, properties.getParagraphs());
// Get an estimate of the file size of our document via the "Bytes" built-in property.
Assert.assertEquals(20310, properties.getBytes());
// Set a different template for our document, and then update the "Template" built-in property manually to reflect this change.
doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx");
Assert.assertEquals("Normal", properties.getTemplate());
properties.setTemplate(doc.getAttachedTemplate());
// "ContentStatus" is a descriptive built-in property.
properties.setContentStatus("Draft");
// Upon saving, the "ContentType" built-in property will contain the MIME type of the output save format.
Assert.assertEquals("", properties.getContentType());
// If the document contains links, and they are all up to date, we can set the "LinksUpToDate" property to "true".
Assert.assertFalse(properties.getLinksUpToDate());
doc.save(getArtifactsDir() + "DocumentProperties.Content.docx");
}
///
/// Counts the lines in a document.
/// Traverses the document's layout entities tree upon construction,
/// counting entities of the "Line" type that also contain real text.
///
private static class LineCounter {
public LineCounter(Document doc) throws Exception {
mLayoutEnumerator = new LayoutEnumerator(doc);
countLines();
}
public int getLineCount() {
return mLineCount;
}
private void countLines() throws Exception {
do {
if (mLayoutEnumerator.getType() == LayoutEntityType.LINE) {
mScanningLineForRealText = true;
}
if (mLayoutEnumerator.moveFirstChild()) {
if (mScanningLineForRealText && mLayoutEnumerator.getKind().startsWith("TEXT")) {
mLineCount++;
mScanningLineForRealText = false;
}
countLines();
mLayoutEnumerator.moveParent();
}
} while (mLayoutEnumerator.moveNext());
}
private final LayoutEnumerator mLayoutEnumerator;
private int mLineCount;
private boolean mScanningLineForRealText;
}
Parameters:
Parameter | Type | Description |
---|---|---|
value | int | The corresponding int value. |
setComments(String value)
public void setComments(String value)
Sets the document comments.
Examples:
Shows how to work with built-in document properties in the “Description” category.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// Below are four built-in document properties that have fields that can display their values in the document body.
// 1 - "Author" property, which we can display using an AUTHOR field:
properties.setAuthor("John Doe");
builder.write("Author:\t");
builder.insertField(FieldType.FIELD_AUTHOR, true);
// 2 - "Title" property, which we can display using a TITLE field:
properties.setTitle("John's Document");
builder.write("\nDoc title:\t");
builder.insertField(FieldType.FIELD_TITLE, true);
// 3 - "Subject" property, which we can display using a SUBJECT field:
properties.setSubject("My subject");
builder.write("\nSubject:\t");
builder.insertField(FieldType.FIELD_SUBJECT, true);
// 4 - "Comments" property, which we can display using a COMMENTS field:
properties.setComments(MessageFormat.format("This is {0}''s document about {1}", properties.getAuthor(), properties.getSubject()));
builder.write("\nComments:\t\"");
builder.insertField(FieldType.FIELD_COMMENTS, true);
builder.write("\"");
// The "Category" built-in property does not have a field that can display its value.
properties.setCategory("My category");
// We can set multiple keywords for a document by separating the string value of the "Keywords" property with semicolons.
properties.setKeywords("Tag 1; Tag 2; Tag 3");
// We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details".
// The "Author" built-in property is in the "Origin" group, and the others are in the "Description" group.
doc.save(getArtifactsDir() + "DocumentProperties.Description.docx");
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.String | The document comments. |
setCompany(String value)
public void setCompany(String value)
Sets the company property.
Examples:
Shows how to work with document properties in the “Origin” category.
// Open a document that we have created and edited using Microsoft Word.
Document doc = new Document(getMyDir() + "Properties.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// The following built-in properties contain information regarding the creation and editing of this document.
// We can right-click this document in Windows Explorer and find
// these properties via "Properties" -> "Details" -> "Origin" category.
// Fields such as PRINTDATE and EDITTIME can display these values in the document body.
System.out.println(MessageFormat.format("Created using {0}, on {1}", properties.getNameOfApplication(), properties.getCreatedTime()));
System.out.println(MessageFormat.format("Minutes spent editing: {0}", properties.getTotalEditingTime()));
System.out.println(MessageFormat.format("Date/time last printed: {0}", properties.getLastPrinted()));
System.out.println(MessageFormat.format("Template document: {0}", properties.getTemplate()));
// We can also change the values of built-in properties.
properties.setCompany("Doe Ltd.");
properties.setManager("Jane Doe");
properties.setVersion(5);
properties.setRevisionNumber(properties.getRevisionNumber() + 1);
// Microsoft Word updates the following properties automatically when we save the document.
// To use these properties with Aspose.Words, we will need to set values for them manually.
properties.setLastSavedBy("John Doe");
properties.setLastSavedTime(new Date());
// We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details" -> "Origin".
doc.save(getArtifactsDir() + "DocumentProperties.Origin.docx");
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.String | The company property. |
setContentStatus(String value)
public void setContentStatus(String value)
Sets the content status of the document.
Examples:
Shows how to work with document properties in the “Content” category.
public void content() throws Exception {
Document doc = new Document(getMyDir() + "Paragraphs.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// By using built in properties,
// we can treat document statistics such as word/page/character counts as metadata that can be glanced at without opening the document
// These properties are accessed by right clicking the file in Windows Explorer and navigating to Properties > Details > Content
// If we want to display this data inside the document, we can use fields such as NUMPAGES, NUMWORDS, NUMCHARS etc.
// Also, these values can also be viewed in Microsoft Word by navigating File > Properties > Advanced Properties > Statistics
// Page count: The PageCount property shows the page count in real time and its value can be assigned to the Pages property
// The "Pages" property stores the page count of the document.
Assert.assertEquals(6, properties.getPages());
// The "Words", "Characters", and "CharactersWithSpaces" built-in properties also display various document statistics,
// but we need to call the "UpdateWordCount" method on the whole document before we can expect them to contain accurate values.
doc.updateWordCount();
Assert.assertEquals(1035, properties.getWords());
Assert.assertEquals(6026, properties.getCharacters());
Assert.assertEquals(7041, properties.getCharactersWithSpaces());
// Count the number of lines in the document, and then assign the result to the "Lines" built-in property.
LineCounter lineCounter = new LineCounter(doc);
properties.setLines(lineCounter.getLineCount());
Assert.assertEquals(142, properties.getLines());
// Assign the number of Paragraph nodes in the document to the "Paragraphs" built-in property.
properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount());
Assert.assertEquals(29, properties.getParagraphs());
// Get an estimate of the file size of our document via the "Bytes" built-in property.
Assert.assertEquals(20310, properties.getBytes());
// Set a different template for our document, and then update the "Template" built-in property manually to reflect this change.
doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx");
Assert.assertEquals("Normal", properties.getTemplate());
properties.setTemplate(doc.getAttachedTemplate());
// "ContentStatus" is a descriptive built-in property.
properties.setContentStatus("Draft");
// Upon saving, the "ContentType" built-in property will contain the MIME type of the output save format.
Assert.assertEquals("", properties.getContentType());
// If the document contains links, and they are all up to date, we can set the "LinksUpToDate" property to "true".
Assert.assertFalse(properties.getLinksUpToDate());
doc.save(getArtifactsDir() + "DocumentProperties.Content.docx");
}
///
/// Counts the lines in a document.
/// Traverses the document's layout entities tree upon construction,
/// counting entities of the "Line" type that also contain real text.
///
private static class LineCounter {
public LineCounter(Document doc) throws Exception {
mLayoutEnumerator = new LayoutEnumerator(doc);
countLines();
}
public int getLineCount() {
return mLineCount;
}
private void countLines() throws Exception {
do {
if (mLayoutEnumerator.getType() == LayoutEntityType.LINE) {
mScanningLineForRealText = true;
}
if (mLayoutEnumerator.moveFirstChild()) {
if (mScanningLineForRealText && mLayoutEnumerator.getKind().startsWith("TEXT")) {
mLineCount++;
mScanningLineForRealText = false;
}
countLines();
mLayoutEnumerator.moveParent();
}
} while (mLayoutEnumerator.moveNext());
}
private final LayoutEnumerator mLayoutEnumerator;
private int mLineCount;
private boolean mScanningLineForRealText;
}
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.String | The content status of the document. |
setContentType(String value)
public void setContentType(String value)
Sets the content type of the document.
Examples:
Shows how to work with document properties in the “Content” category.
public void content() throws Exception {
Document doc = new Document(getMyDir() + "Paragraphs.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// By using built in properties,
// we can treat document statistics such as word/page/character counts as metadata that can be glanced at without opening the document
// These properties are accessed by right clicking the file in Windows Explorer and navigating to Properties > Details > Content
// If we want to display this data inside the document, we can use fields such as NUMPAGES, NUMWORDS, NUMCHARS etc.
// Also, these values can also be viewed in Microsoft Word by navigating File > Properties > Advanced Properties > Statistics
// Page count: The PageCount property shows the page count in real time and its value can be assigned to the Pages property
// The "Pages" property stores the page count of the document.
Assert.assertEquals(6, properties.getPages());
// The "Words", "Characters", and "CharactersWithSpaces" built-in properties also display various document statistics,
// but we need to call the "UpdateWordCount" method on the whole document before we can expect them to contain accurate values.
doc.updateWordCount();
Assert.assertEquals(1035, properties.getWords());
Assert.assertEquals(6026, properties.getCharacters());
Assert.assertEquals(7041, properties.getCharactersWithSpaces());
// Count the number of lines in the document, and then assign the result to the "Lines" built-in property.
LineCounter lineCounter = new LineCounter(doc);
properties.setLines(lineCounter.getLineCount());
Assert.assertEquals(142, properties.getLines());
// Assign the number of Paragraph nodes in the document to the "Paragraphs" built-in property.
properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount());
Assert.assertEquals(29, properties.getParagraphs());
// Get an estimate of the file size of our document via the "Bytes" built-in property.
Assert.assertEquals(20310, properties.getBytes());
// Set a different template for our document, and then update the "Template" built-in property manually to reflect this change.
doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx");
Assert.assertEquals("Normal", properties.getTemplate());
properties.setTemplate(doc.getAttachedTemplate());
// "ContentStatus" is a descriptive built-in property.
properties.setContentStatus("Draft");
// Upon saving, the "ContentType" built-in property will contain the MIME type of the output save format.
Assert.assertEquals("", properties.getContentType());
// If the document contains links, and they are all up to date, we can set the "LinksUpToDate" property to "true".
Assert.assertFalse(properties.getLinksUpToDate());
doc.save(getArtifactsDir() + "DocumentProperties.Content.docx");
}
///
/// Counts the lines in a document.
/// Traverses the document's layout entities tree upon construction,
/// counting entities of the "Line" type that also contain real text.
///
private static class LineCounter {
public LineCounter(Document doc) throws Exception {
mLayoutEnumerator = new LayoutEnumerator(doc);
countLines();
}
public int getLineCount() {
return mLineCount;
}
private void countLines() throws Exception {
do {
if (mLayoutEnumerator.getType() == LayoutEntityType.LINE) {
mScanningLineForRealText = true;
}
if (mLayoutEnumerator.moveFirstChild()) {
if (mScanningLineForRealText && mLayoutEnumerator.getKind().startsWith("TEXT")) {
mLineCount++;
mScanningLineForRealText = false;
}
countLines();
mLayoutEnumerator.moveParent();
}
} while (mLayoutEnumerator.moveNext());
}
private final LayoutEnumerator mLayoutEnumerator;
private int mLineCount;
private boolean mScanningLineForRealText;
}
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.String | The content type of the document. |
setCreatedTime(Date value)
public void setCreatedTime(Date value)
Sets date of the document creation in UTC.
Remarks:
For documents originated from RTF format this property returns local time of the author’s machine at the moment of document creation.
Aspose.Words does not update this property.
Examples:
Shows how to work with document properties in the “Origin” category.
// Open a document that we have created and edited using Microsoft Word.
Document doc = new Document(getMyDir() + "Properties.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// The following built-in properties contain information regarding the creation and editing of this document.
// We can right-click this document in Windows Explorer and find
// these properties via "Properties" -> "Details" -> "Origin" category.
// Fields such as PRINTDATE and EDITTIME can display these values in the document body.
System.out.println(MessageFormat.format("Created using {0}, on {1}", properties.getNameOfApplication(), properties.getCreatedTime()));
System.out.println(MessageFormat.format("Minutes spent editing: {0}", properties.getTotalEditingTime()));
System.out.println(MessageFormat.format("Date/time last printed: {0}", properties.getLastPrinted()));
System.out.println(MessageFormat.format("Template document: {0}", properties.getTemplate()));
// We can also change the values of built-in properties.
properties.setCompany("Doe Ltd.");
properties.setManager("Jane Doe");
properties.setVersion(5);
properties.setRevisionNumber(properties.getRevisionNumber() + 1);
// Microsoft Word updates the following properties automatically when we save the document.
// To use these properties with Aspose.Words, we will need to set values for them manually.
properties.setLastSavedBy("John Doe");
properties.setLastSavedTime(new Date());
// We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details" -> "Origin".
doc.save(getArtifactsDir() + "DocumentProperties.Origin.docx");
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.util.Date | Date of the document creation in UTC. |
setHeadingPairs(Object[] value)
public void setHeadingPairs(Object[] value)
Specifies document headings and their names.
Remarks:
Every heading pair occupies two elements in this array.
The first element of the pair is a java.lang.String and specifies the heading name. The second element of the pair is an int and specifies the count of document parts for this heading in the getTitlesOfParts() / setTitlesOfParts(java.lang.String[]) property.
The total sum of counts for all heading pairs in this property must be equal to the number of elements in the getTitlesOfParts() / setTitlesOfParts(java.lang.String[]) property.
Aspose.Words does not update this property.
Examples:
Shows the relationship between “HeadingPairs” and “TitlesOfParts” properties.
Document doc = new Document(getMyDir() + "Heading pairs and titles of parts.docx");
// We can find the combined values of these collections via
// "File" -> "Properties" -> "Advanced Properties" -> "Contents" tab.
// The HeadingPairs property is a collection of pairs that
// determines how many document parts a heading spans across.
Object[] headingPairs = doc.getBuiltInDocumentProperties().getHeadingPairs();
// The TitlesOfParts property contains the names of parts that belong to the above headings.
String[] titlesOfParts = doc.getBuiltInDocumentProperties().getTitlesOfParts();
int headingPairsIndex = 0;
int titlesOfPartsIndex = 0;
while (headingPairsIndex < headingPairs.length) {
System.out.println(MessageFormat.format("Parts for {0}:", headingPairs[headingPairsIndex++]));
int partsCount = (int) headingPairs[headingPairsIndex++];
for (int i = 0; i < partsCount; i++)
System.out.println(MessageFormat.format("\t\"{0}\"", titlesOfParts[titlesOfPartsIndex++]));
}
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.Object[] | The corresponding java.lang.Object[] value. |
setHyperlinkBase(String value)
public void setHyperlinkBase(String value)
Specifies the base string used for evaluating relative hyperlinks in this document.
Remarks:
Aspose.Words does not use this property.
Examples:
Shows how to store the base part of a hyperlink in the document’s properties.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a relative hyperlink to a document in the local file system named "Document.docx".
// Clicking on the link in Microsoft Word will open the designated document, if it is available.
builder.insertHyperlink("Relative hyperlink", "Document.docx", false);
// This link is relative. If there is no "Document.docx" in the same folder
// as the document that contains this link, the link will be broken.
Assert.assertFalse(new File(getArtifactsDir() + "Document.docx").exists());
doc.save(getArtifactsDir() + "DocumentProperties.HyperlinkBase.BrokenLink.docx");
// The document we are trying to link to is in a different directory to the one we are planning to save the document in.
// We could fix links like this by putting an absolute filename in each one.
// Alternatively, we could provide a base link that every hyperlink with a relative filename
// will prepend to its link when we click on it.
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
properties.setHyperlinkBase(getMyDir());
Assert.assertTrue(new File(properties.getHyperlinkBase() + ((FieldHyperlink) doc.getRange().getFields().get(0)).getAddress()).exists());
doc.save(getArtifactsDir() + "DocumentProperties.HyperlinkBase.WorkingLink.docx");
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.String | The corresponding java.lang.String value. |
setKeywords(String value)
public void setKeywords(String value)
Sets the document keywords.
Examples:
Shows how to work with built-in document properties in the “Description” category.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// Below are four built-in document properties that have fields that can display their values in the document body.
// 1 - "Author" property, which we can display using an AUTHOR field:
properties.setAuthor("John Doe");
builder.write("Author:\t");
builder.insertField(FieldType.FIELD_AUTHOR, true);
// 2 - "Title" property, which we can display using a TITLE field:
properties.setTitle("John's Document");
builder.write("\nDoc title:\t");
builder.insertField(FieldType.FIELD_TITLE, true);
// 3 - "Subject" property, which we can display using a SUBJECT field:
properties.setSubject("My subject");
builder.write("\nSubject:\t");
builder.insertField(FieldType.FIELD_SUBJECT, true);
// 4 - "Comments" property, which we can display using a COMMENTS field:
properties.setComments(MessageFormat.format("This is {0}''s document about {1}", properties.getAuthor(), properties.getSubject()));
builder.write("\nComments:\t\"");
builder.insertField(FieldType.FIELD_COMMENTS, true);
builder.write("\"");
// The "Category" built-in property does not have a field that can display its value.
properties.setCategory("My category");
// We can set multiple keywords for a document by separating the string value of the "Keywords" property with semicolons.
properties.setKeywords("Tag 1; Tag 2; Tag 3");
// We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details".
// The "Author" built-in property is in the "Origin" group, and the others are in the "Description" group.
doc.save(getArtifactsDir() + "DocumentProperties.Description.docx");
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.String | The document keywords. |
setLastPrinted(Date value)
public void setLastPrinted(Date value)
Sets the date when the document was last printed in UTC.
Remarks:
For documents originated from RTF format this property returns the local time of last print operation.
If the document was never printed, this property will return DateTime.MinValue.
Aspose.Words does not update this property.
Examples:
Shows how to work with document properties in the “Origin” category.
// Open a document that we have created and edited using Microsoft Word.
Document doc = new Document(getMyDir() + "Properties.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// The following built-in properties contain information regarding the creation and editing of this document.
// We can right-click this document in Windows Explorer and find
// these properties via "Properties" -> "Details" -> "Origin" category.
// Fields such as PRINTDATE and EDITTIME can display these values in the document body.
System.out.println(MessageFormat.format("Created using {0}, on {1}", properties.getNameOfApplication(), properties.getCreatedTime()));
System.out.println(MessageFormat.format("Minutes spent editing: {0}", properties.getTotalEditingTime()));
System.out.println(MessageFormat.format("Date/time last printed: {0}", properties.getLastPrinted()));
System.out.println(MessageFormat.format("Template document: {0}", properties.getTemplate()));
// We can also change the values of built-in properties.
properties.setCompany("Doe Ltd.");
properties.setManager("Jane Doe");
properties.setVersion(5);
properties.setRevisionNumber(properties.getRevisionNumber() + 1);
// Microsoft Word updates the following properties automatically when we save the document.
// To use these properties with Aspose.Words, we will need to set values for them manually.
properties.setLastSavedBy("John Doe");
properties.setLastSavedTime(new Date());
// We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details" -> "Origin".
doc.save(getArtifactsDir() + "DocumentProperties.Origin.docx");
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.util.Date | The date when the document was last printed in UTC. |
setLastSavedBy(String value)
public void setLastSavedBy(String value)
Sets the name of the last author.
Remarks:
Aspose.Words does not update this property.
Examples:
Shows how to work with document properties in the “Origin” category.
// Open a document that we have created and edited using Microsoft Word.
Document doc = new Document(getMyDir() + "Properties.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// The following built-in properties contain information regarding the creation and editing of this document.
// We can right-click this document in Windows Explorer and find
// these properties via "Properties" -> "Details" -> "Origin" category.
// Fields such as PRINTDATE and EDITTIME can display these values in the document body.
System.out.println(MessageFormat.format("Created using {0}, on {1}", properties.getNameOfApplication(), properties.getCreatedTime()));
System.out.println(MessageFormat.format("Minutes spent editing: {0}", properties.getTotalEditingTime()));
System.out.println(MessageFormat.format("Date/time last printed: {0}", properties.getLastPrinted()));
System.out.println(MessageFormat.format("Template document: {0}", properties.getTemplate()));
// We can also change the values of built-in properties.
properties.setCompany("Doe Ltd.");
properties.setManager("Jane Doe");
properties.setVersion(5);
properties.setRevisionNumber(properties.getRevisionNumber() + 1);
// Microsoft Word updates the following properties automatically when we save the document.
// To use these properties with Aspose.Words, we will need to set values for them manually.
properties.setLastSavedBy("John Doe");
properties.setLastSavedTime(new Date());
// We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details" -> "Origin".
doc.save(getArtifactsDir() + "DocumentProperties.Origin.docx");
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.String | The name of the last author. |
setLastSavedTime(Date value)
public void setLastSavedTime(Date value)
Sets the time of the last save in UTC.
Remarks:
For documents originated from RTF format this property returns the local time of last save operation.
Aspose.Words does not update this property.
Examples:
Shows how to work with document properties in the “Origin” category.
// Open a document that we have created and edited using Microsoft Word.
Document doc = new Document(getMyDir() + "Properties.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// The following built-in properties contain information regarding the creation and editing of this document.
// We can right-click this document in Windows Explorer and find
// these properties via "Properties" -> "Details" -> "Origin" category.
// Fields such as PRINTDATE and EDITTIME can display these values in the document body.
System.out.println(MessageFormat.format("Created using {0}, on {1}", properties.getNameOfApplication(), properties.getCreatedTime()));
System.out.println(MessageFormat.format("Minutes spent editing: {0}", properties.getTotalEditingTime()));
System.out.println(MessageFormat.format("Date/time last printed: {0}", properties.getLastPrinted()));
System.out.println(MessageFormat.format("Template document: {0}", properties.getTemplate()));
// We can also change the values of built-in properties.
properties.setCompany("Doe Ltd.");
properties.setManager("Jane Doe");
properties.setVersion(5);
properties.setRevisionNumber(properties.getRevisionNumber() + 1);
// Microsoft Word updates the following properties automatically when we save the document.
// To use these properties with Aspose.Words, we will need to set values for them manually.
properties.setLastSavedBy("John Doe");
properties.setLastSavedTime(new Date());
// We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details" -> "Origin".
doc.save(getArtifactsDir() + "DocumentProperties.Origin.docx");
Shows how to use the SAVEDATE field to display the date/time of the document’s most recent save operation performed using Microsoft Word.
Document doc = new Document(getMyDir() + "Document.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToDocumentEnd();
builder.writeln(" Date this document was last saved:");
// We can use the SAVEDATE field to display the last save operation's date and time on the document.
// The save operation that these fields refer to is the manual save in an application like Microsoft Word,
// not the document's Save method.
// Below are three different calendar types according to which the SAVEDATE field can display the date/time.
// 1 - Islamic Lunar Calendar:
builder.write("According to the Lunar Calendar - ");
FieldSaveDate field = (FieldSaveDate) builder.insertField(FieldType.FIELD_SAVE_DATE, true);
field.setUseLunarCalendar(true);
Assert.assertEquals(" SAVEDATE \\h", field.getFieldCode());
// 2 - Umm al-Qura calendar:
builder.write("\nAccording to the Umm al-Qura calendar - ");
field = (FieldSaveDate) builder.insertField(FieldType.FIELD_SAVE_DATE, true);
field.setUseUmAlQuraCalendar(true);
Assert.assertEquals(" SAVEDATE \\u", field.getFieldCode());
// 3 - Indian National calendar:
builder.write("\nAccording to the Indian National calendar - ");
field = (FieldSaveDate) builder.insertField(FieldType.FIELD_SAVE_DATE, true);
field.setUseSakaEraCalendar(true);
Assert.assertEquals(" SAVEDATE \\s", field.getFieldCode());
// The SAVEDATE fields draw their date/time values from the LastSavedTime built-in property.
// The document's Save method will not update this value, but we can still update it manually.
doc.getBuiltInDocumentProperties().setLastSavedTime(new Date());
doc.updateFields();
doc.save(getArtifactsDir() + "Field.SAVEDATE.docx");
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.util.Date | The time of the last save in UTC. |
setLines(int value)
public void setLines(int value)
Represents an estimate of the number of lines in the document.
Remarks:
Aspose.Words updates this property when you call Document.updateWordCount(boolean).
Examples:
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());
Shows how to work with document properties in the “Content” category.
public void content() throws Exception {
Document doc = new Document(getMyDir() + "Paragraphs.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// By using built in properties,
// we can treat document statistics such as word/page/character counts as metadata that can be glanced at without opening the document
// These properties are accessed by right clicking the file in Windows Explorer and navigating to Properties > Details > Content
// If we want to display this data inside the document, we can use fields such as NUMPAGES, NUMWORDS, NUMCHARS etc.
// Also, these values can also be viewed in Microsoft Word by navigating File > Properties > Advanced Properties > Statistics
// Page count: The PageCount property shows the page count in real time and its value can be assigned to the Pages property
// The "Pages" property stores the page count of the document.
Assert.assertEquals(6, properties.getPages());
// The "Words", "Characters", and "CharactersWithSpaces" built-in properties also display various document statistics,
// but we need to call the "UpdateWordCount" method on the whole document before we can expect them to contain accurate values.
doc.updateWordCount();
Assert.assertEquals(1035, properties.getWords());
Assert.assertEquals(6026, properties.getCharacters());
Assert.assertEquals(7041, properties.getCharactersWithSpaces());
// Count the number of lines in the document, and then assign the result to the "Lines" built-in property.
LineCounter lineCounter = new LineCounter(doc);
properties.setLines(lineCounter.getLineCount());
Assert.assertEquals(142, properties.getLines());
// Assign the number of Paragraph nodes in the document to the "Paragraphs" built-in property.
properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount());
Assert.assertEquals(29, properties.getParagraphs());
// Get an estimate of the file size of our document via the "Bytes" built-in property.
Assert.assertEquals(20310, properties.getBytes());
// Set a different template for our document, and then update the "Template" built-in property manually to reflect this change.
doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx");
Assert.assertEquals("Normal", properties.getTemplate());
properties.setTemplate(doc.getAttachedTemplate());
// "ContentStatus" is a descriptive built-in property.
properties.setContentStatus("Draft");
// Upon saving, the "ContentType" built-in property will contain the MIME type of the output save format.
Assert.assertEquals("", properties.getContentType());
// If the document contains links, and they are all up to date, we can set the "LinksUpToDate" property to "true".
Assert.assertFalse(properties.getLinksUpToDate());
doc.save(getArtifactsDir() + "DocumentProperties.Content.docx");
}
///
/// Counts the lines in a document.
/// Traverses the document's layout entities tree upon construction,
/// counting entities of the "Line" type that also contain real text.
///
private static class LineCounter {
public LineCounter(Document doc) throws Exception {
mLayoutEnumerator = new LayoutEnumerator(doc);
countLines();
}
public int getLineCount() {
return mLineCount;
}
private void countLines() throws Exception {
do {
if (mLayoutEnumerator.getType() == LayoutEntityType.LINE) {
mScanningLineForRealText = true;
}
if (mLayoutEnumerator.moveFirstChild()) {
if (mScanningLineForRealText && mLayoutEnumerator.getKind().startsWith("TEXT")) {
mLineCount++;
mScanningLineForRealText = false;
}
countLines();
mLayoutEnumerator.moveParent();
}
} while (mLayoutEnumerator.moveNext());
}
private final LayoutEnumerator mLayoutEnumerator;
private int mLineCount;
private boolean mScanningLineForRealText;
}
Parameters:
Parameter | Type | Description |
---|---|---|
value | int | The corresponding int value. |
setLinksUpToDate(boolean value)
public void setLinksUpToDate(boolean value)
Indicates whether hyperlinks in a document are up-to-date.
Remarks:
Aspose.Words does not update this property.
Examples:
Shows how to work with document properties in the “Content” category.
public void content() throws Exception {
Document doc = new Document(getMyDir() + "Paragraphs.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// By using built in properties,
// we can treat document statistics such as word/page/character counts as metadata that can be glanced at without opening the document
// These properties are accessed by right clicking the file in Windows Explorer and navigating to Properties > Details > Content
// If we want to display this data inside the document, we can use fields such as NUMPAGES, NUMWORDS, NUMCHARS etc.
// Also, these values can also be viewed in Microsoft Word by navigating File > Properties > Advanced Properties > Statistics
// Page count: The PageCount property shows the page count in real time and its value can be assigned to the Pages property
// The "Pages" property stores the page count of the document.
Assert.assertEquals(6, properties.getPages());
// The "Words", "Characters", and "CharactersWithSpaces" built-in properties also display various document statistics,
// but we need to call the "UpdateWordCount" method on the whole document before we can expect them to contain accurate values.
doc.updateWordCount();
Assert.assertEquals(1035, properties.getWords());
Assert.assertEquals(6026, properties.getCharacters());
Assert.assertEquals(7041, properties.getCharactersWithSpaces());
// Count the number of lines in the document, and then assign the result to the "Lines" built-in property.
LineCounter lineCounter = new LineCounter(doc);
properties.setLines(lineCounter.getLineCount());
Assert.assertEquals(142, properties.getLines());
// Assign the number of Paragraph nodes in the document to the "Paragraphs" built-in property.
properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount());
Assert.assertEquals(29, properties.getParagraphs());
// Get an estimate of the file size of our document via the "Bytes" built-in property.
Assert.assertEquals(20310, properties.getBytes());
// Set a different template for our document, and then update the "Template" built-in property manually to reflect this change.
doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx");
Assert.assertEquals("Normal", properties.getTemplate());
properties.setTemplate(doc.getAttachedTemplate());
// "ContentStatus" is a descriptive built-in property.
properties.setContentStatus("Draft");
// Upon saving, the "ContentType" built-in property will contain the MIME type of the output save format.
Assert.assertEquals("", properties.getContentType());
// If the document contains links, and they are all up to date, we can set the "LinksUpToDate" property to "true".
Assert.assertFalse(properties.getLinksUpToDate());
doc.save(getArtifactsDir() + "DocumentProperties.Content.docx");
}
///
/// Counts the lines in a document.
/// Traverses the document's layout entities tree upon construction,
/// counting entities of the "Line" type that also contain real text.
///
private static class LineCounter {
public LineCounter(Document doc) throws Exception {
mLayoutEnumerator = new LayoutEnumerator(doc);
countLines();
}
public int getLineCount() {
return mLineCount;
}
private void countLines() throws Exception {
do {
if (mLayoutEnumerator.getType() == LayoutEntityType.LINE) {
mScanningLineForRealText = true;
}
if (mLayoutEnumerator.moveFirstChild()) {
if (mScanningLineForRealText && mLayoutEnumerator.getKind().startsWith("TEXT")) {
mLineCount++;
mScanningLineForRealText = false;
}
countLines();
mLayoutEnumerator.moveParent();
}
} while (mLayoutEnumerator.moveNext());
}
private final LayoutEnumerator mLayoutEnumerator;
private int mLineCount;
private boolean mScanningLineForRealText;
}
Parameters:
Parameter | Type | Description |
---|---|---|
value | boolean | The corresponding boolean value. |
setManager(String value)
public void setManager(String value)
Sets the manager property.
Examples:
Shows how to work with document properties in the “Origin” category.
// Open a document that we have created and edited using Microsoft Word.
Document doc = new Document(getMyDir() + "Properties.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// The following built-in properties contain information regarding the creation and editing of this document.
// We can right-click this document in Windows Explorer and find
// these properties via "Properties" -> "Details" -> "Origin" category.
// Fields such as PRINTDATE and EDITTIME can display these values in the document body.
System.out.println(MessageFormat.format("Created using {0}, on {1}", properties.getNameOfApplication(), properties.getCreatedTime()));
System.out.println(MessageFormat.format("Minutes spent editing: {0}", properties.getTotalEditingTime()));
System.out.println(MessageFormat.format("Date/time last printed: {0}", properties.getLastPrinted()));
System.out.println(MessageFormat.format("Template document: {0}", properties.getTemplate()));
// We can also change the values of built-in properties.
properties.setCompany("Doe Ltd.");
properties.setManager("Jane Doe");
properties.setVersion(5);
properties.setRevisionNumber(properties.getRevisionNumber() + 1);
// Microsoft Word updates the following properties automatically when we save the document.
// To use these properties with Aspose.Words, we will need to set values for them manually.
properties.setLastSavedBy("John Doe");
properties.setLastSavedTime(new Date());
// We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details" -> "Origin".
doc.save(getArtifactsDir() + "DocumentProperties.Origin.docx");
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.String | The manager property. |
setNameOfApplication(String value)
public void setNameOfApplication(String value)
Sets the name of the application.
Examples:
Shows how to work with document properties in the “Origin” category.
// Open a document that we have created and edited using Microsoft Word.
Document doc = new Document(getMyDir() + "Properties.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// The following built-in properties contain information regarding the creation and editing of this document.
// We can right-click this document in Windows Explorer and find
// these properties via "Properties" -> "Details" -> "Origin" category.
// Fields such as PRINTDATE and EDITTIME can display these values in the document body.
System.out.println(MessageFormat.format("Created using {0}, on {1}", properties.getNameOfApplication(), properties.getCreatedTime()));
System.out.println(MessageFormat.format("Minutes spent editing: {0}", properties.getTotalEditingTime()));
System.out.println(MessageFormat.format("Date/time last printed: {0}", properties.getLastPrinted()));
System.out.println(MessageFormat.format("Template document: {0}", properties.getTemplate()));
// We can also change the values of built-in properties.
properties.setCompany("Doe Ltd.");
properties.setManager("Jane Doe");
properties.setVersion(5);
properties.setRevisionNumber(properties.getRevisionNumber() + 1);
// Microsoft Word updates the following properties automatically when we save the document.
// To use these properties with Aspose.Words, we will need to set values for them manually.
properties.setLastSavedBy("John Doe");
properties.setLastSavedTime(new Date());
// We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details" -> "Origin".
doc.save(getArtifactsDir() + "DocumentProperties.Origin.docx");
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.String | The name of the application. |
setPages(int value)
public void setPages(int value)
Represents an estimate of the number of pages in the document.
Remarks:
Aspose.Words updates this property when you call Document.updatePageLayout().
Examples:
Shows how to work with document properties in the “Content” category.
public void content() throws Exception {
Document doc = new Document(getMyDir() + "Paragraphs.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// By using built in properties,
// we can treat document statistics such as word/page/character counts as metadata that can be glanced at without opening the document
// These properties are accessed by right clicking the file in Windows Explorer and navigating to Properties > Details > Content
// If we want to display this data inside the document, we can use fields such as NUMPAGES, NUMWORDS, NUMCHARS etc.
// Also, these values can also be viewed in Microsoft Word by navigating File > Properties > Advanced Properties > Statistics
// Page count: The PageCount property shows the page count in real time and its value can be assigned to the Pages property
// The "Pages" property stores the page count of the document.
Assert.assertEquals(6, properties.getPages());
// The "Words", "Characters", and "CharactersWithSpaces" built-in properties also display various document statistics,
// but we need to call the "UpdateWordCount" method on the whole document before we can expect them to contain accurate values.
doc.updateWordCount();
Assert.assertEquals(1035, properties.getWords());
Assert.assertEquals(6026, properties.getCharacters());
Assert.assertEquals(7041, properties.getCharactersWithSpaces());
// Count the number of lines in the document, and then assign the result to the "Lines" built-in property.
LineCounter lineCounter = new LineCounter(doc);
properties.setLines(lineCounter.getLineCount());
Assert.assertEquals(142, properties.getLines());
// Assign the number of Paragraph nodes in the document to the "Paragraphs" built-in property.
properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount());
Assert.assertEquals(29, properties.getParagraphs());
// Get an estimate of the file size of our document via the "Bytes" built-in property.
Assert.assertEquals(20310, properties.getBytes());
// Set a different template for our document, and then update the "Template" built-in property manually to reflect this change.
doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx");
Assert.assertEquals("Normal", properties.getTemplate());
properties.setTemplate(doc.getAttachedTemplate());
// "ContentStatus" is a descriptive built-in property.
properties.setContentStatus("Draft");
// Upon saving, the "ContentType" built-in property will contain the MIME type of the output save format.
Assert.assertEquals("", properties.getContentType());
// If the document contains links, and they are all up to date, we can set the "LinksUpToDate" property to "true".
Assert.assertFalse(properties.getLinksUpToDate());
doc.save(getArtifactsDir() + "DocumentProperties.Content.docx");
}
///
/// Counts the lines in a document.
/// Traverses the document's layout entities tree upon construction,
/// counting entities of the "Line" type that also contain real text.
///
private static class LineCounter {
public LineCounter(Document doc) throws Exception {
mLayoutEnumerator = new LayoutEnumerator(doc);
countLines();
}
public int getLineCount() {
return mLineCount;
}
private void countLines() throws Exception {
do {
if (mLayoutEnumerator.getType() == LayoutEntityType.LINE) {
mScanningLineForRealText = true;
}
if (mLayoutEnumerator.moveFirstChild()) {
if (mScanningLineForRealText && mLayoutEnumerator.getKind().startsWith("TEXT")) {
mLineCount++;
mScanningLineForRealText = false;
}
countLines();
mLayoutEnumerator.moveParent();
}
} while (mLayoutEnumerator.moveNext());
}
private final LayoutEnumerator mLayoutEnumerator;
private int mLineCount;
private boolean mScanningLineForRealText;
}
Parameters:
Parameter | Type | Description |
---|---|---|
value | int | The corresponding int value. |
setParagraphs(int value)
public void setParagraphs(int value)
Represents an estimate of the number of paragraphs in the document.
Remarks:
Aspose.Words updates this property when you call Document.updateWordCount().
Examples:
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());
Shows how to work with document properties in the “Content” category.
public void content() throws Exception {
Document doc = new Document(getMyDir() + "Paragraphs.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// By using built in properties,
// we can treat document statistics such as word/page/character counts as metadata that can be glanced at without opening the document
// These properties are accessed by right clicking the file in Windows Explorer and navigating to Properties > Details > Content
// If we want to display this data inside the document, we can use fields such as NUMPAGES, NUMWORDS, NUMCHARS etc.
// Also, these values can also be viewed in Microsoft Word by navigating File > Properties > Advanced Properties > Statistics
// Page count: The PageCount property shows the page count in real time and its value can be assigned to the Pages property
// The "Pages" property stores the page count of the document.
Assert.assertEquals(6, properties.getPages());
// The "Words", "Characters", and "CharactersWithSpaces" built-in properties also display various document statistics,
// but we need to call the "UpdateWordCount" method on the whole document before we can expect them to contain accurate values.
doc.updateWordCount();
Assert.assertEquals(1035, properties.getWords());
Assert.assertEquals(6026, properties.getCharacters());
Assert.assertEquals(7041, properties.getCharactersWithSpaces());
// Count the number of lines in the document, and then assign the result to the "Lines" built-in property.
LineCounter lineCounter = new LineCounter(doc);
properties.setLines(lineCounter.getLineCount());
Assert.assertEquals(142, properties.getLines());
// Assign the number of Paragraph nodes in the document to the "Paragraphs" built-in property.
properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount());
Assert.assertEquals(29, properties.getParagraphs());
// Get an estimate of the file size of our document via the "Bytes" built-in property.
Assert.assertEquals(20310, properties.getBytes());
// Set a different template for our document, and then update the "Template" built-in property manually to reflect this change.
doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx");
Assert.assertEquals("Normal", properties.getTemplate());
properties.setTemplate(doc.getAttachedTemplate());
// "ContentStatus" is a descriptive built-in property.
properties.setContentStatus("Draft");
// Upon saving, the "ContentType" built-in property will contain the MIME type of the output save format.
Assert.assertEquals("", properties.getContentType());
// If the document contains links, and they are all up to date, we can set the "LinksUpToDate" property to "true".
Assert.assertFalse(properties.getLinksUpToDate());
doc.save(getArtifactsDir() + "DocumentProperties.Content.docx");
}
///
/// Counts the lines in a document.
/// Traverses the document's layout entities tree upon construction,
/// counting entities of the "Line" type that also contain real text.
///
private static class LineCounter {
public LineCounter(Document doc) throws Exception {
mLayoutEnumerator = new LayoutEnumerator(doc);
countLines();
}
public int getLineCount() {
return mLineCount;
}
private void countLines() throws Exception {
do {
if (mLayoutEnumerator.getType() == LayoutEntityType.LINE) {
mScanningLineForRealText = true;
}
if (mLayoutEnumerator.moveFirstChild()) {
if (mScanningLineForRealText && mLayoutEnumerator.getKind().startsWith("TEXT")) {
mLineCount++;
mScanningLineForRealText = false;
}
countLines();
mLayoutEnumerator.moveParent();
}
} while (mLayoutEnumerator.moveNext());
}
private final LayoutEnumerator mLayoutEnumerator;
private int mLineCount;
private boolean mScanningLineForRealText;
}
Parameters:
Parameter | Type | Description |
---|---|---|
value | int | The corresponding int value. |
setRevisionNumber(int value)
public void setRevisionNumber(int value)
Sets the document revision number.
Remarks:
Aspose.Words does not update this property.
Examples:
Shows how to work with REVNUM fields.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("Current revision #");
// Insert a REVNUM field, which displays the document's current revision number property.
FieldRevNum field = (FieldRevNum) builder.insertField(FieldType.FIELD_REVISION_NUM, true);
Assert.assertEquals(" REVNUM ", field.getFieldCode());
Assert.assertEquals("1", field.getResult());
Assert.assertEquals(1, doc.getBuiltInDocumentProperties().getRevisionNumber());
// This property counts how many times a document has been saved in Microsoft Word,
// and is unrelated to tracked revisions. We can find it by right clicking the document in Windows Explorer
// via Properties -> Details. We can update this property manually.
doc.getBuiltInDocumentProperties().setRevisionNumber(doc.getBuiltInDocumentProperties().getRevisionNumber() + 1);
field.update();
Assert.assertEquals("2", field.getResult());
Shows how to work with document properties in the “Origin” category.
// Open a document that we have created and edited using Microsoft Word.
Document doc = new Document(getMyDir() + "Properties.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// The following built-in properties contain information regarding the creation and editing of this document.
// We can right-click this document in Windows Explorer and find
// these properties via "Properties" -> "Details" -> "Origin" category.
// Fields such as PRINTDATE and EDITTIME can display these values in the document body.
System.out.println(MessageFormat.format("Created using {0}, on {1}", properties.getNameOfApplication(), properties.getCreatedTime()));
System.out.println(MessageFormat.format("Minutes spent editing: {0}", properties.getTotalEditingTime()));
System.out.println(MessageFormat.format("Date/time last printed: {0}", properties.getLastPrinted()));
System.out.println(MessageFormat.format("Template document: {0}", properties.getTemplate()));
// We can also change the values of built-in properties.
properties.setCompany("Doe Ltd.");
properties.setManager("Jane Doe");
properties.setVersion(5);
properties.setRevisionNumber(properties.getRevisionNumber() + 1);
// Microsoft Word updates the following properties automatically when we save the document.
// To use these properties with Aspose.Words, we will need to set values for them manually.
properties.setLastSavedBy("John Doe");
properties.setLastSavedTime(new Date());
// We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details" -> "Origin".
doc.save(getArtifactsDir() + "DocumentProperties.Origin.docx");
Parameters:
Parameter | Type | Description |
---|---|---|
value | int | The document revision number. |
setSecurity(int value)
public void setSecurity(int value)
Specifies the security level of a document as a numeric value.
Remarks:
Use this property for informational purposes only because Microsoft Word does not always set this property. This property is available in DOC and OOXML documents only.
To protect or unprotect a document use the M:Aspose.Words.Document.Protect(Aspose.Words.ProtectionType,System.String) and Document.unprotect() methods.
Aspose.Words updates this property to a correct value before saving a document.
Examples:
Shows how to use document properties to display the security level of a document.
Document doc = new Document();
Assert.assertEquals(DocumentSecurity.NONE, doc.getBuiltInDocumentProperties().getSecurity());
// If we configure a document to be read-only, it will display this status using the "Security" built-in property.
doc.getWriteProtection().setReadOnlyRecommended(true);
doc.save(getArtifactsDir() + "DocumentProperties.Security.ReadOnlyRecommended.docx");
Assert.assertEquals(DocumentSecurity.READ_ONLY_RECOMMENDED,
new Document(getArtifactsDir() + "DocumentProperties.Security.ReadOnlyRecommended.docx").getBuiltInDocumentProperties().getSecurity());
// Write-protect a document, and then verify its security level.
doc = new Document();
Assert.assertFalse(doc.getWriteProtection().isWriteProtected());
doc.getWriteProtection().setPassword("MyPassword");
Assert.assertTrue(doc.getWriteProtection().validatePassword("MyPassword"));
Assert.assertTrue(doc.getWriteProtection().isWriteProtected());
doc.save(getArtifactsDir() + "DocumentProperties.Security.ReadOnlyEnforced.docx");
Assert.assertEquals(DocumentSecurity.READ_ONLY_ENFORCED,
new Document(getArtifactsDir() + "DocumentProperties.Security.ReadOnlyEnforced.docx").getBuiltInDocumentProperties().getSecurity());
// "Security" is a descriptive property. We can edit its value manually.
doc = new Document();
doc.protect(ProtectionType.ALLOW_ONLY_COMMENTS, "MyPassword");
doc.getBuiltInDocumentProperties().setSecurity(DocumentSecurity.READ_ONLY_EXCEPT_ANNOTATIONS);
doc.save(getArtifactsDir() + "DocumentProperties.Security.ReadOnlyExceptAnnotations.docx");
Assert.assertEquals(DocumentSecurity.READ_ONLY_EXCEPT_ANNOTATIONS,
new Document(getArtifactsDir() + "DocumentProperties.Security.ReadOnlyExceptAnnotations.docx").getBuiltInDocumentProperties().getSecurity());
Parameters:
Parameter | Type | Description |
---|---|---|
value | int | The corresponding int value. The value must be a bitwise combination of DocumentSecurity constants. |
setSubject(String value)
public void setSubject(String value)
Sets the subject of the document.
Examples:
Shows how to work with built-in document properties in the “Description” category.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// Below are four built-in document properties that have fields that can display their values in the document body.
// 1 - "Author" property, which we can display using an AUTHOR field:
properties.setAuthor("John Doe");
builder.write("Author:\t");
builder.insertField(FieldType.FIELD_AUTHOR, true);
// 2 - "Title" property, which we can display using a TITLE field:
properties.setTitle("John's Document");
builder.write("\nDoc title:\t");
builder.insertField(FieldType.FIELD_TITLE, true);
// 3 - "Subject" property, which we can display using a SUBJECT field:
properties.setSubject("My subject");
builder.write("\nSubject:\t");
builder.insertField(FieldType.FIELD_SUBJECT, true);
// 4 - "Comments" property, which we can display using a COMMENTS field:
properties.setComments(MessageFormat.format("This is {0}''s document about {1}", properties.getAuthor(), properties.getSubject()));
builder.write("\nComments:\t\"");
builder.insertField(FieldType.FIELD_COMMENTS, true);
builder.write("\"");
// The "Category" built-in property does not have a field that can display its value.
properties.setCategory("My category");
// We can set multiple keywords for a document by separating the string value of the "Keywords" property with semicolons.
properties.setKeywords("Tag 1; Tag 2; Tag 3");
// We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details".
// The "Author" built-in property is in the "Origin" group, and the others are in the "Description" group.
doc.save(getArtifactsDir() + "DocumentProperties.Description.docx");
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.String | The subject of the document. |
setTemplate(String value)
public void setTemplate(String value)
Sets the informational name of the document template.
Remarks:
In Microsoft Word, this property is for informational purposes only and usually contains only the file name of the template without the path.
Empty string means the document is attached to the Normal template.
To get or set the actual name of the attached template, use the Document.getAttachedTemplate() / Document.setAttachedTemplate(java.lang.String) property.
Examples:
Shows how to work with document properties in the “Origin” category.
// Open a document that we have created and edited using Microsoft Word.
Document doc = new Document(getMyDir() + "Properties.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// The following built-in properties contain information regarding the creation and editing of this document.
// We can right-click this document in Windows Explorer and find
// these properties via "Properties" -> "Details" -> "Origin" category.
// Fields such as PRINTDATE and EDITTIME can display these values in the document body.
System.out.println(MessageFormat.format("Created using {0}, on {1}", properties.getNameOfApplication(), properties.getCreatedTime()));
System.out.println(MessageFormat.format("Minutes spent editing: {0}", properties.getTotalEditingTime()));
System.out.println(MessageFormat.format("Date/time last printed: {0}", properties.getLastPrinted()));
System.out.println(MessageFormat.format("Template document: {0}", properties.getTemplate()));
// We can also change the values of built-in properties.
properties.setCompany("Doe Ltd.");
properties.setManager("Jane Doe");
properties.setVersion(5);
properties.setRevisionNumber(properties.getRevisionNumber() + 1);
// Microsoft Word updates the following properties automatically when we save the document.
// To use these properties with Aspose.Words, we will need to set values for them manually.
properties.setLastSavedBy("John Doe");
properties.setLastSavedTime(new Date());
// We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details" -> "Origin".
doc.save(getArtifactsDir() + "DocumentProperties.Origin.docx");
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.String | The informational name of the document template. |
setThumbnail(byte[] value)
public void setThumbnail(byte[] value)
Gets or sets the thumbnail of the document.
Parameters:
Parameter | Type | Description |
---|---|---|
value | byte[] | The corresponding byte[] value. |
setTitle(String value)
public void setTitle(String value)
Sets the title of the document.
Examples:
Shows how to work with built-in document properties in the “Description” category.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// Below are four built-in document properties that have fields that can display their values in the document body.
// 1 - "Author" property, which we can display using an AUTHOR field:
properties.setAuthor("John Doe");
builder.write("Author:\t");
builder.insertField(FieldType.FIELD_AUTHOR, true);
// 2 - "Title" property, which we can display using a TITLE field:
properties.setTitle("John's Document");
builder.write("\nDoc title:\t");
builder.insertField(FieldType.FIELD_TITLE, true);
// 3 - "Subject" property, which we can display using a SUBJECT field:
properties.setSubject("My subject");
builder.write("\nSubject:\t");
builder.insertField(FieldType.FIELD_SUBJECT, true);
// 4 - "Comments" property, which we can display using a COMMENTS field:
properties.setComments(MessageFormat.format("This is {0}''s document about {1}", properties.getAuthor(), properties.getSubject()));
builder.write("\nComments:\t\"");
builder.insertField(FieldType.FIELD_COMMENTS, true);
builder.write("\"");
// The "Category" built-in property does not have a field that can display its value.
properties.setCategory("My category");
// We can set multiple keywords for a document by separating the string value of the "Keywords" property with semicolons.
properties.setKeywords("Tag 1; Tag 2; Tag 3");
// We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details".
// The "Author" built-in property is in the "Origin" group, and the others are in the "Description" group.
doc.save(getArtifactsDir() + "DocumentProperties.Description.docx");
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.String | The title of the document. |
setTitlesOfParts(String[] value)
public void setTitlesOfParts(String[] value)
Each string in the array specifies the name of a part in the document.
Remarks:
Aspose.Words does not update this property.
Examples:
Shows the relationship between “HeadingPairs” and “TitlesOfParts” properties.
Document doc = new Document(getMyDir() + "Heading pairs and titles of parts.docx");
// We can find the combined values of these collections via
// "File" -> "Properties" -> "Advanced Properties" -> "Contents" tab.
// The HeadingPairs property is a collection of pairs that
// determines how many document parts a heading spans across.
Object[] headingPairs = doc.getBuiltInDocumentProperties().getHeadingPairs();
// The TitlesOfParts property contains the names of parts that belong to the above headings.
String[] titlesOfParts = doc.getBuiltInDocumentProperties().getTitlesOfParts();
int headingPairsIndex = 0;
int titlesOfPartsIndex = 0;
while (headingPairsIndex < headingPairs.length) {
System.out.println(MessageFormat.format("Parts for {0}:", headingPairs[headingPairsIndex++]));
int partsCount = (int) headingPairs[headingPairsIndex++];
for (int i = 0; i < partsCount; i++)
System.out.println(MessageFormat.format("\t\"{0}\"", titlesOfParts[titlesOfPartsIndex++]));
}
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.String[] | The corresponding java.lang.String[] value. |
setTotalEditingTime(int value)
public void setTotalEditingTime(int value)
Sets the total editing time in minutes.
Examples:
Shows how to work with document properties in the “Origin” category.
// Open a document that we have created and edited using Microsoft Word.
Document doc = new Document(getMyDir() + "Properties.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// The following built-in properties contain information regarding the creation and editing of this document.
// We can right-click this document in Windows Explorer and find
// these properties via "Properties" -> "Details" -> "Origin" category.
// Fields such as PRINTDATE and EDITTIME can display these values in the document body.
System.out.println(MessageFormat.format("Created using {0}, on {1}", properties.getNameOfApplication(), properties.getCreatedTime()));
System.out.println(MessageFormat.format("Minutes spent editing: {0}", properties.getTotalEditingTime()));
System.out.println(MessageFormat.format("Date/time last printed: {0}", properties.getLastPrinted()));
System.out.println(MessageFormat.format("Template document: {0}", properties.getTemplate()));
// We can also change the values of built-in properties.
properties.setCompany("Doe Ltd.");
properties.setManager("Jane Doe");
properties.setVersion(5);
properties.setRevisionNumber(properties.getRevisionNumber() + 1);
// Microsoft Word updates the following properties automatically when we save the document.
// To use these properties with Aspose.Words, we will need to set values for them manually.
properties.setLastSavedBy("John Doe");
properties.setLastSavedTime(new Date());
// We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details" -> "Origin".
doc.save(getArtifactsDir() + "DocumentProperties.Origin.docx");
Parameters:
Parameter | Type | Description |
---|---|---|
value | int | The total editing time in minutes. |
setVersion(int value)
public void setVersion(int value)
Represents the version number of the application that created the document.
Remarks:
When a document was created by Microsoft Word, then high 16 bit represent the major version and low 16 bit represent the build number.
Examples:
Shows how to work with document properties in the “Origin” category.
// Open a document that we have created and edited using Microsoft Word.
Document doc = new Document(getMyDir() + "Properties.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// The following built-in properties contain information regarding the creation and editing of this document.
// We can right-click this document in Windows Explorer and find
// these properties via "Properties" -> "Details" -> "Origin" category.
// Fields such as PRINTDATE and EDITTIME can display these values in the document body.
System.out.println(MessageFormat.format("Created using {0}, on {1}", properties.getNameOfApplication(), properties.getCreatedTime()));
System.out.println(MessageFormat.format("Minutes spent editing: {0}", properties.getTotalEditingTime()));
System.out.println(MessageFormat.format("Date/time last printed: {0}", properties.getLastPrinted()));
System.out.println(MessageFormat.format("Template document: {0}", properties.getTemplate()));
// We can also change the values of built-in properties.
properties.setCompany("Doe Ltd.");
properties.setManager("Jane Doe");
properties.setVersion(5);
properties.setRevisionNumber(properties.getRevisionNumber() + 1);
// Microsoft Word updates the following properties automatically when we save the document.
// To use these properties with Aspose.Words, we will need to set values for them manually.
properties.setLastSavedBy("John Doe");
properties.setLastSavedTime(new Date());
// We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details" -> "Origin".
doc.save(getArtifactsDir() + "DocumentProperties.Origin.docx");
Parameters:
Parameter | Type | Description |
---|---|---|
value | int | The corresponding int value. |
setWords(int value)
public void setWords(int value)
Represents an estimate of the number of words in the document.
Remarks:
Aspose.Words updates this property when you call Document.updateWordCount().
Examples:
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());
Shows how to work with document properties in the “Content” category.
public void content() throws Exception {
Document doc = new Document(getMyDir() + "Paragraphs.docx");
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();
// By using built in properties,
// we can treat document statistics such as word/page/character counts as metadata that can be glanced at without opening the document
// These properties are accessed by right clicking the file in Windows Explorer and navigating to Properties > Details > Content
// If we want to display this data inside the document, we can use fields such as NUMPAGES, NUMWORDS, NUMCHARS etc.
// Also, these values can also be viewed in Microsoft Word by navigating File > Properties > Advanced Properties > Statistics
// Page count: The PageCount property shows the page count in real time and its value can be assigned to the Pages property
// The "Pages" property stores the page count of the document.
Assert.assertEquals(6, properties.getPages());
// The "Words", "Characters", and "CharactersWithSpaces" built-in properties also display various document statistics,
// but we need to call the "UpdateWordCount" method on the whole document before we can expect them to contain accurate values.
doc.updateWordCount();
Assert.assertEquals(1035, properties.getWords());
Assert.assertEquals(6026, properties.getCharacters());
Assert.assertEquals(7041, properties.getCharactersWithSpaces());
// Count the number of lines in the document, and then assign the result to the "Lines" built-in property.
LineCounter lineCounter = new LineCounter(doc);
properties.setLines(lineCounter.getLineCount());
Assert.assertEquals(142, properties.getLines());
// Assign the number of Paragraph nodes in the document to the "Paragraphs" built-in property.
properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount());
Assert.assertEquals(29, properties.getParagraphs());
// Get an estimate of the file size of our document via the "Bytes" built-in property.
Assert.assertEquals(20310, properties.getBytes());
// Set a different template for our document, and then update the "Template" built-in property manually to reflect this change.
doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx");
Assert.assertEquals("Normal", properties.getTemplate());
properties.setTemplate(doc.getAttachedTemplate());
// "ContentStatus" is a descriptive built-in property.
properties.setContentStatus("Draft");
// Upon saving, the "ContentType" built-in property will contain the MIME type of the output save format.
Assert.assertEquals("", properties.getContentType());
// If the document contains links, and they are all up to date, we can set the "LinksUpToDate" property to "true".
Assert.assertFalse(properties.getLinksUpToDate());
doc.save(getArtifactsDir() + "DocumentProperties.Content.docx");
}
///
/// Counts the lines in a document.
/// Traverses the document's layout entities tree upon construction,
/// counting entities of the "Line" type that also contain real text.
///
private static class LineCounter {
public LineCounter(Document doc) throws Exception {
mLayoutEnumerator = new LayoutEnumerator(doc);
countLines();
}
public int getLineCount() {
return mLineCount;
}
private void countLines() throws Exception {
do {
if (mLayoutEnumerator.getType() == LayoutEntityType.LINE) {
mScanningLineForRealText = true;
}
if (mLayoutEnumerator.moveFirstChild()) {
if (mScanningLineForRealText && mLayoutEnumerator.getKind().startsWith("TEXT")) {
mLineCount++;
mScanningLineForRealText = false;
}
countLines();
mLayoutEnumerator.moveParent();
}
} while (mLayoutEnumerator.moveNext());
}
private final LayoutEnumerator mLayoutEnumerator;
private int mLineCount;
private boolean mScanningLineForRealText;
}
Parameters:
Parameter | Type | Description |
---|---|---|
value | int | The corresponding int value. |