标签:des style blog http color io os ar 使用
itext操作pdf模板:
1、首先使用excel制作好模板文件,将模板文件转换为pdf格式文件。
2、使用Adobe Acrobat XI Pro工具打开pdf文件,在需要的位置添加文本域。(工具-->表单-->编辑)
3、对每个文本域设置文本域属性,为其命名。如title、desion_no等。
4、通过itext方式操作文本域,为期赋值,并重新生成pdf文件。
1 package com.pcm.app.transfer; 2 3 import java.io.ByteArrayOutputStream; 4 import java.io.File; 5 import java.io.FileOutputStream; 6 7 import org.apache.log4j.Logger; 8 9 import com.itextpdf.text.pdf.AcroFields; 10 import com.itextpdf.text.pdf.BaseFont; 11 import com.itextpdf.text.pdf.PdfReader; 12 import com.itextpdf.text.pdf.PdfStamper; 13 import com.itextpdf.text.pdf.PdfWriter; 14 import com.pcm.app.pojo.SimplePunishWrite; 15 import com.pcm.app.pojo.VioBehaviour; 16 import com.pcm.app.utils.CodeFactory; 17 import com.pcm.app.utils.TomcatUtil; 18 import com.pcm.framework.utils.InitUtil; 19 20 /** 21 * 使用pdf模板生成简易程序文件 22 * @author 23 * 24 */ 25 public class GeneratePDFService{ 26 27 private static Logger logger = Logger.getLogger(GeneratePDFService.class); 28 29 private static final String PDF_FILE_TYPE = ".pdf"; 30 31 private PdfStamper ps = null; 32 33 private FileOutputStream fos = null; 34 35 private String path = System.getProperty("user.dir"); 36 37 /** 38 * 冒号 39 */ 40 private static final String COLON = ":"; 41 42 private static final String URL_START = "http://"; 43 private static final String WEBAPPS = "/webapps"; 44 private static final String SERVER_PATH = "/SL_JWT/pdf/"; 45 private static final String TOMCAT_CONFIG_FILE = "/conf/server.xml"; 46 private static final String PDF_FILE_NAME = "simplepunish"; 47 private static final String PDF_TEMPLET_FILENAME = "sp_templet"; 48 49 /** 50 * 使用模板生成PDF文件 51 */ 52 public boolean generateSimplePunishPDFFromPDFTemplet(SimplePunishWrite sp,VioBehaviour behaviour) { 53 logger.info("Enter generateSimplePunishPDFFromPDFTemplet method..."); 54 boolean isSuccess = false; 55 String teamName = InitUtil.getProperty("team_name"); 56 String policeStation = InitUtil.getProperty("police_station"); 57 String courtName = InitUtil.getProperty("court_name"); 58 try { 59 BaseFont font = BaseFont.createFont("font/simsun.ttc,1", 60 BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); 61 String fileNameTemplet = getClientVersionSavePath(PDF_TEMPLET_FILENAME); 62 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 63 PdfReader reader = new PdfReader(fileNameTemplet); 64 ps = new PdfStamper(reader, bos,PdfWriter.VERSION_1_5,false); 65 AcroFields field = ps.getAcroFields(); 66 setFieldProperty("title",font,field); //title为pdf中添加的域 67 field.setField("title", teamName); //设置域的值 132 // 注意pdf中域的大小,这里设置的值太长,pdf中会显示不全 133 // 设置为true/false在点击生成的pdf文档的填充域时有区别, 134 ps.setFormFlattening(true); 135 ps.close(); 136 String fileName = getClientVersionSavePath(PDF_FILE_NAME); 138 fos = new FileOutputStream(fileName); //生成文件 139 fos.write(bos.toByteArray()); 140 fos.close(); 141 isSuccess = true; 142 logger.info("End generated pdf file."); 143 } catch (Exception e) { 144 logger.error("Generate pdf file occur exception.", e); 145 return isSuccess; 146 } finally { 147 logger.info("close stream resource."); 148 try { 149 if (ps != null) { 150 ps.close(); 151 ps = null; 152 } 153 if (fos != null) { 154 fos.close(); 155 fos = null; 156 } 157 } catch (Exception e1) { 158 logger.error("close stream occur exception", e1); 159 } 160 } 161 return isSuccess; 162 } 163 280 private void setFieldProperty(String key,BaseFont font, AcroFields field) { 281 field.setFieldProperty(key,"textfont",font,null); //处理乱码 282 } 283 /** 284 * 移动终端访问文件所在web地址 285 * @param fileName 286 * @param version 287 * @return 288 */ 289 public String getURLAddress(String fileName) { 290 String filepath = getTomcatConfigFilePath(); 291 File serverXml = new File(filepath); 292 String ip = TomcatUtil.getLocalHostAddress(); 293 int port = TomcatUtil.getTomcatPortFromConfigXml(serverXml); 294 String url = new StringBuffer().append(URL_START) 295 .append(ip).append(COLON) 296 .append(port).append(SERVER_PATH) 297 .append(fileName).append(PDF_FILE_TYPE) 298 .toString(); 299 logger.info("The client version url address is : " + url); 300 return url; 301 } 302 303 /** 304 * 获取PDF文件保存路径 305 * @param clientVersion 306 * @return 307 */ 308 private String getClientVersionSavePath(String fileName) { 310 StringBuffer sb = new StringBuffer(); 311 sb.append(path); 312 sb.append(WEBAPPS); 313 sb.append(SERVER_PATH); 314 sb.append(fileName); 315 sb.append(PDF_FILE_TYPE); 316 logger.info("The PDF file save path is " + sb.toString()); 317 return sb.toString(); 318 } 319 320 /** 321 * 获取tomcat服务器../conf/server.xml文件路径 322 * @param clientVersion 323 * @return 324 */ 325 private String getTomcatConfigFilePath() { 326 StringBuffer sb = new StringBuffer(); 327 sb.append(path); 328 sb.append(TOMCAT_CONFIG_FILE); 329 logger.info("The tomcat server.xml file path is " + sb.toString()); 330 return sb.toString(); 331 } 332 333 public String toString(Object obj) { 334 return obj != null ? toString(obj, "utf-8").trim() : "无"; 335 } 336 337 private String toString(Object obj, String charset) { 338 if (obj instanceof byte[]) { 339 try { 340 return new String((byte[]) obj, charset); 341 } catch (Exception e) { 342 return new String((byte[]) obj); 343 } 344 } else { 345 return obj.toString(); 346 } 347 } 348 }
标签:des style blog http color io os ar 使用
原文地址:http://www.cnblogs.com/hutton/p/4046921.html