Add Document Properties in Word Documents with Java
Source: Dev.to
Why Use Spire.Doc for Java?
Spire.Doc for Java is a professional API that lets you create, edit, convert, and print Word documents without requiring Microsoft Word on the server. Its feature set includes:
- Text manipulation
- Table processing
- Image handling
- Document‑property management
Because of its ease of use and comprehensive capabilities, it’s an excellent choice for automating Word‑document tasks in Java.
Adding Spire.Doc to Your Project
If you use Maven, add the repository and dependency to your pom.xml:
<repositories>
<repository>
<id>e-iceblue</id>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc</artifactId>
<version>13.12.2</version>
</dependency>
</dependencies>
Built‑in Document Properties
Built‑in properties are predefined fields that every Word document can have. They provide standard metadata, making it easier to categorize, search, and manage documents. Common built‑in properties include:
| Property | Description |
|---|---|
| Title | The main title of the document |
| Author | The creator of the document |
| Subject | A brief description of the content |
| Keywords | Important terms for searching |
| Company | The organization associated with the document |
| Manager | The manager responsible for the document |
| Category | The category to which the document belongs |
| Comments | General remarks or notes |
Setting Built‑in Properties with Spire.Doc
import com.spire.doc.BuiltinDocumentProperties;
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
public class AddBuiltinDocumentProperties {
public static void main(String[] args) throws Exception {
// Create a Document instance and load a Word file
Document document = new Document();
document.loadFromFile("Sample.docx");
// Access the built‑in document properties
BuiltinDocumentProperties props = document.getBuiltinDocumentProperties();
// Set property values
props.setTitle("Add Document Properties");
props.setSubject("Java Example");
props.setAuthor("James");
props.setCompany("Eiceblue");
props.setManager("Michael");
props.setCategory("Document Manipulation");
props.setKeywords("Java, Word, Document Properties");
props.setComments("This article shows how to add document properties");
// Save the updated document
document.saveToFile("AddStandardDocumentProperties.docx",
FileFormat.Docx_2013);
}
}
In this example:
- A
Documentobject is created and a Word file is loaded. getBuiltinDocumentProperties()returns theBuiltinDocumentPropertiescollection.- Setter methods (e.g.,
setTitle(),setAuthor()) assign values. - The document is saved with the new metadata.
Custom Document Properties
Built‑in properties cover common metadata, but many scenarios require custom properties that are specific to your organization or project. Custom properties let you define any name you like and store values of various data types (text, numbers, booleans, dates).
Typical Use Cases
- Project Management – Project ID, version number, client name
- Document Workflow – Approval status, reviewer, last modified by
- Internal Classification – Department, security level, retention policy
- Automated Processing – Properties used as triggers for other systems
Adding Custom Properties with Spire.Doc
import com.spire.doc.CustomDocumentProperties;
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import java.util.Date;
public class AddCustomDocumentProperties {
public static void main(String[] args) throws Exception {
// Create a Document instance and load a Word file
Document document = new Document();
document.loadFromFile("Sample.docx");
// Access the custom document properties collection
CustomDocumentProperties customProps = document.getCustomDocumentProperties();
// Add custom properties with different data types
customProps.add("Document ID", 1); // Integer
customProps.add("Authorized", true); // Boolean
customProps.add("Authorized By", "John Smith"); // String
customProps.add("Authorized Date", new Date()); // Date
// Save the updated document
document.saveToFile("AddCustomDocumentProperties.docx",
FileFormat.Docx_2013);
}
}
Explanation of the code
- Initialize a
Documentobject and load an existing Word file. - Retrieve the
CustomDocumentPropertiescollection viagetCustomDocumentProperties(). - Use
add(propertyName, value)to insert properties of various types. - Save the document, which now contains the custom metadata.
Recap
- Built‑in properties provide standard metadata (title, author, etc.).
- Custom properties let you store any additional information you need, with flexible data types.
- The Spire.Doc for Java API offers simple, fluent methods to read, modify, and persist both kinds of properties.
By incorporating these techniques into your Java applications, you can automate document classification, improve searchability, and integrate Word files more tightly with other business processes. Happy coding!
Adding Custom Document Properties
stomDocumentProperties()
We use the add() method to create new custom properties, specifying the property name and its value. Spire.Doc handles the underlying data‑type conversion automatically.
Viewing the Added Properties
After running these examples, you can open the generated Word document and navigate to:
- File → Info → Properties → Advanced Properties
- Click the Custom tab to see your added custom properties.
- Use the Summary or Statistics tabs to view built‑in properties.
Why This Matters
Mastering the art of adding document properties in Word files using Java and Spire.Doc for Java is a valuable skill for any developer involved in document automation.
- Standard built‑in properties provide general metadata.
- Custom properties allow you to store flexible, application‑driven information.
Embedding this metadata significantly enhances the discoverability, organization, and overall utility of your Word documents. This capability is paramount in modern document‑management systems, enabling:
- Powerful search functionalities
- Automated workflows
- Streamlined information retrieval
We encourage you to experiment further with Spire.Doc for Java and explore its vast potential in your Java applications.
Happy coding!