com.aspose.words
Class DocumentProperty

java.lang.Object
    extended by 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
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()
voidsetValue(java.lang.Object value)
           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 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");

getLinkSource

public java.lang.String getLinkSource()
Gets the source of a linked custom document 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");

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.

Example:

Shows how to append a thumbnail to an Epub document.
// Create a blank document and add some text with a DocumentBuilder
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Hello world!");

// The thumbnail property resides in a document's built in properties, but is used exclusively by Epub e-book documents
BuiltInDocumentProperties properties = doc.getBuiltInDocumentProperties();

// Load an image from our file system into a byte array
byte[] thumbnailBytes = Files.readAllBytes(Paths.get(getImageDir() + "Aspose.Words.gif"));

// Set the value of the Thumbnail property to the array from above
properties.setThumbnail(thumbnailBytes);

// Our thumbnail should be visible at the start of the document, before the text we added
doc.save(getArtifactsDir() + "Properties.Thumbnail.epub");

// We can also extract a thumbnail property into a byte array and then into the local file system like this
DocumentProperty thumbnail = doc.getBuiltInDocumentProperties().get("Thumbnail");
Files.write(Paths.get(getArtifactsDir() + "Properties.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:

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

See Also:
          Aspose.Words Documentation - the home page for the Aspose.Words Product Documentation.
          Aspose.Words Support Forum - our preferred method of support.