Document properties provide additional information about documents, such as title, author, subject, keywords, etc., which can help users better manage and classify documents. These properties can be set and viewed in document editing software, or set through code. Take Java code as an example. The following is a detailed introduction on this operation.
Step 1: Download Free Spire.Doc for Java and unzip it.
This a the community version of Spire.Doc for Java and there are some limitations on pages during the process. If you want to get rid of them, download the commercial product from this link and apply for a temporary license.
It supports create or edit Word files independently on Java applications. With it, you can also convert Word to PDF, convert Word to Image, merge Word documents, etc.
Step 2: Create a new Java project.
Step 3: Import “Spire.Doc.jar” to your project
Take IntelliJ IDEA 2018 (jdk 1.8.0) for example.
- Click “File”- “Project Structure”- “Modules”- “Dependencies” in turn.
- Choose the “JARs or Directories” under the green plus.
- Find the “Spire.Doc.jar” in the lib folder of the decompressed package and import it to the project.
Step 4: Sample code.
import com.spire.doc.BuiltinDocumentProperties;
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
public class AddDocumentProperties {
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("Document Properties");
standardProperties.setSubject("How to add properties");
standardProperties.setAuthor("Naomi");
standardProperties.setCompany("Co., Ltd");
standardProperties.setManager("Michael");
standardProperties.setCategory("Document");
standardProperties.setKeywords("Word, Document, Properties");
standardProperties.setComments("This is a good tutorial.");
//Save the result document
document.saveToFile("AddProperties.docx", FileFormat.Docx_2013);
}
}
In the above code, first create a Document instance and load a Word document. Then, by calling the getBuiltinDocumentProperties() method, you can access the document's built-in document properties. Next, use the set method to individually set the value of specific built-in document properties. Finally, save the modified document as a new file using the Document.saveToFile() method.

Top comments (0)