Example of mail Merging (Address Example) using RTFTemplate library.
Download the RTF sample and Template file in rtf format.
Add the required classes.
References:-
http://sourceforge.net/projects/rtftemplate/
http://rtftemplate.sourceforge.net/
- Add required JARS in class path.
Required JAR:
commons-collections-3.2.1.jar
commons-digester-2.1.jar
commons-lang.jar
commons-logging-1.1.1.jar
freemarker.jar
org.apache.commons.beanutils.jar
rtftemplate-1.0.1-b14.jar
spring-beans-3.2.2.RELEASE.jar
spring-context-3.2.2.RELEASE.jar
spring-context-support-3.2.2.RELEASE.jar
spring-core-3.2.2.RELEASE.jar
spring-expression-3.2.2.RELEASE.jar
spring-tx-3.2.2.RELEASE.jar
velocity-1.7.jar
xml-apis-1.0.b2.jar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sb.rtf; | |
public class Address { | |
private String typeOfAddress; | |
private String address; | |
private int pinCode; | |
public int getPinCode() { | |
return pinCode; | |
} | |
public void setPinCode(int pinCode) { | |
this.pinCode = pinCode; | |
} | |
public String getTypeOfAddress() { | |
return typeOfAddress; | |
} | |
public void setTypeOfAddress(String typeOfAddress) { | |
this.typeOfAddress = typeOfAddress; | |
} | |
public String getAddress() { | |
return address; | |
} | |
public void setAddress(String address) { | |
this.address = address; | |
} | |
public Address(String typeOfAddress, String address, int i) { | |
super(); | |
this.typeOfAddress = typeOfAddress; | |
this.address = address; | |
this.pinCode = i; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sb.rtf; | |
import java.util.List; | |
public class ContractPartner { | |
private final String businessPartnerName; | |
private final List<Address> businessPartnerAddreses; | |
private final String businessPartnerWebsite; | |
private final List<Partner> partners; | |
private final List<Role> roles; | |
private final String contractWith; | |
public String getBusinessPartnerName() { | |
return businessPartnerName; | |
} | |
public List<Address> getBusinessPartnerAddreses() { | |
return businessPartnerAddreses; | |
} | |
public String getBusinessPartnerWebsite() { | |
return businessPartnerWebsite; | |
} | |
public ContractPartner(String businessPartnerName, | |
List<Address> businessPartnerAddreses, | |
String businessPartnerWebsite, List<Partner> partners, | |
String contractWith,List<Role> roles) { | |
super(); | |
this.businessPartnerName = businessPartnerName; | |
this.businessPartnerAddreses = businessPartnerAddreses; | |
this.businessPartnerWebsite = businessPartnerWebsite; | |
this.partners = partners; | |
this.contractWith = contractWith; | |
this.roles = roles; | |
} | |
public List<Partner> getPartners() { | |
return partners; | |
} | |
public String getContractWith() { | |
return contractWith; | |
} | |
public List<Role> getRoles() { | |
return roles; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sb.rtf; | |
import java.sql.SQLException; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class ContractPartnerProvider { | |
@SuppressWarnings("unchecked") | |
public static List<ContractPartner> getBusinessPartner() | |
throws SQLException { | |
List<ContractPartner> contractPartner = new ArrayList<ContractPartner>(); | |
List<Address> adresses = new ArrayList<Address>(); | |
Address address = new Address("Work", | |
"540, Kalbadevi Rd, Wagh wadi, Kalbadevi Mumbai 022 6622 5454", | |
400096); | |
adresses.add(address); | |
address = new Address("Home", | |
"640 , Wagh wadi, Kalbadevi Mumbai 022 6622 5454", 401090); | |
adresses.add(address); | |
List<Role> roles =new ArrayList<Role>(); | |
roles.add(new Role("Editor")); | |
roles.add(new Role("writer")); | |
roles.add(new Role("Author")); | |
ContractPartner bp = new ContractPartner("Suraj Bhambhani", adresses, | |
"www.sbit.com",getPartner(), "SB IT Solutions",roles); | |
contractPartner.add(bp); | |
return contractPartner; | |
} | |
public static List<Partner> getPartner() { | |
List<Partner> partners = new ArrayList<Partner>(); | |
Partner partner = new Partner("Susen", "susen@gmail.com", "Instructor"); | |
partners.add(partner); | |
partner = new Partner("Saush", "saushh@gmail.com", "Super Instructor"); | |
partners.add(partner); | |
return partners; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sb.rtf; | |
public class Partner { | |
private String partnerName; | |
private String partnerEmail; | |
private String partnerRole; | |
public String getPartnerName() { | |
return partnerName; | |
} | |
public void setPartnerName(String partnerName) { | |
this.partnerName = partnerName; | |
} | |
public String getPartnerEmail() { | |
return partnerEmail; | |
} | |
public void setPartnerEmail(String partnerEmail) { | |
this.partnerEmail = partnerEmail; | |
} | |
public String getPartnerRole() { | |
return partnerRole; | |
} | |
public void setPartnerRole(String partnerRole) { | |
this.partnerRole = partnerRole; | |
} | |
public Partner(String partnerName, String partnerEmail, String partnerRole) { | |
super(); | |
this.partnerName = partnerName; | |
this.partnerEmail = partnerEmail; | |
this.partnerRole = partnerRole; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sb.rtf; | |
public class Role { | |
private final String role; | |
public Role(String role) { | |
this.role = role; | |
} | |
public String getRole() { | |
return role; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sb.rtf; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import net.sourceforge.rtf.RTFTemplate; | |
import net.sourceforge.rtf.UnsupportedRTFTemplate; | |
import net.sourceforge.rtf.helper.RTFTemplateBuilder; | |
public class RTFHandler { | |
public static String getRTFTemplateSource(String rtfSource) { | |
return rtfSource; | |
} | |
public static RTFTemplate getRTFTemplate(String rtfSource) | |
throws UnsupportedRTFTemplate, FileNotFoundException { | |
// 1. Get default RTFtemplateBuilder | |
RTFTemplateBuilder builder = RTFTemplateBuilder.newRTFTemplateBuilder(); | |
// 2. Get RTFtemplate with default Implementation of template engine | |
RTFTemplate rtfTemplate = builder.newRTFTemplate(); | |
// 3. Set the RTF model source | |
rtfTemplate.setTemplate(new File(rtfSource)); | |
return rtfTemplate; | |
} | |
public static void putList(RTFTemplate rtfTemplate,String source,Object object) { | |
rtfTemplate.put(source,object ); | |
} | |
public static void putData(RTFTemplate rtfTemplate,String source,String destination) { | |
rtfTemplate.put(source, destination); | |
} | |
public static void generateFiles(RTFTemplate rtfTemplate, | |
String name) throws Exception { | |
// 4. Put the context. | |
String rtfTarget = "Contract_" + name + ".rtf"; | |
// 5. Merge the RTF source model and the context | |
rtfTemplate.merge(rtfTarget); | |
} | |
public static void generateFile(RTFTemplate rtfTemplate, String string) throws Exception { | |
String rtfTarget = "Project_.rtf"; | |
// 5. Merge the RTF source model and the context | |
rtfTemplate.merge(rtfTarget); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<beans xmlns="http://www.springframework.org/schema/beans" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" | |
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" | |
xmlns:mvc="http://www.springframework.org/schema/mvc" | |
xsi:schemaLocation="http://www.springframework.org/schema/beans | |
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd | |
http://www.springframework.org/schema/context | |
http://www.springframework.org/schema/context/spring-context-3.0.xsd | |
http://www.springframework.org/schema/tx | |
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd | |
http://www.springframework.org/schema/mvc | |
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> | |
<!-- ********************************************************************** | |
********************* RTFTEMPLTE IMPLEMENTATION ********************* | |
********************************************************************** --> | |
<!-- Defautlt RTFTemplate implementation with freemarker template engine --> | |
<bean id="ftlRTFTemplate" | |
class="net.sourceforge.rtf.RTFTemplate" | |
singleton="false" > | |
<property name="parser" ref="defaultRTFParser" /> | |
<property name="transformer" ref="ftlTransformer" /> | |
<property name="templateEngine" ref="ftl" /> | |
</bean> | |
<!-- Defautlt RTFTemplate implementation with velocity template engine --> | |
<bean id="vmRTFTemplate" | |
class="net.sourceforge.rtf.RTFTemplate" | |
singleton="false" > | |
<property name="parser" ref="defaultRTFParser" /> | |
<property name="transformer" ref="vmTransformer" /> | |
<property name="templateEngine" ref="vm" /> | |
</bean> | |
<!-- ********************************************************************** | |
********************* RTFDOCUMENT PARSER ********************* | |
********************************************************************** --> | |
<!-- Defautlt RTFDocument Parser --> | |
<bean id="defaultRTFParser" | |
class="net.sourceforge.rtf.handler.RTFDocumentHandler" | |
singleton="true" > | |
</bean> | |
<!-- ********************************************************************** | |
********************* FREEMARKER TEMPLATE ENGINE ********************* | |
********************************************************************** --> | |
<!-- Freemarker template engine --> | |
<bean id="ftl" | |
class="net.sourceforge.rtf.template.freemarker.FreemarkerTemplateEngineImpl" | |
singleton="false" > | |
<property name="freemarkerConfiguration" ref="ftlConfiguration" /> | |
</bean> | |
<!-- Freemarker Configuration --> | |
<bean id="ftlConfiguration" | |
class="freemarker.template.Configuration" | |
singleton="true" > | |
</bean> | |
<!-- Freemarker RTF Document Transformer --> | |
<bean id="ftlTransformer" | |
class="net.sourceforge.rtf.template.freemarker.RTFFreemarkerTransformerImpl" | |
singleton="true" > | |
</bean> | |
<!-- ********************************************************************** | |
********************* VELOCITY TEMPLATE ENGINE ********************* | |
********************************************************************** --> | |
<bean id="vm" | |
class="net.sourceforge.rtf.template.velocity.VelocityTemplateEngineImpl" | |
singleton="false" > | |
<property name="velocityEngine" ref="vmEngine" /> | |
</bean> | |
<!-- VelocityEngine Configuration --> | |
<bean id="vmEngine" | |
class="org.apache.velocity.app.VelocityEngine" | |
singleton="true" > | |
</bean> | |
<!-- Velocity RTF Document Transformer --> | |
<bean id="vmTransformer" | |
class="net.sourceforge.rtf.template.velocity.RTFVelocityTransformerImpl" | |
singleton="true" > | |
</bean> | |
</beans> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sb.rtf; | |
import java.util.List; | |
import net.sourceforge.rtf.RTFTemplate; | |
/** | |
* This class is to generate a sample contract using RTF Template. | |
* @author Suraj Bhambhani | |
* | |
*/ | |
public class SampleContractGenerator { | |
public static void main(String[] args) throws Exception { | |
// Source of file to be generated. | |
String rtfSource = "ContractTemplate.rtf"; | |
// Generate template. | |
RTFTemplate rtfTemplate = RTFHandler.getRTFTemplate(rtfSource); | |
// Business Partner | |
List<ContractPartner> contractPartners = ContractPartnerProvider | |
.getBusinessPartner(); | |
// Generate file. | |
generateFile(rtfTemplate, contractPartners); | |
// Desktop.getDesktop().open(new File("Contract_Example.rtf")); | |
} | |
private static void generateFile(RTFTemplate rtfTemplate, | |
List<ContractPartner> contractPartners) throws Exception { | |
RTFHandler.putList(rtfTemplate, "businessPartner", | |
(ContractPartner) contractPartners.get(0)); | |
RTFHandler.generateFiles(rtfTemplate, "SB IT Solution"); | |
} | |
} |
http://sourceforge.net/projects/rtftemplate/
http://rtftemplate.sourceforge.net/
No comments:
Post a Comment