java.lang.Objectcom.aspose.words.DocumentProperty
public class DocumentProperty
Example:
Document doc = new Document(getMyDir() + "Properties.docx");
System.out.println(MessageFormat.format("1. Document name: {0}", doc.getOriginalFileName()));
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()));
}
Property Getters/Setters Summary | ||
---|---|---|
boolean | isLinkToContent() | |
Shows whether this property is linked to content or not. | ||
java.lang.String | getLinkSource() | |
Gets the source of a linked custom document property. | ||
java.lang.String | getName() | |
Returns the name of the property. | ||
int | getType() | |
Gets the data type of the property. The value of the property is PropertyType integer constant. | ||
java.lang.Object | getValue() | |
void | setValue(java.lang.Object value) | |
Gets or sets the value of the property. |
Method Summary | ||
---|---|---|
boolean | toBool() | |
Returns the property value as bool. | ||
byte[] | toByteArray() | |
Returns the property value as byte array. | ||
java.util.Date | toDateTime() | |
Returns the property value as DateTime in UTC. | ||
double | toDouble() | |
Returns the property value as double. | ||
int | toInt() | |
Returns the property value as integer. | ||
java.lang.String | toString() | |
Returns the property value as a string formatted according to the current locale. |
Property Getters/Setters Detail |
---|
isLinkToContent | |
public boolean isLinkToContent() |
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() |
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() |
Cannot be null and cannot be an empty string.
Example:
Enumerates through all built-in and custom properties in a document using indexed access.Document doc = new Document(getMyDir() + "Properties.docx"); System.out.println(MessageFormat.format("1. Document name: {0}", doc.getOriginalFileName())); 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() |
Example:
Enumerates through all built-in and custom properties in a document using indexed access.Document doc = new Document(getMyDir() + "Properties.docx"); System.out.println(MessageFormat.format("1. Document name: {0}", doc.getOriginalFileName())); 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.docx"); 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) |
Cannot be null.
Example:
Enumerates through all built-in and custom properties in a document using indexed access.Document doc = new Document(getMyDir() + "Properties.docx"); System.out.println(MessageFormat.format("1. Document name: {0}", doc.getOriginalFileName())); 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() |
Throws an exception if the property type is not
Example:
Retrieves the types and values of the custom document properties.Document doc = new Document(getMyDir() + "Properties.docx"); 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() |
Throws an exception if the property type is not
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() + "Logo.jpg")); // 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() |
Throws an exception if the property type is not
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.docx"); 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.docx"); 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() |
Example:
Retrieves the types and values of the custom document properties.Document doc = new Document(getMyDir() + "Properties.docx"); 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() |
Example:
Retrieves the types and values of the custom document properties.Document doc = new Document(getMyDir() + "Properties.docx"); 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() |
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.docx"); 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.docx"); DocumentProperty docProperty = doc.getBuiltInDocumentProperties().get("Keywords"); System.out.println(docProperty.toString());