public abstract class DocumentPropertyCollection
The names of the properties are case-insensitive.
The properties in the collection are sorted alphabetically by name.
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());
Property Getters/Setters Summary | ||
---|---|---|
int | getCount() | |
Gets number of items in the collection.
|
||
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 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 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)
Returns null if a property with the specified name is not found.
name
- The case-insensitive name of the property to retrieve.Example:
Shows how to create a custom document property which contains a date and time.Document doc = new Document(); doc.getCustomDocumentProperties().add("AuthorizationDate", new Date()); System.out.println("Document authorized on {doc.CustomDocumentProperties[");
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());