public class BuiltInDocumentProperties
Provides access to
The names of the properties are case-insensitive.
The properties in the collection are sorted alphabetically by name.
Property Getters/Setters Summary | ||
---|---|---|
java.lang.String | getAuthor() | |
void | setAuthor(java.lang.Stringvalue) | |
Gets or sets the name of the document's author. | ||
int | getBytes() | |
void | setBytes(intvalue) | |
Represents an estimate of the number of bytes in the document. | ||
java.lang.String | getCategory() | |
void | setCategory(java.lang.Stringvalue) | |
Gets or sets the category of the document. | ||
int | getCharacters() | |
void | setCharacters(intvalue) | |
Represents an estimate of the number of characters in the document. | ||
int | getCharactersWithSpaces() | |
void | setCharactersWithSpaces(intvalue) | |
Represents an estimate of the number of characters (including spaces) in the document. | ||
java.lang.String | getComments() | |
void | setComments(java.lang.Stringvalue) | |
Gets or sets the document comments. | ||
java.lang.String | getCompany() | |
void | setCompany(java.lang.Stringvalue) | |
Gets or sets the company property. | ||
java.lang.String | getContentStatus() | |
void | setContentStatus(java.lang.Stringvalue) | |
Gets or sets the ContentStatus of the document. | ||
java.lang.String | getContentType() | |
void | setContentType(java.lang.Stringvalue) | |
Gets or sets the ContentStatus of the document. | ||
int | getCount() | |
Gets number of items in the collection.
|
||
java.util.Date | getCreatedTime() | |
void | setCreatedTime(java.util.Datevalue) | |
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.Stringvalue) | |
Specifies the base string used for evaluating relative hyperlinks in this document. | ||
java.lang.String | getKeywords() | |
void | setKeywords(java.lang.Stringvalue) | |
Gets or sets the document keywords. | ||
java.util.Date | getLastPrinted() | |
void | setLastPrinted(java.util.Datevalue) | |
Gets or sets the date when the document was last printed in UTC. | ||
java.lang.String | getLastSavedBy() | |
void | setLastSavedBy(java.lang.Stringvalue) | |
Gets or sets the name of the last author. | ||
java.util.Date | getLastSavedTime() | |
void | setLastSavedTime(java.util.Datevalue) | |
Gets or sets the time of the last save in UTC. | ||
int | getLines() | |
void | setLines(intvalue) | |
Represents an estimate of the number of lines in the document. | ||
boolean | getLinksUpToDate() | |
void | setLinksUpToDate(booleanvalue) | |
Indicates whether hyperlinks in a document are up-to-date. | ||
java.lang.String | getManager() | |
void | setManager(java.lang.Stringvalue) | |
Gets or sets the manager property. | ||
java.lang.String | getNameOfApplication() | |
void | setNameOfApplication(java.lang.Stringvalue) | |
Gets or sets the name of the application. | ||
int | getPages() | |
void | setPages(intvalue) | |
Represents an estimate of the number of pages in the document. | ||
int | getParagraphs() | |
void | setParagraphs(intvalue) | |
Represents an estimate of the number of paragraphs in the document. | ||
int | getRevisionNumber() | |
void | setRevisionNumber(intvalue) | |
Gets or sets the document revision number. | ||
int | getSecurity() | |
void | setSecurity(intvalue) | |
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.Stringvalue) | |
Gets or sets the subject of the document. | ||
java.lang.String | getTemplate() | |
void | setTemplate(java.lang.Stringvalue) | |
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.Stringvalue) | |
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(intvalue) | |
Gets or sets the total editing time in minutes. | ||
int | getVersion() | |
void | setVersion(intvalue) | |
Represents the version number of the application that created the document. | ||
int | getWords() | |
void | setWords(intvalue) | |
Represents an estimate of the number of words in the document. | ||
DocumentProperty | get(int index) | |
Returns a |
||
DocumentProperty | get(java.lang.String name) | |
Returns a |
Method Summary | ||
---|---|---|
void | clear() | |
Removes all properties from the collection.
|
||
boolean | contains(java.lang.String name) | |
Returns true if a property with the specified name exists in the collection.
|
||
int | indexOf(java.lang.String name) | |
Gets the index of a property by name.
|
||
java.util.Iterator<DocumentProperty> | iterator() | |
Returns an iterator object that can be used to iterate over all items in the collection.
|
||
void | remove(java.lang.String name) | |
Removes a property with the specified name from the collection.
|
||
void | removeAt(int index) | |
Removes a property at the specified index.
|
public java.lang.String getAuthor() / public void setAuthor(java.lang.String value)
Example:
Shows how to work with built-in document properties in the "Description" category.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // Below are four built-in document properties that have fields that can display their values in the document body. // 1 - "Author" property, which we can display using an AUTHOR field: properties.setAuthor("John Doe"); builder.write("Author:\t"); builder.insertField(FieldType.FIELD_AUTHOR, true); // 2 - "Title" property, which we can display using a TITLE field: properties.setTitle("John's Document"); builder.write("\nDoc title:\t"); builder.insertField(FieldType.FIELD_TITLE, true); // 3 - "Subject" property, which we can display using a SUBJECT field: properties.setSubject("My subject"); builder.write("\nSubject:\t"); builder.insertField(FieldType.FIELD_SUBJECT, true); // 4 - "Comments" property, which we can display using a COMMENTS field: properties.setComments(MessageFormat.format("This is {0}'s document about {1}", properties.getAuthor(), properties.getSubject())); builder.write("\nComments:\t\""); builder.insertField(FieldType.FIELD_COMMENTS, true); builder.write("\""); // The "Category" built-in property does not have a field that can display its value. properties.setCategory("My category"); // We can set multiple keywords for a document by separating the string value of the "Keywords" property with semicolons. properties.setKeywords("Tag 1; Tag 2; Tag 3"); // We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details". // The "Author" built-in property is in the "Origin" group, and the others are in the "Description" group. doc.save(getArtifactsDir() + "DocumentProperties.Description.docx");
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 { Document doc = new Document(getMyDir() + "Paragraphs.docx"); BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // By using built in properties, // we can treat document statistics such as word/page/character counts as metadata that can be glanced at without opening the document // These properties are accessed by right-clicking the file in Windows Explorer and navigating to Properties > Details > Content // If we want to display this data inside the document, we can use fields such as NUMPAGES, NUMWORDS, NUMCHARS etc. // Also, these values can also be viewed in Microsoft Word by navigating File > Properties > Advanced Properties > Statistics // Page count: The PageCount attribute shows the page count in real time and its value can be assigned to the Pages property // The "Pages" property stores the page count of the document. Assert.assertEquals(6, properties.getPages()); // The "Words", "Characters", and "CharactersWithSpaces" built-in properties also display various document statistics, // but we need to call the "UpdateWordCount" method on the whole document before we can expect them to contain accurate values. doc.updateWordCount(); Assert.assertEquals(1035, properties.getWords()); Assert.assertEquals(6026, properties.getCharacters()); Assert.assertEquals(7041, properties.getCharactersWithSpaces()); // Count the number of lines in the document, and then assign the result to the "Lines" built-in property. LineCounter lineCounter = new LineCounter(doc); properties.setLines(lineCounter.getLineCount()); Assert.assertEquals(142, properties.getLines()); // Assign the number of Paragraph nodes in the document to the "Paragraphs" built-in property. properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount()); Assert.assertEquals(29, properties.getParagraphs()); // Get an estimate of the file size of our document via the "Bytes" built-in property. Assert.assertEquals(20310, properties.getBytes()); // Set a different template for our document, and then update the "Template" built-in property manually to reflect this change. doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx"); Assert.assertEquals("Normal", properties.getTemplate()); properties.setTemplate(doc.getAttachedTemplate()); // "ContentStatus" is a descriptive built-in property. properties.setContentStatus("Draft"); // Upon saving, the "ContentType" built-in property will contain the MIME type of the output save format. Assert.assertEquals("", properties.getContentType()); // If the document contains links, and they are all up to date, we can set the "LinksUpToDate" property to "true". Assert.assertFalse(properties.getLinksUpToDate()); doc.save(getArtifactsDir() + "DocumentProperties.Content.docx"); } /// <summary> /// Counts the lines in a document. /// Traverses the document's layout entities tree upon construction, /// counting entities of the "Line" type that also contain real text. /// </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; }
public java.lang.String getCategory() / public void setCategory(java.lang.String value)
Example:
Shows how to work with built-in document properties in the "Description" category.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // Below are four built-in document properties that have fields that can display their values in the document body. // 1 - "Author" property, which we can display using an AUTHOR field: properties.setAuthor("John Doe"); builder.write("Author:\t"); builder.insertField(FieldType.FIELD_AUTHOR, true); // 2 - "Title" property, which we can display using a TITLE field: properties.setTitle("John's Document"); builder.write("\nDoc title:\t"); builder.insertField(FieldType.FIELD_TITLE, true); // 3 - "Subject" property, which we can display using a SUBJECT field: properties.setSubject("My subject"); builder.write("\nSubject:\t"); builder.insertField(FieldType.FIELD_SUBJECT, true); // 4 - "Comments" property, which we can display using a COMMENTS field: properties.setComments(MessageFormat.format("This is {0}'s document about {1}", properties.getAuthor(), properties.getSubject())); builder.write("\nComments:\t\""); builder.insertField(FieldType.FIELD_COMMENTS, true); builder.write("\""); // The "Category" built-in property does not have a field that can display its value. properties.setCategory("My category"); // We can set multiple keywords for a document by separating the string value of the "Keywords" property with semicolons. properties.setKeywords("Tag 1; Tag 2; Tag 3"); // We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details". // The "Author" built-in property is in the "Origin" group, and the others are in the "Description" group. doc.save(getArtifactsDir() + "DocumentProperties.Description.docx");
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); builder.writeln("Lorem ipsum dolor sit amet, consectetur adipiscing elit, " + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."); builder.write("Ut enim ad minim veniam, " + "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."); // Aspose.Words does not track document metrics like these in real time. Assert.assertEquals(0, doc.getBuiltInDocumentProperties().getCharacters()); Assert.assertEquals(0, doc.getBuiltInDocumentProperties().getWords()); Assert.assertEquals(1, doc.getBuiltInDocumentProperties().getParagraphs()); Assert.assertEquals(1, doc.getBuiltInDocumentProperties().getLines()); // To get accurate values for three of these properties, we will need to update them manually. doc.updateWordCount(); Assert.assertEquals(196, doc.getBuiltInDocumentProperties().getCharacters()); Assert.assertEquals(36, doc.getBuiltInDocumentProperties().getWords()); Assert.assertEquals(2, doc.getBuiltInDocumentProperties().getParagraphs()); // For the line count, we will need to call a specific overload of the updating method. Assert.assertEquals(1, doc.getBuiltInDocumentProperties().getLines()); doc.updateWordCount(true); Assert.assertEquals(4, doc.getBuiltInDocumentProperties().getLines());
Example:
Shows how to work with document properties in the "Content" category.public void content() throws Exception { Document doc = new Document(getMyDir() + "Paragraphs.docx"); BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // By using built in properties, // we can treat document statistics such as word/page/character counts as metadata that can be glanced at without opening the document // These properties are accessed by right-clicking the file in Windows Explorer and navigating to Properties > Details > Content // If we want to display this data inside the document, we can use fields such as NUMPAGES, NUMWORDS, NUMCHARS etc. // Also, these values can also be viewed in Microsoft Word by navigating File > Properties > Advanced Properties > Statistics // Page count: The PageCount attribute shows the page count in real time and its value can be assigned to the Pages property // The "Pages" property stores the page count of the document. Assert.assertEquals(6, properties.getPages()); // The "Words", "Characters", and "CharactersWithSpaces" built-in properties also display various document statistics, // but we need to call the "UpdateWordCount" method on the whole document before we can expect them to contain accurate values. doc.updateWordCount(); Assert.assertEquals(1035, properties.getWords()); Assert.assertEquals(6026, properties.getCharacters()); Assert.assertEquals(7041, properties.getCharactersWithSpaces()); // Count the number of lines in the document, and then assign the result to the "Lines" built-in property. LineCounter lineCounter = new LineCounter(doc); properties.setLines(lineCounter.getLineCount()); Assert.assertEquals(142, properties.getLines()); // Assign the number of Paragraph nodes in the document to the "Paragraphs" built-in property. properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount()); Assert.assertEquals(29, properties.getParagraphs()); // Get an estimate of the file size of our document via the "Bytes" built-in property. Assert.assertEquals(20310, properties.getBytes()); // Set a different template for our document, and then update the "Template" built-in property manually to reflect this change. doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx"); Assert.assertEquals("Normal", properties.getTemplate()); properties.setTemplate(doc.getAttachedTemplate()); // "ContentStatus" is a descriptive built-in property. properties.setContentStatus("Draft"); // Upon saving, the "ContentType" built-in property will contain the MIME type of the output save format. Assert.assertEquals("", properties.getContentType()); // If the document contains links, and they are all up to date, we can set the "LinksUpToDate" property to "true". Assert.assertFalse(properties.getLinksUpToDate()); doc.save(getArtifactsDir() + "DocumentProperties.Content.docx"); } /// <summary> /// Counts the lines in a document. /// Traverses the document's layout entities tree upon construction, /// counting entities of the "Line" type that also contain real text. /// </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; }
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 { Document doc = new Document(getMyDir() + "Paragraphs.docx"); BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // By using built in properties, // we can treat document statistics such as word/page/character counts as metadata that can be glanced at without opening the document // These properties are accessed by right-clicking the file in Windows Explorer and navigating to Properties > Details > Content // If we want to display this data inside the document, we can use fields such as NUMPAGES, NUMWORDS, NUMCHARS etc. // Also, these values can also be viewed in Microsoft Word by navigating File > Properties > Advanced Properties > Statistics // Page count: The PageCount attribute shows the page count in real time and its value can be assigned to the Pages property // The "Pages" property stores the page count of the document. Assert.assertEquals(6, properties.getPages()); // The "Words", "Characters", and "CharactersWithSpaces" built-in properties also display various document statistics, // but we need to call the "UpdateWordCount" method on the whole document before we can expect them to contain accurate values. doc.updateWordCount(); Assert.assertEquals(1035, properties.getWords()); Assert.assertEquals(6026, properties.getCharacters()); Assert.assertEquals(7041, properties.getCharactersWithSpaces()); // Count the number of lines in the document, and then assign the result to the "Lines" built-in property. LineCounter lineCounter = new LineCounter(doc); properties.setLines(lineCounter.getLineCount()); Assert.assertEquals(142, properties.getLines()); // Assign the number of Paragraph nodes in the document to the "Paragraphs" built-in property. properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount()); Assert.assertEquals(29, properties.getParagraphs()); // Get an estimate of the file size of our document via the "Bytes" built-in property. Assert.assertEquals(20310, properties.getBytes()); // Set a different template for our document, and then update the "Template" built-in property manually to reflect this change. doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx"); Assert.assertEquals("Normal", properties.getTemplate()); properties.setTemplate(doc.getAttachedTemplate()); // "ContentStatus" is a descriptive built-in property. properties.setContentStatus("Draft"); // Upon saving, the "ContentType" built-in property will contain the MIME type of the output save format. Assert.assertEquals("", properties.getContentType()); // If the document contains links, and they are all up to date, we can set the "LinksUpToDate" property to "true". Assert.assertFalse(properties.getLinksUpToDate()); doc.save(getArtifactsDir() + "DocumentProperties.Content.docx"); } /// <summary> /// Counts the lines in a document. /// Traverses the document's layout entities tree upon construction, /// counting entities of the "Line" type that also contain real text. /// </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; }
public java.lang.String getComments() / public void setComments(java.lang.String value)
Example:
Shows how to work with built-in document properties in the "Description" category.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // Below are four built-in document properties that have fields that can display their values in the document body. // 1 - "Author" property, which we can display using an AUTHOR field: properties.setAuthor("John Doe"); builder.write("Author:\t"); builder.insertField(FieldType.FIELD_AUTHOR, true); // 2 - "Title" property, which we can display using a TITLE field: properties.setTitle("John's Document"); builder.write("\nDoc title:\t"); builder.insertField(FieldType.FIELD_TITLE, true); // 3 - "Subject" property, which we can display using a SUBJECT field: properties.setSubject("My subject"); builder.write("\nSubject:\t"); builder.insertField(FieldType.FIELD_SUBJECT, true); // 4 - "Comments" property, which we can display using a COMMENTS field: properties.setComments(MessageFormat.format("This is {0}'s document about {1}", properties.getAuthor(), properties.getSubject())); builder.write("\nComments:\t\""); builder.insertField(FieldType.FIELD_COMMENTS, true); builder.write("\""); // The "Category" built-in property does not have a field that can display its value. properties.setCategory("My category"); // We can set multiple keywords for a document by separating the string value of the "Keywords" property with semicolons. properties.setKeywords("Tag 1; Tag 2; Tag 3"); // We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details". // The "Author" built-in property is in the "Origin" group, and the others are in the "Description" group. doc.save(getArtifactsDir() + "DocumentProperties.Description.docx");
public java.lang.String getCompany() / public void setCompany(java.lang.String value)
Example:
Shows how to work with document properties in the "Origin" category.// Open a document that we have created and edited using Microsoft Word. Document doc = new Document(getMyDir() + "Properties.docx"); BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // The following built-in properties contain information regarding the creation and editing of this document. // We can right-click this document in Windows Explorer and find // these properties via "Properties" -> "Details" -> "Origin" category. // Fields such as PRINTDATE and EDITTIME can display these values in the document body. System.out.println(MessageFormat.format("Created using {0}, on {1}", properties.getNameOfApplication(), properties.getCreatedTime())); System.out.println(MessageFormat.format("Minutes spent editing: {0}", properties.getTotalEditingTime())); System.out.println(MessageFormat.format("Date/time last printed: {0}", properties.getLastPrinted())); System.out.println(MessageFormat.format("Template document: {0}", properties.getTemplate())); // We can also change the values of built-in properties. properties.setCompany("Doe Ltd."); properties.setManager("Jane Doe"); properties.setVersion(5); properties.setRevisionNumber(properties.getRevisionNumber() + 1); // Microsoft Word updates the following properties automatically when we save the document. // To use these properties with Aspose.Words, we will need to set values for them manually. properties.setLastSavedBy("John Doe"); properties.setLastSavedTime(new Date()); // We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details" -> "Origin". doc.save(getArtifactsDir() + "DocumentProperties.Origin.docx");
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 { Document doc = new Document(getMyDir() + "Paragraphs.docx"); BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // By using built in properties, // we can treat document statistics such as word/page/character counts as metadata that can be glanced at without opening the document // These properties are accessed by right-clicking the file in Windows Explorer and navigating to Properties > Details > Content // If we want to display this data inside the document, we can use fields such as NUMPAGES, NUMWORDS, NUMCHARS etc. // Also, these values can also be viewed in Microsoft Word by navigating File > Properties > Advanced Properties > Statistics // Page count: The PageCount attribute shows the page count in real time and its value can be assigned to the Pages property // The "Pages" property stores the page count of the document. Assert.assertEquals(6, properties.getPages()); // The "Words", "Characters", and "CharactersWithSpaces" built-in properties also display various document statistics, // but we need to call the "UpdateWordCount" method on the whole document before we can expect them to contain accurate values. doc.updateWordCount(); Assert.assertEquals(1035, properties.getWords()); Assert.assertEquals(6026, properties.getCharacters()); Assert.assertEquals(7041, properties.getCharactersWithSpaces()); // Count the number of lines in the document, and then assign the result to the "Lines" built-in property. LineCounter lineCounter = new LineCounter(doc); properties.setLines(lineCounter.getLineCount()); Assert.assertEquals(142, properties.getLines()); // Assign the number of Paragraph nodes in the document to the "Paragraphs" built-in property. properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount()); Assert.assertEquals(29, properties.getParagraphs()); // Get an estimate of the file size of our document via the "Bytes" built-in property. Assert.assertEquals(20310, properties.getBytes()); // Set a different template for our document, and then update the "Template" built-in property manually to reflect this change. doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx"); Assert.assertEquals("Normal", properties.getTemplate()); properties.setTemplate(doc.getAttachedTemplate()); // "ContentStatus" is a descriptive built-in property. properties.setContentStatus("Draft"); // Upon saving, the "ContentType" built-in property will contain the MIME type of the output save format. Assert.assertEquals("", properties.getContentType()); // If the document contains links, and they are all up to date, we can set the "LinksUpToDate" property to "true". Assert.assertFalse(properties.getLinksUpToDate()); doc.save(getArtifactsDir() + "DocumentProperties.Content.docx"); } /// <summary> /// Counts the lines in a document. /// Traverses the document's layout entities tree upon construction, /// counting entities of the "Line" type that also contain real text. /// </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; }
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 { Document doc = new Document(getMyDir() + "Paragraphs.docx"); BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // By using built in properties, // we can treat document statistics such as word/page/character counts as metadata that can be glanced at without opening the document // These properties are accessed by right-clicking the file in Windows Explorer and navigating to Properties > Details > Content // If we want to display this data inside the document, we can use fields such as NUMPAGES, NUMWORDS, NUMCHARS etc. // Also, these values can also be viewed in Microsoft Word by navigating File > Properties > Advanced Properties > Statistics // Page count: The PageCount attribute shows the page count in real time and its value can be assigned to the Pages property // The "Pages" property stores the page count of the document. Assert.assertEquals(6, properties.getPages()); // The "Words", "Characters", and "CharactersWithSpaces" built-in properties also display various document statistics, // but we need to call the "UpdateWordCount" method on the whole document before we can expect them to contain accurate values. doc.updateWordCount(); Assert.assertEquals(1035, properties.getWords()); Assert.assertEquals(6026, properties.getCharacters()); Assert.assertEquals(7041, properties.getCharactersWithSpaces()); // Count the number of lines in the document, and then assign the result to the "Lines" built-in property. LineCounter lineCounter = new LineCounter(doc); properties.setLines(lineCounter.getLineCount()); Assert.assertEquals(142, properties.getLines()); // Assign the number of Paragraph nodes in the document to the "Paragraphs" built-in property. properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount()); Assert.assertEquals(29, properties.getParagraphs()); // Get an estimate of the file size of our document via the "Bytes" built-in property. Assert.assertEquals(20310, properties.getBytes()); // Set a different template for our document, and then update the "Template" built-in property manually to reflect this change. doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx"); Assert.assertEquals("Normal", properties.getTemplate()); properties.setTemplate(doc.getAttachedTemplate()); // "ContentStatus" is a descriptive built-in property. properties.setContentStatus("Draft"); // Upon saving, the "ContentType" built-in property will contain the MIME type of the output save format. Assert.assertEquals("", properties.getContentType()); // If the document contains links, and they are all up to date, we can set the "LinksUpToDate" property to "true". Assert.assertFalse(properties.getLinksUpToDate()); doc.save(getArtifactsDir() + "DocumentProperties.Content.docx"); } /// <summary> /// Counts the lines in a document. /// Traverses the document's layout entities tree upon construction, /// counting entities of the "Line" type that also contain real text. /// </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; }
public int getCount()
Example:
Shows how to work with custom document properties.Document doc = new Document(getMyDir() + "Properties.docx"); // Every document contains a collection of custom properties, which, like the built-in properties, are key-value pairs. // Unlike the built-in properties, many of which the document maintains by itself, we need to create all of our own custom properties. Assert.assertEquals("Value of custom document property", doc.getCustomDocumentProperties().get("CustomProperty").toString()); doc.getCustomDocumentProperties().add("CustomProperty2", "Value of custom document property #2"); System.out.println("Custom Properties:"); for (DocumentProperty customDocumentProperty : doc.getCustomDocumentProperties()) { System.out.println(customDocumentProperty.getName()); System.out.println(MessageFormat.format("\tType:\t{0}", customDocumentProperty.getType())); System.out.println(MessageFormat.format("\tValue:\t\"{0}\"", customDocumentProperty.getValue())); }
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.// Open a document that we have created and edited using Microsoft Word. Document doc = new Document(getMyDir() + "Properties.docx"); BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // The following built-in properties contain information regarding the creation and editing of this document. // We can right-click this document in Windows Explorer and find // these properties via "Properties" -> "Details" -> "Origin" category. // Fields such as PRINTDATE and EDITTIME can display these values in the document body. System.out.println(MessageFormat.format("Created using {0}, on {1}", properties.getNameOfApplication(), properties.getCreatedTime())); System.out.println(MessageFormat.format("Minutes spent editing: {0}", properties.getTotalEditingTime())); System.out.println(MessageFormat.format("Date/time last printed: {0}", properties.getLastPrinted())); System.out.println(MessageFormat.format("Template document: {0}", properties.getTemplate())); // We can also change the values of built-in properties. properties.setCompany("Doe Ltd."); properties.setManager("Jane Doe"); properties.setVersion(5); properties.setRevisionNumber(properties.getRevisionNumber() + 1); // Microsoft Word updates the following properties automatically when we save the document. // To use these properties with Aspose.Words, we will need to set values for them manually. properties.setLastSavedBy("John Doe"); properties.setLastSavedTime(new Date()); // We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details" -> "Origin". doc.save(getArtifactsDir() + "DocumentProperties.Origin.docx");
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.Document doc = new Document(getMyDir() + "Heading pairs and titles of parts.docx"); // We can find the combined values of these collections via "File" -> "Properties" -> "Advanced Properties" > "Contents" tab. // The HeadingPairs property is a collection of <string, int> pairs that determines // how many document parts a heading spans across. Object[] headingPairs = doc.getBuiltInDocumentProperties().getHeadingPairs(); // The TitlesOfParts property contains the names of parts that belong to the above headings. String[] titlesOfParts = doc.getBuiltInDocumentProperties().getTitlesOfParts(); int headingPairsIndex = 0; int titlesOfPartsIndex = 0; while (headingPairsIndex < headingPairs.length) { System.out.println(MessageFormat.format("Parts for {0}:", headingPairs[headingPairsIndex++])); int partsCount = (int)headingPairs[headingPairsIndex++]; for (int i = 0; i < partsCount; i++) System.out.println(MessageFormat.format("\t\"{0}\"", titlesOfParts[titlesOfPartsIndex++])); }
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.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert a relative hyperlink to a document in the local file system named "Document.docx". // When we click the link in Microsoft Word, it will open the designated document, provided that it is available. builder.insertHyperlink("Relative hyperlink", "Document.docx", false); // This link is relative. If there is no "Document.docx" in the same folder // as the document that contains this link, the link will be broken. Assert.assertFalse(new File(getArtifactsDir() + "Document.docx").exists()); doc.save(getArtifactsDir() + "DocumentProperties.HyperlinkBase.BrokenLink.docx"); // The document we are trying to link to is in a different directory to the one we are planning to save the document in. // We could fix links like this by putting an absolute filename in each one. // Alternatively, we could provide a base link that every hyperlink with a relative filename // will prepend to its link when we click on it. BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); properties.setHyperlinkBase(getMyDir()); Assert.assertTrue(new File(properties.getHyperlinkBase() + ((FieldHyperlink)doc.getRange().getFields().get(0)).getAddress()).exists()); doc.save(getArtifactsDir() + "DocumentProperties.HyperlinkBase.WorkingLink.docx");
public java.lang.String getKeywords() / public void setKeywords(java.lang.String value)
Example:
Shows how to work with built-in document properties in the "Description" category.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // Below are four built-in document properties that have fields that can display their values in the document body. // 1 - "Author" property, which we can display using an AUTHOR field: properties.setAuthor("John Doe"); builder.write("Author:\t"); builder.insertField(FieldType.FIELD_AUTHOR, true); // 2 - "Title" property, which we can display using a TITLE field: properties.setTitle("John's Document"); builder.write("\nDoc title:\t"); builder.insertField(FieldType.FIELD_TITLE, true); // 3 - "Subject" property, which we can display using a SUBJECT field: properties.setSubject("My subject"); builder.write("\nSubject:\t"); builder.insertField(FieldType.FIELD_SUBJECT, true); // 4 - "Comments" property, which we can display using a COMMENTS field: properties.setComments(MessageFormat.format("This is {0}'s document about {1}", properties.getAuthor(), properties.getSubject())); builder.write("\nComments:\t\""); builder.insertField(FieldType.FIELD_COMMENTS, true); builder.write("\""); // The "Category" built-in property does not have a field that can display its value. properties.setCategory("My category"); // We can set multiple keywords for a document by separating the string value of the "Keywords" property with semicolons. properties.setKeywords("Tag 1; Tag 2; Tag 3"); // We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details". // The "Author" built-in property is in the "Origin" group, and the others are in the "Description" group. doc.save(getArtifactsDir() + "DocumentProperties.Description.docx");
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.// Open a document that we have created and edited using Microsoft Word. Document doc = new Document(getMyDir() + "Properties.docx"); BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // The following built-in properties contain information regarding the creation and editing of this document. // We can right-click this document in Windows Explorer and find // these properties via "Properties" -> "Details" -> "Origin" category. // Fields such as PRINTDATE and EDITTIME can display these values in the document body. System.out.println(MessageFormat.format("Created using {0}, on {1}", properties.getNameOfApplication(), properties.getCreatedTime())); System.out.println(MessageFormat.format("Minutes spent editing: {0}", properties.getTotalEditingTime())); System.out.println(MessageFormat.format("Date/time last printed: {0}", properties.getLastPrinted())); System.out.println(MessageFormat.format("Template document: {0}", properties.getTemplate())); // We can also change the values of built-in properties. properties.setCompany("Doe Ltd."); properties.setManager("Jane Doe"); properties.setVersion(5); properties.setRevisionNumber(properties.getRevisionNumber() + 1); // Microsoft Word updates the following properties automatically when we save the document. // To use these properties with Aspose.Words, we will need to set values for them manually. properties.setLastSavedBy("John Doe"); properties.setLastSavedTime(new Date()); // We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details" -> "Origin". doc.save(getArtifactsDir() + "DocumentProperties.Origin.docx");
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.// Open a document that we have created and edited using Microsoft Word. Document doc = new Document(getMyDir() + "Properties.docx"); BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // The following built-in properties contain information regarding the creation and editing of this document. // We can right-click this document in Windows Explorer and find // these properties via "Properties" -> "Details" -> "Origin" category. // Fields such as PRINTDATE and EDITTIME can display these values in the document body. System.out.println(MessageFormat.format("Created using {0}, on {1}", properties.getNameOfApplication(), properties.getCreatedTime())); System.out.println(MessageFormat.format("Minutes spent editing: {0}", properties.getTotalEditingTime())); System.out.println(MessageFormat.format("Date/time last printed: {0}", properties.getLastPrinted())); System.out.println(MessageFormat.format("Template document: {0}", properties.getTemplate())); // We can also change the values of built-in properties. properties.setCompany("Doe Ltd."); properties.setManager("Jane Doe"); properties.setVersion(5); properties.setRevisionNumber(properties.getRevisionNumber() + 1); // Microsoft Word updates the following properties automatically when we save the document. // To use these properties with Aspose.Words, we will need to set values for them manually. properties.setLastSavedBy("John Doe"); properties.setLastSavedTime(new Date()); // We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details" -> "Origin". doc.save(getArtifactsDir() + "DocumentProperties.Origin.docx");
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.// Open a document that we have created and edited using Microsoft Word. Document doc = new Document(getMyDir() + "Properties.docx"); BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // The following built-in properties contain information regarding the creation and editing of this document. // We can right-click this document in Windows Explorer and find // these properties via "Properties" -> "Details" -> "Origin" category. // Fields such as PRINTDATE and EDITTIME can display these values in the document body. System.out.println(MessageFormat.format("Created using {0}, on {1}", properties.getNameOfApplication(), properties.getCreatedTime())); System.out.println(MessageFormat.format("Minutes spent editing: {0}", properties.getTotalEditingTime())); System.out.println(MessageFormat.format("Date/time last printed: {0}", properties.getLastPrinted())); System.out.println(MessageFormat.format("Template document: {0}", properties.getTemplate())); // We can also change the values of built-in properties. properties.setCompany("Doe Ltd."); properties.setManager("Jane Doe"); properties.setVersion(5); properties.setRevisionNumber(properties.getRevisionNumber() + 1); // Microsoft Word updates the following properties automatically when we save the document. // To use these properties with Aspose.Words, we will need to set values for them manually. properties.setLastSavedBy("John Doe"); properties.setLastSavedTime(new Date()); // We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details" -> "Origin". doc.save(getArtifactsDir() + "DocumentProperties.Origin.docx");
Example:
Shows how to use the SAVEDATE field to display the date/time of the document's most recent save operation performed using Microsoft Word.Document doc = new Document(getMyDir() + "Document.docx"); DocumentBuilder builder = new DocumentBuilder(doc); builder.moveToDocumentEnd(); builder.writeln(" Date this document was last saved:"); // We can use the SAVEDATE field to display the last save operation's date and time on the document. // The save operation that these fields refer to is the manual save in an application like Microsoft Word, // not the document's Save method. // Below are three different calendar types according to which the SAVEDATE field can display the date/time. // 1 - Islamic Lunar Calendar: builder.write("According to the Lunar Calendar - "); FieldSaveDate field = (FieldSaveDate) builder.insertField(FieldType.FIELD_SAVE_DATE, true); field.setUseLunarCalendar(true); Assert.assertEquals(" SAVEDATE \\h", field.getFieldCode()); // 2 - Umm al-Qura calendar: builder.write("\nAccording to the Umm al-Qura calendar - "); field = (FieldSaveDate) builder.insertField(FieldType.FIELD_SAVE_DATE, true); field.setUseUmAlQuraCalendar(true); Assert.assertEquals(" SAVEDATE \\u", field.getFieldCode()); // 3 - Indian National calendar: builder.write("\nAccording to the Indian National calendar - "); field = (FieldSaveDate) builder.insertField(FieldType.FIELD_SAVE_DATE, true); field.setUseSakaEraCalendar(true); Assert.assertEquals(" SAVEDATE \\s", field.getFieldCode()); // The SAVEDATE fields draw their date/time values from the LastSavedTime built-in property. // The document's Save method will not update this value, but we can still update it manually. doc.getBuiltInDocumentProperties().setLastSavedTime(new Date()); doc.updateFields(); doc.save(getArtifactsDir() + "Field.SAVEDATE.docx");
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); builder.writeln("Lorem ipsum dolor sit amet, consectetur adipiscing elit, " + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."); builder.write("Ut enim ad minim veniam, " + "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."); // Aspose.Words does not track document metrics like these in real time. Assert.assertEquals(0, doc.getBuiltInDocumentProperties().getCharacters()); Assert.assertEquals(0, doc.getBuiltInDocumentProperties().getWords()); Assert.assertEquals(1, doc.getBuiltInDocumentProperties().getParagraphs()); Assert.assertEquals(1, doc.getBuiltInDocumentProperties().getLines()); // To get accurate values for three of these properties, we will need to update them manually. doc.updateWordCount(); Assert.assertEquals(196, doc.getBuiltInDocumentProperties().getCharacters()); Assert.assertEquals(36, doc.getBuiltInDocumentProperties().getWords()); Assert.assertEquals(2, doc.getBuiltInDocumentProperties().getParagraphs()); // For the line count, we will need to call a specific overload of the updating method. Assert.assertEquals(1, doc.getBuiltInDocumentProperties().getLines()); doc.updateWordCount(true); Assert.assertEquals(4, doc.getBuiltInDocumentProperties().getLines());
Example:
Shows how to work with document properties in the "Content" category.public void content() throws Exception { Document doc = new Document(getMyDir() + "Paragraphs.docx"); BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // By using built in properties, // we can treat document statistics such as word/page/character counts as metadata that can be glanced at without opening the document // These properties are accessed by right-clicking the file in Windows Explorer and navigating to Properties > Details > Content // If we want to display this data inside the document, we can use fields such as NUMPAGES, NUMWORDS, NUMCHARS etc. // Also, these values can also be viewed in Microsoft Word by navigating File > Properties > Advanced Properties > Statistics // Page count: The PageCount attribute shows the page count in real time and its value can be assigned to the Pages property // The "Pages" property stores the page count of the document. Assert.assertEquals(6, properties.getPages()); // The "Words", "Characters", and "CharactersWithSpaces" built-in properties also display various document statistics, // but we need to call the "UpdateWordCount" method on the whole document before we can expect them to contain accurate values. doc.updateWordCount(); Assert.assertEquals(1035, properties.getWords()); Assert.assertEquals(6026, properties.getCharacters()); Assert.assertEquals(7041, properties.getCharactersWithSpaces()); // Count the number of lines in the document, and then assign the result to the "Lines" built-in property. LineCounter lineCounter = new LineCounter(doc); properties.setLines(lineCounter.getLineCount()); Assert.assertEquals(142, properties.getLines()); // Assign the number of Paragraph nodes in the document to the "Paragraphs" built-in property. properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount()); Assert.assertEquals(29, properties.getParagraphs()); // Get an estimate of the file size of our document via the "Bytes" built-in property. Assert.assertEquals(20310, properties.getBytes()); // Set a different template for our document, and then update the "Template" built-in property manually to reflect this change. doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx"); Assert.assertEquals("Normal", properties.getTemplate()); properties.setTemplate(doc.getAttachedTemplate()); // "ContentStatus" is a descriptive built-in property. properties.setContentStatus("Draft"); // Upon saving, the "ContentType" built-in property will contain the MIME type of the output save format. Assert.assertEquals("", properties.getContentType()); // If the document contains links, and they are all up to date, we can set the "LinksUpToDate" property to "true". Assert.assertFalse(properties.getLinksUpToDate()); doc.save(getArtifactsDir() + "DocumentProperties.Content.docx"); } /// <summary> /// Counts the lines in a document. /// Traverses the document's layout entities tree upon construction, /// counting entities of the "Line" type that also contain real text. /// </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; }
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 { Document doc = new Document(getMyDir() + "Paragraphs.docx"); BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // By using built in properties, // we can treat document statistics such as word/page/character counts as metadata that can be glanced at without opening the document // These properties are accessed by right-clicking the file in Windows Explorer and navigating to Properties > Details > Content // If we want to display this data inside the document, we can use fields such as NUMPAGES, NUMWORDS, NUMCHARS etc. // Also, these values can also be viewed in Microsoft Word by navigating File > Properties > Advanced Properties > Statistics // Page count: The PageCount attribute shows the page count in real time and its value can be assigned to the Pages property // The "Pages" property stores the page count of the document. Assert.assertEquals(6, properties.getPages()); // The "Words", "Characters", and "CharactersWithSpaces" built-in properties also display various document statistics, // but we need to call the "UpdateWordCount" method on the whole document before we can expect them to contain accurate values. doc.updateWordCount(); Assert.assertEquals(1035, properties.getWords()); Assert.assertEquals(6026, properties.getCharacters()); Assert.assertEquals(7041, properties.getCharactersWithSpaces()); // Count the number of lines in the document, and then assign the result to the "Lines" built-in property. LineCounter lineCounter = new LineCounter(doc); properties.setLines(lineCounter.getLineCount()); Assert.assertEquals(142, properties.getLines()); // Assign the number of Paragraph nodes in the document to the "Paragraphs" built-in property. properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount()); Assert.assertEquals(29, properties.getParagraphs()); // Get an estimate of the file size of our document via the "Bytes" built-in property. Assert.assertEquals(20310, properties.getBytes()); // Set a different template for our document, and then update the "Template" built-in property manually to reflect this change. doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx"); Assert.assertEquals("Normal", properties.getTemplate()); properties.setTemplate(doc.getAttachedTemplate()); // "ContentStatus" is a descriptive built-in property. properties.setContentStatus("Draft"); // Upon saving, the "ContentType" built-in property will contain the MIME type of the output save format. Assert.assertEquals("", properties.getContentType()); // If the document contains links, and they are all up to date, we can set the "LinksUpToDate" property to "true". Assert.assertFalse(properties.getLinksUpToDate()); doc.save(getArtifactsDir() + "DocumentProperties.Content.docx"); } /// <summary> /// Counts the lines in a document. /// Traverses the document's layout entities tree upon construction, /// counting entities of the "Line" type that also contain real text. /// </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; }
public java.lang.String getManager() / public void setManager(java.lang.String value)
Example:
Shows how to work with document properties in the "Origin" category.// Open a document that we have created and edited using Microsoft Word. Document doc = new Document(getMyDir() + "Properties.docx"); BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // The following built-in properties contain information regarding the creation and editing of this document. // We can right-click this document in Windows Explorer and find // these properties via "Properties" -> "Details" -> "Origin" category. // Fields such as PRINTDATE and EDITTIME can display these values in the document body. System.out.println(MessageFormat.format("Created using {0}, on {1}", properties.getNameOfApplication(), properties.getCreatedTime())); System.out.println(MessageFormat.format("Minutes spent editing: {0}", properties.getTotalEditingTime())); System.out.println(MessageFormat.format("Date/time last printed: {0}", properties.getLastPrinted())); System.out.println(MessageFormat.format("Template document: {0}", properties.getTemplate())); // We can also change the values of built-in properties. properties.setCompany("Doe Ltd."); properties.setManager("Jane Doe"); properties.setVersion(5); properties.setRevisionNumber(properties.getRevisionNumber() + 1); // Microsoft Word updates the following properties automatically when we save the document. // To use these properties with Aspose.Words, we will need to set values for them manually. properties.setLastSavedBy("John Doe"); properties.setLastSavedTime(new Date()); // We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details" -> "Origin". doc.save(getArtifactsDir() + "DocumentProperties.Origin.docx");
public java.lang.String getNameOfApplication() / public void setNameOfApplication(java.lang.String value)
Example:
Shows how to work with document properties in the "Origin" category.// Open a document that we have created and edited using Microsoft Word. Document doc = new Document(getMyDir() + "Properties.docx"); BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // The following built-in properties contain information regarding the creation and editing of this document. // We can right-click this document in Windows Explorer and find // these properties via "Properties" -> "Details" -> "Origin" category. // Fields such as PRINTDATE and EDITTIME can display these values in the document body. System.out.println(MessageFormat.format("Created using {0}, on {1}", properties.getNameOfApplication(), properties.getCreatedTime())); System.out.println(MessageFormat.format("Minutes spent editing: {0}", properties.getTotalEditingTime())); System.out.println(MessageFormat.format("Date/time last printed: {0}", properties.getLastPrinted())); System.out.println(MessageFormat.format("Template document: {0}", properties.getTemplate())); // We can also change the values of built-in properties. properties.setCompany("Doe Ltd."); properties.setManager("Jane Doe"); properties.setVersion(5); properties.setRevisionNumber(properties.getRevisionNumber() + 1); // Microsoft Word updates the following properties automatically when we save the document. // To use these properties with Aspose.Words, we will need to set values for them manually. properties.setLastSavedBy("John Doe"); properties.setLastSavedTime(new Date()); // We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details" -> "Origin". doc.save(getArtifactsDir() + "DocumentProperties.Origin.docx");
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 { Document doc = new Document(getMyDir() + "Paragraphs.docx"); BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // By using built in properties, // we can treat document statistics such as word/page/character counts as metadata that can be glanced at without opening the document // These properties are accessed by right-clicking the file in Windows Explorer and navigating to Properties > Details > Content // If we want to display this data inside the document, we can use fields such as NUMPAGES, NUMWORDS, NUMCHARS etc. // Also, these values can also be viewed in Microsoft Word by navigating File > Properties > Advanced Properties > Statistics // Page count: The PageCount attribute shows the page count in real time and its value can be assigned to the Pages property // The "Pages" property stores the page count of the document. Assert.assertEquals(6, properties.getPages()); // The "Words", "Characters", and "CharactersWithSpaces" built-in properties also display various document statistics, // but we need to call the "UpdateWordCount" method on the whole document before we can expect them to contain accurate values. doc.updateWordCount(); Assert.assertEquals(1035, properties.getWords()); Assert.assertEquals(6026, properties.getCharacters()); Assert.assertEquals(7041, properties.getCharactersWithSpaces()); // Count the number of lines in the document, and then assign the result to the "Lines" built-in property. LineCounter lineCounter = new LineCounter(doc); properties.setLines(lineCounter.getLineCount()); Assert.assertEquals(142, properties.getLines()); // Assign the number of Paragraph nodes in the document to the "Paragraphs" built-in property. properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount()); Assert.assertEquals(29, properties.getParagraphs()); // Get an estimate of the file size of our document via the "Bytes" built-in property. Assert.assertEquals(20310, properties.getBytes()); // Set a different template for our document, and then update the "Template" built-in property manually to reflect this change. doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx"); Assert.assertEquals("Normal", properties.getTemplate()); properties.setTemplate(doc.getAttachedTemplate()); // "ContentStatus" is a descriptive built-in property. properties.setContentStatus("Draft"); // Upon saving, the "ContentType" built-in property will contain the MIME type of the output save format. Assert.assertEquals("", properties.getContentType()); // If the document contains links, and they are all up to date, we can set the "LinksUpToDate" property to "true". Assert.assertFalse(properties.getLinksUpToDate()); doc.save(getArtifactsDir() + "DocumentProperties.Content.docx"); } /// <summary> /// Counts the lines in a document. /// Traverses the document's layout entities tree upon construction, /// counting entities of the "Line" type that also contain real text. /// </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; }
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); builder.writeln("Lorem ipsum dolor sit amet, consectetur adipiscing elit, " + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."); builder.write("Ut enim ad minim veniam, " + "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."); // Aspose.Words does not track document metrics like these in real time. Assert.assertEquals(0, doc.getBuiltInDocumentProperties().getCharacters()); Assert.assertEquals(0, doc.getBuiltInDocumentProperties().getWords()); Assert.assertEquals(1, doc.getBuiltInDocumentProperties().getParagraphs()); Assert.assertEquals(1, doc.getBuiltInDocumentProperties().getLines()); // To get accurate values for three of these properties, we will need to update them manually. doc.updateWordCount(); Assert.assertEquals(196, doc.getBuiltInDocumentProperties().getCharacters()); Assert.assertEquals(36, doc.getBuiltInDocumentProperties().getWords()); Assert.assertEquals(2, doc.getBuiltInDocumentProperties().getParagraphs()); // For the line count, we will need to call a specific overload of the updating method. Assert.assertEquals(1, doc.getBuiltInDocumentProperties().getLines()); doc.updateWordCount(true); Assert.assertEquals(4, doc.getBuiltInDocumentProperties().getLines());
Example:
Shows how to work with document properties in the "Content" category.public void content() throws Exception { Document doc = new Document(getMyDir() + "Paragraphs.docx"); BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // By using built in properties, // we can treat document statistics such as word/page/character counts as metadata that can be glanced at without opening the document // These properties are accessed by right-clicking the file in Windows Explorer and navigating to Properties > Details > Content // If we want to display this data inside the document, we can use fields such as NUMPAGES, NUMWORDS, NUMCHARS etc. // Also, these values can also be viewed in Microsoft Word by navigating File > Properties > Advanced Properties > Statistics // Page count: The PageCount attribute shows the page count in real time and its value can be assigned to the Pages property // The "Pages" property stores the page count of the document. Assert.assertEquals(6, properties.getPages()); // The "Words", "Characters", and "CharactersWithSpaces" built-in properties also display various document statistics, // but we need to call the "UpdateWordCount" method on the whole document before we can expect them to contain accurate values. doc.updateWordCount(); Assert.assertEquals(1035, properties.getWords()); Assert.assertEquals(6026, properties.getCharacters()); Assert.assertEquals(7041, properties.getCharactersWithSpaces()); // Count the number of lines in the document, and then assign the result to the "Lines" built-in property. LineCounter lineCounter = new LineCounter(doc); properties.setLines(lineCounter.getLineCount()); Assert.assertEquals(142, properties.getLines()); // Assign the number of Paragraph nodes in the document to the "Paragraphs" built-in property. properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount()); Assert.assertEquals(29, properties.getParagraphs()); // Get an estimate of the file size of our document via the "Bytes" built-in property. Assert.assertEquals(20310, properties.getBytes()); // Set a different template for our document, and then update the "Template" built-in property manually to reflect this change. doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx"); Assert.assertEquals("Normal", properties.getTemplate()); properties.setTemplate(doc.getAttachedTemplate()); // "ContentStatus" is a descriptive built-in property. properties.setContentStatus("Draft"); // Upon saving, the "ContentType" built-in property will contain the MIME type of the output save format. Assert.assertEquals("", properties.getContentType()); // If the document contains links, and they are all up to date, we can set the "LinksUpToDate" property to "true". Assert.assertFalse(properties.getLinksUpToDate()); doc.save(getArtifactsDir() + "DocumentProperties.Content.docx"); } /// <summary> /// Counts the lines in a document. /// Traverses the document's layout entities tree upon construction, /// counting entities of the "Line" type that also contain real text. /// </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; }
public int getRevisionNumber() / public void setRevisionNumber(int value)
Aspose.Words does not update this property.
Example:
Shows how to work with REVNUM fields.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.write("Current revision #"); // Insert a REVNUM field, which displays the document's current revision number property. FieldRevNum field = (FieldRevNum)builder.insertField(FieldType.FIELD_REVISION_NUM, true); Assert.assertEquals(" REVNUM ", field.getFieldCode()); Assert.assertEquals("1", field.getResult()); Assert.assertEquals(1, doc.getBuiltInDocumentProperties().getRevisionNumber()); // This property counts how many times a document has been saved in Microsoft Word, // and is unrelated to tracked revisions. We can find it by right clicking the document in Windows Explorer // via Properties -> Details. We can update this property manually. doc.getBuiltInDocumentProperties().setRevisionNumber(doc.getBuiltInDocumentProperties().getRevisionNumber() + 1)/*Property++*/; field.update(); Assert.assertEquals("2", field.getResult());
Example:
Shows how to work with document properties in the "Origin" category.// Open a document that we have created and edited using Microsoft Word. Document doc = new Document(getMyDir() + "Properties.docx"); BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // The following built-in properties contain information regarding the creation and editing of this document. // We can right-click this document in Windows Explorer and find // these properties via "Properties" -> "Details" -> "Origin" category. // Fields such as PRINTDATE and EDITTIME can display these values in the document body. System.out.println(MessageFormat.format("Created using {0}, on {1}", properties.getNameOfApplication(), properties.getCreatedTime())); System.out.println(MessageFormat.format("Minutes spent editing: {0}", properties.getTotalEditingTime())); System.out.println(MessageFormat.format("Date/time last printed: {0}", properties.getLastPrinted())); System.out.println(MessageFormat.format("Template document: {0}", properties.getTemplate())); // We can also change the values of built-in properties. properties.setCompany("Doe Ltd."); properties.setManager("Jane Doe"); properties.setVersion(5); properties.setRevisionNumber(properties.getRevisionNumber() + 1); // Microsoft Word updates the following properties automatically when we save the document. // To use these properties with Aspose.Words, we will need to set values for them manually. properties.setLastSavedBy("John Doe"); properties.setLastSavedTime(new Date()); // We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details" -> "Origin". doc.save(getArtifactsDir() + "DocumentProperties.Origin.docx");
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.Document doc = new Document(); Assert.assertEquals(DocumentSecurity.NONE, doc.getBuiltInDocumentProperties().getSecurity()); // If we configure a document to be read-only, it will display this status using the "Security" built-in property. doc.getWriteProtection().setReadOnlyRecommended(true); doc.save(getArtifactsDir() + "DocumentProperties.Security.ReadOnlyRecommended.docx"); Assert.assertEquals(DocumentSecurity.READ_ONLY_RECOMMENDED, new Document(getArtifactsDir() + "DocumentProperties.Security.ReadOnlyRecommended.docx").getBuiltInDocumentProperties().getSecurity()); // Write-protect a document, and then verify its security level. doc = new Document(); Assert.assertFalse(doc.getWriteProtection().isWriteProtected()); doc.getWriteProtection().setPassword("MyPassword"); Assert.assertTrue(doc.getWriteProtection().validatePassword("MyPassword")); Assert.assertTrue(doc.getWriteProtection().isWriteProtected()); doc.save(getArtifactsDir() + "DocumentProperties.Security.ReadOnlyEnforced.docx"); Assert.assertEquals(DocumentSecurity.READ_ONLY_ENFORCED, new Document(getArtifactsDir() + "DocumentProperties.Security.ReadOnlyEnforced.docx").getBuiltInDocumentProperties().getSecurity()); // "Security" is a descriptive property. We can edit its value manually. doc = new Document(); doc.protect(ProtectionType.ALLOW_ONLY_COMMENTS, "MyPassword"); doc.getBuiltInDocumentProperties().setSecurity(DocumentSecurity.READ_ONLY_EXCEPT_ANNOTATIONS); doc.save(getArtifactsDir() + "DocumentProperties.Security.ReadOnlyExceptAnnotations.docx"); Assert.assertEquals(DocumentSecurity.READ_ONLY_EXCEPT_ANNOTATIONS, new Document(getArtifactsDir() + "DocumentProperties.Security.ReadOnlyExceptAnnotations.docx").getBuiltInDocumentProperties().getSecurity());
public java.lang.String getSubject() / public void setSubject(java.lang.String value)
Example:
Shows how to work with built-in document properties in the "Description" category.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // Below are four built-in document properties that have fields that can display their values in the document body. // 1 - "Author" property, which we can display using an AUTHOR field: properties.setAuthor("John Doe"); builder.write("Author:\t"); builder.insertField(FieldType.FIELD_AUTHOR, true); // 2 - "Title" property, which we can display using a TITLE field: properties.setTitle("John's Document"); builder.write("\nDoc title:\t"); builder.insertField(FieldType.FIELD_TITLE, true); // 3 - "Subject" property, which we can display using a SUBJECT field: properties.setSubject("My subject"); builder.write("\nSubject:\t"); builder.insertField(FieldType.FIELD_SUBJECT, true); // 4 - "Comments" property, which we can display using a COMMENTS field: properties.setComments(MessageFormat.format("This is {0}'s document about {1}", properties.getAuthor(), properties.getSubject())); builder.write("\nComments:\t\""); builder.insertField(FieldType.FIELD_COMMENTS, true); builder.write("\""); // The "Category" built-in property does not have a field that can display its value. properties.setCategory("My category"); // We can set multiple keywords for a document by separating the string value of the "Keywords" property with semicolons. properties.setKeywords("Tag 1; Tag 2; Tag 3"); // We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details". // The "Author" built-in property is in the "Origin" group, and the others are in the "Description" group. doc.save(getArtifactsDir() + "DocumentProperties.Description.docx");
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.// Open a document that we have created and edited using Microsoft Word. Document doc = new Document(getMyDir() + "Properties.docx"); BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // The following built-in properties contain information regarding the creation and editing of this document. // We can right-click this document in Windows Explorer and find // these properties via "Properties" -> "Details" -> "Origin" category. // Fields such as PRINTDATE and EDITTIME can display these values in the document body. System.out.println(MessageFormat.format("Created using {0}, on {1}", properties.getNameOfApplication(), properties.getCreatedTime())); System.out.println(MessageFormat.format("Minutes spent editing: {0}", properties.getTotalEditingTime())); System.out.println(MessageFormat.format("Date/time last printed: {0}", properties.getLastPrinted())); System.out.println(MessageFormat.format("Template document: {0}", properties.getTemplate())); // We can also change the values of built-in properties. properties.setCompany("Doe Ltd."); properties.setManager("Jane Doe"); properties.setVersion(5); properties.setRevisionNumber(properties.getRevisionNumber() + 1); // Microsoft Word updates the following properties automatically when we save the document. // To use these properties with Aspose.Words, we will need to set values for them manually. properties.setLastSavedBy("John Doe"); properties.setLastSavedTime(new Date()); // We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details" -> "Origin". doc.save(getArtifactsDir() + "DocumentProperties.Origin.docx");
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 add a thumbnail to a document that we save as an Epub.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.writeln("Hello world!"); // If we save a document, whose "Thumbnail" property contains image data that we added, as an Epub, // a reader that opens that document may display the image before the first page. BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); byte[] thumbnailBytes = DocumentHelper.getBytesFromStream(new FileInputStream(getImageDir() + "Logo.jpg")); properties.setThumbnail(thumbnailBytes); doc.save(getArtifactsDir() + "DocumentProperties.Thumbnail.epub"); // We can extract a document's thumbnail image and save it to the local file system. DocumentProperty thumbnail = doc.getBuiltInDocumentProperties().get("Thumbnail"); FileUtils.writeByteArrayToFile(new File(getArtifactsDir() + "DocumentProperties.Thumbnail.gif"), thumbnail.toByteArray());
public java.lang.String getTitle() / public void setTitle(java.lang.String value)
Example:
Shows how to work with built-in document properties in the "Description" category.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // Below are four built-in document properties that have fields that can display their values in the document body. // 1 - "Author" property, which we can display using an AUTHOR field: properties.setAuthor("John Doe"); builder.write("Author:\t"); builder.insertField(FieldType.FIELD_AUTHOR, true); // 2 - "Title" property, which we can display using a TITLE field: properties.setTitle("John's Document"); builder.write("\nDoc title:\t"); builder.insertField(FieldType.FIELD_TITLE, true); // 3 - "Subject" property, which we can display using a SUBJECT field: properties.setSubject("My subject"); builder.write("\nSubject:\t"); builder.insertField(FieldType.FIELD_SUBJECT, true); // 4 - "Comments" property, which we can display using a COMMENTS field: properties.setComments(MessageFormat.format("This is {0}'s document about {1}", properties.getAuthor(), properties.getSubject())); builder.write("\nComments:\t\""); builder.insertField(FieldType.FIELD_COMMENTS, true); builder.write("\""); // The "Category" built-in property does not have a field that can display its value. properties.setCategory("My category"); // We can set multiple keywords for a document by separating the string value of the "Keywords" property with semicolons. properties.setKeywords("Tag 1; Tag 2; Tag 3"); // We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details". // The "Author" built-in property is in the "Origin" group, and the others are in the "Description" group. doc.save(getArtifactsDir() + "DocumentProperties.Description.docx");
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.Document doc = new Document(getMyDir() + "Heading pairs and titles of parts.docx"); // We can find the combined values of these collections via "File" -> "Properties" -> "Advanced Properties" > "Contents" tab. // The HeadingPairs property is a collection of <string, int> pairs that determines // how many document parts a heading spans across. Object[] headingPairs = doc.getBuiltInDocumentProperties().getHeadingPairs(); // The TitlesOfParts property contains the names of parts that belong to the above headings. String[] titlesOfParts = doc.getBuiltInDocumentProperties().getTitlesOfParts(); int headingPairsIndex = 0; int titlesOfPartsIndex = 0; while (headingPairsIndex < headingPairs.length) { System.out.println(MessageFormat.format("Parts for {0}:", headingPairs[headingPairsIndex++])); int partsCount = (int)headingPairs[headingPairsIndex++]; for (int i = 0; i < partsCount; i++) System.out.println(MessageFormat.format("\t\"{0}\"", titlesOfParts[titlesOfPartsIndex++])); }
public int getTotalEditingTime() / public void setTotalEditingTime(int value)
Example:
Shows how to work with document properties in the "Origin" category.// Open a document that we have created and edited using Microsoft Word. Document doc = new Document(getMyDir() + "Properties.docx"); BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // The following built-in properties contain information regarding the creation and editing of this document. // We can right-click this document in Windows Explorer and find // these properties via "Properties" -> "Details" -> "Origin" category. // Fields such as PRINTDATE and EDITTIME can display these values in the document body. System.out.println(MessageFormat.format("Created using {0}, on {1}", properties.getNameOfApplication(), properties.getCreatedTime())); System.out.println(MessageFormat.format("Minutes spent editing: {0}", properties.getTotalEditingTime())); System.out.println(MessageFormat.format("Date/time last printed: {0}", properties.getLastPrinted())); System.out.println(MessageFormat.format("Template document: {0}", properties.getTemplate())); // We can also change the values of built-in properties. properties.setCompany("Doe Ltd."); properties.setManager("Jane Doe"); properties.setVersion(5); properties.setRevisionNumber(properties.getRevisionNumber() + 1); // Microsoft Word updates the following properties automatically when we save the document. // To use these properties with Aspose.Words, we will need to set values for them manually. properties.setLastSavedBy("John Doe"); properties.setLastSavedTime(new Date()); // We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details" -> "Origin". doc.save(getArtifactsDir() + "DocumentProperties.Origin.docx");
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.// Open a document that we have created and edited using Microsoft Word. Document doc = new Document(getMyDir() + "Properties.docx"); BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // The following built-in properties contain information regarding the creation and editing of this document. // We can right-click this document in Windows Explorer and find // these properties via "Properties" -> "Details" -> "Origin" category. // Fields such as PRINTDATE and EDITTIME can display these values in the document body. System.out.println(MessageFormat.format("Created using {0}, on {1}", properties.getNameOfApplication(), properties.getCreatedTime())); System.out.println(MessageFormat.format("Minutes spent editing: {0}", properties.getTotalEditingTime())); System.out.println(MessageFormat.format("Date/time last printed: {0}", properties.getLastPrinted())); System.out.println(MessageFormat.format("Template document: {0}", properties.getTemplate())); // We can also change the values of built-in properties. properties.setCompany("Doe Ltd."); properties.setManager("Jane Doe"); properties.setVersion(5); properties.setRevisionNumber(properties.getRevisionNumber() + 1); // Microsoft Word updates the following properties automatically when we save the document. // To use these properties with Aspose.Words, we will need to set values for them manually. properties.setLastSavedBy("John Doe"); properties.setLastSavedTime(new Date()); // We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details" -> "Origin". doc.save(getArtifactsDir() + "DocumentProperties.Origin.docx");
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); builder.writeln("Lorem ipsum dolor sit amet, consectetur adipiscing elit, " + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."); builder.write("Ut enim ad minim veniam, " + "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."); // Aspose.Words does not track document metrics like these in real time. Assert.assertEquals(0, doc.getBuiltInDocumentProperties().getCharacters()); Assert.assertEquals(0, doc.getBuiltInDocumentProperties().getWords()); Assert.assertEquals(1, doc.getBuiltInDocumentProperties().getParagraphs()); Assert.assertEquals(1, doc.getBuiltInDocumentProperties().getLines()); // To get accurate values for three of these properties, we will need to update them manually. doc.updateWordCount(); Assert.assertEquals(196, doc.getBuiltInDocumentProperties().getCharacters()); Assert.assertEquals(36, doc.getBuiltInDocumentProperties().getWords()); Assert.assertEquals(2, doc.getBuiltInDocumentProperties().getParagraphs()); // For the line count, we will need to call a specific overload of the updating method. Assert.assertEquals(1, doc.getBuiltInDocumentProperties().getLines()); doc.updateWordCount(true); Assert.assertEquals(4, doc.getBuiltInDocumentProperties().getLines());
Example:
Shows how to work with document properties in the "Content" category.public void content() throws Exception { Document doc = new Document(getMyDir() + "Paragraphs.docx"); BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties(); // By using built in properties, // we can treat document statistics such as word/page/character counts as metadata that can be glanced at without opening the document // These properties are accessed by right-clicking the file in Windows Explorer and navigating to Properties > Details > Content // If we want to display this data inside the document, we can use fields such as NUMPAGES, NUMWORDS, NUMCHARS etc. // Also, these values can also be viewed in Microsoft Word by navigating File > Properties > Advanced Properties > Statistics // Page count: The PageCount attribute shows the page count in real time and its value can be assigned to the Pages property // The "Pages" property stores the page count of the document. Assert.assertEquals(6, properties.getPages()); // The "Words", "Characters", and "CharactersWithSpaces" built-in properties also display various document statistics, // but we need to call the "UpdateWordCount" method on the whole document before we can expect them to contain accurate values. doc.updateWordCount(); Assert.assertEquals(1035, properties.getWords()); Assert.assertEquals(6026, properties.getCharacters()); Assert.assertEquals(7041, properties.getCharactersWithSpaces()); // Count the number of lines in the document, and then assign the result to the "Lines" built-in property. LineCounter lineCounter = new LineCounter(doc); properties.setLines(lineCounter.getLineCount()); Assert.assertEquals(142, properties.getLines()); // Assign the number of Paragraph nodes in the document to the "Paragraphs" built-in property. properties.setParagraphs(doc.getChildNodes(NodeType.PARAGRAPH, true).getCount()); Assert.assertEquals(29, properties.getParagraphs()); // Get an estimate of the file size of our document via the "Bytes" built-in property. Assert.assertEquals(20310, properties.getBytes()); // Set a different template for our document, and then update the "Template" built-in property manually to reflect this change. doc.setAttachedTemplate(getMyDir() + "Business brochure.dotx"); Assert.assertEquals("Normal", properties.getTemplate()); properties.setTemplate(doc.getAttachedTemplate()); // "ContentStatus" is a descriptive built-in property. properties.setContentStatus("Draft"); // Upon saving, the "ContentType" built-in property will contain the MIME type of the output save format. Assert.assertEquals("", properties.getContentType()); // If the document contains links, and they are all up to date, we can set the "LinksUpToDate" property to "true". Assert.assertFalse(properties.getLinksUpToDate()); doc.save(getArtifactsDir() + "DocumentProperties.Content.docx"); } /// <summary> /// Counts the lines in a document. /// Traverses the document's layout entities tree upon construction, /// counting entities of the "Line" type that also contain real text. /// </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; }
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"); // Every document contains a collection of custom properties, which, like the built-in properties, are key-value pairs. // Unlike the built-in properties, many of which the document maintains by itself, we need to create all of our own custom properties. Assert.assertEquals("Value of custom document property", doc.getCustomDocumentProperties().get("CustomProperty").toString()); doc.getCustomDocumentProperties().add("CustomProperty2", "Value of custom document property #2"); System.out.println("Custom Properties:"); for (DocumentProperty customDocumentProperty : doc.getCustomDocumentProperties()) { System.out.println(customDocumentProperty.getName()); System.out.println(MessageFormat.format("\tType:\t{0}", customDocumentProperty.getType())); System.out.println(MessageFormat.format("\tValue:\t\"{0}\"", customDocumentProperty.getValue())); }
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"); // Every document contains a collection of custom properties, which, like the built-in properties, are key-value pairs. // Unlike the built-in properties, many of which the document maintains by itself, we need to create all of our own custom properties. Assert.assertEquals("Value of custom document property", doc.getCustomDocumentProperties().get("CustomProperty").toString()); doc.getCustomDocumentProperties().add("CustomProperty2", "Value of custom document property #2"); System.out.println("Custom Properties:"); for (DocumentProperty customDocumentProperty : doc.getCustomDocumentProperties()) { System.out.println(customDocumentProperty.getName()); System.out.println(MessageFormat.format("\tType:\t{0}", customDocumentProperty.getType())); System.out.println(MessageFormat.format("\tValue:\t\"{0}\"", customDocumentProperty.getValue())); }
public void clear()
Example:
Shows how to work with a document's custom properties.Document doc = new Document(); CustomDocumentProperties properties = doc.getCustomDocumentProperties(); Assert.assertEquals(0, properties.getCount()); // Custom document properties are key-value pairs that we can add to the document. properties.add("Authorized", true); properties.add("Authorized By", "John Doe"); properties.add("Authorized Date", new Date()); properties.add("Authorized Revision", doc.getBuiltInDocumentProperties().getRevisionNumber()); properties.add("Authorized Amount", 123.45); // The collection sorts the custom properties in alphabetic order. Assert.assertEquals(1, properties.indexOf("Authorized Amount")); Assert.assertEquals(5, properties.getCount()); // Print every custom property in the document. Iterator<DocumentProperty> enumerator = properties.iterator(); while (enumerator.hasNext()) { DocumentProperty property = enumerator.next(); System.out.println(MessageFormat.format("Name: \"{0}\"\n\tType: \"{1}\"\n\tValue: \"{2}\"", property.getName(), property.getType(), property.getValue())); } // Display the value of a custom property using a DOCPROPERTY field. DocumentBuilder builder = new DocumentBuilder(doc); FieldDocProperty field = (FieldDocProperty)builder.insertField(" DOCPROPERTY \"Authorized By\""); field.update(); Assert.assertEquals("John Doe", field.getResult()); // We can find these custom properties in Microsoft Word via "File" -> "Properties" > "Advanced Properties" > "Custom". doc.save(getArtifactsDir() + "DocumentProperties.DocumentPropertyCollection.docx"); // Below are three ways or removing custom properties from a document. // 1 - Remove by index: properties.removeAt(1); Assert.assertFalse(properties.contains("Authorized Amount")); Assert.assertEquals(4, properties.getCount()); // 2 - Remove by name: properties.remove("Authorized Revision"); Assert.assertFalse(properties.contains("Authorized Revision")); Assert.assertEquals(3, properties.getCount()); // 3 - Empty the entire collection at once: properties.clear(); Assert.assertEquals(0, properties.getCount());
public boolean contains(java.lang.String name)
name
- The case-insensitive name of the property.Example:
Shows how to work with a document's custom properties.Document doc = new Document(); CustomDocumentProperties properties = doc.getCustomDocumentProperties(); Assert.assertEquals(0, properties.getCount()); // Custom document properties are key-value pairs that we can add to the document. properties.add("Authorized", true); properties.add("Authorized By", "John Doe"); properties.add("Authorized Date", new Date()); properties.add("Authorized Revision", doc.getBuiltInDocumentProperties().getRevisionNumber()); properties.add("Authorized Amount", 123.45); // The collection sorts the custom properties in alphabetic order. Assert.assertEquals(1, properties.indexOf("Authorized Amount")); Assert.assertEquals(5, properties.getCount()); // Print every custom property in the document. Iterator<DocumentProperty> enumerator = properties.iterator(); while (enumerator.hasNext()) { DocumentProperty property = enumerator.next(); System.out.println(MessageFormat.format("Name: \"{0}\"\n\tType: \"{1}\"\n\tValue: \"{2}\"", property.getName(), property.getType(), property.getValue())); } // Display the value of a custom property using a DOCPROPERTY field. DocumentBuilder builder = new DocumentBuilder(doc); FieldDocProperty field = (FieldDocProperty)builder.insertField(" DOCPROPERTY \"Authorized By\""); field.update(); Assert.assertEquals("John Doe", field.getResult()); // We can find these custom properties in Microsoft Word via "File" -> "Properties" > "Advanced Properties" > "Custom". doc.save(getArtifactsDir() + "DocumentProperties.DocumentPropertyCollection.docx"); // Below are three ways or removing custom properties from a document. // 1 - Remove by index: properties.removeAt(1); Assert.assertFalse(properties.contains("Authorized Amount")); Assert.assertEquals(4, properties.getCount()); // 2 - Remove by name: properties.remove("Authorized Revision"); Assert.assertFalse(properties.contains("Authorized Revision")); Assert.assertEquals(3, properties.getCount()); // 3 - Empty the entire collection at once: properties.clear(); Assert.assertEquals(0, properties.getCount());
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 work with a document's custom properties.Document doc = new Document(); CustomDocumentProperties properties = doc.getCustomDocumentProperties(); Assert.assertEquals(0, properties.getCount()); // Custom document properties are key-value pairs that we can add to the document. properties.add("Authorized", true); properties.add("Authorized By", "John Doe"); properties.add("Authorized Date", new Date()); properties.add("Authorized Revision", doc.getBuiltInDocumentProperties().getRevisionNumber()); properties.add("Authorized Amount", 123.45); // The collection sorts the custom properties in alphabetic order. Assert.assertEquals(1, properties.indexOf("Authorized Amount")); Assert.assertEquals(5, properties.getCount()); // Print every custom property in the document. Iterator<DocumentProperty> enumerator = properties.iterator(); while (enumerator.hasNext()) { DocumentProperty property = enumerator.next(); System.out.println(MessageFormat.format("Name: \"{0}\"\n\tType: \"{1}\"\n\tValue: \"{2}\"", property.getName(), property.getType(), property.getValue())); } // Display the value of a custom property using a DOCPROPERTY field. DocumentBuilder builder = new DocumentBuilder(doc); FieldDocProperty field = (FieldDocProperty)builder.insertField(" DOCPROPERTY \"Authorized By\""); field.update(); Assert.assertEquals("John Doe", field.getResult()); // We can find these custom properties in Microsoft Word via "File" -> "Properties" > "Advanced Properties" > "Custom". doc.save(getArtifactsDir() + "DocumentProperties.DocumentPropertyCollection.docx"); // Below are three ways or removing custom properties from a document. // 1 - Remove by index: properties.removeAt(1); Assert.assertFalse(properties.contains("Authorized Amount")); Assert.assertEquals(4, properties.getCount()); // 2 - Remove by name: properties.remove("Authorized Revision"); Assert.assertFalse(properties.contains("Authorized Revision")); Assert.assertEquals(3, properties.getCount()); // 3 - Empty the entire collection at once: properties.clear(); Assert.assertEquals(0, properties.getCount());
public java.util.Iterator<DocumentProperty> iterator()
Example:
Shows how to work with a document's custom properties.Document doc = new Document(); CustomDocumentProperties properties = doc.getCustomDocumentProperties(); Assert.assertEquals(0, properties.getCount()); // Custom document properties are key-value pairs that we can add to the document. properties.add("Authorized", true); properties.add("Authorized By", "John Doe"); properties.add("Authorized Date", new Date()); properties.add("Authorized Revision", doc.getBuiltInDocumentProperties().getRevisionNumber()); properties.add("Authorized Amount", 123.45); // The collection sorts the custom properties in alphabetic order. Assert.assertEquals(1, properties.indexOf("Authorized Amount")); Assert.assertEquals(5, properties.getCount()); // Print every custom property in the document. Iterator<DocumentProperty> enumerator = properties.iterator(); while (enumerator.hasNext()) { DocumentProperty property = enumerator.next(); System.out.println(MessageFormat.format("Name: \"{0}\"\n\tType: \"{1}\"\n\tValue: \"{2}\"", property.getName(), property.getType(), property.getValue())); } // Display the value of a custom property using a DOCPROPERTY field. DocumentBuilder builder = new DocumentBuilder(doc); FieldDocProperty field = (FieldDocProperty)builder.insertField(" DOCPROPERTY \"Authorized By\""); field.update(); Assert.assertEquals("John Doe", field.getResult()); // We can find these custom properties in Microsoft Word via "File" -> "Properties" > "Advanced Properties" > "Custom". doc.save(getArtifactsDir() + "DocumentProperties.DocumentPropertyCollection.docx"); // Below are three ways or removing custom properties from a document. // 1 - Remove by index: properties.removeAt(1); Assert.assertFalse(properties.contains("Authorized Amount")); Assert.assertEquals(4, properties.getCount()); // 2 - Remove by name: properties.remove("Authorized Revision"); Assert.assertFalse(properties.contains("Authorized Revision")); Assert.assertEquals(3, properties.getCount()); // 3 - Empty the entire collection at once: properties.clear(); Assert.assertEquals(0, properties.getCount());
public void remove(java.lang.String name)
name
- The case-insensitive name of the property.Example:
Shows how to work with a document's custom properties.Document doc = new Document(); CustomDocumentProperties properties = doc.getCustomDocumentProperties(); Assert.assertEquals(0, properties.getCount()); // Custom document properties are key-value pairs that we can add to the document. properties.add("Authorized", true); properties.add("Authorized By", "John Doe"); properties.add("Authorized Date", new Date()); properties.add("Authorized Revision", doc.getBuiltInDocumentProperties().getRevisionNumber()); properties.add("Authorized Amount", 123.45); // The collection sorts the custom properties in alphabetic order. Assert.assertEquals(1, properties.indexOf("Authorized Amount")); Assert.assertEquals(5, properties.getCount()); // Print every custom property in the document. Iterator<DocumentProperty> enumerator = properties.iterator(); while (enumerator.hasNext()) { DocumentProperty property = enumerator.next(); System.out.println(MessageFormat.format("Name: \"{0}\"\n\tType: \"{1}\"\n\tValue: \"{2}\"", property.getName(), property.getType(), property.getValue())); } // Display the value of a custom property using a DOCPROPERTY field. DocumentBuilder builder = new DocumentBuilder(doc); FieldDocProperty field = (FieldDocProperty)builder.insertField(" DOCPROPERTY \"Authorized By\""); field.update(); Assert.assertEquals("John Doe", field.getResult()); // We can find these custom properties in Microsoft Word via "File" -> "Properties" > "Advanced Properties" > "Custom". doc.save(getArtifactsDir() + "DocumentProperties.DocumentPropertyCollection.docx"); // Below are three ways or removing custom properties from a document. // 1 - Remove by index: properties.removeAt(1); Assert.assertFalse(properties.contains("Authorized Amount")); Assert.assertEquals(4, properties.getCount()); // 2 - Remove by name: properties.remove("Authorized Revision"); Assert.assertFalse(properties.contains("Authorized Revision")); Assert.assertEquals(3, properties.getCount()); // 3 - Empty the entire collection at once: properties.clear(); Assert.assertEquals(0, properties.getCount());
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 work with a document's custom properties.Document doc = new Document(); CustomDocumentProperties properties = doc.getCustomDocumentProperties(); Assert.assertEquals(0, properties.getCount()); // Custom document properties are key-value pairs that we can add to the document. properties.add("Authorized", true); properties.add("Authorized By", "John Doe"); properties.add("Authorized Date", new Date()); properties.add("Authorized Revision", doc.getBuiltInDocumentProperties().getRevisionNumber()); properties.add("Authorized Amount", 123.45); // The collection sorts the custom properties in alphabetic order. Assert.assertEquals(1, properties.indexOf("Authorized Amount")); Assert.assertEquals(5, properties.getCount()); // Print every custom property in the document. Iterator<DocumentProperty> enumerator = properties.iterator(); while (enumerator.hasNext()) { DocumentProperty property = enumerator.next(); System.out.println(MessageFormat.format("Name: \"{0}\"\n\tType: \"{1}\"\n\tValue: \"{2}\"", property.getName(), property.getType(), property.getValue())); } // Display the value of a custom property using a DOCPROPERTY field. DocumentBuilder builder = new DocumentBuilder(doc); FieldDocProperty field = (FieldDocProperty)builder.insertField(" DOCPROPERTY \"Authorized By\""); field.update(); Assert.assertEquals("John Doe", field.getResult()); // We can find these custom properties in Microsoft Word via "File" -> "Properties" > "Advanced Properties" > "Custom". doc.save(getArtifactsDir() + "DocumentProperties.DocumentPropertyCollection.docx"); // Below are three ways or removing custom properties from a document. // 1 - Remove by index: properties.removeAt(1); Assert.assertFalse(properties.contains("Authorized Amount")); Assert.assertEquals(4, properties.getCount()); // 2 - Remove by name: properties.remove("Authorized Revision"); Assert.assertFalse(properties.contains("Authorized Revision")); Assert.assertEquals(3, properties.getCount()); // 3 - Empty the entire collection at once: properties.clear(); Assert.assertEquals(0, properties.getCount());