Variables in Word documents are a type of field that is characterized by the ability of convenient and accurate text management, such as text replacement and deletion. Compared with the find-and-replace function, replacing text by assigning values to variables is faster and less error-prone. This article is going to show how to add or change variables in Word documents programmatically using Spire.Doc for Java.
Install Spire.Doc for Java
First, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc</artifactId> <version>12.10.3</version> </dependency> </dependencies>
Insert Variables into Word Documents
As variables are a kind of Word fields, we can use the Paragraph.appendField(String fieldName, FieldType.Field_Doc_Variable) method to insert variables into Word documents, and then use the VariableCollection.add() method to assign values to the variables. It should be noted that after assigning values to variables, document fields need to be updated to display the assigned values. The detailed steps are as follows.
- Create an object of Document.
- Add a section to the document using Document.addSection() method.
- Add a paragraph to the section using Section.addParagraph() method.
- Add variable fields to the paragraph using Paragraph.appendField(String fieldName, FieldType.Field_Doc_Variable) method.
- Get the variable collection using Document.getVariables() method.
- Assign a value to the variable using VariableCollection.add() method.
- Update the fields in the document using Document.isUpdateFields() method.
- Save the document using Document.saveToFile() method.
- Java
import com.spire.doc.*; import com.spire.doc.documents.Paragraph; import com.spire.doc.formatting.CharacterFormat; public class AddVariables { public static void main(String[] args) { //Create an object of Document Document document = new Document(); //Add a section Section section = document.addSection(); //Add a paragraph Paragraph paragraph = section.addParagraph(); //Set text format CharacterFormat characterFormat = paragraph.getStyle().getCharacterFormat(); characterFormat.setFontName("Times New Roman"); characterFormat.setFontSize(14); //Set the page margin section.getPageSetup().getMargins().setTop(80f); //Add variable fields to the paragraph paragraph.appendField("Term", FieldType.Field_Doc_Variable); paragraph.appendText(" is an object.\r\n"); paragraph.appendField("Term", FieldType.Field_Doc_Variable); paragraph.appendText(" is not a backdrop, an illusion, or an emergent phenomenon.\r\n"); paragraph.appendField("Term", FieldType.Field_Doc_Variable); paragraph.appendText(" has a physical size that be measured in laboratories."); //Get the variable collection VariableCollection variableCollection = document.getVariables(); //Assign a value to the variable variableCollection.add("Term", "Time"); //Update the fields in the document document.isUpdateFields(true); //Save the document document.saveToFile("AddVariables.docx", FileFormat.Auto); document.dispose(); } }
Change the Value of Variables in Word Documents
Spire.Doc for Java provides the VariableCollection.set() method to change the values of variables. And after updating fields in the document, all the occurrences of the variables will display the newly assigned value, thus achieving fast and accurate text replacement. The detailed steps are as follows.
- Create an object of Document.
- Load a Word document using Document.loaFromFile() method.
- Get the variable collection using Document.getVariables() method.
- Assign a new value to a specific variable through its name using VariableCollection.set() method.
- Update the fields in the document using Document.isUpdateFields() method.
- Save the document using Document.saveToFile() method.
- Java
import com.spire.doc.Document; import com.spire.doc.FileFormat; import com.spire.doc.VariableCollection; public class ChangeVariableValue { public static void main(String[] args) { //Create an object of Document Document document = new Document(); //Load a Word document document.loadFromFile("AddVariables.docx"); //Get the variable collection VariableCollection variableCollection = document.getVariables(); //Assign a new value to a variable variableCollection.set("Term", "The time"); //Update the fields in the document document.isUpdateFields(true); //Save the document document.saveToFile("ChangeVariable.docx", FileFormat.Auto); document.dispose(); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.