com.aspose.words

Class DocumentProperty

  • java.lang.Object
    • com.aspose.words.DocumentProperty
  • All Implemented Interfaces:
    java.lang.Cloneable
    public class DocumentProperty 
    extends java.lang.Object

Represents a custom or built-in document property.

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()));
}
See Also:
DocumentPropertyCollection

Property Getters/Setters Summary
java.lang.StringgetName()
Returns the name of the property.
intgetType()
Gets the data type of the property. The value of the property is PropertyType integer constant.
java.lang.ObjectgetValue()
void
setValue(java.lang.Objectvalue)
           Gets or sets the value of the property.
 
Method Summary
booleantoBool()
Returns the property value as bool.
byte[]toByteArray()
Returns the property value as byte array.
java.util.DatetoDateTime()
Returns the property value as DateTime in UTC.
doubletoDouble()
Returns the property value as double.
inttoInt()
Returns the property value as integer.
java.lang.StringtoString()
Returns the property value as a string formatted according to the current locale.
 

    • Property Getters/Setters Detail

      • getName

        public java.lang.String getName()
        
        Returns the name of the property.

        Cannot be null and cannot be an empty string.

        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()));
        }
      • getType

        public int getType()
        
        Gets the data type of the property. The value of the property is PropertyType integer constant.

        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()));
        }

        Example:

        Retrieves the types and values of the custom document properties.
        Document doc = new Document(getMyDir() + "Properties.doc");
        
        for (DocumentProperty docProperty : doc.getCustomDocumentProperties())
        {
            System.out.println(docProperty.getName());
            switch (docProperty.getType())
            {
                case PropertyType.STRING:
                    System.out.println("It's a string value.");
                    System.out.println(docProperty.toString());
                    break;
                case PropertyType.BOOLEAN:
                    System.out.println("It's a boolean value.");
                    System.out.println(docProperty.toBool());
                    break;
                case PropertyType.NUMBER:
                    System.out.println("It's an integer value.");
                    System.out.println(docProperty.toInt());
                    break;
                case PropertyType.DATE_TIME:
                    System.out.println("It's a date time value.");
                    System.out.println(docProperty.toDateTime());
                    break;
                case PropertyType.DOUBLE:
                    System.out.println("It's a double value.");
                    System.out.println(docProperty.toDouble());
                    break;
                case PropertyType.OTHER:
                    System.out.println("Other value.");
                    break;
                default:
                    throw new Exception("Unknown property type.");
            }
        }
      • getValue/setValue

        public java.lang.Object getValue() / public void setValue(java.lang.Object value)
        
        Gets or sets the value of the property.

        Cannot be null.

        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()));
        }
    • Method Detail

      • toBool

        public boolean toBool()
        Returns the property value as bool.

        Throws an exception if the property type is not PropertyType.BOOLEAN.

        Example:

        Retrieves the types and values of the custom document properties.
        Document doc = new Document(getMyDir() + "Properties.doc");
        
        for (DocumentProperty docProperty : doc.getCustomDocumentProperties())
        {
            System.out.println(docProperty.getName());
            switch (docProperty.getType())
            {
                case PropertyType.STRING:
                    System.out.println("It's a string value.");
                    System.out.println(docProperty.toString());
                    break;
                case PropertyType.BOOLEAN:
                    System.out.println("It's a boolean value.");
                    System.out.println(docProperty.toBool());
                    break;
                case PropertyType.NUMBER:
                    System.out.println("It's an integer value.");
                    System.out.println(docProperty.toInt());
                    break;
                case PropertyType.DATE_TIME:
                    System.out.println("It's a date time value.");
                    System.out.println(docProperty.toDateTime());
                    break;
                case PropertyType.DOUBLE:
                    System.out.println("It's a double value.");
                    System.out.println(docProperty.toDouble());
                    break;
                case PropertyType.OTHER:
                    System.out.println("Other value.");
                    break;
                default:
                    throw new Exception("Unknown property type.");
            }
        }
      • toByteArray

        public byte[] toByteArray()
        Returns the property value as byte array.

        Throws an exception if the property type is not PropertyType.BYTE_ARRAY.

      • toDateTime

        public java.util.Date toDateTime()
        Returns the property value as DateTime in UTC.

        Throws an exception if the property type is not PropertyType.DATE_TIME.

        Microsoft Word stores only the date part (no time) for custom date properties.

        Example:

        Retrieves the types and values of the custom document properties.
        Document doc = new Document(getMyDir() + "Properties.doc");
        
        for (DocumentProperty docProperty : doc.getCustomDocumentProperties())
        {
            System.out.println(docProperty.getName());
            switch (docProperty.getType())
            {
                case PropertyType.STRING:
                    System.out.println("It's a string value.");
                    System.out.println(docProperty.toString());
                    break;
                case PropertyType.BOOLEAN:
                    System.out.println("It's a boolean value.");
                    System.out.println(docProperty.toBool());
                    break;
                case PropertyType.NUMBER:
                    System.out.println("It's an integer value.");
                    System.out.println(docProperty.toInt());
                    break;
                case PropertyType.DATE_TIME:
                    System.out.println("It's a date time value.");
                    System.out.println(docProperty.toDateTime());
                    break;
                case PropertyType.DOUBLE:
                    System.out.println("It's a double value.");
                    System.out.println(docProperty.toDouble());
                    break;
                case PropertyType.OTHER:
                    System.out.println("Other value.");
                    break;
                default:
                    throw new Exception("Unknown property type.");
            }
        }

        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());
        }
      • toDouble

        public double toDouble()
        Returns the property value as double. Throws an exception if the property type is not PropertyType.NUMBER.

        Example:

        Retrieves the types and values of the custom document properties.
        Document doc = new Document(getMyDir() + "Properties.doc");
        
        for (DocumentProperty docProperty : doc.getCustomDocumentProperties())
        {
            System.out.println(docProperty.getName());
            switch (docProperty.getType())
            {
                case PropertyType.STRING:
                    System.out.println("It's a string value.");
                    System.out.println(docProperty.toString());
                    break;
                case PropertyType.BOOLEAN:
                    System.out.println("It's a boolean value.");
                    System.out.println(docProperty.toBool());
                    break;
                case PropertyType.NUMBER:
                    System.out.println("It's an integer value.");
                    System.out.println(docProperty.toInt());
                    break;
                case PropertyType.DATE_TIME:
                    System.out.println("It's a date time value.");
                    System.out.println(docProperty.toDateTime());
                    break;
                case PropertyType.DOUBLE:
                    System.out.println("It's a double value.");
                    System.out.println(docProperty.toDouble());
                    break;
                case PropertyType.OTHER:
                    System.out.println("Other value.");
                    break;
                default:
                    throw new Exception("Unknown property type.");
            }
        }
      • toInt

        public int toInt()
        Returns the property value as integer. Throws an exception if the property type is not PropertyType.NUMBER.

        Example:

        Retrieves the types and values of the custom document properties.
        Document doc = new Document(getMyDir() + "Properties.doc");
        
        for (DocumentProperty docProperty : doc.getCustomDocumentProperties())
        {
            System.out.println(docProperty.getName());
            switch (docProperty.getType())
            {
                case PropertyType.STRING:
                    System.out.println("It's a string value.");
                    System.out.println(docProperty.toString());
                    break;
                case PropertyType.BOOLEAN:
                    System.out.println("It's a boolean value.");
                    System.out.println(docProperty.toBool());
                    break;
                case PropertyType.NUMBER:
                    System.out.println("It's an integer value.");
                    System.out.println(docProperty.toInt());
                    break;
                case PropertyType.DATE_TIME:
                    System.out.println("It's a date time value.");
                    System.out.println(docProperty.toDateTime());
                    break;
                case PropertyType.DOUBLE:
                    System.out.println("It's a double value.");
                    System.out.println(docProperty.toDouble());
                    break;
                case PropertyType.OTHER:
                    System.out.println("Other value.");
                    break;
                default:
                    throw new Exception("Unknown property type.");
            }
        }
      • toString

        public java.lang.String toString()
        Returns the property value as a string formatted according to the current locale.

        Converts a boolean property into "Y" or "N". Converts a date property into a short date string. For all other types converts a property using Object.ToString().

        Example:

        Retrieves the types and values of the custom document properties.
        Document doc = new Document(getMyDir() + "Properties.doc");
        
        for (DocumentProperty docProperty : doc.getCustomDocumentProperties())
        {
            System.out.println(docProperty.getName());
            switch (docProperty.getType())
            {
                case PropertyType.STRING:
                    System.out.println("It's a string value.");
                    System.out.println(docProperty.toString());
                    break;
                case PropertyType.BOOLEAN:
                    System.out.println("It's a boolean value.");
                    System.out.println(docProperty.toBool());
                    break;
                case PropertyType.NUMBER:
                    System.out.println("It's an integer value.");
                    System.out.println(docProperty.toInt());
                    break;
                case PropertyType.DATE_TIME:
                    System.out.println("It's a date time value.");
                    System.out.println(docProperty.toDateTime());
                    break;
                case PropertyType.DOUBLE:
                    System.out.println("It's a double value.");
                    System.out.println(docProperty.toDouble());
                    break;
                case PropertyType.OTHER:
                    System.out.println("Other value.");
                    break;
                default:
                    throw new Exception("Unknown property type.");
            }
        }

        Example:

        Retrieves a built-in document property by name.
        Document doc = new Document(getMyDir() + "Properties.doc");
        
        DocumentProperty docProperty = doc.getBuiltInDocumentProperties().get("Keywords");
        System.out.println(docProperty.toString());