Add Document Properties in Word Documents with Java

Published: (January 5, 2026 at 08:37 PM EST)
4 min read
Source: Dev.to

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:

PropertyDescription
TitleThe main title of the document
AuthorThe creator of the document
SubjectA brief description of the content
KeywordsImportant terms for searching
CompanyThe organization associated with the document
ManagerThe manager responsible for the document
CategoryThe category to which the document belongs
CommentsGeneral 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:

  1. A Document object is created and a Word file is loaded.
  2. getBuiltinDocumentProperties() returns the BuiltinDocumentProperties collection.
  3. Setter methods (e.g., setTitle(), setAuthor()) assign values.
  4. 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

  1. Initialize a Document object and load an existing Word file.
  2. Retrieve the CustomDocumentProperties collection via getCustomDocumentProperties().
  3. Use add(propertyName, value) to insert properties of various types.
  4. 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:

  1. FileInfoPropertiesAdvanced Properties
  2. Click the Custom tab to see your added custom properties.
  3. 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!

Back to Blog

Related posts

Read more »

Introduction to Dev.to API

Getting Started - Log in to your dev.to account. - Go to Settings → Account. - Scroll down to the DEV API Keys section. - Generate a new key and copy it somewh...

Embedding JVM in Rust

Java ↔ Rust Interop with the jni Crate !Ivan Yurchenkohttps://media2.dev.to/dynamic/image/width=50,height=50,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fd...

Introduction to Java

How does Java code written by humans get executed by machines? It uses a translation system JDK → JRE → JVM → JIT to convert the programming language to machin...