Add Document Properties to Word Document using Java

In daily work, adding document properties can make the documents management more convenient. And my previous article have introduced how to set PDF document properties, now I will introduce how to add document properties (including built-in document properties & custom document properties) to a Word document with Free Spire.Doc for Java.

【1】Import the jar dependency to your Java application (2 methods)

● Download Free Spire.Doc for Java and unzip it, then add the Spire.Doc.jar file to your Java application as dependency.

● Directly add the jar dependency to maven project by adding the following configurations to the pom.xml.

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

【2Add Built-In Document Properties

import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class SetDocumentProperties {
public static void main(String[] args){
//Load Word document
Document document = new Document("Input.docx");

//Add built-in document properties to the Word document
document.getBuiltinDocumentProperties().setTitle("Test Document");
document.getBuiltinDocumentProperties().setSubject("Document Properties");
document.getBuiltinDocumentProperties().setAuthor("Mary");
document.getBuiltinDocumentProperties().setCompany("AA Technology Co. Ltd");
document.getBuiltinDocumentProperties().setManager("Jakson");
document.getBuiltinDocumentProperties().setCategory("Doc Demos");
document.getBuiltinDocumentProperties().setKeywords("Document, Property, Test");
document.getBuiltinDocumentProperties().setComments("Add document properties to this test document.");

//Save the resultant document
document.saveToFile("SetBuiltInProperties.docx", FileFormat.Docx_2013);
}
}

3Add Custom Document Properties

import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class SetDocumentProperties {
public static void main(String[] args){

//Load Word document
Document document = new Document("Input.docx");

//Add custom document properties to the Word document
document.getCustomDocumentProperties().add("TrackingID", "AB01");
document.getCustomDocumentProperties().add("Checked By", "Wilson");

//Save the resultant document
document.saveToFile("SetCustomProperties.docx", FileFormat.Docx_2013);

}
}

Sharing Java Code