How to Edit Word Document Metadata in Java

In this short guide, you will get a brief description of how to edit Word document metadata in Java. It explains the required configuration in the IDE, a programming logic through detailed steps, and a runnable sample code to develop a DOCX metadata editor in Java. Different types of custom and built-in properties are demonstrated for a clear understanding of this feature.

Steps to Change Properties of Word Document in Java

  1. Set the environment to use Aspose.Words for Java to edit the document properties
  2. Load the target Word file where properties are to be modified
  3. Access the collection of custom properties and check if the desired property is available or not
  4. Set new values of the custom properties
  5. Access and update the built-in properties
  6. Save the resultant Word file with updated properties

By adhering to these steps, you can edit Word metadata in Java. This process is commenced by loading the source Word file, accessing the custom properties, and then modifying them as per the requirements. In the next steps, the built-in properties are accessed and updated accordingly before saving the resultant Word file.

Code to Edit Document Properties in Word in Java

import com.aspose.words.*;
public class Main {
public static void main(String[] args) throws Exception // Update Word Metadata in Java
{
// Set the licenses
new com.aspose.words.License().setLicense("Aspose.Total.lic");
// Load the document
Document doc = new Document("SampleProps.docx");
// Access the properties
CustomDocumentProperties custProps = doc.getCustomDocumentProperties();
// Check the desired property
if (custProps.get("Reviewed") != null)
{
// Set new properties
custProps.get("Reviewed By").setValue("Mart");
custProps.get("Reviewed Date").setValue(new java.util.Date());
}
// Access the properties
BuiltInDocumentProperties documentProperties = doc.getBuiltInDocumentProperties();
// Update the properties
documentProperties.get("Pages").setValue(doc.getPageCount());
documentProperties.get("Comments").setValue("Document Comments");
documentProperties.get("Title").setValue("Document Title");
// Save the output file
doc.save("Output.docx");
System.out.println("Done");
}
}

You have explored the process to edit Word document metadata in Java through this coded snippet. The getCustomDocumentProperties() method in the Document class provides the collection of custom properties for editing where individual properties can be accessed by using the get() method. Similarly, the getBuiltInDocumentProperties() method provides access to the built-in properties for modification.

This article has directed us to access and modify the Word metadata. If you want to convert a Word file to a markdown file, refer to the article on how to convert Word to markdown using Java.

 English