itext制作pdf报表

            最近在公司维护一个项目,这个项目是使用itext1.3制作报表的(估计已近是古董了,现在itext5.5都出来了)。

            问题:使用adobe acrobat proffessional 7.0制作的模板文本域中不能填充 数据(默认字体情况下) 。

            根据字体选择的不同,文本域是可以填充数字和英文的,但还是不同填充 中文。

            而以前系统中的模板,中英文都能填充

            原因:itext1.3支持STsong-Light字体,也是说文本域要选择STsong-light字体,但是高版本的adobe acrobat中都也是有STsong-light字体,可能是字体的版本不同吧,反正不能填充进去。所以中文无法显示,adobe acrobat5.0中好像有这个字体

 

            解决:把以前项目中的模板中的 文本域 拷贝  到 你pdf模板中,加以修改 文本域  的名字,就可以正常使用显示中文了。

            

            使用itext.2.1.7,配合adobe acrobat proffessional 9.0是没有任何问题的,但是换成itext1.3不能显示中文

import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.lowagie.text.pdf.AcroFields;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;

public class ActionGetFrPolicyPdf {
	// 模板存放 的 目录
	public static final String frpolicypdfpath = "G:\\report_tool\\template";
	
	public static void main(String[] args) {
		Map<String,String> hm = new HashMap<String, String>(); 
		hm.put("fileName", "template");
		hm.put("name", "高伟刚");
		hm.put("address", "湖北武汉");
		hm.put("love", "basketball");
		
        try{
		String pdfpath = CreatePdf((HashMap<String,String>)hm);
        }catch(Exception e){
        	e.printStackTrace();
        }
		
	}
	
	public static String CreatePdf(HashMap<String, String> hm) throws Exception {
			
		String sysdate = GetSysdate();
		String year_sys = sysdate.substring(0, 4);
		String month_sys = sysdate.substring(5, 7);
		String day_sys = sysdate.substring(8, 10);

		String PdfFileName = frpolicypdfpath + "\\" + year_sys + month_sys
				+ day_sys + "_" +hm.get("love") + "_freepolicy.pdf";
		
		String PdfFileTemplate = GetPdfPath((String) hm.get("fileName"));
		PdfReader reader = new PdfReader(PdfFileTemplate);
		FileOutputStream fos = new FileOutputStream(PdfFileName);
		PdfStamper stamp = new PdfStamper(reader, fos);
		AcroFields fields = stamp.getAcroFields();
		for (Iterator it = fields.getFields().keySet().iterator(); it 
				.hasNext();) { 
				System.out.println("field:--"+it.next()); 
		} 
		
		/* 为字段赋值,注意字段名称是区分大小写的 */
		for(String key:hm.keySet()){
			fields.setField(key, hm.get(key));
		}
		stamp.setFormFlattening(true);
		stamp.close();
		reader = null;
		return PdfFileName;
	}

	public static String GetSysdate() {
		java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd");
		java.util.Date date = new java.util.Date();
		String sysdate = df.format(date);
		return sysdate;
	}
	// 获取pdf模板的全路径
	public static String GetPdfPath(String fileName) {
		String pdfpath = frpolicypdfpath + "\\"+ fileName + ".pdf";
		return pdfpath;
	}
}

 

        iText默认不支持亚洲语言,导致iText生成中文出现问题,一般都是中文内容不能出现在pdf上,这是因为没有中文字体的原因.

       要让iText支持中文,需要一个jar包:

                     iText语言包:iTextAsian.jar

但缺乏iTextAsian.jar时,可能就会报如下错误,不能将java程序中的中文  转换成 文本域中的字体(文本域中的字体是STsong-Light)



 
      

猜你喜欢

转载自weigang-gao.iteye.com/blog/2094285