首页 > 代码库 > Java导出pdf文件数据

Java导出pdf文件数据

 提示:导出pdf文件,需要3个jar包iText-2.1.5.jar,iTextAsian.jar,iText-rtf-2.1.4.jar。

public boolean outputPdfJhsy(EntityBean data) { try { Global.getInstance().LogApp("导出pdf开始"); String pdfpath = File.get("LEAP/NSESTModule/WRModule/nsjhsyzm.pdf").getAbsolutePath(); //从eclipse文件夹中得到pdf模版路径 String imgpath = File.get("LEAP/NSESTModule/WRModule/image/weiji.gif").getAbsolutePath(); //右下角盖章图片 PdfReader reader = new PdfReader(pdfpath); //创建输出目录 String pdfSavePath = NameedPathUtil.getPath("default")+"nsestpdf/"; File.get(pdfSavePath).mkdir(); String filename = Global.getInstance().GetUUID()+".pdf"; pdfSavePath = pdfSavePath+filename; PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(pdfSavePath)); // 拿到第一页的上部内容部分 PdfContentByte over = stamp.getOverContent(1); //获取表单域(用Adobe Acrobat Pro工具) AcroFields form = stamp.getAcroFields(); SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日"); EntityBean bean = data; try { form.setField("busino", bean.getString("busino")); form.setField("jsbm", bean.getString("jsbm")); form.setField("wname",bean.getString("wname")); form.setField("wsfz",bean.getString("wsfz")); form.setField("mname",bean.getString("mname")); form.setField("msfz",bean.getString("msfz")); form.setField("whjd",bean.getString("whjd")); form.setField("wxjzd",bean.getString("wxjzd")); form.setField("hyzk",bean.getString("hyzk")); form.setField("jsqk",bean.getString("jsqk")); form.setField("zncount",bean.getString("zncount")); form.setField("znqk",bean.getString("znqk")); form.setField("slrq",sdf2.format( sdf.parse(bean.getString("slrq")) ) ); form.setField("wfjhsyzc",bean.getString("wfjhsyzc")); form.setField("qtsm",bean.getString("qtsm")); form.setField("wname2",bean.getString("wname2")); form.setField("wsfz2",bean.getString("wsfz2")); form.setField("zmlxname", bean.getString("zmlxname")); form.setField("jbr",bean.getString("jbr")); form.setField("lxdh",bean.getString("lxdh")); form.setField("sprq",bean.getString("sprq")); } catch (Exception e) { Global.getInstance().LogApp("插入pdf表单域异常"); e.printStackTrace(); } stamp.setFormFlattening(true); // 这句不能少 //创建一个image对象 Image image = Image.getInstance(imgpath); //设置image对象的输出位置pa.getAsNumber(pa.size()-1).floatValue() 是该页pdf坐标轴的y轴的最大值 image.setAbsolutePosition(398,170); image.scalePercent(70); over.addImage(image); over.stroke(); stamp.close(); Global.getInstance().LogApp("导出pdf结束"); } catch (FileNotFoundException e) { Global.getInstance().LogApp("异常1"); e.printStackTrace(); return false; } catch (DocumentException e) { Global.getInstance().LogApp("异常2"); e.printStackTrace(); return false; } catch (IOException e) { Global.getInstance().LogApp("异常3"); e.printStackTrace(); return false; } finally { } return true; }

 

Java导出pdf文件数据