Document properties are some important information contained in Word documents, including title, subject, author, company, keywords, etc. Reading this information can help identify the document, quickly determine the document type or source, and improve the efficiency of document storage and organization. However, if the document properties such as author and company are inappropriate for others to know, you can remove them to protect privacy. This article is going to show how to read or remove document properties in Word documents programmatically using Spire.Doc for Java.
- Read Built-in and Custom Properties from Word Documents
- Remove Built-in and Custom Properties from Word Documents
Install Spire.Doc for Java
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. 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> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc</artifactId> <version>12.11.0</version> </dependency> </dependencies>
Read Built-in and Custom Properties from Word Documents
There are two types of properties in Word documents: built-in properties and custom properties. Built-in properties are some predefined properties, while custom properties are properties with custom names, types, and values. The detailed steps for reading the two kinds of document properties are as follows:
- Create an object of Document.
- Load a Word document using Document.loadFromFile() method.
- Get all the built-in properties and custom properties using Document.getBuiltinDocumentProperties() method and Document.getCustomDocumentProperties() method.
- Get each built-in property using methods under BuiltinDocumentProperties class.
- Loop through the custom properties to get their name and value using methods under CustomDocumentProperties class.
- Java
import com.spire.doc.BuiltinDocumentProperties; import com.spire.doc.CustomDocumentProperties; import com.spire.doc.Document; public class GetDocumentProperties { public static void main(String[] args) { //Create an object of Document Document document = new Document(); //Load a Word document document.loadFromFile("C:/Sample.docx"); //Create an object of StringBuilder StringBuilder properties = new StringBuilder(); //Get all the built-in properties and custom properties BuiltinDocumentProperties builtinDocumentProperties = document.getBuiltinDocumentProperties(); CustomDocumentProperties customDocumentProperties = document.getCustomDocumentProperties(); //Get each built-in property String title = builtinDocumentProperties.getTitle(); String subject = builtinDocumentProperties.getSubject(); String author = builtinDocumentProperties.getAuthor(); String manager = builtinDocumentProperties.getManager(); String category = builtinDocumentProperties.getCategory(); String company = builtinDocumentProperties.getCompany(); String keywords = builtinDocumentProperties.getKeywords(); String comments = builtinDocumentProperties.getComments(); //Set string format for displaying String builtinProperties = String.format("The built-in properties:\r\nTitle: " + title + "\r\nSubject: " + subject + "\r\nAuthor: " + author + "\r\nManager: " + manager + "\r\nCategory: " + category + "\r\nCompany: " + company + "\r\nKeywords: "+ keywords + "\r\nComments:" + comments ); //Add the built-in properties to the StringBuilder object properties.append(builtinProperties); //Get each custom property properties.append("\r\n\r\nThe custom properties:"); for (int i = 0; i < customDocumentProperties.getCount(); i++) { String customProperties = String.format("\r\n" + customDocumentProperties.get(i).getName() + ": " + document.getCustomDocumentProperties().get(i).getValue()); //Add the custom properties to the StringBuilder object properties.append(customProperties); } //Output the properties of the document System.out.println(properties); } }
Remove Built-in and Custom Properties from Word Documents
Built-in document properties can be removed by setting their values as empty. For custom document properties, we can use the CustomDocumentProperties.get().getName() method to get their name and then use CustomDocumentProperties.remove() method to remove them. The detailed steps are as follows:
- Create an object of Document.
- Load a Word document using Document.loadFromFile() method.
- Get all built-in properties and custom properties using Document.getBuiltinDocumentProperties() method and Document.getCustomDocumentProperties() method.
- Remove the built-in properties by setting their values as empty using methods under BuiltinDocumentProperties class.
- Get the count of custom properties using CustomDocumentProperties.getCount() method.
- Loop through the custom properties, get their names by using CustomDocumentProperties.get().getName() method, and remove each custom property by their name using CustomDocumentProperties.remove() method.
- Save the document using Document.saveToFile() method.
- Java
import com.spire.doc.BuiltinDocumentProperties; import com.spire.doc.CustomDocumentProperties; import com.spire.doc.Document; import com.spire.doc.FileFormat; public class RemoveDocumentProperties { public static void main(String[] args) { //Create an object of Document Document document = new Document(); //Load a Word document document.loadFromFile("C:/Sample.docx"); //Get all built-in properties and custom properties BuiltinDocumentProperties builtinDocumentProperties = document.getBuiltinDocumentProperties(); CustomDocumentProperties customDocumentProperties = document.getCustomDocumentProperties(); //Remove built-in properties by setting their value to empty builtinDocumentProperties.setTitle(""); builtinDocumentProperties.setSubject(""); builtinDocumentProperties.setAuthor(""); builtinDocumentProperties.setManager(""); builtinDocumentProperties.setCompany(""); builtinDocumentProperties.setCategory(""); builtinDocumentProperties.setKeywords(""); builtinDocumentProperties.setComments(""); //Get the count of custom properties int count = customDocumentProperties.getCount(); //Loop through the custom properties to remove them for (int i = count; i > 0; i-- ){ //Get the name of a custom property String name = customDocumentProperties.get(i-1).getName(); //Remove the custom property by its name customDocumentProperties.remove(name); } //Save the document document.saveToFile("RemoveDocumentProperties.docx", FileFormat.Auto); document.dispose(); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.