Mail Merge (3)
When you run mail merge with a region, all merge fields within the region are repeated for each record in the data source. This is useful when you want to dynamically add rows to a Word table. In this article, you will learn how to perform mail merge with a region using Spire.Doc for Java.
Install Spire.Doc for Java
First of all, 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.3.1</version> </dependency> </dependencies>
Create a Template
To create a mail merge region, you need to specify the start point and the end point of the region. For example, the following Word template contains the region "Country" which is marked by «TableStart:Country» and «TableEnd:Country». Mail Merge will repeat that region for each record in the data source.
The following is the sample XML file that will be used as the data source.
- Package Manager
<?xml version="1.0" encoding="UTF-8"?> <Data> <Country> <Capital>Buenos Aires</Capital> <Name>Argentina</Name> <Continent>South America</Continent> <Area>2777815</Area> <Population>32300003</Population> </Country> <Country> <Capital>La Paz</Capital> <Name>Bolivia</Name> <Continent>South America</Continent> <Area>1098575</Area> <Population>7300000</Population> </Country> <Country> <Capital>Brasilia</Capital> <Name>Brazil</Name> <Continent>South America</Continent> <Area>8511196</Area> <Population>150400000</Population> </Country> <Country> <Capital>Buenos Aires</Capital> <Name>Argentina</Name> <Continent>South America</Continent> <Area>2777815</Area> <Population>32300003</Population> </Country> <Country> <Capital>La Paz</Capital> <Name>Bolivia</Name> <Continent>South America</Continent> <Area>1098575</Area> <Population>7300000</Population> </Country> </Data>
Preform Mail Merge with a Region
The following are the steps to preform mail merge with a region.
- Create a Document object.
- Load the Word template file using Document.loadFromFile() method.
- Execute mail merge with a region using Document.getMailMerge().executeWidthRegion() method.
- Save the changes to another file using Document.saveToFile() method.
- Java
import com.spire.doc.Document; import com.spire.doc.FileFormat; public class MailMergeWithRegions { public static void main(String[] args) throws Exception { //Create a Document object Document doc = new Document(); //Load the Word template file doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\MailMergeTemplate.docx"); //Execute mail merge with a region doc.getMailMerge().executeWidthRegion("C:\\Users\\Administrator\\Desktop\\Data.xml"); //Save the changes to another file doc.saveToFile("output/MailMergeWithRegions.docx", FileFormat.Docx_2013); } }
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.
This article demonstrates how to mail merge image in Word document in Java using Spire.Doc for Java.
The template document:
import com.spire.doc.Document; import com.spire.doc.FileFormat; import com.spire.doc.reporting.MergeImageFieldEventArgs; import com.spire.doc.reporting.MergeImageFieldEventHandler; import java.text.SimpleDateFormat; import java.util.Date; public class SimpleMailMerge { public static void main(String[] args) throws Exception { //create a Document instance Document document = new Document(); //load the template document document.loadFromFile("template - Copy.docx"); //specify the merge field name String[] filedNames = new String[]{"image"}; //specify the path of image String[] filedValues = new String[]{"logo.png"}; //invoke the mail merge event to load image document.getMailMerge().MergeImageField = new MergeImageFieldEventHandler() { public void invoke(Object sender, MergeImageFieldEventArgs args) { mailMerge_MergeImageField(sender, args); } }; //execute mail merge document.getMailMerge().execute(filedNames, filedValues); //save file document.saveToFile("MailMergeImage.docx", FileFormat.Docx_2013); } //create a mail merge event to load image private static void mailMerge_MergeImageField(Object sender, MergeImageFieldEventArgs field) { String filePath = field.getImageFileName(); if (filePath != null && !"".equals(filePath)) { try { field.setImage(filePath); } catch (Exception e) { e.printStackTrace(); } } } }
The output document:
This article will demonstrate how to create a mail merge template and then merge the text value to the template in Java application with the help of Spire.Doc.
import com.spire.doc.*; import com.spire.doc.documents.Paragraph; import java.text.SimpleDateFormat; import java.util.Date; public class MailMerge { public static void main(String[] args) throws Exception { String output = "output/mailMerge.docx"; //Create a Document instance Document document = new Document(); //Add a section Section section = document.addSection(); //Add 3 paragraphs to the section Paragraph para = section.addParagraph(); Paragraph para2 = section.addParagraph(); Paragraph para3 = section.addParagraph(); //Add mail merge templates to each paragraph para.setText("Contact Name: "); para.appendField("Contact Name", FieldType.Field_Merge_Field); para2.setText("Phone: "); para2.appendField("Phone", FieldType.Field_Merge_Field); para3.setText("Date: "); para3.appendField("Date", FieldType.Field_Merge_Field); //Set the value for the mail merge template by the field name Date currentTime = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateString = formatter.format(currentTime); String[] filedNames = new String[]{"Contact Name", "Phone", "Date"}; String[] filedValues = new String[]{"John Smith", "+1 (69) 123456", dateString}; //Merge the specified value into template document.getMailMerge().execute(filedNames, filedValues); //save the document to file document.saveToFile(output, FileFormat.Docx); } }
Effective screenshot for the mail merge: