itex利用模板导出pdf

博客介绍了如何将Word文档导出为PDF模板,通过Adobe编辑并保存为模板,用于itex进行PDF导出。过程中提到了可能遇到的字体问题,需要调整jar包目录并重新打包发布。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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包可能需要调整目录结构 重新打包发布(字体问题)



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值