itex利用模板导出pdf

来源:互联网 发布:c语言技术 编辑:程序博客网 时间:2024/06/05 06:50
private static void createZLDLR(Content content, String path) throws Exception {if(content == null)return ;File file = new File(path);if(!file.getParentFile().exists())file.getParentFile().mkdirs();// 设置中文字体,采用系统字体BaseFont baseFont = BaseFont.createFont(PropConfig.SYSTEM_FONT_PATH, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);        //导入模板String appAbsolutePath = PathUtils.getAppAbsolutePath();        String templatePath = appAbsolutePath+"/r/cms/www/whb/plugin/pdfmodel/专利代理人pdf模板.pdf";        PdfReader reader;        FileOutputStream out;          ByteArrayOutputStream bos;         PdfStamper stamper;        try {        out = new FileOutputStream(path);// 输出流         reader = new PdfReader(templatePath);// 读取pdf模板          bos = new ByteArrayOutputStream();        stamper = new PdfStamper(reader, bos);        AcroFields form = stamper.getAcroFields();        //民族前台没有填入为""(空)                String classType = content.getAttr().get("classType");        String subject = content.getAttr().get("subject");        String zipCode="";        if(content.getAttr().get("zipCode")!=null){        zipCode=content.getAttr().get("zipCode");        }        String bir="";        if(content.getDate1()!=null){        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");        bir=df.format(content.getDate1());        }        String homeAddress="";        if(content.getAttr().get("homeAddress")!=null){        homeAddress=content.getAttr().get("homeAddress");        }        String unitPhone="";        if(content.getAttr().get("unitPhone")!=null){        unitPhone=content.getAttr().get("unitPhone");        }        String unitAddress="";        if(content.getAttr().get("unitAddress")!=null){        unitAddress=content.getAttr().get("unitAddress");        }        String txt1="";        if(content.getTxt1()!=null){        String str=content.getTxt1();        txt1=str.substring(str.indexOf(">")+1, str.lastIndexOf("<"));        }        String txt="";        if(content.getTxt()!=null){        String str=content.getTxt();        txt=str.substring(str.indexOf(">")+1, str.lastIndexOf("<"));        }        String qq="";        if(content.getAttr().get("QQ")!=null){        qq=content.getAttr().get("QQ");        }        String identity="";        if(content.getAttr().get("identity")!=null){        identity=content.getAttr().get("identity");        }        List<String> list=new ArrayList<String>();        if(classType.equals("基础班")&&subject.equals("相关法和专利法")){            String[] str={zipCode,"",bir,homeAddress,content.getAuthor(),unitPhone,content.getAttr().get("sex"),unitAddress,String.valueOf((char)8730),            content.getAttr().get("mobile"),"",String.valueOf((char)8730),"",            classType,content.getAttr().get("email"),txt1,identity,txt,content.getAttr().get("workUnit"),            content.getAttr().get("major"),content.getAttr().get("technology"),content.getAttr().get("degree"),qq};            for(int i=0;i<str.length;i++){            list.add(str[i]);            }        }        if(classType.equals("基础班")&&subject.equals("专利代理实务")){            String[] str={zipCode,"",bir,homeAddress,content.getAuthor(),unitPhone,content.getAttr().get("sex"),unitAddress,"",            content.getAttr().get("mobile"),String.valueOf((char)8730),String.valueOf((char)8730),"",            classType,content.getAttr().get("email"),txt1,identity,txt,content.getAttr().get("workUnit"),            content.getAttr().get("major"),content.getAttr().get("technology"),content.getAttr().get("degree"),qq};            for(int i=0;i<str.length;i++){            list.add(str[i]);            }        }        if(classType.equals("强化班")&&subject.equals("相关法和专利法")){            String[] str={zipCode,"",bir,homeAddress,content.getAuthor(),unitPhone,content.getAttr().get("sex"),unitAddress,String.valueOf((char)8730),            content.getAttr().get("mobile"),"","",String.valueOf((char)8730),            classType,content.getAttr().get("email"),txt1,identity,txt,content.getAttr().get("workUnit"),            content.getAttr().get("major"),content.getAttr().get("technology"),content.getAttr().get("degree"),qq};            for(int i=0;i<str.length;i++){            list.add(str[i]);            }        }        if(classType.equals("强化班")&&subject.equals("专利代理实务")){            String[] str={zipCode,"",bir,homeAddress,content.getAuthor(),unitPhone,content.getAttr().get("sex"),unitAddress,"",            content.getAttr().get("mobile"),String.valueOf((char)8730),"",String.valueOf((char)8730),            classType,content.getAttr().get("email"),txt1,identity,txt,content.getAttr().get("workUnit"),            content.getAttr().get("major"),content.getAttr().get("technology"),content.getAttr().get("degree"),qq};            for(int i=0;i<str.length;i++){            list.add(str[i]);            }        }        int i = 0;          java.util.Iterator<String> it = form.getFields().keySet().iterator();          while (it.hasNext()) {              String name = it.next().toString();              System.out.println(name);            form.setFieldProperty(name, "textfont", baseFont, null);            form.setField(name, list.get(i++));          }          stamper.setFormFlattening(true);// 如果为false那么生成的PDF文件还能编辑,一定要设为true          stamper.close();                          Document doc = new Document();        PdfCopy copy = new PdfCopy(doc, out);        doc.open();        PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);        copy.addPage(importPage);        doc.close();        }catch(Exception e){        e.printStackTrace();        log.debug("生成PDF文件失败debug",e);        throw new RuntimeException("生成PDF文件时失败", e);        }}



由word导出pdf模板,用adobe打开,选则准备表单,保存文件,将该文件作为导出模板

注意:部分jar包可能需要调整目录结构 重新打包发布(字体问题)



原创粉丝点击