Java: Add Document Properties to Word Documents

The properties of a document are a set of information about the document and its content, such as title, subject, author's name, manager, company, category, keywords (also known as tags), and comments. This information does not appear in the content of the document, but it can help you better search, sort, and filter files. In this article, you will learn how to add document properties to Word documents in Java using Spire.Doc for Java.

Install Spire.Doc for Java

First, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>12.4.6</version>
    </dependency>
</dependencies>
    

Add Built-in Document Properties to a Word Document in Java

A built-in document property (also known as a standard document property) consists of a name and a value. You cannot set or change the name of a built-in document property as it’s predefined by Microsoft Word, but you can set or change its value. The following steps demonstrate how to set values for built-in document properties in a Word document in Java using Spire.Doc for Java:

  • Initialize an instance of Document class.
  • Load a Word document using Document.loadFromFile() method.
  • Access the built-in document properties of the document using Document.getBuiltinDocumentProperties() method.
  • Set the values of specific document properties such as title, subject and author using setTitle(), setSubject() and setAuthor() methods provided by BuiltinDocumentProperties class.
  • Save the result document using Document.saveToFile() method.
  • Java
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
        Document document = new Document();
        //Load a Word document
        document.loadFromFile("Sample.docx");

        //Access the built-in document properties of the document
        BuiltinDocumentProperties standardProperties = document.getBuiltinDocumentProperties();
        //Set the values of specific built-in document properties 
        standardProperties.setTitle("Add Document Properties");
        standardProperties.setSubject("Java Example");
        standardProperties.setAuthor("James");
        standardProperties.setCompany("Eiceblue");
        standardProperties.setManager("Michael");
        standardProperties.setCategory("Document Manipulation");
        standardProperties.setKeywords("Java, Word, Document Properties");
        standardProperties.setComments("This article shows how to add document properties");

        //Save the result document
        document.saveToFile("AddStandardDocumentProperties.docx", FileFormat.Docx_2013);
    }
}

Java: Add Document Properties to Word Documents

Add Custom Document Properties to a Word Document in Java

A custom document property can be defined by a document author or user. Each custom document property should contain a name, a value and a data type. The data type can be one of these four types: Text, Date, Number and Yes or No. The following steps demonstrate how to add custom document properties with different data types to a Word document in Java using Spire.Doc for Java:

  • Initialize an instance of Document class.
  • Load a Word document using Document.loadFromFile() method.
  • Access the custom document properties of the document using Document.getCustomDocumentProperties() method.
  • Add custom document properties with different data types to the document using CustomDocumentProperties.add(String, Object) method.
  • Save the result document using Document.saveToFile() method.
  • Java
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
        Document document = new Document();
        //Load a Word document
        document.loadFromFile("Sample.docx");

        //Access the custom document properties of the document
        CustomDocumentProperties customProperties = document.getCustomDocumentProperties();
        //Add custom document properties with different data types to the document
        customProperties.add("Document ID", 1);
        customProperties.add("Authorized", true);
        customProperties.add("Authorized By", "John Smith");
        customProperties.add("Authorized Date", new Date());

        //Save the result document
        document.saveToFile("AddCustomDocumentProperties.docx", FileFormat.Docx_2013);
    }
}

Java: Add Document Properties to Word Documents

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.