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.
See Also:
DocumentPropertyCollection

Property Getters/Setters Summary
booleanisLinkToContent()
Shows whether this property is linked to content or not.
java.lang.StringgetLinkSource()
Gets the source of a linked custom document property.
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

      • isLinkToContent

        public boolean isLinkToContent()
        
        Shows whether this property is linked to content or not.

        Example:

        Shows how to link a custom document property to a bookmark.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        builder.startBookmark("MyBookmark");
        builder.write("Hello world!");
        builder.endBookmark("MyBookmark");
        
        // Link a new custom property to a bookmark. The value of this property
        // will be the contents of the bookmark that it references in the "LinkSource" member.
        CustomDocumentProperties customProperties = doc.getCustomDocumentProperties();
        DocumentProperty customProperty = customProperties.addLinkToContent("Bookmark", "MyBookmark");
        
        Assert.assertEquals(true, customProperty.isLinkToContent());
        Assert.assertEquals("MyBookmark", customProperty.getLinkSource());
        Assert.assertEquals("Hello world!", customProperty.getValue());
        
        doc.save(getArtifactsDir() + "DocumentProperties.LinkCustomDocumentPropertiesToBookmark.docx");
      • getLinkSource

        public java.lang.String getLinkSource()
        
        Gets the source of a linked custom document property.

        Example:

        Shows how to link a custom document property to a bookmark.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        builder.startBookmark("MyBookmark");
        builder.write("Hello world!");
        builder.endBookmark("MyBookmark");
        
        // Link a new custom property to a bookmark. The value of this property
        // will be the contents of the bookmark that it references in the "LinkSource" member.
        CustomDocumentProperties customProperties = doc.getCustomDocumentProperties();
        DocumentProperty customProperty = customProperties.addLinkToContent("Bookmark", "MyBookmark");
        
        Assert.assertEquals(true, customProperty.isLinkToContent());
        Assert.assertEquals("MyBookmark", customProperty.getLinkSource());
        Assert.assertEquals("Hello world!", customProperty.getValue());
        
        doc.save(getArtifactsDir() + "DocumentProperties.LinkCustomDocumentPropertiesToBookmark.docx");
      • getName

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

        Cannot be null and cannot be an empty string.

      • getType

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

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

    • Method Detail

      • toBool

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

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

        Example:

        Shows various type conversion methods of custom document properties.
        Document doc = new Document();
        CustomDocumentProperties properties = doc.getCustomDocumentProperties();
        
        Date authDate = new Date();
        properties.add("Authorized", true);
        properties.add("Authorized By", "John Doe");
        properties.add("Authorized Date", authDate);
        properties.add("Authorized Revision", doc.getBuiltInDocumentProperties().getRevisionNumber());
        properties.add("Authorized Amount", 123.45);
        
        Assert.assertEquals(true, properties.get("Authorized").toBool());
        Assert.assertEquals("John Doe", properties.get("Authorized By").toString());
        Assert.assertEquals(authDate, properties.get("Authorized Date").toDateTime());
        Assert.assertEquals(1, properties.get("Authorized Revision").toInt());
        Assert.assertEquals(123.45d, properties.get("Authorized Amount").toDouble());
      • toByteArray

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

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

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

        Shows various type conversion methods of custom document properties.
        Document doc = new Document();
        CustomDocumentProperties properties = doc.getCustomDocumentProperties();
        
        Date authDate = new Date();
        properties.add("Authorized", true);
        properties.add("Authorized By", "John Doe");
        properties.add("Authorized Date", authDate);
        properties.add("Authorized Revision", doc.getBuiltInDocumentProperties().getRevisionNumber());
        properties.add("Authorized Amount", 123.45);
        
        Assert.assertEquals(true, properties.get("Authorized").toBool());
        Assert.assertEquals("John Doe", properties.get("Authorized By").toString());
        Assert.assertEquals(authDate, properties.get("Authorized Date").toDateTime());
        Assert.assertEquals(1, properties.get("Authorized Revision").toInt());
        Assert.assertEquals(123.45d, properties.get("Authorized Amount").toDouble());

        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(MessageFormat.format("Document authorized on {0}", doc.getCustomDocumentProperties().get("AuthorizationDate")));
      • toDouble

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

        Example:

        Shows various type conversion methods of custom document properties.
        Document doc = new Document();
        CustomDocumentProperties properties = doc.getCustomDocumentProperties();
        
        Date authDate = new Date();
        properties.add("Authorized", true);
        properties.add("Authorized By", "John Doe");
        properties.add("Authorized Date", authDate);
        properties.add("Authorized Revision", doc.getBuiltInDocumentProperties().getRevisionNumber());
        properties.add("Authorized Amount", 123.45);
        
        Assert.assertEquals(true, properties.get("Authorized").toBool());
        Assert.assertEquals("John Doe", properties.get("Authorized By").toString());
        Assert.assertEquals(authDate, properties.get("Authorized Date").toDateTime());
        Assert.assertEquals(1, properties.get("Authorized Revision").toInt());
        Assert.assertEquals(123.45d, properties.get("Authorized Amount").toDouble());
      • toInt

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

        Example:

        Shows various type conversion methods of custom document properties.
        Document doc = new Document();
        CustomDocumentProperties properties = doc.getCustomDocumentProperties();
        
        Date authDate = new Date();
        properties.add("Authorized", true);
        properties.add("Authorized By", "John Doe");
        properties.add("Authorized Date", authDate);
        properties.add("Authorized Revision", doc.getBuiltInDocumentProperties().getRevisionNumber());
        properties.add("Authorized Amount", 123.45);
        
        Assert.assertEquals(true, properties.get("Authorized").toBool());
        Assert.assertEquals("John Doe", properties.get("Authorized By").toString());
        Assert.assertEquals(authDate, properties.get("Authorized Date").toDateTime());
        Assert.assertEquals(1, properties.get("Authorized Revision").toInt());
        Assert.assertEquals(123.45d, properties.get("Authorized Amount").toDouble());
      • 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:

        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.
        // The document has a fixed list of built-in properties. The user creates all of the 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()));
        }

        Example:

        Shows various type conversion methods of custom document properties.
        Document doc = new Document();
        CustomDocumentProperties properties = doc.getCustomDocumentProperties();
        
        Date authDate = new Date();
        properties.add("Authorized", true);
        properties.add("Authorized By", "John Doe");
        properties.add("Authorized Date", authDate);
        properties.add("Authorized Revision", doc.getBuiltInDocumentProperties().getRevisionNumber());
        properties.add("Authorized Amount", 123.45);
        
        Assert.assertEquals(true, properties.get("Authorized").toBool());
        Assert.assertEquals("John Doe", properties.get("Authorized By").toString());
        Assert.assertEquals(authDate, properties.get("Authorized Date").toDateTime());
        Assert.assertEquals(1, properties.get("Authorized Revision").toInt());
        Assert.assertEquals(123.45d, properties.get("Authorized Amount").toDouble());