java.lang.ObjectDocumentPropertyCollection
com.aspose.words.CustomDocumentProperties
public class CustomDocumentProperties
Each The names of the properties are case-insensitive. The properties in the collection are sorted alphabetically by name. Example:
String fileName = getMyDir() + "Properties.doc";
Document doc = new Document(fileName);
System.out.println(MessageFormat.format("1. Document name: {0}", doc.getOriginalFileName()));
System.out.println("2. Built-in Properties");
for (DocumentProperty docProperty : doc.getBuiltInDocumentProperties())
System.out.println(MessageFormat.format("{0} : {1}", docProperty.getName(), docProperty.getValue()));
System.out.println("3. Custom Properties");
for (DocumentProperty docProperty : doc.getCustomDocumentProperties())
System.out.println(MessageFormat.format("{0} : {1}", docProperty.getName(), docProperty.getValue()));
| Property Getters/Setters Summary | ||
|---|---|---|
int | getCount() | → inherited from DocumentPropertyCollection |
| Gets number of items in the collection. | ||
DocumentProperty | get(int index) | → inherited from DocumentPropertyCollection |
|
Returns a |
||
DocumentProperty | get(java.lang.String name) | → inherited from DocumentPropertyCollection |
|
Returns a |
||
| Method Summary | ||
|---|---|---|
DocumentProperty | add(java.lang.String name, boolean value) | |
| Creates a new custom document property of the PropertyType.Boolean data type. | ||
DocumentProperty | add(java.lang.String name, double value) | |
| Creates a new custom document property of the PropertyType.Float data type. | ||
DocumentProperty | add(java.lang.String name, int value) | |
| Creates a new custom document property of the PropertyType.Number data type. | ||
DocumentProperty | add(java.lang.String name, java.lang.String value) | |
| Creates a new custom document property of the PropertyType.String data type. | ||
DocumentProperty | add(java.lang.String name, java.util.Date value) | |
| Creates a new custom document property of the PropertyType.DateTime data type. | ||
DocumentProperty | addLinkToContent(java.lang.String name, java.lang.String linkSource) | |
| Creates a new linked to content custom document property. | ||
void | clear() | → inherited from DocumentPropertyCollection |
| Removes all properties from the collection. | ||
boolean | contains(java.lang.String name) | → inherited from DocumentPropertyCollection |
| Returns true if a property with the specified name exists in the collection. | ||
int | indexOf(java.lang.String name) | → inherited from DocumentPropertyCollection |
| Gets the index of a property by name. | ||
java.util.Iterator<DocumentProperty> | iterator() | → inherited from DocumentPropertyCollection |
| Returns an iterator object that can be used to iterate over all items in the collection. | ||
void | remove(java.lang.String name) | → inherited from DocumentPropertyCollection |
| Removes a property with the specified name from the collection. | ||
void | removeAt(int index) | → inherited from DocumentPropertyCollection |
| Removes a property at the specified index. | ||
| Property Getters/Setters Detail |
|---|
getCount | → inherited from DocumentPropertyCollection |
public int getCount() | |
Example:
Enumerates through all built-in and custom properties in a document using indexed access.
String fileName = getMyDir() + "Properties.doc";
Document doc = new Document(fileName);
System.out.println(MessageFormat.format("1. Document name: {0}", fileName));
System.out.println("2. Built-in Properties");
for (int i = 0; i < doc.getBuiltInDocumentProperties().getCount(); i++) {
DocumentProperty docProperty = doc.getBuiltInDocumentProperties().get(i);
System.out.println(MessageFormat.format("{0}({1}) : {2}", docProperty.getName(), docProperty.getType(), docProperty.getValue()));
}
System.out.println("3. Custom Properties");
for (int i = 0; i < doc.getCustomDocumentProperties().getCount(); i++) {
DocumentProperty docProperty = doc.getCustomDocumentProperties().get(i);
System.out.println(MessageFormat.format("{0}({1}) : {2}", docProperty.getName(), docProperty.getType(), docProperty.getValue()));
}get | → inherited from DocumentPropertyCollection |
public DocumentProperty get(int index) | |
Note: In Java this method is slow because iterates over all nodes.
index - Zero-based index of the Example:
Enumerates through all built-in and custom properties in a document using indexed access.
String fileName = getMyDir() + "Properties.doc";
Document doc = new Document(fileName);
System.out.println(MessageFormat.format("1. Document name: {0}", fileName));
System.out.println("2. Built-in Properties");
for (int i = 0; i < doc.getBuiltInDocumentProperties().getCount(); i++) {
DocumentProperty docProperty = doc.getBuiltInDocumentProperties().get(i);
System.out.println(MessageFormat.format("{0}({1}) : {2}", docProperty.getName(), docProperty.getType(), docProperty.getValue()));
}
System.out.println("3. Custom Properties");
for (int i = 0; i < doc.getCustomDocumentProperties().getCount(); i++) {
DocumentProperty docProperty = doc.getCustomDocumentProperties().get(i);
System.out.println(MessageFormat.format("{0}({1}) : {2}", docProperty.getName(), docProperty.getType(), docProperty.getValue()));
}get | → inherited from DocumentPropertyCollection |
public DocumentProperty get(java.lang.String name) | |
Returns null if a property with the specified name is not found.
name - The case-insensitive name of the property to retrieve.Example:
Retrieves a custom document property by name.
Document doc = new Document(getMyDir() + "Properties.doc");
DocumentProperty docProperty = doc.getCustomDocumentProperties().get("Authorized Date");
if (docProperty != null) {
System.out.println(docProperty.toDateTime());
} else {
System.out.println("The document is not authorized. Authorizing...");
doc.getCustomDocumentProperties().add("AuthorizedDate", new Date());
}| Method Detail |
|---|
add | |
public DocumentProperty add(java.lang.String name, boolean value) | |
name - The name of the property.value - The value of the property.Example:
Shows how to add custom properties to a document.
// Create a blank document and get its custom property collection
Document doc = new Document();
CustomDocumentProperties properties = doc.getCustomDocumentProperties();
// The collection will be empty by default
Assert.assertEquals(properties.getCount(), 0);
// We can populate it with key/value pairs with a variety of value types
properties.add("Authorized", true);
properties.add("Authorized By", "John Doe");
properties.add("Authorized Date", new Date());
properties.add("Authorized Revision", doc.getBuiltInDocumentProperties().getRevisionNumber());
properties.add("Authorized Amount", 123.45);
// Custom properties are automatically sorted in alphabetic order
Assert.assertEquals(properties.indexOf("Authorized Amount"), 1);
Assert.assertEquals(properties.getCount(), 5);
// Enumerate and print all custom properties
Iterator<DocumentProperty> enumerator = properties.iterator();
try {
while (enumerator.hasNext()) {
DocumentProperty property = enumerator.next();
System.out.println(MessageFormat.format("Name: \"{0}\", Type: \"{1}\", Value: \"{2}\"", property.getName(), property.getType(), property.getValue()));
}
} finally {
if (enumerator != null) enumerator.remove();
}
// We can view/edit custom properties by opening the document and looking in File > Properties > Advanced Properties > Custom
doc.save(getArtifactsDir() + "Properties.DocumentPropertyCollection.docx");
// We can remove elements from the property collection by index or by name
properties.removeAt(1);
Assert.assertFalse(properties.contains("Authorized Amount"));
Assert.assertEquals(properties.getCount(), 4);
properties.remove("Authorized Revision");
Assert.assertFalse(properties.contains("Authorized Revision"));
Assert.assertEquals(properties.getCount(), 3);
// We can also empty the entire custom property collection at once
properties.clear();
Assert.assertEquals(properties.getCount(), 0);add | |
public DocumentProperty add(java.lang.String name, double value) | |
name - The name of the property.value - The value of the property.Example:
Shows how to add custom properties to a document.
// Create a blank document and get its custom property collection
Document doc = new Document();
CustomDocumentProperties properties = doc.getCustomDocumentProperties();
// The collection will be empty by default
Assert.assertEquals(properties.getCount(), 0);
// We can populate it with key/value pairs with a variety of value types
properties.add("Authorized", true);
properties.add("Authorized By", "John Doe");
properties.add("Authorized Date", new Date());
properties.add("Authorized Revision", doc.getBuiltInDocumentProperties().getRevisionNumber());
properties.add("Authorized Amount", 123.45);
// Custom properties are automatically sorted in alphabetic order
Assert.assertEquals(properties.indexOf("Authorized Amount"), 1);
Assert.assertEquals(properties.getCount(), 5);
// Enumerate and print all custom properties
Iterator<DocumentProperty> enumerator = properties.iterator();
try {
while (enumerator.hasNext()) {
DocumentProperty property = enumerator.next();
System.out.println(MessageFormat.format("Name: \"{0}\", Type: \"{1}\", Value: \"{2}\"", property.getName(), property.getType(), property.getValue()));
}
} finally {
if (enumerator != null) enumerator.remove();
}
// We can view/edit custom properties by opening the document and looking in File > Properties > Advanced Properties > Custom
doc.save(getArtifactsDir() + "Properties.DocumentPropertyCollection.docx");
// We can remove elements from the property collection by index or by name
properties.removeAt(1);
Assert.assertFalse(properties.contains("Authorized Amount"));
Assert.assertEquals(properties.getCount(), 4);
properties.remove("Authorized Revision");
Assert.assertFalse(properties.contains("Authorized Revision"));
Assert.assertEquals(properties.getCount(), 3);
// We can also empty the entire custom property collection at once
properties.clear();
Assert.assertEquals(properties.getCount(), 0);add | |
public DocumentProperty add(java.lang.String name, int value) | |
name - The name of the property.value - The value of the property.Example:
Shows how to add custom properties to a document.
// Create a blank document and get its custom property collection
Document doc = new Document();
CustomDocumentProperties properties = doc.getCustomDocumentProperties();
// The collection will be empty by default
Assert.assertEquals(properties.getCount(), 0);
// We can populate it with key/value pairs with a variety of value types
properties.add("Authorized", true);
properties.add("Authorized By", "John Doe");
properties.add("Authorized Date", new Date());
properties.add("Authorized Revision", doc.getBuiltInDocumentProperties().getRevisionNumber());
properties.add("Authorized Amount", 123.45);
// Custom properties are automatically sorted in alphabetic order
Assert.assertEquals(properties.indexOf("Authorized Amount"), 1);
Assert.assertEquals(properties.getCount(), 5);
// Enumerate and print all custom properties
Iterator<DocumentProperty> enumerator = properties.iterator();
try {
while (enumerator.hasNext()) {
DocumentProperty property = enumerator.next();
System.out.println(MessageFormat.format("Name: \"{0}\", Type: \"{1}\", Value: \"{2}\"", property.getName(), property.getType(), property.getValue()));
}
} finally {
if (enumerator != null) enumerator.remove();
}
// We can view/edit custom properties by opening the document and looking in File > Properties > Advanced Properties > Custom
doc.save(getArtifactsDir() + "Properties.DocumentPropertyCollection.docx");
// We can remove elements from the property collection by index or by name
properties.removeAt(1);
Assert.assertFalse(properties.contains("Authorized Amount"));
Assert.assertEquals(properties.getCount(), 4);
properties.remove("Authorized Revision");
Assert.assertFalse(properties.contains("Authorized Revision"));
Assert.assertEquals(properties.getCount(), 3);
// We can also empty the entire custom property collection at once
properties.clear();
Assert.assertEquals(properties.getCount(), 0);add | |
public DocumentProperty add(java.lang.String name, java.lang.String value) | |
name - The name of the property.value - The value of the property.Example:
Shows how to add custom properties to a document.
// Create a blank document and get its custom property collection
Document doc = new Document();
CustomDocumentProperties properties = doc.getCustomDocumentProperties();
// The collection will be empty by default
Assert.assertEquals(properties.getCount(), 0);
// We can populate it with key/value pairs with a variety of value types
properties.add("Authorized", true);
properties.add("Authorized By", "John Doe");
properties.add("Authorized Date", new Date());
properties.add("Authorized Revision", doc.getBuiltInDocumentProperties().getRevisionNumber());
properties.add("Authorized Amount", 123.45);
// Custom properties are automatically sorted in alphabetic order
Assert.assertEquals(properties.indexOf("Authorized Amount"), 1);
Assert.assertEquals(properties.getCount(), 5);
// Enumerate and print all custom properties
Iterator<DocumentProperty> enumerator = properties.iterator();
try {
while (enumerator.hasNext()) {
DocumentProperty property = enumerator.next();
System.out.println(MessageFormat.format("Name: \"{0}\", Type: \"{1}\", Value: \"{2}\"", property.getName(), property.getType(), property.getValue()));
}
} finally {
if (enumerator != null) enumerator.remove();
}
// We can view/edit custom properties by opening the document and looking in File > Properties > Advanced Properties > Custom
doc.save(getArtifactsDir() + "Properties.DocumentPropertyCollection.docx");
// We can remove elements from the property collection by index or by name
properties.removeAt(1);
Assert.assertFalse(properties.contains("Authorized Amount"));
Assert.assertEquals(properties.getCount(), 4);
properties.remove("Authorized Revision");
Assert.assertFalse(properties.contains("Authorized Revision"));
Assert.assertEquals(properties.getCount(), 3);
// We can also empty the entire custom property collection at once
properties.clear();
Assert.assertEquals(properties.getCount(), 0);add | |
public DocumentProperty add(java.lang.String name, java.util.Date value) | |
name - The name of the property.value - The value of the property.Example:
Shows how to add custom properties to a document.
// Create a blank document and get its custom property collection
Document doc = new Document();
CustomDocumentProperties properties = doc.getCustomDocumentProperties();
// The collection will be empty by default
Assert.assertEquals(properties.getCount(), 0);
// We can populate it with key/value pairs with a variety of value types
properties.add("Authorized", true);
properties.add("Authorized By", "John Doe");
properties.add("Authorized Date", new Date());
properties.add("Authorized Revision", doc.getBuiltInDocumentProperties().getRevisionNumber());
properties.add("Authorized Amount", 123.45);
// Custom properties are automatically sorted in alphabetic order
Assert.assertEquals(properties.indexOf("Authorized Amount"), 1);
Assert.assertEquals(properties.getCount(), 5);
// Enumerate and print all custom properties
Iterator<DocumentProperty> enumerator = properties.iterator();
try {
while (enumerator.hasNext()) {
DocumentProperty property = enumerator.next();
System.out.println(MessageFormat.format("Name: \"{0}\", Type: \"{1}\", Value: \"{2}\"", property.getName(), property.getType(), property.getValue()));
}
} finally {
if (enumerator != null) enumerator.remove();
}
// We can view/edit custom properties by opening the document and looking in File > Properties > Advanced Properties > Custom
doc.save(getArtifactsDir() + "Properties.DocumentPropertyCollection.docx");
// We can remove elements from the property collection by index or by name
properties.removeAt(1);
Assert.assertFalse(properties.contains("Authorized Amount"));
Assert.assertEquals(properties.getCount(), 4);
properties.remove("Authorized Revision");
Assert.assertFalse(properties.contains("Authorized Revision"));
Assert.assertEquals(properties.getCount(), 3);
// We can also empty the entire custom property collection at once
properties.clear();
Assert.assertEquals(properties.getCount(), 0);Example:
Retrieves a custom document property by name.
Document doc = new Document(getMyDir() + "Properties.doc");
DocumentProperty docProperty = doc.getCustomDocumentProperties().get("Authorized Date");
if (docProperty != null) {
System.out.println(docProperty.toDateTime());
} else {
System.out.println("The document is not authorized. Authorizing...");
doc.getCustomDocumentProperties().add("AuthorizedDate", new Date());
}addLinkToContent | |
public DocumentProperty addLinkToContent(java.lang.String name, java.lang.String linkSource) throws java.lang.Exception | |
name - The name of the property.linkSource - The source of the property.Example:
Shows how to add linked custom document property.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.startBookmark("MyBookmark");
builder.writeln("Text inside a bookmark.");
builder.endBookmark("MyBookmark");
// Add linked to content property
CustomDocumentProperties customProperties = doc.getCustomDocumentProperties();
DocumentProperty customProperty = customProperties.addLinkToContent("Bookmark", "MyBookmark");
// Check whether the property is linked to content
Assert.assertEquals(customProperty.isLinkToContent(), true);
// Get the source of the property
Assert.assertEquals(customProperty.getLinkSource(), "MyBookmark");
// Get the value of the property
Assert.assertEquals(customProperty.getValue(), "Text inside a bookmark.\r");
doc.save(getArtifactsDir() + "Properties.LinkCustomDocumentPropertiesToBookmark.docx");clear | → inherited from DocumentPropertyCollection |
public void clear() | |
Example:
Shows how to add custom properties to a document.
// Create a blank document and get its custom property collection
Document doc = new Document();
CustomDocumentProperties properties = doc.getCustomDocumentProperties();
// The collection will be empty by default
Assert.assertEquals(properties.getCount(), 0);
// We can populate it with key/value pairs with a variety of value types
properties.add("Authorized", true);
properties.add("Authorized By", "John Doe");
properties.add("Authorized Date", new Date());
properties.add("Authorized Revision", doc.getBuiltInDocumentProperties().getRevisionNumber());
properties.add("Authorized Amount", 123.45);
// Custom properties are automatically sorted in alphabetic order
Assert.assertEquals(properties.indexOf("Authorized Amount"), 1);
Assert.assertEquals(properties.getCount(), 5);
// Enumerate and print all custom properties
Iterator<DocumentProperty> enumerator = properties.iterator();
try {
while (enumerator.hasNext()) {
DocumentProperty property = enumerator.next();
System.out.println(MessageFormat.format("Name: \"{0}\", Type: \"{1}\", Value: \"{2}\"", property.getName(), property.getType(), property.getValue()));
}
} finally {
if (enumerator != null) enumerator.remove();
}
// We can view/edit custom properties by opening the document and looking in File > Properties > Advanced Properties > Custom
doc.save(getArtifactsDir() + "Properties.DocumentPropertyCollection.docx");
// We can remove elements from the property collection by index or by name
properties.removeAt(1);
Assert.assertFalse(properties.contains("Authorized Amount"));
Assert.assertEquals(properties.getCount(), 4);
properties.remove("Authorized Revision");
Assert.assertFalse(properties.contains("Authorized Revision"));
Assert.assertEquals(properties.getCount(), 3);
// We can also empty the entire custom property collection at once
properties.clear();
Assert.assertEquals(properties.getCount(), 0);contains | → inherited from DocumentPropertyCollection |
public boolean contains(java.lang.String name) | |
name - The case-insensitive name of the property.Example:
Shows how to add custom properties to a document.
// Create a blank document and get its custom property collection
Document doc = new Document();
CustomDocumentProperties properties = doc.getCustomDocumentProperties();
// The collection will be empty by default
Assert.assertEquals(properties.getCount(), 0);
// We can populate it with key/value pairs with a variety of value types
properties.add("Authorized", true);
properties.add("Authorized By", "John Doe");
properties.add("Authorized Date", new Date());
properties.add("Authorized Revision", doc.getBuiltInDocumentProperties().getRevisionNumber());
properties.add("Authorized Amount", 123.45);
// Custom properties are automatically sorted in alphabetic order
Assert.assertEquals(properties.indexOf("Authorized Amount"), 1);
Assert.assertEquals(properties.getCount(), 5);
// Enumerate and print all custom properties
Iterator<DocumentProperty> enumerator = properties.iterator();
try {
while (enumerator.hasNext()) {
DocumentProperty property = enumerator.next();
System.out.println(MessageFormat.format("Name: \"{0}\", Type: \"{1}\", Value: \"{2}\"", property.getName(), property.getType(), property.getValue()));
}
} finally {
if (enumerator != null) enumerator.remove();
}
// We can view/edit custom properties by opening the document and looking in File > Properties > Advanced Properties > Custom
doc.save(getArtifactsDir() + "Properties.DocumentPropertyCollection.docx");
// We can remove elements from the property collection by index or by name
properties.removeAt(1);
Assert.assertFalse(properties.contains("Authorized Amount"));
Assert.assertEquals(properties.getCount(), 4);
properties.remove("Authorized Revision");
Assert.assertFalse(properties.contains("Authorized Revision"));
Assert.assertEquals(properties.getCount(), 3);
// We can also empty the entire custom property collection at once
properties.clear();
Assert.assertEquals(properties.getCount(), 0);indexOf | → inherited from DocumentPropertyCollection |
public int indexOf(java.lang.String name) | |
Note: In Java this method is slow because iterates over all nodes.
name - The case-insensitive name of the property.Example:
Shows how to add custom properties to a document.
// Create a blank document and get its custom property collection
Document doc = new Document();
CustomDocumentProperties properties = doc.getCustomDocumentProperties();
// The collection will be empty by default
Assert.assertEquals(properties.getCount(), 0);
// We can populate it with key/value pairs with a variety of value types
properties.add("Authorized", true);
properties.add("Authorized By", "John Doe");
properties.add("Authorized Date", new Date());
properties.add("Authorized Revision", doc.getBuiltInDocumentProperties().getRevisionNumber());
properties.add("Authorized Amount", 123.45);
// Custom properties are automatically sorted in alphabetic order
Assert.assertEquals(properties.indexOf("Authorized Amount"), 1);
Assert.assertEquals(properties.getCount(), 5);
// Enumerate and print all custom properties
Iterator<DocumentProperty> enumerator = properties.iterator();
try {
while (enumerator.hasNext()) {
DocumentProperty property = enumerator.next();
System.out.println(MessageFormat.format("Name: \"{0}\", Type: \"{1}\", Value: \"{2}\"", property.getName(), property.getType(), property.getValue()));
}
} finally {
if (enumerator != null) enumerator.remove();
}
// We can view/edit custom properties by opening the document and looking in File > Properties > Advanced Properties > Custom
doc.save(getArtifactsDir() + "Properties.DocumentPropertyCollection.docx");
// We can remove elements from the property collection by index or by name
properties.removeAt(1);
Assert.assertFalse(properties.contains("Authorized Amount"));
Assert.assertEquals(properties.getCount(), 4);
properties.remove("Authorized Revision");
Assert.assertFalse(properties.contains("Authorized Revision"));
Assert.assertEquals(properties.getCount(), 3);
// We can also empty the entire custom property collection at once
properties.clear();
Assert.assertEquals(properties.getCount(), 0);iterator | → inherited from DocumentPropertyCollection |
public java.util.Iterator<DocumentProperty> iterator() | |
Example:
Shows how to add custom properties to a document.
// Create a blank document and get its custom property collection
Document doc = new Document();
CustomDocumentProperties properties = doc.getCustomDocumentProperties();
// The collection will be empty by default
Assert.assertEquals(properties.getCount(), 0);
// We can populate it with key/value pairs with a variety of value types
properties.add("Authorized", true);
properties.add("Authorized By", "John Doe");
properties.add("Authorized Date", new Date());
properties.add("Authorized Revision", doc.getBuiltInDocumentProperties().getRevisionNumber());
properties.add("Authorized Amount", 123.45);
// Custom properties are automatically sorted in alphabetic order
Assert.assertEquals(properties.indexOf("Authorized Amount"), 1);
Assert.assertEquals(properties.getCount(), 5);
// Enumerate and print all custom properties
Iterator<DocumentProperty> enumerator = properties.iterator();
try {
while (enumerator.hasNext()) {
DocumentProperty property = enumerator.next();
System.out.println(MessageFormat.format("Name: \"{0}\", Type: \"{1}\", Value: \"{2}\"", property.getName(), property.getType(), property.getValue()));
}
} finally {
if (enumerator != null) enumerator.remove();
}
// We can view/edit custom properties by opening the document and looking in File > Properties > Advanced Properties > Custom
doc.save(getArtifactsDir() + "Properties.DocumentPropertyCollection.docx");
// We can remove elements from the property collection by index or by name
properties.removeAt(1);
Assert.assertFalse(properties.contains("Authorized Amount"));
Assert.assertEquals(properties.getCount(), 4);
properties.remove("Authorized Revision");
Assert.assertFalse(properties.contains("Authorized Revision"));
Assert.assertEquals(properties.getCount(), 3);
// We can also empty the entire custom property collection at once
properties.clear();
Assert.assertEquals(properties.getCount(), 0);remove | → inherited from DocumentPropertyCollection |
public void remove(java.lang.String name) | |
name - The case-insensitive name of the property.Example:
Shows how to add custom properties to a document.
// Create a blank document and get its custom property collection
Document doc = new Document();
CustomDocumentProperties properties = doc.getCustomDocumentProperties();
// The collection will be empty by default
Assert.assertEquals(properties.getCount(), 0);
// We can populate it with key/value pairs with a variety of value types
properties.add("Authorized", true);
properties.add("Authorized By", "John Doe");
properties.add("Authorized Date", new Date());
properties.add("Authorized Revision", doc.getBuiltInDocumentProperties().getRevisionNumber());
properties.add("Authorized Amount", 123.45);
// Custom properties are automatically sorted in alphabetic order
Assert.assertEquals(properties.indexOf("Authorized Amount"), 1);
Assert.assertEquals(properties.getCount(), 5);
// Enumerate and print all custom properties
Iterator<DocumentProperty> enumerator = properties.iterator();
try {
while (enumerator.hasNext()) {
DocumentProperty property = enumerator.next();
System.out.println(MessageFormat.format("Name: \"{0}\", Type: \"{1}\", Value: \"{2}\"", property.getName(), property.getType(), property.getValue()));
}
} finally {
if (enumerator != null) enumerator.remove();
}
// We can view/edit custom properties by opening the document and looking in File > Properties > Advanced Properties > Custom
doc.save(getArtifactsDir() + "Properties.DocumentPropertyCollection.docx");
// We can remove elements from the property collection by index or by name
properties.removeAt(1);
Assert.assertFalse(properties.contains("Authorized Amount"));
Assert.assertEquals(properties.getCount(), 4);
properties.remove("Authorized Revision");
Assert.assertFalse(properties.contains("Authorized Revision"));
Assert.assertEquals(properties.getCount(), 3);
// We can also empty the entire custom property collection at once
properties.clear();
Assert.assertEquals(properties.getCount(), 0);removeAt | → inherited from DocumentPropertyCollection |
public void removeAt(int index) | |
Note: In Java this method is slow because iterates over all nodes.
index - The zero based index.Example:
Shows how to add custom properties to a document.
// Create a blank document and get its custom property collection
Document doc = new Document();
CustomDocumentProperties properties = doc.getCustomDocumentProperties();
// The collection will be empty by default
Assert.assertEquals(properties.getCount(), 0);
// We can populate it with key/value pairs with a variety of value types
properties.add("Authorized", true);
properties.add("Authorized By", "John Doe");
properties.add("Authorized Date", new Date());
properties.add("Authorized Revision", doc.getBuiltInDocumentProperties().getRevisionNumber());
properties.add("Authorized Amount", 123.45);
// Custom properties are automatically sorted in alphabetic order
Assert.assertEquals(properties.indexOf("Authorized Amount"), 1);
Assert.assertEquals(properties.getCount(), 5);
// Enumerate and print all custom properties
Iterator<DocumentProperty> enumerator = properties.iterator();
try {
while (enumerator.hasNext()) {
DocumentProperty property = enumerator.next();
System.out.println(MessageFormat.format("Name: \"{0}\", Type: \"{1}\", Value: \"{2}\"", property.getName(), property.getType(), property.getValue()));
}
} finally {
if (enumerator != null) enumerator.remove();
}
// We can view/edit custom properties by opening the document and looking in File > Properties > Advanced Properties > Custom
doc.save(getArtifactsDir() + "Properties.DocumentPropertyCollection.docx");
// We can remove elements from the property collection by index or by name
properties.removeAt(1);
Assert.assertFalse(properties.contains("Authorized Amount"));
Assert.assertEquals(properties.getCount(), 4);
properties.remove("Authorized Revision");
Assert.assertFalse(properties.contains("Authorized Revision"));
Assert.assertEquals(properties.getCount(), 3);
// We can also empty the entire custom property collection at once
properties.clear();
Assert.assertEquals(properties.getCount(), 0);