How to Add Properties to Word Documents in Java
Tags: programming, java, beginners, api
Published: 2023-08-09 07:54:43

Document properties provide additional information about documents, such as title, author, subject, keywords, etc., which can help users better manage and classify documents. These properties can be set and viewed in document editing software, or set through code. Take Java code as an example. The following is a detailed introduction on this operation.

**Step 1:** Download [Free Spire.Doc for Java](https://www.e-iceblue.com/Introduce/free-doc-for-java.html) and unzip it.
This a the community version of Spire.Doc for Java and there are some limitations on pages during the process. If you want to get rid of them, download the commercial product from [this link](https://www.e-iceblue.com/Download/doc-for-java-free.html) and apply for a [temporary license](https://www.e-iceblue.com/TemLicense.html). 
It supports create or edit Word files independently on Java applications. With it, you can also [convert Word to PDF](https://www.e-iceblue.com/Tutorials/Java/Spire.Doc-for-Java/Program-Guide/Conversion/Convert-Word-to-PDF-in-Java.html), [convert Word to Image](https://www.e-iceblue.com/Tutorials/Java/Spire.Doc-for-Java/Program-Guide/Conversion/Convert-Word-to-Images-in-Java.html), [merge Word documents](https://www.e-iceblue.com/Tutorials/Java/Spire.Doc-for-Java/Program-Guide/Document-Operation/Merge-Word-Documents-in-Java.html), etc.

**Step 2:** Create a new Java project.

**Step 3:** Import “Spire.Doc.jar” to your project
Take IntelliJ IDEA 2018 (jdk 1.8.0) for example.
* Click “File”- “Project Structure”- “Modules”- “Dependencies” in turn.
* Choose the “JARs or Directories” under the green plus.
* Find the “Spire.Doc.jar” in the lib folder of the decompressed package and import it to the project.

**Step 4:** Sample code. 
```java
import com.spire.doc.BuiltinDocumentProperties;
import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class AddDocumentProperties {
    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("Document Properties");
        standardProperties.setSubject("How to add properties");
        standardProperties.setAuthor("Naomi");
        standardProperties.setCompany("Co., Ltd");
        standardProperties.setManager("Michael");
        standardProperties.setCategory("Document");
        standardProperties.setKeywords("Word, Document, Properties");
        standardProperties.setComments("This is a good tutorial.");

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

In the above code, first create a Document instance and load a Word document. Then, by calling the **getBuiltinDocumentProperties()** method, you can access the document's built-in document properties. Next, use the set method to individually set the value of specific built-in document properties. Finally, save the modified document as a new file using the **Document.saveToFile()** method.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/22j0xqla29yxnxs1m50t.png)