Mail Merge in Java
- 3 minutes read - 527 wordsMailMerge is a process to create personalized letters and pre-addressed envelopes or mailing labels for mass mailings from a word processing document (Template). A template contains placeholders (Fields) along with the actual content. The placeholders are filled by the data source which is typically a spreadsheet or a database having column for each place holders.
This article talks about the java approach for generating multiple documents from a single MS Word template.
A typical mail merge
:
Microsoft Words generates individual documents, email or prints as a result of the mail merge. It does following :
- Process template w.r.t each record available in data source and generate content for each record.
- Generate individual documents or Email/Print the generated content.
Java Solution using MS Word Mail Merge.
We can use a 3rd party library “Aspose.Words for Java” which is an advanced class library for Java that enables you to perform a great range of document processing tasks directly within your Java applications. We can generate multiple documents from a single template in the same way as MS word does; output of this process is the documents, one for one record in data source, not the email or print. We can generate output documents in various formats (doc, docx, mhtml, msg etc) but for sending email or sending these documents to print, we need to develop custom solution.
Solution Diagram:
In the demo example, we want to generate some invitation letters for the given template.
We have following data and keys in the template
ContactName | ContactTitle | Address1 | CourseName |
---|---|---|---|
Kuldeep Singh | Professional | Address1 | Test |
User 2 | Technical operator | Test | Ready |
User 3 | Expert | Regular | Normal |
Here is the java file which generate one document for each record (MailMerge.java
) :
package com.aspose.words.demos;
import java.io.File;
import java.net.URI;
import java.text.MessageFormat;
import com.aspose.words.Document;
/**
* Demo Class.
*/
public class MailMerge
{
private static String [] fields = {"ContactName",
"ContactTitle",
"Address1",
"CourseName"};
private static Object[][] valueRows =
{{"Kuldeep Singh", "Professional", "Address1", "Test"},
{"User 2", "Technical operator", "Test", "Read"},
{"User 3", "Expert", "Regular", "Normal"}};
public static void main(String[] args) throws Exception
{
//Sample infrastructure.
URI exeDir = MailMerge.class.getResource("").toURI();
String dataDir = new File(exeDir.resolve("data")) + File.separator;
produceMultipleDocuments(dataDir, "CourseInvitationDemo.doc");
}
public static void produceMultipleDocuments(String dataDir, String srcDoc) throws Exception
{
// Open the template document.
Document doc = new Document(dataDir + srcDoc);
// Loop though all records in the data source.
for (int i = 0; i < valueRows.length; i++) {
// Clone the template instead of loading it from disk (for speed).
Document dstDoc = (Document)doc.deepClone(true);
// Execute mail merge.
dstDoc.getMailMerge().execute(fields, valueRows[i]);
dstDoc.save(MessageFormat.format(dataDir + "TestFile Out {0}.doc", i+1));
System.out.println("Done " + i);
}
}
}
It will generate
TestFile Out1.doc
TestFile Out2.doc
TestFile Out3.doc
and so on. “Aspose.Words” can generate these output documents in various formats and streams. We can develop logic to direct the data streams to email or print.
Aspose.Words
Aspose.Words for Java is an advanced class library for Java that enables you to perform a great range of document processing tasks directly within your Java applications. Aspose.Words for Java supports DOC, OOXML, RTF, HTML and OpenDocument formats. With Aspose.Words you can generate, modify, and convert documents without using Microsoft Word
References
- http://www.aspose.com/categories/java-components/aspose.words-for-java/default.aspx
- http://www.javaworld.com/javaworld/jw-10-2000/jw-1020-print.html
- http://java.sun.com/developer/onlineTraining/JavaMail/contents.html