Document Properties

Overview

This guide shows how to set and read the built-in and custom properties of a Word document with Aspose.Words FOSS for Java. Every Word document carries properties that are not part of its text: built-in ones such as the title, the author and the keywords, and custom ones that you define yourself. Document.getBuiltInDocumentProperties returns a BuiltInDocumentProperties collection and Document.getCustomDocumentProperties returns a CustomDocumentProperties collection. This page sets built-in properties and reads them from the saved file, then adds custom properties of three types. Both examples were compiled and run against the published package on Java 8, 17 and 21.


Prerequisites

RequirementDetail
Java8 or later
DependencyThe package from Installation
Importimport com.aspose.words.*;
CodeThe examples are statements. Put them in a method that declares throws Exception.

Set and Read Built-in Properties

The built-in collection has a getter for each property and a setter for most of them, for example setTitle and getTitle. The example sets the title, the author and the keywords, saves the document, reloads it and prints the three values: they survived the save.

import com.aspose.words.*;

Document doc = new Document();
new DocumentBuilder(doc).writeln("Quarterly report");

BuiltInDocumentProperties props = doc.getBuiltInDocumentProperties();
props.setTitle("Quarterly Report");
props.setAuthor("Finance Team");
props.setKeywords("report, finance");
doc.save("report.docx");

Document reloaded = new Document("report.docx");
BuiltInDocumentProperties saved = reloaded.getBuiltInDocumentProperties();
System.out.println("Title: " + saved.getTitle());
System.out.println("Author: " + saved.getAuthor());
System.out.println("Keywords: " + saved.getKeywords());

The program printed:

Title: Quarterly Report
Author: Finance Team
Keywords: report, finance

Add and Remove Custom Properties

CustomDocumentProperties.add creates a property with a name and a value; the value can be a string, an int, a boolean, a double or a date. get returns a DocumentProperty whose getValue gives the value back, contains tells whether a name exists and remove deletes it. The example saves and reloads the document and read 3 custom properties. It stores no date: in tests on Java 8, 17 and 21 a date value read back after a save was shifted by the offset of the JVM time zone (5 hours earlier in New York, 5 hours later in Karachi, none in UTC), so run with -Duser.timezone=UTC or store the date as a string if the exact value matters.

import com.aspose.words.*;

Document doc = new Document();
CustomDocumentProperties custom = doc.getCustomDocumentProperties();
custom.add("Project", "Atlas");
custom.add("Revision", 3);
custom.add("Approved", true);
doc.save("custom.docx");

Document reloaded = new Document("custom.docx");
CustomDocumentProperties saved = reloaded.getCustomDocumentProperties();
System.out.println("Custom properties: " + saved.getCount());
System.out.println("Project = " + saved.get("Project").getValue());
System.out.println("Revision = " + saved.get("Revision").getValue());
System.out.println("Approved = " + saved.get("Approved").getValue());

saved.remove("Revision");
System.out.println("Contains Revision: " + saved.contains("Revision"));

The program printed:

Custom properties: 3
Project = Atlas
Revision = 3
Approved = true
Contains Revision: false

Tips and Best Practices

  • Set the title and the author before you save, so that the saved file carries them.
  • Use contains before get when a property may not exist.
  • Custom property names are strings you choose, such as Project or Revision.

Common Issues

IssueCauseFix
The compiler reports cannot find symbol for Document.The com.aspose.words package is not imported.Add import com.aspose.words.*;.
The compiler reports an unreported exception.The Document constructors, Document.save and some other methods declare throws Exception.Declare it on the calling method or catch it.

FAQ

Which types can a custom property have?

add has overloads for a string, an int, a boolean, a double and a date. Check date values after a save and reload: in tests on Java 8, 17 and 21 they were shifted by the offset of the JVM time zone.

Are the properties saved in the file?

Yes. In both examples the reloaded document returned the values that were set.


API Reference Summary

Class / MethodDescription
BuiltInDocumentPropertiesA collection of built-in document properties.
CustomDocumentPropertiesA collection of custom document properties.
DocumentPropertyRepresents a custom or built-in document property.
CustomDocumentProperties.add(name, value)Creates a new custom document property.

Related Guides

See Also