Thursday, 19 August 2021

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

In a Word document, a variable is seen as a container storing information which may change frequently and using variables in source documents allows you to quickly and easily control the content in generated documents. This article will demonstrate how to use Java codes to add a variable to Word, count the number of variables in Word, retrieve the name and value of variables and remove specified variables with the help of Free Spire.Doc for Java.

BEFORE CODES

Before running codes, we need to create a running environment by installing JDK 1.8.0 and Intellij IDEA. The next thing is adding a Jar file to IDEA and there are two methods to do it. One is getting the package of the library from the link, finding Spire.Xls.jar in the “lib” folder and then manually adding it to IDEA. The other is that create a Maven project in IDEA, type the following codes in the pom.xml file and finally click the button “Import Changes”.

<repositories>
<repository>
<id>com.e-iceblue</id>
<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

RUNNING THE CODE

Add a Variable

The following code snippets add 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 resulting document
document.saveToFile("output/AddVariables.docx", FileFormat.Docx_2013);
}
}

Output


Count the number of Variables

import com.spire.doc.Document;
public class CountVariables {
public static void main(String[] args) {
//Load a Word document
Document document = new Document();
document.loadFromFile("C:\\Users\\Test1\\Desktop\\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());
}
}

Output


Retrieve Name and Value of a Variable

import com.spire.doc.Document;

public class RetrieveVariables {
public static void main(String[] args) {
//Load a Word document
Document document = new Document();
document.loadFromFile("C:\\Users\\Test1\\Desktop\\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);
}
}

Output


Remove a specific Variable

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

public class RemoveVariables {
public static void main(String[] args) {
//Load a Word document
Document document = new Document();
document.loadFromFile("C:\\Users\\Test1\\Desktop\\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("output/RemoveVariables.docx", FileFormat.Docx_2013);
}
}

No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...