Work with Document Properties

Document properties allow storing some useful information about your document. These properties can be divided into two groups:

  • System or built-in that contain values such as document title, author name, document statistics, and others.
  • User-defined or custom, provided as name-value pairs where the user can define both the name and value.

It is useful to know that information about API and Version Number is directly written to output documents. For example, upon converting a document to PDF, Aspose.Words fills in the “Application” field with “Aspose.Words”, and the “PDF Producer” field with “Aspose.Words for Java YY.M.N”, where YY.M.N is the version of Aspose.Words used for conversion. For more details, see “Generator or Producer Name Included in Output Documents”.

Access Document Properties

To access document properties in Aspose.Words use:

BuiltInDocumentProperties and CustomDocumentProperties are collections of DocumentProperty objects. These objects can be obtained through the indexer property by name or by index.

BuiltInDocumentProperties additionally provides access to document properties through a set of entered properties that return values of the appropriate type. CustomDocumentProperties enable you to add or remove document properties from a document.

The DocumentProperty class allows you to get the name, value, and type of a document property. [Value]https://apireference.aspose.com/words/java/com.aspose.words/documentproperty#Value) returns an object, but there is a set of methods allowing you to get the property value converted to a specific type. After you get to know what type the property is, you can use one of the DocumentProperty.ToXXX methods, such as DocumentProperty.ToString and DocumentProperty.ToInt, to obtain the value of the appropriate type.

The following code example shows how to enumerate all built-in and custom properties in a document:

In Microsoft Word, you can access document properties using the “File → Properties” menu.

work-with-document-properties_1.png

Add or Remove Document Properties

You cannot add or remove built-in document properties using Aspose.Words. You can only change or update their values.

To add custom document properties with Aspose.Words, use the Add method, passing the new property name and the value of the appropriate type. The method returns the newly created DocumentProperty object.

To remove custom properties, use the Remove method, passing it the property name to remove, or the RemoveAt method to remove the property by index. You can also remove all properties using the Clear method.

The following code example checks whether a custom property with a given name exists in a document and adds a few more custom document properties:

The following code example shows how to remove a custom document property:

Update Built-In Document Properties

Aspose.Words does not automatically update document properties, as Microsoft Word does with some properties, but provides a method to update some statistical built-in document properties. Call the UpdateWordCount method to recalculate and update the following properties:

Create a New Custom Property Linked to Content

Aspose.Words provides the AddLinkToContent method to create a new custom document property linked to content. This property returns the newly created property object or null if the LinkSource is invalid.

The following code example shows how to configure a link to a custom property:

Get Document Variables

You can get a collection of document variables using the Variables property. Variable names and values are strings.

The following code example shows how to enumerate document variables:

Remove Personal Information from Document

If you want to share a Word document with other people, you may want to remove personal information such as author name and company. To do this use the RemovePersonalInformation property to set the flag indicating that Microsoft Word will remove all user information from comments, revisions, and document properties upon saving the document.