java.lang.ObjectDocumentPropertyCollection
com.aspose.words.BuiltInDocumentProperties
public class BuiltInDocumentProperties
Provides access to The names of the properties are case-insensitive. The properties in the collection are sorted alphabetically by name. Example:
Document doc = new Document(getMyDir() + "Properties.docx");
// Some information about the document is stored in member attributes, and can be accessed like this
System.out.println(MessageFormat.format("Document filename:\n\t \"{0}\"", doc.getOriginalFileName()));
// The majority of metadata, such as author name, file size,
// word/page counts can be found in the built in properties collection like this
System.out.println("Built-in Properties:");
for (DocumentProperty docProperty : doc.getBuiltInDocumentProperties()) {
System.out.println(docProperty.getName());
System.out.println(MessageFormat.format("\tType:\t{0}", docProperty.getType()));
}
Property Getters/Setters Summary | ||
---|---|---|
java.lang.String | getAuthor() | |
void | setAuthor(java.lang.String value) | |
Gets or sets the name of the document's author. | ||
int | getBytes() | |
void | setBytes(int value) | |
Represents an estimate of the number of bytes in the document. | ||
java.lang.String | getCategory() | |
void | setCategory(java.lang.String value) | |
Gets or sets the category of the document. | ||
int | getCharacters() | |
void | setCharacters(int value) | |
Represents an estimate of the number of characters in the document. | ||
int | getCharactersWithSpaces() | |
void | setCharactersWithSpaces(int value) | |
Represents an estimate of the number of characters (including spaces) in the document. | ||
java.lang.String | getComments() | |
void | setComments(java.lang.String value) | |
Gets or sets the document comments. | ||
java.lang.String | getCompany() | |
void | setCompany(java.lang.String value) | |
Gets or sets the company property. | ||
java.lang.String | getContentStatus() | |
void | setContentStatus(java.lang.String value) | |
Gets or sets the ContentStatus of the document. | ||
java.lang.String | getContentType() | |
void | setContentType(java.lang.String value) | |
Gets or sets the ContentStatus of the document. | ||
int | getCount() | → inherited from DocumentPropertyCollection |
Gets number of items in the collection. | ||
java.util.Date | getCreatedTime() | |
void | setCreatedTime(java.util.Date value) | |
Gets or sets date of the document creation in UTC. | ||
java.lang.Object[] | getHeadingPairs() | |
void | setHeadingPairs(java.lang.Object[] value) | |
Specifies document headings and their names. | ||
java.lang.String | getHyperlinkBase() | |
void | setHyperlinkBase(java.lang.String value) | |
Specifies the base string used for evaluating relative hyperlinks in this document. | ||
java.lang.String | getKeywords() | |
void | setKeywords(java.lang.String value) | |
Gets or sets the document keywords. | ||
java.util.Date | getLastPrinted() | |
void | setLastPrinted(java.util.Date value) | |
Gets or sets the date when the document was last printed in UTC. | ||
java.lang.String | getLastSavedBy() | |
void | setLastSavedBy(java.lang.String value) | |
Gets or sets the name of the last author. | ||
java.util.Date | getLastSavedTime() | |
void | setLastSavedTime(java.util.Date value) | |
Gets or sets the time of the last save in UTC. | ||
int | getLines() | |
void | setLines(int value) | |
Represents an estimate of the number of lines in the document. | ||
boolean | getLinksUpToDate() | |
void | setLinksUpToDate(boolean value) | |
Indicates whether hyperlinks in a document are up-to-date. | ||
java.lang.String | getManager() | |
void | setManager(java.lang.String value) | |
Gets or sets the manager property. | ||
java.lang.String | getNameOfApplication() | |
void | setNameOfApplication(java.lang.String value) | |
Gets or sets the name of the application. | ||
int | getPages() | |
void | setPages(int value) | |
Represents an estimate of the number of pages in the document. | ||
int | getParagraphs() | |
void | setParagraphs(int value) | |
Represents an estimate of the number of paragraphs in the document. | ||
int | getRevisionNumber() | |
void | setRevisionNumber(int value) | |
Gets or sets the document revision number. | ||
int | getSecurity() | |
void | setSecurity(int value) | |
Specifies the security level of a document as a numeric value. The value of the property is DocumentSecurity integer constant. | ||
java.lang.String | getSubject() | |
void | setSubject(java.lang.String value) | |
Gets or sets the subject of the document. | ||
java.lang.String | getTemplate() | |
void | setTemplate(java.lang.String value) | |
Gets or sets the informational name of the document template. | ||
byte[] | getThumbnail() | |
void | setThumbnail(byte[] value) | |
Gets or sets the thumbnail of the document. |
||
java.lang.String | getTitle() | |
void | setTitle(java.lang.String value) | |
Gets or sets the title of the document. | ||
java.lang.String[] | getTitlesOfParts() | |
void | setTitlesOfParts(java.lang.String[] value) | |
Each string in the array specifies the name of a part in the document. | ||
int | getTotalEditingTime() | |
void | setTotalEditingTime(int value) | |
Gets or sets the total editing time in minutes. | ||
int | getVersion() | |
void | setVersion(int value) | |
Represents the version number of the application that created the document. | ||
int | getWords() | |
void | setWords(int value) | |
Represents an estimate of the number of words in the document. | ||
DocumentProperty | get(int index) | → inherited from DocumentPropertyCollection |
Returns a |
||
DocumentProperty | get(java.lang.String name) | |
Returns a |
Method Summary | ||
---|---|---|
void | clear() | → inherited from DocumentPropertyCollection |
Removes all properties from the collection. | ||
boolean | contains(java.lang.String name) | → inherited from DocumentPropertyCollection |
Returns true if a property with the specified name exists in the collection. | ||
int | indexOf(java.lang.String name) | → inherited from DocumentPropertyCollection |
Gets the index of a property by name. | ||
java.util.Iterator<DocumentProperty> | iterator() | → inherited from DocumentPropertyCollection |
Returns an iterator object that can be used to iterate over all items in the collection. | ||
void | remove(java.lang.String name) | → inherited from DocumentPropertyCollection |
Removes a property with the specified name from the collection. | ||
void | removeAt(int index) | → inherited from DocumentPropertyCollection |
Removes a property at the specified index. |
Property Getters/Setters Detail |
---|
getAuthor/setAuthor | |
public java.lang.String getAuthor() / public void setAuthor(java.lang.String value) |
Example:
Shows how to work with document properties in the "Description" category.Document doc = new Document(); // The properties we will work with are members of the BuiltInDocumentProperties attribute BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // Set the values of some descriptive properties // These are metadata that can be glanced at without opening the document in the "Details" or "Content" folder views in Windows Explorer // The "Details" view has columns dedicated to these properties // Fields such as AUTHOR, SUBJECT, TITLE etc. can be used to display these values inside the document properties.setAuthor("John Doe"); properties.setTitle("John's Document"); properties.setSubject("My subject"); properties.setCategory("My category"); properties.setComments(MessageFormat.format("This is {0}'s document about {1}", properties.getAuthor(), properties.getSubject())); // Tags can be used as keywords and are separated by semicolons properties.setKeywords("Tag 1; Tag 2; Tag 3"); // When right clicking the document file in Windows Explorer, these properties are found in Properties > Details > Description doc.save(getArtifactsDir() + "Properties.Description.docx");
getBytes/setBytes | |
public int getBytes() / public void setBytes(int value) |
Microsoft Word does not always set this property.
Aspose.Words does not update this property.
Example:
Shows how to work with document properties in the "Content" category.public void content() throws Exception { // Open a document with a couple paragraphs of content Document doc = new Document(getMyDir() + "Paragraphs.docx"); // The properties we will work with are members of the BuiltInDocumentProperties attribute 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 attribute shows the page count in real time and its value can be assigned to the Pages property properties.setPages(doc.getPageCount()); Assert.assertEquals(6, properties.getPages()); // Word count: The UpdateWordCount() automatically assigns the real time word/character counts to the respective built in properties doc.updateWordCount(); Assert.assertEquals(1035, properties.getWords()); Assert.assertEquals(6026, properties.getCharacters()); Assert.assertEquals(7041, properties.getCharactersWithSpaces()); // Line count: Count the lines in a document and assign value to the Lines property LineCounter lineCounter = new LineCounter(doc); properties.setLines(lineCounter.getLineCount()); Assert.assertEquals(properties.getLines(), 142); // Paragraph count: Assign the size of the count of child Paragraph-nodes to the Paragraphs built in property properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount()); Assert.assertEquals(29, properties.getParagraphs()); // Check the real file size of our document Assert.assertEquals(20310, properties.getBytes()); // Template: The Template attribute can reflect the filename of the attached template document doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx"); Assert.assertEquals("Normal", properties.getTemplate()); properties.setTemplate(doc.getAttachedTemplate()); // Content status: This is a descriptive field properties.setContentStatus("Draft"); // Content type: Upon saving, any value we assign to this field will be overwritten by 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 this to true Assert.assertFalse(properties.getLinksUpToDate()); doc.save(getArtifactsDir() + "Properties.Content.docx"); } /// <summary> /// Util class that counts the lines in a document. /// Upon construction, traverses the document's layout entities tree, /// counting entities of the "Line" type that also contain real text. /// </summary> 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 LayoutEnumerator mLayoutEnumerator; private int mLineCount; private boolean mScanningLineForRealText; }
getCategory/setCategory | |
public java.lang.String getCategory() / public void setCategory(java.lang.String value) |
Example:
Shows how to work with document properties in the "Description" category.Document doc = new Document(); // The properties we will work with are members of the BuiltInDocumentProperties attribute BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // Set the values of some descriptive properties // These are metadata that can be glanced at without opening the document in the "Details" or "Content" folder views in Windows Explorer // The "Details" view has columns dedicated to these properties // Fields such as AUTHOR, SUBJECT, TITLE etc. can be used to display these values inside the document properties.setAuthor("John Doe"); properties.setTitle("John's Document"); properties.setSubject("My subject"); properties.setCategory("My category"); properties.setComments(MessageFormat.format("This is {0}'s document about {1}", properties.getAuthor(), properties.getSubject())); // Tags can be used as keywords and are separated by semicolons properties.setKeywords("Tag 1; Tag 2; Tag 3"); // When right clicking the document file in Windows Explorer, these properties are found in Properties > Details > Description doc.save(getArtifactsDir() + "Properties.Description.docx");
getCharacters/setCharacters | |
public int getCharacters() / public void setCharacters(int value) |
Aspose.Words updates this property when you call
Example:
Shows how to update all list labels in a document.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Add a paragraph of text to the document 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."); // Document metrics are not tracked in code 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()); // We will need to call this method to update them doc.updateWordCount(); // Check the values of the properties Assert.assertEquals(196, doc.getBuiltInDocumentProperties().getCharacters()); Assert.assertEquals(36, doc.getBuiltInDocumentProperties().getWords()); Assert.assertEquals(2, doc.getBuiltInDocumentProperties().getParagraphs()); Assert.assertEquals(1, doc.getBuiltInDocumentProperties().getLines()); // To also get the line count as it would appear in Microsoft Word, // we will need to pass "true" to UpdateWordCount() doc.updateWordCount(true); Assert.assertEquals(4, doc.getBuiltInDocumentProperties().getLines());
Example:
Shows how to work with document properties in the "Content" category.public void content() throws Exception { // Open a document with a couple paragraphs of content Document doc = new Document(getMyDir() + "Paragraphs.docx"); // The properties we will work with are members of the BuiltInDocumentProperties attribute 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 attribute shows the page count in real time and its value can be assigned to the Pages property properties.setPages(doc.getPageCount()); Assert.assertEquals(6, properties.getPages()); // Word count: The UpdateWordCount() automatically assigns the real time word/character counts to the respective built in properties doc.updateWordCount(); Assert.assertEquals(1035, properties.getWords()); Assert.assertEquals(6026, properties.getCharacters()); Assert.assertEquals(7041, properties.getCharactersWithSpaces()); // Line count: Count the lines in a document and assign value to the Lines property LineCounter lineCounter = new LineCounter(doc); properties.setLines(lineCounter.getLineCount()); Assert.assertEquals(properties.getLines(), 142); // Paragraph count: Assign the size of the count of child Paragraph-nodes to the Paragraphs built in property properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount()); Assert.assertEquals(29, properties.getParagraphs()); // Check the real file size of our document Assert.assertEquals(20310, properties.getBytes()); // Template: The Template attribute can reflect the filename of the attached template document doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx"); Assert.assertEquals("Normal", properties.getTemplate()); properties.setTemplate(doc.getAttachedTemplate()); // Content status: This is a descriptive field properties.setContentStatus("Draft"); // Content type: Upon saving, any value we assign to this field will be overwritten by 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 this to true Assert.assertFalse(properties.getLinksUpToDate()); doc.save(getArtifactsDir() + "Properties.Content.docx"); } /// <summary> /// Util class that counts the lines in a document. /// Upon construction, traverses the document's layout entities tree, /// counting entities of the "Line" type that also contain real text. /// </summary> 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 LayoutEnumerator mLayoutEnumerator; private int mLineCount; private boolean mScanningLineForRealText; }
getCharactersWithSpaces/setCharactersWithSpaces | |
public int getCharactersWithSpaces() / public void setCharactersWithSpaces(int value) |
Aspose.Words updates this property when you call
Example:
Shows how to work with document properties in the "Content" category.public void content() throws Exception { // Open a document with a couple paragraphs of content Document doc = new Document(getMyDir() + "Paragraphs.docx"); // The properties we will work with are members of the BuiltInDocumentProperties attribute 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 attribute shows the page count in real time and its value can be assigned to the Pages property properties.setPages(doc.getPageCount()); Assert.assertEquals(6, properties.getPages()); // Word count: The UpdateWordCount() automatically assigns the real time word/character counts to the respective built in properties doc.updateWordCount(); Assert.assertEquals(1035, properties.getWords()); Assert.assertEquals(6026, properties.getCharacters()); Assert.assertEquals(7041, properties.getCharactersWithSpaces()); // Line count: Count the lines in a document and assign value to the Lines property LineCounter lineCounter = new LineCounter(doc); properties.setLines(lineCounter.getLineCount()); Assert.assertEquals(properties.getLines(), 142); // Paragraph count: Assign the size of the count of child Paragraph-nodes to the Paragraphs built in property properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount()); Assert.assertEquals(29, properties.getParagraphs()); // Check the real file size of our document Assert.assertEquals(20310, properties.getBytes()); // Template: The Template attribute can reflect the filename of the attached template document doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx"); Assert.assertEquals("Normal", properties.getTemplate()); properties.setTemplate(doc.getAttachedTemplate()); // Content status: This is a descriptive field properties.setContentStatus("Draft"); // Content type: Upon saving, any value we assign to this field will be overwritten by 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 this to true Assert.assertFalse(properties.getLinksUpToDate()); doc.save(getArtifactsDir() + "Properties.Content.docx"); } /// <summary> /// Util class that counts the lines in a document. /// Upon construction, traverses the document's layout entities tree, /// counting entities of the "Line" type that also contain real text. /// </summary> 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 LayoutEnumerator mLayoutEnumerator; private int mLineCount; private boolean mScanningLineForRealText; }
getComments/setComments | |
public java.lang.String getComments() / public void setComments(java.lang.String value) |
Example:
Shows how to work with document properties in the "Description" category.Document doc = new Document(); // The properties we will work with are members of the BuiltInDocumentProperties attribute BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // Set the values of some descriptive properties // These are metadata that can be glanced at without opening the document in the "Details" or "Content" folder views in Windows Explorer // The "Details" view has columns dedicated to these properties // Fields such as AUTHOR, SUBJECT, TITLE etc. can be used to display these values inside the document properties.setAuthor("John Doe"); properties.setTitle("John's Document"); properties.setSubject("My subject"); properties.setCategory("My category"); properties.setComments(MessageFormat.format("This is {0}'s document about {1}", properties.getAuthor(), properties.getSubject())); // Tags can be used as keywords and are separated by semicolons properties.setKeywords("Tag 1; Tag 2; Tag 3"); // When right clicking the document file in Windows Explorer, these properties are found in Properties > Details > Description doc.save(getArtifactsDir() + "Properties.Description.docx");
getCompany/setCompany | |
public java.lang.String getCompany() / public void setCompany(java.lang.String value) |
Example:
Shows how to work with document properties in the "Origin" category.Document doc = new Document(getMyDir() + "Properties.docx"); // The properties we will work with are members of the BuiltInDocumentProperties attribute BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // Since this document has been edited and printed in the past, values generated by Microsoft Word will appear here // These values can be glanced at by right clicking the file in Windows Explorer, without actually opening the document // Fields such as PRINTDATE, EDITTIME etc. can display these values inside the document 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 set these properties ourselves properties.setCompany("Doe Ltd."); properties.setManager("Jane Doe"); properties.setVersion(5); properties.setRevisionNumber(properties.getRevisionNumber() + 1)/*Property++*/; // If we plan on programmatically saving the document, we may record some details like this properties.setLastSavedBy("John Doe"); properties.setLastSavedTime(new Date()); // When right clicking the document file in Windows Explorer, these properties are found in Properties > Details > Origin doc.save(getArtifactsDir() + "Properties.Origin.docx");
getContentStatus/setContentStatus | |
public java.lang.String getContentStatus() / public void setContentStatus(java.lang.String value) |
Example:
Shows how to work with document properties in the "Content" category.public void content() throws Exception { // Open a document with a couple paragraphs of content Document doc = new Document(getMyDir() + "Paragraphs.docx"); // The properties we will work with are members of the BuiltInDocumentProperties attribute 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 attribute shows the page count in real time and its value can be assigned to the Pages property properties.setPages(doc.getPageCount()); Assert.assertEquals(6, properties.getPages()); // Word count: The UpdateWordCount() automatically assigns the real time word/character counts to the respective built in properties doc.updateWordCount(); Assert.assertEquals(1035, properties.getWords()); Assert.assertEquals(6026, properties.getCharacters()); Assert.assertEquals(7041, properties.getCharactersWithSpaces()); // Line count: Count the lines in a document and assign value to the Lines property LineCounter lineCounter = new LineCounter(doc); properties.setLines(lineCounter.getLineCount()); Assert.assertEquals(properties.getLines(), 142); // Paragraph count: Assign the size of the count of child Paragraph-nodes to the Paragraphs built in property properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount()); Assert.assertEquals(29, properties.getParagraphs()); // Check the real file size of our document Assert.assertEquals(20310, properties.getBytes()); // Template: The Template attribute can reflect the filename of the attached template document doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx"); Assert.assertEquals("Normal", properties.getTemplate()); properties.setTemplate(doc.getAttachedTemplate()); // Content status: This is a descriptive field properties.setContentStatus("Draft"); // Content type: Upon saving, any value we assign to this field will be overwritten by 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 this to true Assert.assertFalse(properties.getLinksUpToDate()); doc.save(getArtifactsDir() + "Properties.Content.docx"); } /// <summary> /// Util class that counts the lines in a document. /// Upon construction, traverses the document's layout entities tree, /// counting entities of the "Line" type that also contain real text. /// </summary> 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 LayoutEnumerator mLayoutEnumerator; private int mLineCount; private boolean mScanningLineForRealText; }
getContentType/setContentType | |
public java.lang.String getContentType() / public void setContentType(java.lang.String value) |
Example:
Shows how to work with document properties in the "Content" category.public void content() throws Exception { // Open a document with a couple paragraphs of content Document doc = new Document(getMyDir() + "Paragraphs.docx"); // The properties we will work with are members of the BuiltInDocumentProperties attribute 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 attribute shows the page count in real time and its value can be assigned to the Pages property properties.setPages(doc.getPageCount()); Assert.assertEquals(6, properties.getPages()); // Word count: The UpdateWordCount() automatically assigns the real time word/character counts to the respective built in properties doc.updateWordCount(); Assert.assertEquals(1035, properties.getWords()); Assert.assertEquals(6026, properties.getCharacters()); Assert.assertEquals(7041, properties.getCharactersWithSpaces()); // Line count: Count the lines in a document and assign value to the Lines property LineCounter lineCounter = new LineCounter(doc); properties.setLines(lineCounter.getLineCount()); Assert.assertEquals(properties.getLines(), 142); // Paragraph count: Assign the size of the count of child Paragraph-nodes to the Paragraphs built in property properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount()); Assert.assertEquals(29, properties.getParagraphs()); // Check the real file size of our document Assert.assertEquals(20310, properties.getBytes()); // Template: The Template attribute can reflect the filename of the attached template document doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx"); Assert.assertEquals("Normal", properties.getTemplate()); properties.setTemplate(doc.getAttachedTemplate()); // Content status: This is a descriptive field properties.setContentStatus("Draft"); // Content type: Upon saving, any value we assign to this field will be overwritten by 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 this to true Assert.assertFalse(properties.getLinksUpToDate()); doc.save(getArtifactsDir() + "Properties.Content.docx"); } /// <summary> /// Util class that counts the lines in a document. /// Upon construction, traverses the document's layout entities tree, /// counting entities of the "Line" type that also contain real text. /// </summary> 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 LayoutEnumerator mLayoutEnumerator; private int mLineCount; private boolean mScanningLineForRealText; }
getCount | → inherited from DocumentPropertyCollection |
public int getCount() |
Example:
Shows how to work with custom document properties.Document doc = new Document(getMyDir() + "Properties.docx"); // A document's built in properties contains a set of predetermined keys // with values such as the author's name or document's word count // We can add our own keys and values to a custom properties collection also // Before we add a custom property, we need to make sure that one with the same name doesn't already exist Assert.assertEquals("Value of custom document property", doc.getCustomDocumentProperties().get("CustomProperty").toString()); doc.getCustomDocumentProperties().add("CustomProperty2", "Value of custom document property #2"); // Iterate over all the custom document properties System.out.println("Custom Properties:"); for (DocumentProperty customDocumentProperty : (Iterable<DocumentProperty>) doc.getCustomDocumentProperties()) { System.out.println(customDocumentProperty.getName()); System.out.println("\tType:\t{customDocumentProperty.Type}"); System.out.println("\tValue:\t\"{customDocumentProperty.Value}\""); }
getCreatedTime/setCreatedTime | |
public java.util.Date getCreatedTime() / public void setCreatedTime(java.util.Date value) |
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.
Example:
Shows how to work with document properties in the "Origin" category.Document doc = new Document(getMyDir() + "Properties.docx"); // The properties we will work with are members of the BuiltInDocumentProperties attribute BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // Since this document has been edited and printed in the past, values generated by Microsoft Word will appear here // These values can be glanced at by right clicking the file in Windows Explorer, without actually opening the document // Fields such as PRINTDATE, EDITTIME etc. can display these values inside the document 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 set these properties ourselves properties.setCompany("Doe Ltd."); properties.setManager("Jane Doe"); properties.setVersion(5); properties.setRevisionNumber(properties.getRevisionNumber() + 1)/*Property++*/; // If we plan on programmatically saving the document, we may record some details like this properties.setLastSavedBy("John Doe"); properties.setLastSavedTime(new Date()); // When right clicking the document file in Windows Explorer, these properties are found in Properties > Details > Origin doc.save(getArtifactsDir() + "Properties.Origin.docx");
getHeadingPairs/setHeadingPairs | |
public java.lang.Object[] getHeadingPairs() / public void setHeadingPairs(java.lang.Object[] value) |
Every heading pair occupies two elements in this array.
The first element of the pair is a
The total sum of counts for all heading pairs in this property must be equal to the
number of elements in the
Aspose.Words does not update this property.
Example:
Shows the relationship between HeadingPairs and TitlesOfParts properties.// Open a document that contains entries in the HeadingPairs/TitlesOfParts properties Document doc = new Document(getMyDir() + "Heading pairs and titles of parts.docx"); // We can find the combined values of these collections in File > Properties > Advanced Properties > Contents tab // The HeadingPairs property is a collection of <string, int> pairs that determines // how many document parts a heading spans over 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 = Integer.valueOf((Integer) headingPairs[headingPairsIndex++]); for (int i = 0; i < partsCount; i++) System.out.println(MessageFormat.format("\t\"{0}\"", titlesOfParts[titlesOfPartsIndex++])); }
getHyperlinkBase/setHyperlinkBase | |
public java.lang.String getHyperlinkBase() / public void setHyperlinkBase(java.lang.String value) |
Aspose.Words does not use this property.
Example:
Shows how to store the base part of a hyperlink in the document's properties.// Create a blank document and a DocumentBuilder Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert a relative hyperlink to "Document.docx", which will open that document when clicked on builder.insertHyperlink("Relative hyperlink", "Document.docx", false); // If we don't have a "Document.docx" in the same folder as the document we are about to save, we will end up with a broken link Assert.assertFalse(Files.exists(Paths.get(getArtifactsDir() + "Document.docx"))); doc.save(getArtifactsDir() + "Properties.HyperlinkBase.BrokenLink.docx"); // We could keep prepending something like 'C:\\users\\...\\data' to every hyperlink we place to remedy this // Alternatively, if we know that all our linked files will come from the same folder, // we could set a base hyperlink in the document properties, keeping our hyperlinks short BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); properties.setHyperlinkBase(getMyDir()); Assert.assertTrue(Files.exists(Paths.get(properties.getHyperlinkBase() + ((FieldHyperlink) doc.getRange().getFields().get(0)).getAddress()))); doc.save(getArtifactsDir() + "Properties.HyperlinkBase.WorkingLink.docx");
getKeywords/setKeywords | |
public java.lang.String getKeywords() / public void setKeywords(java.lang.String value) |
Example:
Shows how to work with document properties in the "Description" category.Document doc = new Document(); // The properties we will work with are members of the BuiltInDocumentProperties attribute BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // Set the values of some descriptive properties // These are metadata that can be glanced at without opening the document in the "Details" or "Content" folder views in Windows Explorer // The "Details" view has columns dedicated to these properties // Fields such as AUTHOR, SUBJECT, TITLE etc. can be used to display these values inside the document properties.setAuthor("John Doe"); properties.setTitle("John's Document"); properties.setSubject("My subject"); properties.setCategory("My category"); properties.setComments(MessageFormat.format("This is {0}'s document about {1}", properties.getAuthor(), properties.getSubject())); // Tags can be used as keywords and are separated by semicolons properties.setKeywords("Tag 1; Tag 2; Tag 3"); // When right clicking the document file in Windows Explorer, these properties are found in Properties > Details > Description doc.save(getArtifactsDir() + "Properties.Description.docx");
getLastPrinted/setLastPrinted | |
public java.util.Date getLastPrinted() / public void setLastPrinted(java.util.Date value) |
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.
Example:
Shows how to work with document properties in the "Origin" category.Document doc = new Document(getMyDir() + "Properties.docx"); // The properties we will work with are members of the BuiltInDocumentProperties attribute BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // Since this document has been edited and printed in the past, values generated by Microsoft Word will appear here // These values can be glanced at by right clicking the file in Windows Explorer, without actually opening the document // Fields such as PRINTDATE, EDITTIME etc. can display these values inside the document 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 set these properties ourselves properties.setCompany("Doe Ltd."); properties.setManager("Jane Doe"); properties.setVersion(5); properties.setRevisionNumber(properties.getRevisionNumber() + 1)/*Property++*/; // If we plan on programmatically saving the document, we may record some details like this properties.setLastSavedBy("John Doe"); properties.setLastSavedTime(new Date()); // When right clicking the document file in Windows Explorer, these properties are found in Properties > Details > Origin doc.save(getArtifactsDir() + "Properties.Origin.docx");
getLastSavedBy/setLastSavedBy | |
public java.lang.String getLastSavedBy() / public void setLastSavedBy(java.lang.String value) |
Aspose.Words does not update this property.
Example:
Shows how to work with document properties in the "Origin" category.Document doc = new Document(getMyDir() + "Properties.docx"); // The properties we will work with are members of the BuiltInDocumentProperties attribute BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // Since this document has been edited and printed in the past, values generated by Microsoft Word will appear here // These values can be glanced at by right clicking the file in Windows Explorer, without actually opening the document // Fields such as PRINTDATE, EDITTIME etc. can display these values inside the document 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 set these properties ourselves properties.setCompany("Doe Ltd."); properties.setManager("Jane Doe"); properties.setVersion(5); properties.setRevisionNumber(properties.getRevisionNumber() + 1)/*Property++*/; // If we plan on programmatically saving the document, we may record some details like this properties.setLastSavedBy("John Doe"); properties.setLastSavedTime(new Date()); // When right clicking the document file in Windows Explorer, these properties are found in Properties > Details > Origin doc.save(getArtifactsDir() + "Properties.Origin.docx");
getLastSavedTime/setLastSavedTime | |
public java.util.Date getLastSavedTime() / public void setLastSavedTime(java.util.Date value) |
For documents originated from RTF format this property returns the local time of last save operation.
Aspose.Words does not update this property.
Example:
Shows how to work with document properties in the "Origin" category.Document doc = new Document(getMyDir() + "Properties.docx"); // The properties we will work with are members of the BuiltInDocumentProperties attribute BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // Since this document has been edited and printed in the past, values generated by Microsoft Word will appear here // These values can be glanced at by right clicking the file in Windows Explorer, without actually opening the document // Fields such as PRINTDATE, EDITTIME etc. can display these values inside the document 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 set these properties ourselves properties.setCompany("Doe Ltd."); properties.setManager("Jane Doe"); properties.setVersion(5); properties.setRevisionNumber(properties.getRevisionNumber() + 1)/*Property++*/; // If we plan on programmatically saving the document, we may record some details like this properties.setLastSavedBy("John Doe"); properties.setLastSavedTime(new Date()); // When right clicking the document file in Windows Explorer, these properties are found in Properties > Details > Origin doc.save(getArtifactsDir() + "Properties.Origin.docx");
getLines/setLines | |
public int getLines() / public void setLines(int value) |
Aspose.Words updates this property when you call
Example:
Shows how to update all list labels in a document.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Add a paragraph of text to the document 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."); // Document metrics are not tracked in code 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()); // We will need to call this method to update them doc.updateWordCount(); // Check the values of the properties Assert.assertEquals(196, doc.getBuiltInDocumentProperties().getCharacters()); Assert.assertEquals(36, doc.getBuiltInDocumentProperties().getWords()); Assert.assertEquals(2, doc.getBuiltInDocumentProperties().getParagraphs()); Assert.assertEquals(1, doc.getBuiltInDocumentProperties().getLines()); // To also get the line count as it would appear in Microsoft Word, // we will need to pass "true" to UpdateWordCount() doc.updateWordCount(true); Assert.assertEquals(4, doc.getBuiltInDocumentProperties().getLines());
Example:
Shows how to work with document properties in the "Content" category.public void content() throws Exception { // Open a document with a couple paragraphs of content Document doc = new Document(getMyDir() + "Paragraphs.docx"); // The properties we will work with are members of the BuiltInDocumentProperties attribute 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 attribute shows the page count in real time and its value can be assigned to the Pages property properties.setPages(doc.getPageCount()); Assert.assertEquals(6, properties.getPages()); // Word count: The UpdateWordCount() automatically assigns the real time word/character counts to the respective built in properties doc.updateWordCount(); Assert.assertEquals(1035, properties.getWords()); Assert.assertEquals(6026, properties.getCharacters()); Assert.assertEquals(7041, properties.getCharactersWithSpaces()); // Line count: Count the lines in a document and assign value to the Lines property LineCounter lineCounter = new LineCounter(doc); properties.setLines(lineCounter.getLineCount()); Assert.assertEquals(properties.getLines(), 142); // Paragraph count: Assign the size of the count of child Paragraph-nodes to the Paragraphs built in property properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount()); Assert.assertEquals(29, properties.getParagraphs()); // Check the real file size of our document Assert.assertEquals(20310, properties.getBytes()); // Template: The Template attribute can reflect the filename of the attached template document doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx"); Assert.assertEquals("Normal", properties.getTemplate()); properties.setTemplate(doc.getAttachedTemplate()); // Content status: This is a descriptive field properties.setContentStatus("Draft"); // Content type: Upon saving, any value we assign to this field will be overwritten by 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 this to true Assert.assertFalse(properties.getLinksUpToDate()); doc.save(getArtifactsDir() + "Properties.Content.docx"); } /// <summary> /// Util class that counts the lines in a document. /// Upon construction, traverses the document's layout entities tree, /// counting entities of the "Line" type that also contain real text. /// </summary> 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 LayoutEnumerator mLayoutEnumerator; private int mLineCount; private boolean mScanningLineForRealText; }
getLinksUpToDate/setLinksUpToDate | |
public boolean getLinksUpToDate() / public void setLinksUpToDate(boolean value) |
Aspose.Words does not update this property.
Example:
Shows how to work with document properties in the "Content" category.public void content() throws Exception { // Open a document with a couple paragraphs of content Document doc = new Document(getMyDir() + "Paragraphs.docx"); // The properties we will work with are members of the BuiltInDocumentProperties attribute 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 attribute shows the page count in real time and its value can be assigned to the Pages property properties.setPages(doc.getPageCount()); Assert.assertEquals(6, properties.getPages()); // Word count: The UpdateWordCount() automatically assigns the real time word/character counts to the respective built in properties doc.updateWordCount(); Assert.assertEquals(1035, properties.getWords()); Assert.assertEquals(6026, properties.getCharacters()); Assert.assertEquals(7041, properties.getCharactersWithSpaces()); // Line count: Count the lines in a document and assign value to the Lines property LineCounter lineCounter = new LineCounter(doc); properties.setLines(lineCounter.getLineCount()); Assert.assertEquals(properties.getLines(), 142); // Paragraph count: Assign the size of the count of child Paragraph-nodes to the Paragraphs built in property properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount()); Assert.assertEquals(29, properties.getParagraphs()); // Check the real file size of our document Assert.assertEquals(20310, properties.getBytes()); // Template: The Template attribute can reflect the filename of the attached template document doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx"); Assert.assertEquals("Normal", properties.getTemplate()); properties.setTemplate(doc.getAttachedTemplate()); // Content status: This is a descriptive field properties.setContentStatus("Draft"); // Content type: Upon saving, any value we assign to this field will be overwritten by 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 this to true Assert.assertFalse(properties.getLinksUpToDate()); doc.save(getArtifactsDir() + "Properties.Content.docx"); } /// <summary> /// Util class that counts the lines in a document. /// Upon construction, traverses the document's layout entities tree, /// counting entities of the "Line" type that also contain real text. /// </summary> 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 LayoutEnumerator mLayoutEnumerator; private int mLineCount; private boolean mScanningLineForRealText; }
getManager/setManager | |
public java.lang.String getManager() / public void setManager(java.lang.String value) |
Example:
Shows how to work with document properties in the "Origin" category.Document doc = new Document(getMyDir() + "Properties.docx"); // The properties we will work with are members of the BuiltInDocumentProperties attribute BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // Since this document has been edited and printed in the past, values generated by Microsoft Word will appear here // These values can be glanced at by right clicking the file in Windows Explorer, without actually opening the document // Fields such as PRINTDATE, EDITTIME etc. can display these values inside the document 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 set these properties ourselves properties.setCompany("Doe Ltd."); properties.setManager("Jane Doe"); properties.setVersion(5); properties.setRevisionNumber(properties.getRevisionNumber() + 1)/*Property++*/; // If we plan on programmatically saving the document, we may record some details like this properties.setLastSavedBy("John Doe"); properties.setLastSavedTime(new Date()); // When right clicking the document file in Windows Explorer, these properties are found in Properties > Details > Origin doc.save(getArtifactsDir() + "Properties.Origin.docx");
getNameOfApplication/setNameOfApplication | |
public java.lang.String getNameOfApplication() / public void setNameOfApplication(java.lang.String value) |
Example:
Shows how to work with document properties in the "Origin" category.Document doc = new Document(getMyDir() + "Properties.docx"); // The properties we will work with are members of the BuiltInDocumentProperties attribute BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // Since this document has been edited and printed in the past, values generated by Microsoft Word will appear here // These values can be glanced at by right clicking the file in Windows Explorer, without actually opening the document // Fields such as PRINTDATE, EDITTIME etc. can display these values inside the document 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 set these properties ourselves properties.setCompany("Doe Ltd."); properties.setManager("Jane Doe"); properties.setVersion(5); properties.setRevisionNumber(properties.getRevisionNumber() + 1)/*Property++*/; // If we plan on programmatically saving the document, we may record some details like this properties.setLastSavedBy("John Doe"); properties.setLastSavedTime(new Date()); // When right clicking the document file in Windows Explorer, these properties are found in Properties > Details > Origin doc.save(getArtifactsDir() + "Properties.Origin.docx");
getPages/setPages | |
public int getPages() / public void setPages(int value) |
Aspose.Words updates this property when you call
Example:
Shows how to work with document properties in the "Content" category.public void content() throws Exception { // Open a document with a couple paragraphs of content Document doc = new Document(getMyDir() + "Paragraphs.docx"); // The properties we will work with are members of the BuiltInDocumentProperties attribute 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 attribute shows the page count in real time and its value can be assigned to the Pages property properties.setPages(doc.getPageCount()); Assert.assertEquals(6, properties.getPages()); // Word count: The UpdateWordCount() automatically assigns the real time word/character counts to the respective built in properties doc.updateWordCount(); Assert.assertEquals(1035, properties.getWords()); Assert.assertEquals(6026, properties.getCharacters()); Assert.assertEquals(7041, properties.getCharactersWithSpaces()); // Line count: Count the lines in a document and assign value to the Lines property LineCounter lineCounter = new LineCounter(doc); properties.setLines(lineCounter.getLineCount()); Assert.assertEquals(properties.getLines(), 142); // Paragraph count: Assign the size of the count of child Paragraph-nodes to the Paragraphs built in property properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount()); Assert.assertEquals(29, properties.getParagraphs()); // Check the real file size of our document Assert.assertEquals(20310, properties.getBytes()); // Template: The Template attribute can reflect the filename of the attached template document doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx"); Assert.assertEquals("Normal", properties.getTemplate()); properties.setTemplate(doc.getAttachedTemplate()); // Content status: This is a descriptive field properties.setContentStatus("Draft"); // Content type: Upon saving, any value we assign to this field will be overwritten by 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 this to true Assert.assertFalse(properties.getLinksUpToDate()); doc.save(getArtifactsDir() + "Properties.Content.docx"); } /// <summary> /// Util class that counts the lines in a document. /// Upon construction, traverses the document's layout entities tree, /// counting entities of the "Line" type that also contain real text. /// </summary> 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 LayoutEnumerator mLayoutEnumerator; private int mLineCount; private boolean mScanningLineForRealText; }
getParagraphs/setParagraphs | |
public int getParagraphs() / public void setParagraphs(int value) |
Aspose.Words updates this property when you call
Example:
Shows how to update all list labels in a document.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Add a paragraph of text to the document 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."); // Document metrics are not tracked in code 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()); // We will need to call this method to update them doc.updateWordCount(); // Check the values of the properties Assert.assertEquals(196, doc.getBuiltInDocumentProperties().getCharacters()); Assert.assertEquals(36, doc.getBuiltInDocumentProperties().getWords()); Assert.assertEquals(2, doc.getBuiltInDocumentProperties().getParagraphs()); Assert.assertEquals(1, doc.getBuiltInDocumentProperties().getLines()); // To also get the line count as it would appear in Microsoft Word, // we will need to pass "true" to UpdateWordCount() doc.updateWordCount(true); Assert.assertEquals(4, doc.getBuiltInDocumentProperties().getLines());
Example:
Shows how to work with document properties in the "Content" category.public void content() throws Exception { // Open a document with a couple paragraphs of content Document doc = new Document(getMyDir() + "Paragraphs.docx"); // The properties we will work with are members of the BuiltInDocumentProperties attribute 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 attribute shows the page count in real time and its value can be assigned to the Pages property properties.setPages(doc.getPageCount()); Assert.assertEquals(6, properties.getPages()); // Word count: The UpdateWordCount() automatically assigns the real time word/character counts to the respective built in properties doc.updateWordCount(); Assert.assertEquals(1035, properties.getWords()); Assert.assertEquals(6026, properties.getCharacters()); Assert.assertEquals(7041, properties.getCharactersWithSpaces()); // Line count: Count the lines in a document and assign value to the Lines property LineCounter lineCounter = new LineCounter(doc); properties.setLines(lineCounter.getLineCount()); Assert.assertEquals(properties.getLines(), 142); // Paragraph count: Assign the size of the count of child Paragraph-nodes to the Paragraphs built in property properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount()); Assert.assertEquals(29, properties.getParagraphs()); // Check the real file size of our document Assert.assertEquals(20310, properties.getBytes()); // Template: The Template attribute can reflect the filename of the attached template document doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx"); Assert.assertEquals("Normal", properties.getTemplate()); properties.setTemplate(doc.getAttachedTemplate()); // Content status: This is a descriptive field properties.setContentStatus("Draft"); // Content type: Upon saving, any value we assign to this field will be overwritten by 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 this to true Assert.assertFalse(properties.getLinksUpToDate()); doc.save(getArtifactsDir() + "Properties.Content.docx"); } /// <summary> /// Util class that counts the lines in a document. /// Upon construction, traverses the document's layout entities tree, /// counting entities of the "Line" type that also contain real text. /// </summary> 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 LayoutEnumerator mLayoutEnumerator; private int mLineCount; private boolean mScanningLineForRealText; }
getRevisionNumber/setRevisionNumber | |
public int getRevisionNumber() / public void setRevisionNumber(int value) |
Aspose.Words does not update this property.
Example:
Shows how to work with document properties in the "Origin" category.Document doc = new Document(getMyDir() + "Properties.docx"); // The properties we will work with are members of the BuiltInDocumentProperties attribute BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // Since this document has been edited and printed in the past, values generated by Microsoft Word will appear here // These values can be glanced at by right clicking the file in Windows Explorer, without actually opening the document // Fields such as PRINTDATE, EDITTIME etc. can display these values inside the document 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 set these properties ourselves properties.setCompany("Doe Ltd."); properties.setManager("Jane Doe"); properties.setVersion(5); properties.setRevisionNumber(properties.getRevisionNumber() + 1)/*Property++*/; // If we plan on programmatically saving the document, we may record some details like this properties.setLastSavedBy("John Doe"); properties.setLastSavedTime(new Date()); // When right clicking the document file in Windows Explorer, these properties are found in Properties > Details > Origin doc.save(getArtifactsDir() + "Properties.Origin.docx");
getSecurity/setSecurity | |
public int getSecurity() / public void setSecurity(int value) |
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
Aspose.Words updates this property to a correct value before saving a document.
Example:
Shows how to use document properties to display the security level of a document.// Create a blank document, which has no security of any kind by default Document doc = new Document(); // The "Security" property serves as a description of the security level of a document Assert.assertEquals(doc.getBuiltInDocumentProperties().getSecurity(), DocumentSecurity.NONE); // Upon saving a document after setting its security level, Aspose automatically updates this property to the appropriate value doc.getWriteProtection().setReadOnlyRecommended(true); doc.save(getArtifactsDir() + "Properties.Security.ReadOnlyRecommended.docx"); // We can open a document and glance at its security level like this Assert.assertEquals(new Document(getArtifactsDir() + "Properties.Security.ReadOnlyRecommended.docx").getBuiltInDocumentProperties().getSecurity(), DocumentSecurity.READ_ONLY_RECOMMENDED); // Create a new document and set it to Write-Protected 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() + "Properties.Security.ReadOnlyEnforced.docx"); // This document's security level counts as "ReadOnlyEnforced" Assert.assertEquals(new Document(getArtifactsDir() + "Properties.Security.ReadOnlyEnforced.docx").getBuiltInDocumentProperties().getSecurity(), DocumentSecurity.READ_ONLY_ENFORCED); // Since this is still a descriptive property, we can protect a document and pick a suitable value ourselves doc = new Document(); doc.protect(ProtectionType.ALLOW_ONLY_COMMENTS, "MyPassword"); doc.getBuiltInDocumentProperties().setSecurity(DocumentSecurity.READ_ONLY_EXCEPT_ANNOTATIONS); doc.save(getArtifactsDir() + "Properties.Security.ReadOnlyExceptAnnotations.docx"); Assert.assertEquals(new Document(getArtifactsDir() + "Properties.Security.ReadOnlyExceptAnnotations.docx").getBuiltInDocumentProperties().getSecurity(), DocumentSecurity.READ_ONLY_EXCEPT_ANNOTATIONS);
getSubject/setSubject | |
public java.lang.String getSubject() / public void setSubject(java.lang.String value) |
Example:
Shows how to work with document properties in the "Description" category.Document doc = new Document(); // The properties we will work with are members of the BuiltInDocumentProperties attribute BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // Set the values of some descriptive properties // These are metadata that can be glanced at without opening the document in the "Details" or "Content" folder views in Windows Explorer // The "Details" view has columns dedicated to these properties // Fields such as AUTHOR, SUBJECT, TITLE etc. can be used to display these values inside the document properties.setAuthor("John Doe"); properties.setTitle("John's Document"); properties.setSubject("My subject"); properties.setCategory("My category"); properties.setComments(MessageFormat.format("This is {0}'s document about {1}", properties.getAuthor(), properties.getSubject())); // Tags can be used as keywords and are separated by semicolons properties.setKeywords("Tag 1; Tag 2; Tag 3"); // When right clicking the document file in Windows Explorer, these properties are found in Properties > Details > Description doc.save(getArtifactsDir() + "Properties.Description.docx");
getTemplate/setTemplate | |
public java.lang.String getTemplate() / public void setTemplate(java.lang.String value) |
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
Example:
Shows how to work with document properties in the "Origin" category.Document doc = new Document(getMyDir() + "Properties.docx"); // The properties we will work with are members of the BuiltInDocumentProperties attribute BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // Since this document has been edited and printed in the past, values generated by Microsoft Word will appear here // These values can be glanced at by right clicking the file in Windows Explorer, without actually opening the document // Fields such as PRINTDATE, EDITTIME etc. can display these values inside the document 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 set these properties ourselves properties.setCompany("Doe Ltd."); properties.setManager("Jane Doe"); properties.setVersion(5); properties.setRevisionNumber(properties.getRevisionNumber() + 1)/*Property++*/; // If we plan on programmatically saving the document, we may record some details like this properties.setLastSavedBy("John Doe"); properties.setLastSavedTime(new Date()); // When right clicking the document file in Windows Explorer, these properties are found in Properties > Details > Origin doc.save(getArtifactsDir() + "Properties.Origin.docx");
getThumbnail/setThumbnail | |
public byte[] getThumbnail() / public void setThumbnail(byte[] value) |
Gets or sets the thumbnail of the document.
For now this property is used only when a document is being exported to ePub, it's not read from and written to other document formats.
Image of arbitrary format can be set to this property, but the format is checked during export.
Only gif, jpeg and png images can be used for ePub publication.
Example:
Shows how to append a thumbnail to an Epub document.// Create a blank document and add some text with a DocumentBuilder Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.writeln("Hello world!"); // The thumbnail property resides in a document's built in properties, but is used exclusively by Epub e-book documents BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // Load an image from our file system into a byte array byte[] thumbnailBytes = Files.readAllBytes(Paths.get(getImageDir() + "Logo.jpg")); // Set the value of the Thumbnail property to the array from above properties.setThumbnail(thumbnailBytes); // Our thumbnail should be visible at the start of the document, before the text we added doc.save(getArtifactsDir() + "Properties.Thumbnail.epub"); // We can also extract a thumbnail property into a byte array and then into the local file system like this DocumentProperty thumbnail = doc.getBuiltInDocumentProperties().get("Thumbnail"); Files.write(Paths.get(getArtifactsDir() + "Properties.Thumbnail.gif"), thumbnail.toByteArray());
getTitle/setTitle | |
public java.lang.String getTitle() / public void setTitle(java.lang.String value) |
Example:
Shows how to work with document properties in the "Description" category.Document doc = new Document(); // The properties we will work with are members of the BuiltInDocumentProperties attribute BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // Set the values of some descriptive properties // These are metadata that can be glanced at without opening the document in the "Details" or "Content" folder views in Windows Explorer // The "Details" view has columns dedicated to these properties // Fields such as AUTHOR, SUBJECT, TITLE etc. can be used to display these values inside the document properties.setAuthor("John Doe"); properties.setTitle("John's Document"); properties.setSubject("My subject"); properties.setCategory("My category"); properties.setComments(MessageFormat.format("This is {0}'s document about {1}", properties.getAuthor(), properties.getSubject())); // Tags can be used as keywords and are separated by semicolons properties.setKeywords("Tag 1; Tag 2; Tag 3"); // When right clicking the document file in Windows Explorer, these properties are found in Properties > Details > Description doc.save(getArtifactsDir() + "Properties.Description.docx");
getTitlesOfParts/setTitlesOfParts | |
public java.lang.String[] getTitlesOfParts() / public void setTitlesOfParts(java.lang.String[] value) |
Aspose.Words does not update this property.
Example:
Shows the relationship between HeadingPairs and TitlesOfParts properties.// Open a document that contains entries in the HeadingPairs/TitlesOfParts properties Document doc = new Document(getMyDir() + "Heading pairs and titles of parts.docx"); // We can find the combined values of these collections in File > Properties > Advanced Properties > Contents tab // The HeadingPairs property is a collection of <string, int> pairs that determines // how many document parts a heading spans over 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 = Integer.valueOf((Integer) headingPairs[headingPairsIndex++]); for (int i = 0; i < partsCount; i++) System.out.println(MessageFormat.format("\t\"{0}\"", titlesOfParts[titlesOfPartsIndex++])); }
getTotalEditingTime/setTotalEditingTime | |
public int getTotalEditingTime() / public void setTotalEditingTime(int value) |
Example:
Shows how to work with document properties in the "Origin" category.Document doc = new Document(getMyDir() + "Properties.docx"); // The properties we will work with are members of the BuiltInDocumentProperties attribute BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // Since this document has been edited and printed in the past, values generated by Microsoft Word will appear here // These values can be glanced at by right clicking the file in Windows Explorer, without actually opening the document // Fields such as PRINTDATE, EDITTIME etc. can display these values inside the document 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 set these properties ourselves properties.setCompany("Doe Ltd."); properties.setManager("Jane Doe"); properties.setVersion(5); properties.setRevisionNumber(properties.getRevisionNumber() + 1)/*Property++*/; // If we plan on programmatically saving the document, we may record some details like this properties.setLastSavedBy("John Doe"); properties.setLastSavedTime(new Date()); // When right clicking the document file in Windows Explorer, these properties are found in Properties > Details > Origin doc.save(getArtifactsDir() + "Properties.Origin.docx");
getVersion/setVersion | |
public int getVersion() / public void setVersion(int value) |
When a document was created by Microsoft Word, then high 16 bit represent the major version and low 16 bit represent the build number.
Example:
Shows how to work with document properties in the "Origin" category.Document doc = new Document(getMyDir() + "Properties.docx"); // The properties we will work with are members of the BuiltInDocumentProperties attribute BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // Since this document has been edited and printed in the past, values generated by Microsoft Word will appear here // These values can be glanced at by right clicking the file in Windows Explorer, without actually opening the document // Fields such as PRINTDATE, EDITTIME etc. can display these values inside the document 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 set these properties ourselves properties.setCompany("Doe Ltd."); properties.setManager("Jane Doe"); properties.setVersion(5); properties.setRevisionNumber(properties.getRevisionNumber() + 1)/*Property++*/; // If we plan on programmatically saving the document, we may record some details like this properties.setLastSavedBy("John Doe"); properties.setLastSavedTime(new Date()); // When right clicking the document file in Windows Explorer, these properties are found in Properties > Details > Origin doc.save(getArtifactsDir() + "Properties.Origin.docx");
getWords/setWords | |
public int getWords() / public void setWords(int value) |
Aspose.Words updates this property when you call
Example:
Shows how to update all list labels in a document.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Add a paragraph of text to the document 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."); // Document metrics are not tracked in code 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()); // We will need to call this method to update them doc.updateWordCount(); // Check the values of the properties Assert.assertEquals(196, doc.getBuiltInDocumentProperties().getCharacters()); Assert.assertEquals(36, doc.getBuiltInDocumentProperties().getWords()); Assert.assertEquals(2, doc.getBuiltInDocumentProperties().getParagraphs()); Assert.assertEquals(1, doc.getBuiltInDocumentProperties().getLines()); // To also get the line count as it would appear in Microsoft Word, // we will need to pass "true" to UpdateWordCount() doc.updateWordCount(true); Assert.assertEquals(4, doc.getBuiltInDocumentProperties().getLines());
Example:
Shows how to work with document properties in the "Content" category.public void content() throws Exception { // Open a document with a couple paragraphs of content Document doc = new Document(getMyDir() + "Paragraphs.docx"); // The properties we will work with are members of the BuiltInDocumentProperties attribute 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 attribute shows the page count in real time and its value can be assigned to the Pages property properties.setPages(doc.getPageCount()); Assert.assertEquals(6, properties.getPages()); // Word count: The UpdateWordCount() automatically assigns the real time word/character counts to the respective built in properties doc.updateWordCount(); Assert.assertEquals(1035, properties.getWords()); Assert.assertEquals(6026, properties.getCharacters()); Assert.assertEquals(7041, properties.getCharactersWithSpaces()); // Line count: Count the lines in a document and assign value to the Lines property LineCounter lineCounter = new LineCounter(doc); properties.setLines(lineCounter.getLineCount()); Assert.assertEquals(properties.getLines(), 142); // Paragraph count: Assign the size of the count of child Paragraph-nodes to the Paragraphs built in property properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount()); Assert.assertEquals(29, properties.getParagraphs()); // Check the real file size of our document Assert.assertEquals(20310, properties.getBytes()); // Template: The Template attribute can reflect the filename of the attached template document doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx"); Assert.assertEquals("Normal", properties.getTemplate()); properties.setTemplate(doc.getAttachedTemplate()); // Content status: This is a descriptive field properties.setContentStatus("Draft"); // Content type: Upon saving, any value we assign to this field will be overwritten by 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 this to true Assert.assertFalse(properties.getLinksUpToDate()); doc.save(getArtifactsDir() + "Properties.Content.docx"); } /// <summary> /// Util class that counts the lines in a document. /// Upon construction, traverses the document's layout entities tree, /// counting entities of the "Line" type that also contain real text. /// </summary> 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 LayoutEnumerator mLayoutEnumerator; private int mLineCount; private boolean mScanningLineForRealText; }
get | → inherited from DocumentPropertyCollection |
public DocumentProperty get(int index) |
Note: In Java this method is slow because iterates over all nodes.
index
- Zero-based index of the Example:
Shows how to work with custom document properties.Document doc = new Document(getMyDir() + "Properties.docx"); // A document's built in properties contains a set of predetermined keys // with values such as the author's name or document's word count // We can add our own keys and values to a custom properties collection also // Before we add a custom property, we need to make sure that one with the same name doesn't already exist Assert.assertEquals("Value of custom document property", doc.getCustomDocumentProperties().get("CustomProperty").toString()); doc.getCustomDocumentProperties().add("CustomProperty2", "Value of custom document property #2"); // Iterate over all the custom document properties System.out.println("Custom Properties:"); for (DocumentProperty customDocumentProperty : (Iterable<DocumentProperty>) doc.getCustomDocumentProperties()) { System.out.println(customDocumentProperty.getName()); System.out.println("\tType:\t{customDocumentProperty.Type}"); System.out.println("\tValue:\t\"{customDocumentProperty.Value}\""); }
get | |
public DocumentProperty get(java.lang.String name) |
The string names of the properties correspond to the names of the typed
properties available from
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
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.
name
- The case-insensitive name of the property to retrieve.Example:
Shows how to work with custom document properties.Document doc = new Document(getMyDir() + "Properties.docx"); // A document's built in properties contains a set of predetermined keys // with values such as the author's name or document's word count // We can add our own keys and values to a custom properties collection also // Before we add a custom property, we need to make sure that one with the same name doesn't already exist Assert.assertEquals("Value of custom document property", doc.getCustomDocumentProperties().get("CustomProperty").toString()); doc.getCustomDocumentProperties().add("CustomProperty2", "Value of custom document property #2"); // Iterate over all the custom document properties System.out.println("Custom Properties:"); for (DocumentProperty customDocumentProperty : (Iterable<DocumentProperty>) doc.getCustomDocumentProperties()) { System.out.println(customDocumentProperty.getName()); System.out.println("\tType:\t{customDocumentProperty.Type}"); System.out.println("\tValue:\t\"{customDocumentProperty.Value}\""); }
Method Detail |
---|
clear | → inherited from DocumentPropertyCollection |
public void clear() |
Example:
Shows how to add custom properties to a document.Document doc = new Document(); CustomDocumentProperties properties = doc.getCustomDocumentProperties(); // The custom property collection will be empty by default Assert.assertEquals(0, properties.getCount()); // We can populate it with key/value pairs with a variety of value types 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); // Custom properties are automatically sorted in alphabetic order Assert.assertEquals(properties.indexOf("Authorized Amount"), 1); Assert.assertEquals(properties.getCount(), 5); // Enumerate and print all custom properties Iterator<DocumentProperty> enumerator = properties.iterator(); try { while (enumerator.hasNext()) { DocumentProperty property = enumerator.next(); System.out.println(MessageFormat.format("Name: \"{0}\", Type: \"{1}\", Value: \"{2}\"", property.getName(), property.getType(), property.getValue())); } } finally { if (enumerator != null) enumerator.remove(); } // We can view/edit custom properties by opening the document and looking in File > Properties > Advanced Properties > Custom doc.save(getArtifactsDir() + "Properties.DocumentPropertyCollection.docx"); // We can remove elements from the property collection by index or by name properties.removeAt(1); Assert.assertFalse(properties.contains("Authorized Amount")); Assert.assertEquals(properties.getCount(), 4); properties.remove("Authorized Revision"); Assert.assertFalse(properties.contains("Authorized Revision")); Assert.assertEquals(properties.getCount(), 3); // We can also empty the entire custom property collection at once properties.clear(); Assert.assertEquals(properties.getCount(), 0);
contains | → inherited from DocumentPropertyCollection |
public boolean contains(java.lang.String name) |
name
- The case-insensitive name of the property.Example:
Shows how to add custom properties to a document.Document doc = new Document(); CustomDocumentProperties properties = doc.getCustomDocumentProperties(); // The custom property collection will be empty by default Assert.assertEquals(0, properties.getCount()); // We can populate it with key/value pairs with a variety of value types 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); // Custom properties are automatically sorted in alphabetic order Assert.assertEquals(properties.indexOf("Authorized Amount"), 1); Assert.assertEquals(properties.getCount(), 5); // Enumerate and print all custom properties Iterator<DocumentProperty> enumerator = properties.iterator(); try { while (enumerator.hasNext()) { DocumentProperty property = enumerator.next(); System.out.println(MessageFormat.format("Name: \"{0}\", Type: \"{1}\", Value: \"{2}\"", property.getName(), property.getType(), property.getValue())); } } finally { if (enumerator != null) enumerator.remove(); } // We can view/edit custom properties by opening the document and looking in File > Properties > Advanced Properties > Custom doc.save(getArtifactsDir() + "Properties.DocumentPropertyCollection.docx"); // We can remove elements from the property collection by index or by name properties.removeAt(1); Assert.assertFalse(properties.contains("Authorized Amount")); Assert.assertEquals(properties.getCount(), 4); properties.remove("Authorized Revision"); Assert.assertFalse(properties.contains("Authorized Revision")); Assert.assertEquals(properties.getCount(), 3); // We can also empty the entire custom property collection at once properties.clear(); Assert.assertEquals(properties.getCount(), 0);
indexOf | → inherited from DocumentPropertyCollection |
public int indexOf(java.lang.String name) |
Note: In Java this method is slow because iterates over all nodes.
name
- The case-insensitive name of the property.Example:
Shows how to add custom properties to a document.Document doc = new Document(); CustomDocumentProperties properties = doc.getCustomDocumentProperties(); // The custom property collection will be empty by default Assert.assertEquals(0, properties.getCount()); // We can populate it with key/value pairs with a variety of value types 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); // Custom properties are automatically sorted in alphabetic order Assert.assertEquals(properties.indexOf("Authorized Amount"), 1); Assert.assertEquals(properties.getCount(), 5); // Enumerate and print all custom properties Iterator<DocumentProperty> enumerator = properties.iterator(); try { while (enumerator.hasNext()) { DocumentProperty property = enumerator.next(); System.out.println(MessageFormat.format("Name: \"{0}\", Type: \"{1}\", Value: \"{2}\"", property.getName(), property.getType(), property.getValue())); } } finally { if (enumerator != null) enumerator.remove(); } // We can view/edit custom properties by opening the document and looking in File > Properties > Advanced Properties > Custom doc.save(getArtifactsDir() + "Properties.DocumentPropertyCollection.docx"); // We can remove elements from the property collection by index or by name properties.removeAt(1); Assert.assertFalse(properties.contains("Authorized Amount")); Assert.assertEquals(properties.getCount(), 4); properties.remove("Authorized Revision"); Assert.assertFalse(properties.contains("Authorized Revision")); Assert.assertEquals(properties.getCount(), 3); // We can also empty the entire custom property collection at once properties.clear(); Assert.assertEquals(properties.getCount(), 0);
iterator | → inherited from DocumentPropertyCollection |
public java.util.Iterator<DocumentProperty> iterator() |
Example:
Shows how to add custom properties to a document.Document doc = new Document(); CustomDocumentProperties properties = doc.getCustomDocumentProperties(); // The custom property collection will be empty by default Assert.assertEquals(0, properties.getCount()); // We can populate it with key/value pairs with a variety of value types 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); // Custom properties are automatically sorted in alphabetic order Assert.assertEquals(properties.indexOf("Authorized Amount"), 1); Assert.assertEquals(properties.getCount(), 5); // Enumerate and print all custom properties Iterator<DocumentProperty> enumerator = properties.iterator(); try { while (enumerator.hasNext()) { DocumentProperty property = enumerator.next(); System.out.println(MessageFormat.format("Name: \"{0}\", Type: \"{1}\", Value: \"{2}\"", property.getName(), property.getType(), property.getValue())); } } finally { if (enumerator != null) enumerator.remove(); } // We can view/edit custom properties by opening the document and looking in File > Properties > Advanced Properties > Custom doc.save(getArtifactsDir() + "Properties.DocumentPropertyCollection.docx"); // We can remove elements from the property collection by index or by name properties.removeAt(1); Assert.assertFalse(properties.contains("Authorized Amount")); Assert.assertEquals(properties.getCount(), 4); properties.remove("Authorized Revision"); Assert.assertFalse(properties.contains("Authorized Revision")); Assert.assertEquals(properties.getCount(), 3); // We can also empty the entire custom property collection at once properties.clear(); Assert.assertEquals(properties.getCount(), 0);
remove | → inherited from DocumentPropertyCollection |
public void remove(java.lang.String name) |
name
- The case-insensitive name of the property.Example:
Shows how to add custom properties to a document.Document doc = new Document(); CustomDocumentProperties properties = doc.getCustomDocumentProperties(); // The custom property collection will be empty by default Assert.assertEquals(0, properties.getCount()); // We can populate it with key/value pairs with a variety of value types 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); // Custom properties are automatically sorted in alphabetic order Assert.assertEquals(properties.indexOf("Authorized Amount"), 1); Assert.assertEquals(properties.getCount(), 5); // Enumerate and print all custom properties Iterator<DocumentProperty> enumerator = properties.iterator(); try { while (enumerator.hasNext()) { DocumentProperty property = enumerator.next(); System.out.println(MessageFormat.format("Name: \"{0}\", Type: \"{1}\", Value: \"{2}\"", property.getName(), property.getType(), property.getValue())); } } finally { if (enumerator != null) enumerator.remove(); } // We can view/edit custom properties by opening the document and looking in File > Properties > Advanced Properties > Custom doc.save(getArtifactsDir() + "Properties.DocumentPropertyCollection.docx"); // We can remove elements from the property collection by index or by name properties.removeAt(1); Assert.assertFalse(properties.contains("Authorized Amount")); Assert.assertEquals(properties.getCount(), 4); properties.remove("Authorized Revision"); Assert.assertFalse(properties.contains("Authorized Revision")); Assert.assertEquals(properties.getCount(), 3); // We can also empty the entire custom property collection at once properties.clear(); Assert.assertEquals(properties.getCount(), 0);
removeAt | → inherited from DocumentPropertyCollection |
public void removeAt(int index) |
Note: In Java this method is slow because iterates over all nodes.
index
- The zero based index.Example:
Shows how to add custom properties to a document.Document doc = new Document(); CustomDocumentProperties properties = doc.getCustomDocumentProperties(); // The custom property collection will be empty by default Assert.assertEquals(0, properties.getCount()); // We can populate it with key/value pairs with a variety of value types 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); // Custom properties are automatically sorted in alphabetic order Assert.assertEquals(properties.indexOf("Authorized Amount"), 1); Assert.assertEquals(properties.getCount(), 5); // Enumerate and print all custom properties Iterator<DocumentProperty> enumerator = properties.iterator(); try { while (enumerator.hasNext()) { DocumentProperty property = enumerator.next(); System.out.println(MessageFormat.format("Name: \"{0}\", Type: \"{1}\", Value: \"{2}\"", property.getName(), property.getType(), property.getValue())); } } finally { if (enumerator != null) enumerator.remove(); } // We can view/edit custom properties by opening the document and looking in File > Properties > Advanced Properties > Custom doc.save(getArtifactsDir() + "Properties.DocumentPropertyCollection.docx"); // We can remove elements from the property collection by index or by name properties.removeAt(1); Assert.assertFalse(properties.contains("Authorized Amount")); Assert.assertEquals(properties.getCount(), 4); properties.remove("Authorized Revision"); Assert.assertFalse(properties.contains("Authorized Revision")); Assert.assertEquals(properties.getCount(), 3); // We can also empty the entire custom property collection at once properties.clear(); Assert.assertEquals(properties.getCount(), 0);