java使用itext操作填充pdf模板,(根据一个模板生成多页数据)

直接开始需要两个jar包 itext-asian-5.2.0.jar和 itextpdf-5.5.6.jar ,我的业务逻辑可能比较复杂,在这里我就简单的提供一个demo ,需要的人自己按照自己的业务逻辑来改。
首先我们要用 Adobe Acrobat 来编辑pdf

简单讲讲思路

1 .读取到要填充的pdf 地址
2. 然后转成PdfReader对象,代表一个pdf
3. 然后转成PdfStamper
4. 获取到 AcroFields s = ps.getAcroFields();
5. 填充 s.setField(“表单里面你写的值” , 具体的要填充的数据);
6. 然后把pdf放到 PdfReader里面,然后多个PdfReader直接往list里面放就完事了
7. 合并成一个pdf,(可能多页)
java使用itext操作填充pdf模板,(根据一个模板生成多页数据)_第1张图片
然后
java使用itext操作填充pdf模板,(根据一个模板生成多页数据)_第2张图片
对,没有错你得把这些表单全部填满,这些就是需要替换的地方,当然你可以根据你的业务需求来填写,但是这个名字需要在代码中用到。
代码开始 :

  //我自己的数据源
        final List rows = pa.getRows();
    		//每一条数据代表一个pdf表格
            List list = new ArrayList();
            try {
                PdfStamper sd = null;
                //记录行 !我的业务需求
                int NO = 1;
                //页码 !我的 业务需求
                int page = 1;
                //不确定数据有多少,所以要无限循环
                while (true) {
                    String realPath = request.getSession().getServletContext().getRealPath("/");//项目路径
                      //使用微软雅黑字体显示中文
                    BaseFont chinessFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
                    PdfReader reader = new PdfReader(realPath + "/Template/receiveMoney.pdf");//读取
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    PdfReader pdfReader = null;
                     //因为一页有25条数据    
                    if (rows.size() > 26) {
                    //回去填充pdf的对象
                        PdfStamper ps = new PdfStamper(reader, bos);
                        AcroFields s = ps.getAcroFields();
                  
                    for (int j = 0; j < 25; j++) {
                      
                        int k = j + 1;
                        s.setField("NO" + k, NO + "");
                        BasicDBObject basicDBObject2 = (BasicDBObject) rows.get(0);
                       
                        if (basicDBObject2.get("SN") != null) {
                        **//仔细看这里,具体填充**
                            s.setField("SN" + k, basicDBObject2.get("SN").toString());
                        }
                        if (basicDBObject2.get("CompanyName") != null) {
                            s.setFieldProperty("CompanyName" + k, "textfont", chinessFont, null); //设置中文格式
                            s.setField("CompanyName" + k, basicDBObject2.get("CompanyName").toString());
                        }
                        if (basicDBObject2.get("VATReward") != null) {
                            s.setField("VATReward" + k, basicDBObject2.get("VATReward").toString());
                        }
                        if (basicDBObject2.get("BusinessTaxReward") != null) {
                            s.setField("BusinessTaxReward" + k, basicDBObject2.get("BusinessTaxReward").toString());
                        }
                        if (basicDBObject2.get("BIncomeTaxReward") != null) {
                            s.setField("BIncomeTaxReward" + k, basicDBObject2.get("BIncomeTaxReward").toString());
                        }
                        if (basicDBObject2.get("PIncomeTaxReward") != null) {
                            s.setField("PIncomeTaxReward" + k, basicDBObject2.get("PIncomeTaxReward").toString());
                        }
                        if (basicDBObject2.get("ChangeTaxReward") != null) {
                            s.setField("ChangeTaxReward" + k, basicDBObject2.get("ChangeTaxReward").toString());
                        }
                        if (basicDBObject2.get("Totals") != null) {
                            s.setField("Totals" + k, basicDBObject2.get("Totals").toString());
                        }
                        if (basicDBObject2.get("WithholdTotal") != null) {
                            s.setField("WithholdTotal" + k, basicDBObject2.get("WithholdTotal").toString());
                        }
                        //TODO 扣除预付款及代扣费
                     /*   if(basicDBObject2.get("SN")!=null){
                            s.setField("SN"+j, basicDBObject2.get("SN").toString());
                        }*/
                        if (basicDBObject2.get("SupportAmount") != null) {
                            s.setField("SupportAmount" + k, basicDBObject2.get("SupportAmount").toString());
                        }
                        NO++;
                        rows.remove(0);
                    }
                    BasicDBObject object = (BasicDBObject) rows.get(0);
                    Object startHandleDate = object.get("StartHandleDate");
                    if (startHandleDate != null && !startHandleDate.toString().equals("")) {
                        String[] split = startHandleDate.toString().split("-");
                        String s1 = split[0] + split[1];
                        int days = QzUtil.getDays(Integer.parseInt(split[0]), Integer.parseInt(split[1]));
                        s.setField("ReceiveData", split[0] + split[1] + "01" + "~" + s1 + days + "");
                    }
                    s.setField("User", getSessionUser().getUsername());
                    s.setField("CrrentData", DateUtil.getDay());
                    s.setField("Page", page + "");
                    page++;
                    ps.close();
                    pdfReader = new PdfReader(bos.toByteArray());
                    list.add(pdfReader);
                    //没到25条的
                } else {
                    PdfStamper ps = new PdfStamper(reader, bos);
                    AcroFields s = ps.getAcroFields();
                    for (int j = 0; j < rows.size(); j++) {
                        //填充模板pdf
                        int k = j + 1;
                        s.setField("NO" + k, NO + "");

                        BasicDBObject basicDBObject2 = (BasicDBObject) rows.get(j);
                        if (basicDBObject2.get("SN") != null) {
                            s.setField("SN" + k, basicDBObject2.get("SN").toString());
                        }

                        if (basicDBObject2.get("CompanyName") != null) {
                            s.setFieldProperty("CompanyName" + k, "textfont", chinessFont, null); //设置中文格式
                            s.setField("CompanyName" + k, basicDBObject2.get("CompanyName").toString());
                        }
                        if (basicDBObject2.get("VATReward") != null) {
                            s.setField("VATReward" + k, basicDBObject2.get("VATReward").toString());
                        }
                        if (basicDBObject2.get("BusinessTaxReward") != null) {
                            s.setField("BusinessTaxReward" + k, basicDBObject2.get("BusinessTaxReward").toString());
                        }
                        if (basicDBObject2.get("BIncomeTaxReward") != null) {
                            s.setField("BIncomeTaxReward" + k, basicDBObject2.get("BIncomeTaxReward").toString());
                        }
                        if (basicDBObject2.get("PIncomeTaxReward") != null) {
                            s.setField("PIncomeTaxReward" + k, basicDBObject2.get("PIncomeTaxReward").toString());
                        }
                        if (basicDBObject2.get("ChangeTaxReward") != null) {
                            s.setField("ChangeTaxReward" + k, basicDBObject2.get("ChangeTaxReward").toString());
                        }
                        if (basicDBObject2.get("Totals") != null) {
                            s.setField("Totals" + k, basicDBObject2.get("Totals").toString());
                        }

                        if (basicDBObject2.get("WithholdTotal") != null) {
                            s.setField("WithholdTotal" + k, basicDBObject2.get("WithholdTotal").toString());
                        }
                  
                        if (basicDBObject2.get("SupportAmount") != null) {
                            s.setField("SupportAmount" + k, basicDBObject2.get("SupportAmount").toString());
                        }
                        NO++;
                    }
                    BasicDBObject object = (BasicDBObject) rows.get(0);
                    Object startHandleDate = object.get("StartHandleDate");
                    if (startHandleDate != null && !startHandleDate.toString().equals("")) {
                        String[] split = startHandleDate.toString().split("-");
                        String s1 = split[0] + split[1];
                        int days = QzUtil.getDays(Integer.parseInt(split[0]), Integer.parseInt(split[1]));
                        s.setField("ReceiveData", split[0] + split[1] + "01" + "~" + s1 + days + "");
                    }

                    s.setField("User", getSessionUser().getUsername());
                    s.setField("CrrentData", DateUtil.getDay());
                    s.setField("Page", page + "");
                    page++;
                    ps.close();
                    pdfReader = new PdfReader(bos.toByteArray());
                    list.add(pdfReader);
                    //  sd = new PdfStamper(pdfReader, response.getOutputStream());
                    break;
                }
            }
//上面已经获得了pdf的每一页,这里我只需要合并成为一个pdf,然后返回
            Document document = new Document();
            PdfCopy copy = new PdfCopy(document, response.getOutputStream());
            document.open();
            for (int k = 0; k < list.size(); k++) {
                PdfReader pdfReader = list.get(k);
                document.newPage();
                copy.addDocument(pdfReader);
            }
            copy.close();

   
        } catch (Exception e) {
            e.printStackTrace();
        }

对了返回的格式要加下

 @RequestMapping(value = "/queryReceiveMoneyePrint.do", produces = "application/pdf;charset=UTF-8")

页面

 

代码质量有点差,大家凑合看。不懂的直接评论

你可能感兴趣的:(java)