Add/ Count/ Retrieve/ Remove Variables in Word using Java
Tags: java, variables, word, free
Published: 2021-02-23 07:15:38

This article will give examples of how to add, count, retrieve and remove variables in a Word document in Java with a free Java Word API.

**【1】**Import the jar dependency of the free API to your Java application (2 methods)

● Download the free API ([Free Spire.Doc for Java](https://www.e-iceblue.com/Download/doc-for-java-free.html)) and unzip it, then add the Spire.Doc.jar file to your Java application as dependency.

● Directly add the jar dependency to maven project by adding the following configurations to the pom.xml.
```xml
<repositories>
   <repository>
      <id>com.e-iceblue</id>
      <name>e-iceblue</name>
      <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
   </repository>
</repositories>
<dependencies>
   <dependency>
      <groupId>e-iceblue</groupId>
      <artifactId>spire.doc.free</artifactId>
      <version>3.9.0</version>
   </dependency>
</dependencies>
```

**【2】Add a Variable**

Adding a document variable named "Temp" with a value of 12 to a Word document.
```java
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("Temp", FieldType.Field_Doc_Variable);

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

        //Update fields in the document
        document.isUpdateFields(true);

        //Save the result document
        document.saveToFile("AddVariables.docx", FileFormat.Docx_2013);
    }
}
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v2tw7qmtolbgvxwg9uzs.jpg)


**【3】Count the number of Variables**
```java
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());
    }
}
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8oz72vh9e4l2ev48w7xx.png)


**【4】Retrieve Name and Value of a Variable**
```java
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("Temp");

        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 \"Temp\" is: " + s3);
    }
}
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vm76faugxf38solei4jq.jpg)


**【5】Remove a specific Variable**
```java
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("Temp");

        //Update fields in the document
        document.isUpdateFields (true);

        //Save the result document
        document.saveToFile("RemoveVariables.docx", FileFormat.Docx_2013);
    }
}
```