Every Word document can contain built-in properties and custom properties like Author, Category, Title, Subject, keyword (Tag), Comment etc. In this article, we will see how to create these properties for a Word document, and will learn how to read and remove these properties from the Word document programmatically in Java using Free Spire.Doc for Java library.
Create document properties
1.创建内置的文档属性
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("Input.docx");
//Create built-in properties
document.getBuiltinDocumentProperties().setTitle("Creating document properties");
document.getBuiltinDocumentProperties().setSubject("Word document properties");
document.getBuiltinDocumentProperties().setAuthor("James");
document.getBuiltinDocumentProperties().setCompany("Codex");
document.getBuiltinDocumentProperties().setManager("Chris");
document.getBuiltinDocumentProperties().setCategory("Word manipulation");
document.getBuiltinDocumentProperties().setKeywords("Document properties, Word");
document.getBuiltinDocumentProperties().setComments("For internal use only");
//Save the resultant document
document.saveToFile("SetBuiltInProperties.docx", FileFormat.Docx_2013);
}
}
2.创建自定义文档属性
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("Input.docx");
//Create custom properties
document.getCustomDocumentProperties().add("TrackingID", "AB01");
document.getCustomDocumentProperties().add("Checked by", "Wilson");
//Save the resultant document
document.saveToFile("SetCustomProperties.docx", FileFormat.Docx_2013);
}
}
Read document properties
1.阅读内置的文档属性
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());
}
}
2.阅读自定义文档属性
import com.spire.doc.Document;
import com.spire.doc.DocumentProperty;
public class SetDocumentProperties {
public static void main(String[] args){
//Load Word document
Document document = new Document("SetCustomProperties.docx");
//Get the first custom property
DocumentProperty property = document.getCustomDocumentProperties().get(0);
//DocumentProperty property = document.getCustomDocumentProperties().get("TrackingID");
//Read name and value of the first custom property
System.out.println("Name: " + property.getName());
System.out.println("Value: " + property.getValue());
}
}
Remove document properties
1.删除内置属性
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);
}
}
2.删除自定义属性
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);
}
}