`

RTFTemplate的初步使用

 
阅读更多
一、模版制作
1 设计 *.fields.xml文件,在此文件中定义好你要填充的参数。
<mergefield>
		<list>false</list>
		<name>$title</name>
		<description><![CDATA[]]></description>
	</mergefield>
	
	<mergefield>
		<list>false</list>
		<name>$serialNumber</name>
		<description><![CDATA[]]></description>
	</mergefield>


2 RTFTemplate.dot 设计rtf模版文档。注意,最好将rtf模版文档、RTFTemplate.dot设计器、*.fields.xml文件放在统一文件夹下。否则有可能找不到word加载项里的在*.fields.xml里设定的参数。

二、rtf服务类
package com.safetys.zjsg.util;

import java.io.File;
import java.io.FileInputStream;
import java.util.Map;
import net.sourceforge.rtf.RTFTemplate;
import net.sourceforge.rtf.context.RTFContextFieldsReader;
import net.sourceforge.rtf.context.RTFContextUtil;
import net.sourceforge.rtf.context.RTFXmlFieldsReader;
import net.sourceforge.rtf.document.RTFDocument;
import net.sourceforge.rtf.document.transformer.config.DigesterTransformerConfig;
import net.sourceforge.rtf.document.transformer.config.TransformerConfig;
import net.sourceforge.rtf.helper.RTFTemplateBuilder;
import net.sourceforge.rtf.template.IContext;
import org.springframework.context.ApplicationContext;
public class RTFGenerator {

	private Map<String, Object> contextMap;

	private RTFTemplate rtfTemplate = null;
	private File transformerConfigFile = null;

	private ApplicationContext applicationContext;
	private String rtfTemplateImpl;

	private boolean saveTransformedDocument = false;

	/**
	 * This value allow to group by content when there is PageBreak in order to group by content.
	 */
	private int groupByPerPageBreak = -1;

	/**
	 * Run RTFTemplate for merging rtfSource with the context putted with the method putContext which be must implement. After execution of this method, files rtfSource + ".<rtfTemplateImpl>.rtf"
	 * (RTF template implementation (vmRTFtemplate,...) and rtfSource + ".out.rtf" (RTF final with values of the context) will be generate.
	 * 
	 * @param rtfSource
	 *            RTF source model.
	 * @throws Exception
	 */
	public final void run(String rtfSource, String rtfTarget) throws Exception {

		File rtfSourceFile = new File(rtfSource);

		String rtfTransformedDocumentOutput = rtfSource + "." + getRtfTemplateImpl() + ".rtf";

		/**
		 * 1. Get RTFtemplate builder
		 */
		RTFTemplateBuilder builder = null;
		if (applicationContext == null)
			builder = RTFTemplateBuilder.newRTFTemplateBuilder();
		else
			builder = RTFTemplateBuilder.newRTFTemplateBuilder(applicationContext);

		/**
		 * 2. Get RTFtemplate with Implementation
		 */
		this.rtfTemplate = builder.newRTFTemplate(rtfTemplateImpl);
		this.rtfTemplate.setGroupByPerPageBreak(groupByPerPageBreak);

		/**
		 * 3. Put default format
		 */
		putDefaultFormat(rtfTemplate);

		/**
		 * 4. Create a common inner context - not required but showing how common context values can be re-used
		 */
		IContext ctx = rtfTemplate.getTemplateEngine().newContext();
		putGlobalContext(ctx);

		/**
		 * 5. Set the template
		 */
		rtfTemplate.setTemplate(rtfSourceFile);

		/**
		 * 6. Set Global Context
		 */
		rtfTemplate.setGlobalContext(ctx);

		/**
		 * 7. Set Transformer Config
		 */
		if (transformerConfigFile != null) {
			TransformerConfig transformConfig = DigesterTransformerConfig.getTransformerConfig(new FileInputStream(transformerConfigFile));
			rtfTemplate.setTransformerConfig(transformConfig);
		}

		/**
		 * 8. Put Context
		 */
		putContext(rtfTemplate.getContext());
		if (saveTransformedDocument) {
			RTFDocument transformedDocument = rtfTemplate.transform();
			transformedDocument.save(new File(rtfTransformedDocumentOutput));
		}

		/**
		 * 9. Merge template and context
		 */
		rtfTemplate.merge(rtfTarget);
	}

	/**
	 * This method must be implement by class wich manage your RTF model. Put the context of your model (eg : context("date", new Date()); )
	 * 
	 * @param context
	 *            IContext
	 */
	protected void putContext(IContext context) {
		for (String key : contextMap.keySet()) {
			context.put(key, contextMap.get(key));
		}
	}

	/**
	 * Return String XML Mergefields used in your context and Bookmarks (for start and end loop)
	 * 
	 * @return
	 */
	public String getXMLFields() {
		// XML
		RTFXmlFieldsReader reader = new RTFXmlFieldsReader();
		reader.readContext(rtfTemplate.getContext(), rtfTemplate.getTransformerConfig());
		return reader.getXMLFields();
	}

	protected void putDefaultFormat(RTFTemplate template) {
	}

	protected void putGlobalContext(IContext context) {
	}

	/**
	 * Save XML fields available into file. If force parameter is false, the file is updated with new context (by keeping just description) otherwise the file is crushed with new context.
	 * 
	 * @param filename
	 * @throws Exception
	 */
	public void saveXmlFields(String filename, boolean force) throws Exception {
		RTFContextFieldsReader reader = new RTFContextFieldsReader();
		reader.readContext(rtfTemplate.getContext(), rtfTemplate.getTransformerConfig());
		RTFContextUtil.saveXmlFields(filename, reader.getContextFields(), force);
	}

	public void setTransformerConfigFile(String transformerConfig) {
		setTransformerConfigFile(new File(transformerConfig));
	}

	public void setTransformerConfigFile(File transformerConfigFile) {
		this.transformerConfigFile = transformerConfigFile;
	}

	/**
	 * set true if RTF with (velocity, freemarker,... macro) file must be generated and false otherwise.
	 * 
	 * @param saveTransformedDocument
	 */
	public void saveTransformedDocument(boolean saveTransformedDocument) {
		this.saveTransformedDocument = saveTransformedDocument;
	}

	public String getRtfTemplateImpl() {
		if (rtfTemplateImpl == null) {
			this.rtfTemplateImpl = RTFTemplateBuilder.DEFAULT_VELOCITY_RTFTEMPLATE;
		}
		return rtfTemplateImpl;
	}

	public void setRtfTemplateImpl(String rtfTemplateImpl) {
		this.rtfTemplateImpl = rtfTemplateImpl;
	}

	protected int getGroupByPerPageBreak() {
		return groupByPerPageBreak;
	}

	/**
	 * This value allow to group by content when there is PageBreak in order to group by content.
	 * 
	 * @param groupByPerPageBreak
	 */
	protected void setGroupByPerPageBreak(int groupByPerPageBreak) {
		this.groupByPerPageBreak = groupByPerPageBreak;
	}

	protected RTFTemplate getRtfTemplate() {
		return rtfTemplate;
	}

	protected void setApplicationContext(ApplicationContext applicationContext) {
		this.applicationContext = applicationContext;
	}

	public void setContextMap(Map<String, Object> contextMap) {
		this.contextMap = contextMap;
	}

}



三、使用
Map<String, Object> contextMap = new HashMap<String, Object>();
		contextMap.put("title", title); //标题
		contextMap.put("serialNumber",serialNumber); //事故编号
//rtf生成工具
		RTFGenerator generator = new RTFGenerator();
		generator.setContextMap(contextMap);
		generator.run(rtfSource, rtfTarget);


四、注意事项
1 解析生成的rtf文件有可能是乱码,用文本编辑器将rtf模版文件打开,将
\f0\froman\fcharset0\fprq2{\*\panose 02020603050405020304}Times New Roman;替换成\f0\dbch\charset0\fprq2 宋体; 保存
分享到:
评论
1 楼 美味人间 2013-06-04  
根本找不到你说的
\f0\froman\fcharset0\fprq2{\*\panose 02020603050405020304}Times New Roman;
这个字符串

相关推荐

Global site tag (gtag.js) - Google Analytics