Add, Count, Retrieve and Remove Variables in Word 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);
    }
}