Read and Remove Document Properties in Word in Java

This article demonstrates how to read and remove built-in and custom document properties in Word document using Spire.Doc for Java.

Read document properties

Read 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("SetBuiltInProperties.docx");

        //Read built-in properties
        System.out.println("Title: " + document.getBuiltinDocumentProperties().getTitle());
        System.out.println("Subject: " + document.getBuiltinDocumentProperties().getSubject());
        System.out.println("author: " + document.getBuiltinDocumentProperties().getAuthor());
        System.out.println("company: " + document.getBuiltinDocumentProperties().getCompany());
        System.out.println("manager: " + document.getBuiltinDocumentProperties().getManager());
        System.out.println("category: " + document.getBuiltinDocumentProperties().getCategory());
        System.out.println("keyWords: " + document.getBuiltinDocumentProperties().getKeywords());
        System.out.println("comments: " + document.getBuiltinDocumentProperties().getComments());
    }
}

Read and Remove Document Properties in Word in Java

Read custom document properties

Remove built-in 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("SetBuiltInProperties.docx");

        //Remove built-in properties by setting the value to null
        document.getBuiltinDocumentProperties().setTitle("");
        document.getBuiltinDocumentProperties().setSubject("");
        document.getBuiltinDocumentProperties().setAuthor("");
        document.getBuiltinDocumentProperties().setCompany("");
        document.getBuiltinDocumentProperties().setManager("");
        document.getBuiltinDocumentProperties().setCategory("");
        document.getBuiltinDocumentProperties().setKeywords("");
        document.getBuiltinDocumentProperties().setComments("");

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

Read and Remove Document Properties in Word in Java

Remove 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("SetCustomProperties.docx");

        //Remove specific custom property by name
        document.getCustomDocumentProperties().remove("TrackingID");
        document.getCustomDocumentProperties().remove("Checked by");

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