1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| package test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import net.sourceforge.rtf.RTFTemplate;
import net.sourceforge.rtf.UnsupportedRTFTemplate;
import net.sourceforge.rtf.helper.RTFTemplateBuilder;
import net.sourceforge.rtf.helper.test.RTFTemplateWithVelocityAndXmlFields;
public class genererTemplateRtf {
public static void main(String[] args){
String rtfSource = "C:/Sample.rtf";
String rtfTarget = "jakarta-velocity-model.rtf.rtf";
// 1. Get default RTFtemplateBuilder
RTFTemplateBuilder builder = RTFTemplateBuilder.newRTFTemplateBuilder();
// 2. Get RTFtemplate with default Implementation of template engine (Velocity)
RTFTemplate rtfTemplate = null;
try {
rtfTemplate = builder.newRTFTemplate();
} catch (UnsupportedRTFTemplate e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 3 Load XML fields available and set it to the RTFTemplate
InputStream xmlFieldsStream = RTFTemplateWithVelocityAndXmlFields.class.getResourceAsStream("test.fields.xml");
rtfTemplate.setXmlFields(xmlFieldsStream);
// 4. Set the RTF model source
try {
rtfTemplate.setTemplate(new File(rtfSource));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 5. Put the context
rtfTemplate.put("champTest", "kokokokokoko");
// 6. Merge the RTF source model and the context
try {
rtfTemplate.merge(rtfTarget);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} |
Partager