Java: How to Add Document Properties to Word Documents

 Word document properties include general, summary, statistics, content, custom. The summary includes items such as title, subject, author, manager, unit, category, keywords, remarks, etc. Attributes are equivalent to the business card of the document, and you can add comments, instructions, etc. you want. You can also mark the copyright. Today I will introduce to you how to add document properties to Word documents through Java code. Please read below for details.

  • Add Built-in Document Properties to a Word Document
  • Add Custom Document Properties to a Word Document

Programming Environment

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.

Method 1:

Introduced manually. Download Free Spire.Doc for Java locally, unzip it, and find the Spire.Doc.jar file in the lib folder. Open the following interface in IDEA, and import the jar file in the local path into the Java program:

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>
            <url>https://repo.e-iceblue.com/repository/maven-public/</url>
        </repository>
    </repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc.free</artifactId>
        <version>5.2.0</version>
    </dependency>
</dependencies>

Add Built-in Document Properties to a Word Document

  •  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.

Full Code

C#

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("Staff, Rules, Document Properties");
       
standardProperties.setComments("This handbook is an introduction to the employee handbook and rules and regulations");

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

Effective Shot



Add Custom Document Properties to a Word Document in Java

  • The following steps demonstrate how to add custom document properties with different data types to a Word document in 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.

Full Code

C#

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", 1008611);         customProperties.add("Authorized", true);         customProperties.add("Authorized By", "Tommy Shelby");         customProperties.add("Authorized Date", new Date().toString());
       
//Save the result document         document.saveToFile("AddCustomDocumentProperties.docx", FileFormat.Docx_2013);
   
}
}

Effective Shot


Conclusion:

In this post, you have learned how to hide a specific paragraph in a Word document in Java. Not only that, we also have other functions, such as, Java: Split Word DocumentsJava: Accept or Reject Tracked Changes in Word and so on. Apart from that, if you'd like to learn more, you can visit this link to explore more about for Spire.Doc for Java.

Comments

Popular posts from this blog

How to Change Font Color in Word via Java

How to Convert OpenDocument Presentation (.odp) to PDF via Java Application