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 .NET 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:
-
built_in_document_properties to obtain built-in properties.
-
custom_document_properties to obtain custom properties.
built_in_document_properties and custom_document_properties are collections of DocumentProperty objects. These objects can be obtained through the indexer property by name or by index.
built_in_document_properties additionally provides access to document properties through a set of entered properties that return values of the appropriate type. custom_document_properties 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 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.to_XXX methods, such as DocumentProperty.__str__ and DocumentProperty.to_int, 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.
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 remove_at 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 update_word_count method to recalculate and update the following properties:
Create a New Custom Property Linked to Content
Aspose.Words provides the add_link_to_content method to create a new custom document property linked to content. This property returns the newly created property object or null if the link_source 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 add and access 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 remove_personal_information property to set the flag indicating that Microsoft Word will remove all user information from comments, revisions, and document properties upon saving the document.
The following code example shows how to remove personal information: