Document Operation

Document Operation (12)

Java insert math equation to Word

Monday, 17 May 2021 03:38 Written by support iceblue

This article will demonstrate how to use Spire.Doc for Java to Latex Math equation and MathMLCode to Word document in Java applciations.

import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.omath.*;

public class wordEquation {
    public static void main(String[] args){
        //Create a new instance of Word and add a section
        Document doc = new Document();
        Section section = doc.addSection();
        //Add a paragraph
        Paragraph paragraph = section.addParagraph();

        //Add Latex Math code
        OfficeMath officeMath = new OfficeMath(doc);
        paragraph.getItems().add(officeMath);
        officeMath.fromLatexMathCode("x^{2}+\\sqrt{{x^{2}+1}}+1");

        //Add another paragraph
        Paragraph paragraph2 = section.addParagraph();

        //Add MathMLCode
        OfficeMath officeMath2 = new OfficeMath(doc);
        paragraph2.getItems().add(officeMath2);
        officeMath2.fromMathMLCode("<mml:math xmlns:mml=\"http://www.w3.org/1998/Math/MathML\" xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\"><mml:msup><mml:mrow><mml:mi>x</mml:mi></mml:mrow><mml:mrow><mml:mn>2</mml:mn></mml:mrow></mml:msup><mml:mo>+</mml:mo><mml:msqrt><mml:msup><mml:mrow><mml:mi>x</mml:mi></mml:mrow><mml:mrow><mml:mn>2</mml:mn></mml:mrow></mml:msup><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:msqrt><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:math>");

        //Save the document to file
        String result = "output/addMathEquation.docx";
        doc.saveToFile(result, FileFormat.Docx_2013);
    }
}

Output after inserting math code to Word document:

Java insert math equation to Word

Compare Two Word Documents in Java

Monday, 31 August 2020 06:51 Written by support iceblue

In this article, we'll introduce how to compare two Word documents and find the differences between them by using Spire.Doc for Java.

The input Word documents look as follows:

Compare Two Word Documents in Java

import com.spire.doc.Document;

public class CompareWordDocuments {
    public static void main(String[] args) throws Exception {
        //Create a Document instance
        Document doc1 = new Document();
        //Load the first Word document
        doc1.loadFromFile("Doc1.docx");

        //Create a Document instance
        Document doc2 = new Document();
        //Load the second Word document
        doc2.loadFromFile("Doc2.docx");

        //Compare the two Word documents
        doc1.compare(doc2, "E-iceblue");

        //Save the result to file
        doc1.saveToFile("Result.docx");
    }
}

Output:

Compare Two Word Documents in Java

This article demonstrates how to add, count, retrieve and remove variables in a Word document in Java using Spire.Doc for Java library.

Add a Variable

The following example adds a document variable named "A1" with a value of 12 to a Word document.

import com.spire.doc.Document;
import com.spire.doc.FieldType;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;

public class AddVariables {
    public static void main(String[] args){
        //Create a Document instance
        Document document = new Document();
        //Add a section
        Section section = document.addSection();

        //Add a paragraph to the section
        Paragraph paragraph = section.addParagraph();

        //Add a DocVariable field to the paragraph
        paragraph.appendField("A1", FieldType.Field_Doc_Variable);

        //Add a document variable to the DocVariable field
        document.getVariables().add("A1", "12");

        //Update fields in the document
        document.isUpdateFields(true);
        
        //Save the result document
        document.saveToFile("AddVariables.docx", FileFormat.Docx_2013);
    }
}

Add, Count, Retrieve and Remove Variables in Word in Java

Count the number of Variables

import com.spire.doc.Document;

public class CountVariables {
    public static void main(String[] args){
        //Load the Word document
        Document document = new Document();
        document.loadFromFile("AddVariables.docx");

        //Get the number of variables in the document
        int number = document.getVariables().getCount();

        StringBuilder content = new StringBuilder();
        content.append("The number of variables is: " + number);

        System.out.println(content.toString());
    }
}

Add, Count, Retrieve and Remove Variables in Word in Java

Retrieve Name and Value of a Variable

import com.spire.doc.Document;

public class RetrieveVariables {
    public static void main(String[] args){
        //Load the Word document
        Document document = new Document();
        document.loadFromFile("AddVariables.docx");

        //Retrieve the name of a variable by index
        String s1 = document.getVariables().getNameByIndex(0);

        //Retrieve the value of a variable by index
        String s2 = document.getVariables().getValueByIndex(0);

        //Retrieve the value of a variable by name
        String s3 = document.getVariables().get("A1");

        System.out.println("The name of the variable retrieved by index 0 is: " + s1);
        System.out.println("The value of the variable retrieved by index 0 is: " + s2);
        System.out.println("The value of the variable retrieved by name \"A1\" is: " + s3);
    }
}

Add, Count, Retrieve and Remove Variables in Word in Java

Remove a specific Variable

import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class RemoveVariables {
    public static void main(String[] args){
        //Load the Word document
        Document document = new Document();
        document.loadFromFile("AddVariables.docx");

        //Remove a variable by name
        document.getVariables().remove("A1");
        
        //Update fields in the document
        document.isUpdateFields (true);
        
        //Save the result document
        document.saveToFile("RemoveVariables.docx", FileFormat.Docx_2013);
    }
}

This article demonstrates how to insert math equations i.e. Latex and MathML equations and Symbols in a Word document in Java using Spire.Doc for Java.

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.Table;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.omath.OfficeMath;

public class AddMathEquationsAndSymbols {
    public static void main(String[] args){
        //Latex code
        String[] latexMathCode1 = {
                "x^{2}+\\sqrt{{x^{2}+1}}+1",
                "2\\alpha - \\sin y + x",
        };

        //MathML code
        String[] mathMLCode = {
                "<mml:math xmlns:mml=\"http://www.w3.org/1998/Math/MathML\" xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\"><mml:msup><mml:mrow><mml:mi>x</mml:mi></mml:mrow><mml:mrow><mml:mn>2</mml:mn></mml:mrow></mml:msup><mml:mo>+</mml:mo><mml:msqrt><mml:msup><mml:mrow><mml:mi>x</mml:mi></mml:mrow><mml:mrow><mml:mn>2</mml:mn></mml:mrow></mml:msup><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:msqrt><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:math>",
                "<mml:math xmlns:mml=\"http://www.w3.org/1998/Math/MathML\" xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\"><mml:mn>2</mml:mn><mml:mi>α</mml:mi><mml:mo>-</mml:mo><mml:mi>s</mml:mi><mml:mi>i</mml:mi><mml:mi>n</mml:mi><mml:mi>y</mml:mi><mml:mo>+</mml:mo><mml:mi>x</mml:mi></mml:math>",
        };

        //Latex code
        String[] latexMathCode2 = {
                "\\alpha",
                "\\beta",
        };

        //Create a Document instance
        Document doc = new Document();
        //Load the Word document
        doc.loadFromFile("MathEquationTemplate.docx");
        //Get the first section
        Section section = doc.getSections().get(0);

        Paragraph paragraph = null;
        OfficeMath officeMath;

        //Insert Latex equations
        Table table1 = section.getTables().get(0);
        for (int i = 0; i < latexMathCode1.length; i++) {
            paragraph = table1.getRows().get(i + 1).getCells().get(0).addParagraph();
            paragraph.setText(latexMathCode1[i]);
            paragraph = table1.getRows().get(i + 1).getCells().get(1).addParagraph();
            officeMath = new OfficeMath(doc);
            officeMath.fromLatexMathCode(latexMathCode1[i]);
            paragraph.getItems().add(officeMath);
        }

        //Insert MathML equations
        Table table2 = section.getTables().get(1);
        for (int i = 0; i < mathMLCode.length; i++) {
            paragraph = table2.getRows().get(i + 1).getCells().get(0).addParagraph();
            paragraph.setText(mathMLCode[i]);
            paragraph = table2.getRows().get(i + 1).getCells().get(1).addParagraph();
            officeMath = new OfficeMath(doc);
            officeMath.fromMathMLCode(mathMLCode[i]);
            paragraph.getItems().add(officeMath);
        }

        //Insert Symbols
        Table table3 = section.getTables().get(2);
        for (int i = 0; i < latexMathCode2.length; i++) {
            //Insert Latex code to the first column of the table
            paragraph = table3.getRows().get(i + 1).getCells().get(0).addParagraph();
            paragraph.setText(latexMathCode2[i]);
            //Insert symbols to the second column of the table
            paragraph = table3.getRows().get(i + 1).getCells().get(1).addParagraph();
            officeMath = new OfficeMath(doc);
            officeMath.fromLatexMathCode(latexMathCode2[i]);
            paragraph.getItems().add(officeMath);
        }

        //Save the document
        String result = "addMathEquationAndSymbols.docx";
        doc.saveToFile(result, FileFormat.Docx_2013);
    }
}

Output:

Insert Math Equations and Symbols in Word Document in Java

This article demonstrrates how to accept or reject all tracked changes in a Word document using Spire.XLS for Java.

Sample Document

Accept or Reject Tracked Changes in Word in Java

import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class AcceptOrRejectChanges {

    public static void main(String[] args) {

        //Create a Document object
        Document doc = new Document();
        
        //Load the sample Word file
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.docx");

        //Accept all changes in the entire document
        doc.acceptChanges();
        
        ////Reject all changes in the entire document
        //doc.rejectChanges();
        
        //Save the document
        doc.saveToFile("AcceptAllChanges.docx", FileFormat.Docx);
    }
}

Output

Accept or Reject Tracked Changes in Word in Java

Create Table of Contents in Word in Java

Thursday, 22 August 2019 09:54 Written by support iceblue

This article demonstrates how to create table of contents in a Word document using Spire.Doc for Java.

The following example shows how to create a table of contents with default appearance that includes all text formatted with built-in styles Heading 1, Heading 2, Heading 3, and page numbers right-aligned with tab leaders.

import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;

import java.awt.*;

public class TableofContents {
    public static void main(String[] args){
        //instantiate a Document object
        Document doc = new Document();
        //add a section
        Section section = doc.addSection();

        //add a paragraph
        Paragraph para = section.addParagraph();
        TextRange tr = para.appendText("Table of Contents");
        //set font size and text color
        tr.getCharacterFormat().setFontSize(11);
        tr.getCharacterFormat().setTextColor(Color.blue);       
        //set the space after the paragraph
        para.getFormat().setAfterSpacing(10);

        //add a paragraph
        para = section.addParagraph();
        //add a table of contents with default appearance by specifying lower heading level and upper heading level. The heading level range must be from 1 to 9.
        para.appendTOC(1, 3);

        //add a new section
        section = doc.addSection();
        //add a paragraph
        para = section.addParagraph();
        para.appendText("Heading 1");
        //apply Heading 1 style to the paragraph
        para.applyStyle(BuiltinStyle.Heading_1);
        section.addParagraph();

        //add a paragraph
        para = section.addParagraph();
        para.appendText("Heading 2");
        //apply Heading 2 style to the paragraph
        para.applyStyle(BuiltinStyle.Heading_2);
        section.addParagraph();

        //add a paragraph
        para = section.addParagraph();
        para.appendText("Heading 3");
        //apply Heading 3 style to the paragraph
        para.applyStyle(BuiltinStyle.Heading_3);
        section.addParagraph();

        //update Table of Contents
        doc.updateTableOfContents();

        //save the resultant document
        doc.saveToFile("createTableOfContents.docx", FileFormat.Docx);
    }
}

Output:

Create Table of Contents in Word in Java

We can also create a custom table of contents and determine what entries to appear in the table of contents by using TOC switches. The following example shows how to create a custom table of contents that includes all text formatted with built-in styles Heading 1, Heading 2 and Heading 3 but omits page numbers from heading levels 1-3.

import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;

import java.awt.*;

public class TableofContents {
    public static void main(String[] args){
        //instantiate a Document object
        Document doc = new Document();
        //add a section
        Section section = doc.addSection();

        //add a paragraph
        Paragraph para = section.addParagraph();
        TextRange tr = para.appendText("Table of Contents");
        //set font size and text color
        tr.getCharacterFormat().setFontSize(11);
        tr.getCharacterFormat().setTextColor(Color.blue);
        //set the space after the paragraph
        para.getFormat().setAfterSpacing(10);

        //create a custom table of contents that omits page numbers from heading levels 1-3.
        TableOfContent toc = new TableOfContent(doc, "{\\o \"1-3\" \\n 1-3}");
        para = section.addParagraph();
        para.getItems().add(toc);
        para.appendFieldMark(FieldMarkType.Field_Separator);
        para.appendText("TOC");
        para.appendFieldMark(FieldMarkType.Field_End);
        doc.setTOC(toc);

        //add a new section
        section = doc.addSection();
        //add a paragraph
        para = section.addParagraph();
        para.appendText("Heading 1");
        //apply Heading 1 style to the paragraph
        para.applyStyle(BuiltinStyle.Heading_1);
        section.addParagraph();

        //add a paragraph
        para = section.addParagraph();
        para.appendText("Heading 2");
        //apply Heading 2 style to the paragraph
        para.applyStyle(BuiltinStyle.Heading_2);
        section.addParagraph();

        //add a paragraph
        para = section.addParagraph();
        para.appendText("Heading 3");
        //apply Heading 3 style to the paragraph
        para.applyStyle(BuiltinStyle.Heading_3);
        section.addParagraph();

        //update Table of Contents
        doc.updateTableOfContents();

        //save the resultant document
        doc.saveToFile("customTableOfContents.docx", FileFormat.Docx);

    }
}

Output:

Create Table of Contents in Word in Java

This article demonstrates how to add combo box, check box, text, picture, date picker and drop-down list content controls to a Word document using Spire.Doc for Java.

import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;

import java.util.Date;

public class ContentControls {
    public static void main(String[] args){
        //create a new Word document
        Document document = new Document();
        Section section = document.addSection();
        Paragraph paragraph = section.addParagraph();
        TextRange txtRange = paragraph.appendText("The following example shows how to add content controls in a Word document.");
        section.addParagraph();

        //add combo box content control
        paragraph = section.addParagraph();
        txtRange = paragraph.appendText("Add Combo Box Content Control:  ");
        txtRange.getCharacterFormat().setItalic(true);
        StructureDocumentTagInline sd = new StructureDocumentTagInline(document);
        paragraph.getChildObjects().add(sd);
        sd.getSDTProperties().setSDTType(SdtType.Combo_Box);
        sd.getSDTProperties().setAlias("ComboBox");
        sd.getSDTProperties().setTag("ComboBox");
        SdtComboBox cb = new SdtComboBox();
        cb.getListItems().add(new SdtListItem("Spire.Doc"));
        cb.getListItems().add(new SdtListItem("Spire.XLS"));
        cb.getListItems().add(new SdtListItem("Spire.PDF"));
        sd.getSDTProperties().setControlProperties(cb);
        TextRange rt = new TextRange(document);
        rt.setText(cb.getListItems().get(0).getDisplayText());
        sd.getSDTContent().getChildObjects().add(rt);
        section.addParagraph();

        //Add checkbox content control
        paragraph = section.addParagraph();
        txtRange = paragraph.appendText("Add Check Box Content Control:  ");
        txtRange.getCharacterFormat().setItalic(true);
        sd = new StructureDocumentTagInline(document);
        paragraph.getChildObjects().add(sd);
        sd.getSDTProperties().setSDTType(SdtType.Check_Box);
        sd.getSDTProperties().setAlias("CheckBox");
        sd.getSDTProperties().setTag("CheckBox");
        SdtCheckBox scb = new SdtCheckBox();
        sd.getSDTProperties().setControlProperties(scb);
        rt = new TextRange(document);
        sd.getChildObjects().add(rt);
        scb.setChecked(true);
        section.addParagraph();

        //add text content control
        paragraph = section.addParagraph();
        txtRange = paragraph.appendText("Add Text Content Control:  ");
        txtRange.getCharacterFormat().setItalic(true);
        sd = new StructureDocumentTagInline(document);
        paragraph.getChildObjects().add(sd);
        sd.getSDTProperties().setSDTType(SdtType.Text);
        sd.getSDTProperties().setAlias("Text");
        sd.getSDTProperties().setTag("Text");
        SdtText text = new SdtText(true);
        text.isMultiline(true);
        sd.getSDTProperties().setControlProperties(text);
        rt = new TextRange(document);
        rt.setText("Text");
        sd.getSDTContent().getChildObjects().add(rt);
        section.addParagraph();

        //add picture content control
        paragraph = section.addParagraph();
        txtRange = paragraph.appendText("Add Picture Content Control:  ");
        txtRange.getCharacterFormat().setItalic(true);
        sd = new StructureDocumentTagInline(document);
        paragraph.getChildObjects().add(sd);
        sd.getSDTProperties().setControlProperties(new SdtPicture());
        sd.getSDTProperties().setAlias("Picture");
        sd.getSDTProperties().setTag("Picture");
        DocPicture pic = new DocPicture(document);
        pic.setWidth(10f);
        pic.setHeight(10f);
        pic.loadImage("logo.png");
        sd.getSDTContent().getChildObjects().add(pic);
        section.addParagraph();

        //add date picker content control
        paragraph = section.addParagraph();
        txtRange = paragraph.appendText("Add Date Picker Content Control:  ");
        txtRange.getCharacterFormat().setItalic(true);
        sd = new StructureDocumentTagInline(document);
        paragraph.getChildObjects().add(sd);
        sd.getSDTProperties().setSDTType(SdtType.Date_Picker);
        sd.getSDTProperties().setAlias("Date");
        sd.getSDTProperties().setTag("Date");
        SdtDate date = new SdtDate();
        date.setCalendarType(CalendarType.Default);
        date.setDateFormat("yyyy.MM.dd");
        date.setFullDate(new Date());
        sd.getSDTProperties().setControlProperties(date);
        rt = new TextRange(document);
        rt.setText("2018.12.25");
        sd.getSDTContent().getChildObjects().add(rt);
        section.addParagraph();

        //add drop-down list content control
        paragraph = section.addParagraph();
        txtRange = paragraph.appendText("Add Drop-Down List Content Control:  ");
        txtRange.getCharacterFormat().setItalic(true);
        sd = new StructureDocumentTagInline(document);
        paragraph.getChildObjects().add(sd);
        sd.getSDTProperties().setSDTType(SdtType.Drop_Down_List);
        sd.getSDTProperties().setAlias("DropDownList");
        sd.getSDTProperties().setTag("DropDownList");
        SdtDropDownList sddl = new SdtDropDownList();
        sddl.getListItems().add(new SdtListItem("Harry"));
        sddl.getListItems().add(new SdtListItem("Jerry"));
        sd.getSDTProperties().setControlProperties(sddl);
        rt = new TextRange(document);
        rt.setText(sddl.getListItems().get(0).getDisplayText());
        sd.getSDTContent().getChildObjects().add(rt);

        //save the resultant document
        document.saveToFile("addContentControls.docx", FileFormat.Docx_2013);
    }
}

Output:

Add Content Controls to Word Document in Java

We can use page break to split a page into two pages and use section break to start a new section. This article demonstrates how to insert page break and section break into a Word document using Spire.Doc for Java.

Insert page break

The following code example shows how to insert page break to a Word document by using the appendBreak method in Paragraph class.

import com.spire.doc.*;
import com.spire.doc.documents.*;

public class SectionBreakAndPageBreak {
    public static void main(String[] args){
        //load Word document
        Document document = new Document();
        document.loadFromFile("Input.docx");

        //get the first section
        Section section = document.getSections().get(0);
        //add page break to paragraph 4
        Paragraph paragraph = section.getParagraphs().get(3);
        paragraph.appendBreak(BreakType.Page_Break);

        //save the resultant document
        document.saveToFile("AddPageBreak.docx", FileFormat.Docx_2013);
    }
}

Output:

Insert Page Break and Section Break in Word Document in Java

Insert Section break

The following code example shows how to insert section break to a Word document by using the insertSectionBreak method in Paragraph class.

import com.spire.doc.*;
import com.spire.doc.documents.*;

public class SectionBreakAndPageBreak {
    public static void main(String[] args){
        //load Word document
        Document document = new Document();
        document.loadFromFile("Input.docx");

        //get the first section
        Section section = document.getSections().get(0);
        
        //add a section break to paragraph 4 and start the new section on the next page
        Paragraph paragraph = section.getParagraphs().get(3);
        paragraph.insertSectionBreak(SectionBreakType.New_Page);

        //save the resultant document
        document.saveToFile("AddSectionBreak.docx", FileFormat.Docx_2013);
    }
}

Output:

Insert Page Break and Section Break in Word Document 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);
    }
}

Spire.Doc for Java supports adding the following two types of document properties to Word document:

  • Built-in document properties
  • Custom document properties

In this article, we will see how to use Spire.Doc for Java to add the above document properties into a Word document.

Add 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("Input.docx");

        //Add built-in document properties to the Word document
        document.getBuiltinDocumentProperties().setTitle("Document Demo Document");
        document.getBuiltinDocumentProperties().setSubject("demo");
        document.getBuiltinDocumentProperties().setAuthor("James");
        document.getBuiltinDocumentProperties().setCompany("e-iceblue");
        document.getBuiltinDocumentProperties().setManager("Jakson");
        document.getBuiltinDocumentProperties().setCategory("Doc Demos");
        document.getBuiltinDocumentProperties().setKeywords("Document, Property, Demo");
        document.getBuiltinDocumentProperties().setComments("This document is just a demo.");

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

Add Document Properties to Word Document in Java

Add 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("Input.docx");

        //Add custom document properties to the Word document
        document.getCustomDocumentProperties().add("TrackingID", "AB01");
        document.getCustomDocumentProperties().add("Checked By", "Wilson");

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

    }
}

Add Document Properties to Word Document in Java