下载java生成PDF

/**
     * 下载打印PDF
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @throws DocumentException 
     * @throws com.itextpdf.text.DocumentException 
     * @throws com.itextpdf.text.DocumentException 
     */
    @RequestMapping(value="/downPrintPDF")
    public void downPrintPDF(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException, DocumentException, com.itextpdf.text.DocumentException { 
        SessionUserPojo user=CommonController.getUserInfo(request);
        // 禁止数据缓存。
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        // 流程id
        String procInstanceId=request.getParameter("procInstanceId");
        request.setCharacterEncoding("UTF-8"); 
//        boolean checkPermissions=this.examTaskService.checkPermissions(user.getLoginCode(), procInstanceId);
        ExternalCuringParam taskExt=(ExternalCuringParam)this.examTaskService.getTaskDetail(procInstanceId,user);
        //查询附件列表
//                List<Attachment> attachmentList=this.examTaskService.getFileListByProcessInstanceId(Pid);
        //查询备注列表
        List<Tcontent> tontentList=this.examTaskService.getListByPid(procInstanceId);
        
        String filePath = request.getSession().getServletContext().getRealPath(STATIC_FILE_PATH);
        File file_floder =new File(filePath);    
        //如果文件夹不存在则创建    
        if  (!file_floder .exists()  && !file_floder .isDirectory()) { 
            file_floder .mkdir();    
        } 
        File file = null;
         //创建file对象 
        if(ProcessType.RCBXSQ.getType().equals(taskExt.getProcInstanceType())){
            file = createPrintRcbxPdf(filePath+"\\MOLBASE_办公管理系统_"+user.getLoginCode().trim()+".pdf",taskExt,tontentList,user);
        }else if(ProcessType.CCBXSQ.getType().equals(taskExt.getProcInstanceType())){
            file = createPrintCcbxPdf(filePath+"\\MOLBASE_办公管理系统_"+user.getLoginCode().trim()+".pdf",taskExt,tontentList,user);
        }
         //设置response的编码方式 
         response.setContentType("application/x-msdownload");
         // 文件尺寸 
         response.setContentLength((int)file.length()); 

         String filename = new String(file.getName().getBytes("GB2312"), "ISO_8859_1");
         response.setHeader("Content-Disposition","attachment;filename=" + filename); 
         response.setHeader("Pragma","no-cache");
         OutputStream myout = response.getOutputStream(); 
         if(file.exists()){
             FileInputStream fis = new FileInputStream(file); 
             BufferedInputStream buff = new BufferedInputStream(fis); 
             byte [] b = new byte[1024];
             long k = 0;
             while(k < file.length()){ 
                 int j = buff.read(b,0,1024); 
                 k += j; 
                 myout.write(b,0,j); 
                 myout.flush(); 
             } 
             buff.close();
         }
         myout.close();
    }
    
    @SuppressWarnings("static-access")
    private File createPrintCcbxPdf(String filepath,ExternalCuringParam taskExt,List<Tcontent> tontentList,SessionUserPojo user) throws DocumentException, com.itextpdf.text.DocumentException, IOException{
        BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        Font headfont = new Font(bfChinese, 13, Font.BOLD);// 设置字体大小
//        Font keyfont = new Font(bfChinese, 10, Font.BOLD);// 设置字体大小
        Font textfont = new Font(bfChinese, 10, Font.NORMAL);// 设置字体大小
//        Font textfont_underline = new Font(bfChinese, 10, Font.UNDERLINE);// 设置字体大小
//        Font textfont1 = new Font(bfChinese, 12, Font.ITALIC);// 设置字体大小
        // 1.新建document对象
        // 第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
        //Document document = new Document(PageSize.A4, 50, 50, 50, 50);
        com.itextpdf.text.Document document = new com.itextpdf.text.Document();
        //add Chinese font
        //BaseFont bfChinese=BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
         //Font headfont=new Font(bfChinese,10,Font.BOLD);
         //Font keyfont=new Font(bfChinese,8,Font.BOLD);
         //Font textfont=new Font(bfChinese,8,Font.NORMAL);

        // 2.建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
        // 创建 PdfWriter 对象 第一个参数是对文档对象的引用,第二个参数是文件的实际名称,在该名称中还会给出其输出路径。
//        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:\\MOLBASE 办公管理系统_"+user.getLoginCode()+".pdf"));
        //Create Writer associated with document
        
        File file = new File(filepath);
        if(file.exists()){
            file.delete();
        }
        file.createNewFile();
        
        com.itextpdf.text.pdf.PdfWriter writer = com.itextpdf.text.pdf.PdfWriter.getInstance(document, new FileOutputStream(file));
        writer.setViewerPreferences(writer.HideToolbar);
        // 3.打开文档
        document.open();

        // 4.向文档中添加内容
        // 标题
//        document.addTitle("Hello mingri example");
        // 作者
//        document.addAuthor("wolf");

        // 主题
//        document.addSubject("This exam/ple explains how to add metadata.");
//        document.addKeywords("iText, Hello mingri");
//        document.addCreator("My program using iText");

        // document.newPage();
        // 向文档中添加内容
//        document.add(new Paragraph("\n"));
        // 通过 com.lowagie.text.Paragraph 来添加文本。可以用文本及其默认的字体、颜色、大小等等设置来创建一个默认段落
//        document.add(new Paragraph("First page of the document."));
//        document.add(new Paragraph("Some more text on the  first page with different color and font type.", FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD)));
        
//        document.add(new Paragraph("Some more text on the first page with different color and font type.", FontFactory.getFont(FontFactory.defaultEncoding, 10, Font.BOLD)));
//        document.add(new Paragraph("xx租赁有限公司\n\n\n", textfont));
        Paragraph title = new Paragraph(taskExt.getProcInstanceName()+"\n", headfont);
        title.setAlignment(Element.ALIGN_CENTER);
        document.add(title);
        document.add(new Paragraph("\n",new Font(bfChinese, 8, Font.NORMAL)));
        // 增加表格
        PdfPTable table1 = createTable(new float[]{15,35,15,35}, true, null);
        table1.completeRow();
        table1.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table1.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table1.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table1.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table1.addCell(createCell("发起人", textfont, Element.ALIGN_CENTER, 1, 1, true));
        table1.addCell(createCell(taskExt.getOwnerName()+"/"+taskExt.getDeptName()+"_"+taskExt.getStartTime().split(" ")[0], textfont, Element.ALIGN_CENTER, 1, 1, true));
        table1.addCell(createCell("流程编号", textfont, Element.ALIGN_CENTER, 1, 1, true));
        table1.addCell(createCell(taskExt.getProcInstanceCode(), textfont, Element.ALIGN_CENTER, 1, 1, true));
        document.add(table1);
        document.add(new Paragraph("\n",new Font(bfChinese, 4, Font.NORMAL)));
        
        PdfPTable table1_1 = createTable(new float[]{15,85}, true, null);
        table1_1.completeRow();
        table1_1.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table1_1.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table1_1.addCell(createCell("备注", textfont, Element.ALIGN_CENTER, 1, 1, true));
        table1_1.addCell(createCell(taskExt.getStartContent(), textfont, Element.ALIGN_LEFT, 1, 1, true));
        document.add(table1_1);
        document.add(new Paragraph("\n",new Font(bfChinese, 4, Font.NORMAL)));
        
        String process_param = taskExt.getProcess_param();
        JSONObject obj=JSONObject.parseObject(process_param);
        
        PdfPTable table2 = createTable(new float[]{15,35,15,35}, true, null);
        table2.completeRow();
        table2.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table2.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table2.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table2.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table2.addCell(createCell("报销人", textfont, Element.ALIGN_CENTER, 1, 1, true));
        table2.addCell(createCell(obj.getString("expense_person_name"), textfont, Element.ALIGN_CENTER, 1, 1, true));
        table2.addCell(createCell("报销人部门", textfont, Element.ALIGN_CENTER, 1, 1, true));
        table2.addCell(createCell(obj.getString("expense_person_dept_name"), textfont, Element.ALIGN_CENTER, 1, 1, true));
        table2.addCell(createCell("费用公司", textfont, Element.ALIGN_CENTER, 1, 1, true));
        table2.addCell(createCell(obj.getString("expense_company"), textfont, Element.ALIGN_CENTER, 1, 1, true));
        table2.addCell(createCell("费用部门", textfont, Element.ALIGN_CENTER, 1, 1, true));
        table2.addCell(createCell(obj.getString("expense_dept_name"), textfont, Element.ALIGN_CENTER, 1, 1, true));
        table2.addCell(createCell("出发日期", textfont, Element.ALIGN_CENTER, 1, 1, true));
        table2.addCell(createCell(obj.getString("startOutDate"), textfont, Element.ALIGN_CENTER, 1, 1, true));
        table2.addCell(createCell("返回日期", textfont, Element.ALIGN_CENTER, 1, 1, true));
        table2.addCell(createCell(obj.getString("endOutDate"), textfont, Element.ALIGN_CENTER, 1, 1, true));
        table2.addCell(createCell("费用合计", textfont, Element.ALIGN_CENTER, 1, 1, true));
        table2.addCell(createCell(obj.getString("expense_money_total"), textfont, Element.ALIGN_CENTER, 1, 1, true));
        table2.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, true));
        table2.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, true));
        document.add(table2);
        document.add(new Paragraph("\n",new Font(bfChinese, 4, Font.NORMAL)));
        
        // 增加表格
        PdfPTable table = createTable(new float[]{14,10,10,22,10,34}, true, null);
        table.completeRow();
        //出差城市    出差天数    单据数    费用项目    单据金额    费用说明
        table.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table.addCell(createCell("出差城市", textfont, Element.ALIGN_CENTER, 1, 1, true));
        table.addCell(createCell("出差天数", textfont, Element.ALIGN_CENTER, 1, 1, true));
        table.addCell(createCell("单据数", textfont, Element.ALIGN_CENTER, 1, 1, true));
        table.addCell(createCell("费用项目", textfont, Element.ALIGN_CENTER, 1, 1, true));
        table.addCell(createCell("单据金额", textfont, Element.ALIGN_CENTER, 1, 1, true));
        table.addCell(createCell("费用说明", textfont, Element.ALIGN_CENTER, 1, 1, true));
        
        JSONArray arr1=obj.getJSONArray("trip_expense_message");
        Iterator<Object> ite=arr1.iterator();
        while(ite.hasNext()){
            JSONObject objx=JSONObject.parseObject(ite.next().toString());
            
            table.addCell(createCell(objx.get("tripCity").toString(), textfont, Element.ALIGN_CENTER, 1, 1, true));
            table.addCell(createCell(objx.get("tripDays").toString(), textfont, Element.ALIGN_CENTER, 1, 1, true));
            table.addCell(createCell(objx.get("invoiceAmounte").toString(), textfont, Element.ALIGN_CENTER, 1, 1, true));
            table.addCell(createCell(objx.get("expenseItem").toString(), textfont, Element.ALIGN_CENTER, 1, 1, true));
            table.addCell(createCell(objx.get("invoiceMoney").toString(), textfont, Element.ALIGN_CENTER, 1, 1, true));
            table.addCell(createCell(objx.get("expenseExplanation").toString(), textfont, Element.ALIGN_CENTER, 1, 1, true));
        }
        document.add(table);
        document.add(new Paragraph("\n",new Font(bfChinese, 4, Font.NORMAL)));
        
        PdfPTable table3 = createTable(new float[]{6,17,17,60}, true, null);
        table3.completeRow();
        table3.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table3.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table3.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table3.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table3.addCell(createCell("序号", textfont, Element.ALIGN_CENTER, 1, 1, true));
        table3.addCell(createCell("审批角色", textfont, Element.ALIGN_CENTER, 1, 1, true));
        table3.addCell(createCell("审批人/审核人", textfont, Element.ALIGN_CENTER, 1, 1, true));
        table3.addCell(createCell("审批意见", textfont, Element.ALIGN_CENTER, 1, 1, true));
        
        String assignee_1 = obj.getString("assignee_1");
        String assignee_2 = obj.getString("assignee_2"); 
        String address_assignee = obj.getString("address_assignee"); 
        int i_num = 1;
        for(int i=0;i<6;i++){
            boolean exam_boo = false;
            int size = tontentList.size();
            if(i==0){
                if(size>=1){
                    exam_boo = true;
                }
            }else if(i==1){
                if(!("chang").equals(assignee_2)){
                    if(size>=2){
                        exam_boo = true;
                    }
                }
            }else if(i==2){
                if(("chang").equals(assignee_2)){
                    if(size>=2){
                        exam_boo = true;
                    }
                }else{
                    if(size>=3){
                        exam_boo = true;
                    }
                }
            }else if(i==3){
                if(("chang").equals(assignee_2)){
                    if(size>=3){
                        exam_boo = true;
                    }
                }else{
                    if(size>=4){
                        exam_boo = true;
                    }
                }
            }else if(i==4){
                if(("chang").equals(assignee_2)){
                    if(size>=4){
                        exam_boo = true;
                    }
                }else{
                    if(size>=5){
                        exam_boo = true;
                    }
                }
            }else if(i==5){
                if(("chang").equals(assignee_2)){
                    if(size>=5){
                        exam_boo = true;
                    }
                }else{
                    if(size>=6){
                        exam_boo = true;
                    }
                }
            }
            
            if(exam_boo){
                int content_type = 0;
                String content_type_str = "";
                String content = "";
                String content_time = "";
                
                if(tontentList.size()>=i_num && tontentList.get(i_num-1)!=null){
                    content_type = tontentList.get(i_num-1).getContentType();
                    content_time = "审核时间: "+tontentList.get(i_num-1).getStrCreateTime();
                    if(tontentList.get(i_num-1).getContent()!=null){
                        content = tontentList.get(i_num-1).getContent();
                    }
                }
                if(content_type == 1){
                    content_type_str = "审批意见: 同意";
                }else if(content_type == 2){
                    content_type_str = "审批意见: 不同意";
                }else if(content_type == 3){
                    content_type_str = "审批意见: 不同意且继续";
                }
                
                String exam_role = "";
                if(i==0){
                    exam_role = "部门负责人";
                }else if(i==1){
                    exam_role = "分管合伙人";
                }else if(i==2){
                    exam_role = "财务岗1";
                }else if(i==3){
                    exam_role = "财务岗2";
                }else if(i==4){
                    exam_role = "公司CEO";
                }else if(i==5){
                    exam_role = "收文人";
                }
                
                String exam_assignee = "";
                if(i==0){
                    exam_assignee = assignee_1;
                }else if(i==1){
                    exam_assignee = assignee_2;
                }else if(i==2){
                    exam_assignee = "张瑞平";
                }else if(i==3){
                    exam_assignee = "蒋中杰";
                }else if(i==4){
                    exam_assignee = "常东亮";
                }else if(i==5){
                    exam_assignee = address_assignee;
                }
                
                if(i==0||i==1||i==5){
                    Map<String, Object> userMap=this.userService.getUserByLoginCode(exam_assignee);
                    exam_assignee = userMap.get("user_name").toString();
                    //userMap.get("dept_name").toString();
                }
                
                table3.addCell(createCell(i_num+"", textfont, Element.ALIGN_CENTER, 1, 1, true));
                table3.addCell(createCell(exam_role, textfont, Element.ALIGN_CENTER, 1, 1, true));
                table3.addCell(createCell(exam_assignee, textfont, Element.ALIGN_CENTER, 1, 1, true));
                table3.addCell(createCell(content_type_str+"    "+content+"    "+content_time, textfont, Element.ALIGN_CENTER, 1, 1, true));
                i_num +=1;
            }
            
            
        }
        document.add(table3);
        document.add(new Paragraph("\n",new Font(bfChinese, 4, Font.NORMAL)));
        
        // 5.关闭文档
        document.close();
        return new File(filepath);
    }
    
    @SuppressWarnings("static-access")
    private File createPrintRcbxPdf(String filepath,ExternalCuringParam taskExt,List<Tcontent> tontentList,SessionUserPojo user) throws DocumentException, com.itextpdf.text.DocumentException, IOException{
        BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        Font headfont = new Font(bfChinese, 13, Font.BOLD);// 设置字体大小
//        Font keyfont = new Font(bfChinese, 10, Font.BOLD);// 设置字体大小
        Font textfont = new Font(bfChinese, 10, Font.NORMAL);// 设置字体大小
//        Font textfont_underline = new Font(bfChinese, 10, Font.UNDERLINE);// 设置字体大小
//        Font textfont1 = new Font(bfChinese, 12, Font.ITALIC);// 设置字体大小
        // 1.新建document对象
        // 第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
        //Document document = new Document(PageSize.A4, 50, 50, 50, 50);
        com.itextpdf.text.Document document = new com.itextpdf.text.Document();
        //add Chinese font
        //BaseFont bfChinese=BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
         //Font headfont=new Font(bfChinese,10,Font.BOLD);
         //Font keyfont=new Font(bfChinese,8,Font.BOLD);
         //Font textfont=new Font(bfChinese,8,Font.NORMAL);

        // 2.建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
        // 创建 PdfWriter 对象 第一个参数是对文档对象的引用,第二个参数是文件的实际名称,在该名称中还会给出其输出路径。
//        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:\\MOLBASE 办公管理系统_"+user.getLoginCode()+".pdf"));
        //Create Writer associated with document
        
        File file = new File(filepath);
        if(file.exists()){
            file.delete();
        }
        file.createNewFile();
        
        com.itextpdf.text.pdf.PdfWriter writer = com.itextpdf.text.pdf.PdfWriter.getInstance(document, new FileOutputStream(file));
        writer.setViewerPreferences(writer.HideToolbar);
        // 3.打开文档
        document.open();

        // 4.向文档中添加内容
        // 标题
//        document.addTitle("Hello mingri example");
        // 作者
//        document.addAuthor("wolf");

        // 主题
//        document.addSubject("This exam/ple explains how to add metadata.");
//        document.addKeywords("iText, Hello mingri");
//        document.addCreator("My program using iText");

        // document.newPage();
        // 向文档中添加内容
//        document.add(new Paragraph("\n"));
        // 通过 com.lowagie.text.Paragraph 来添加文本。可以用文本及其默认的字体、颜色、大小等等设置来创建一个默认段落
//        document.add(new Paragraph("First page of the document."));
//        document.add(new Paragraph("Some more text on the  first page with different color and font type.", FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD)));
        
//        document.add(new Paragraph("Some more text on the first page with different color and font type.", FontFactory.getFont(FontFactory.defaultEncoding, 10, Font.BOLD)));
//        document.add(new Paragraph("xx租赁有限公司\n\n\n", textfont));
        Paragraph title = new Paragraph(taskExt.getProcInstanceName()+"\n", headfont);
        title.setAlignment(Element.ALIGN_CENTER);
        document.add(title);
        document.add(new Paragraph("\n",new Font(bfChinese, 8, Font.NORMAL)));
        // 增加表格
        PdfPTable table1 = createTable(new float[]{15,35,15,35}, true, null);
        table1.completeRow();
        table1.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table1.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table1.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table1.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table1.addCell(createCell("发起人", textfont, Element.ALIGN_CENTER, 1, 1, true));
        table1.addCell(createCell(taskExt.getOwnerName()+"/"+taskExt.getDeptName()+"_"+taskExt.getStartTime().split(" ")[0], textfont, Element.ALIGN_CENTER, 1, 1, true));
        table1.addCell(createCell("流程编号", textfont, Element.ALIGN_CENTER, 1, 1, true));
        table1.addCell(createCell(taskExt.getProcInstanceCode(), textfont, Element.ALIGN_CENTER, 1, 1, true));
        document.add(table1);
        document.add(new Paragraph("\n",new Font(bfChinese, 4, Font.NORMAL)));
        
        PdfPTable table1_1 = createTable(new float[]{15,85}, true, null);
        table1_1.completeRow();
        table1_1.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table1_1.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table1_1.addCell(createCell("备注", textfont, Element.ALIGN_CENTER, 1, 1, true));
        table1_1.addCell(createCell(taskExt.getStartContent(), textfont, Element.ALIGN_LEFT, 1, 1, true));
        document.add(table1_1);
        document.add(new Paragraph("\n",new Font(bfChinese, 4, Font.NORMAL)));
        
        String process_param = taskExt.getProcess_param();
        JSONObject obj=JSONObject.parseObject(process_param);
        
        PdfPTable table2 = createTable(new float[]{15,35,15,35}, true, null);
        table2.completeRow();
        table2.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table2.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table2.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table2.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table2.addCell(createCell("报销人", textfont, Element.ALIGN_CENTER, 1, 1, true));
        table2.addCell(createCell(obj.getString("expense_person_name"), textfont, Element.ALIGN_CENTER, 1, 1, true));
        table2.addCell(createCell("报销人部门", textfont, Element.ALIGN_CENTER, 1, 1, true));
        table2.addCell(createCell(obj.getString("expense_person_dept_name"), textfont, Element.ALIGN_CENTER, 1, 1, true));
        table2.addCell(createCell("费用公司", textfont, Element.ALIGN_CENTER, 1, 1, true));
        table2.addCell(createCell(obj.getString("expense_company"), textfont, Element.ALIGN_CENTER, 1, 1, true));
        table2.addCell(createCell("费用部门", textfont, Element.ALIGN_CENTER, 1, 1, true));
        table2.addCell(createCell(obj.getString("expense_dept_name"), textfont, Element.ALIGN_CENTER, 1, 1, true));
        table2.addCell(createCell("费用合计", textfont, Element.ALIGN_CENTER, 1, 1, true));
        table2.addCell(createCell(obj.getString("expense_money_total"), textfont, Element.ALIGN_CENTER, 1, 1, true));
        table2.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, true));
        table2.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, true));
        document.add(table2);
        document.add(new Paragraph("\n",new Font(bfChinese, 4, Font.NORMAL)));
        
        // 增加表格
        PdfPTable table = createTable(new float[]{15,35,15,35}, true, null);
        table.completeRow();
        //单据数    费用项目    单据金额    费用说明
        table.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table.addCell(createCell("单据数", textfont, Element.ALIGN_CENTER, 1, 1, true));
        table.addCell(createCell("费用项目", textfont, Element.ALIGN_CENTER, 1, 1, true));
        table.addCell(createCell("单据金额", textfont, Element.ALIGN_CENTER, 1, 1, true));
        table.addCell(createCell("费用说明", textfont, Element.ALIGN_CENTER, 1, 1, true));
        
        JSONArray arr1=obj.getJSONArray("daily_expense_message");
        Iterator<Object> ite=arr1.iterator();
        while(ite.hasNext()){
            JSONObject objx=JSONObject.parseObject(ite.next().toString());
            
            table.addCell(createCell(objx.get("invoiceAmounte").toString(), textfont, Element.ALIGN_CENTER, 1, 1, true));
            table.addCell(createCell(objx.get("expenseItem").toString(), textfont, Element.ALIGN_CENTER, 1, 1, true));
            table.addCell(createCell(objx.get("invoiceMoney").toString(), textfont, Element.ALIGN_CENTER, 1, 1, true));
            table.addCell(createCell(objx.get("expenseExplanation").toString(), textfont, Element.ALIGN_CENTER, 1, 1, true));
        }
        document.add(table);
        document.add(new Paragraph("\n",new Font(bfChinese, 4, Font.NORMAL)));
        
        PdfPTable table3 = createTable(new float[]{6,17,17,60}, true, null);
        table3.completeRow();
        table3.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table3.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table3.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table3.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false));
        table3.addCell(createCell("序号", textfont, Element.ALIGN_CENTER, 1, 1, true));
        table3.addCell(createCell("审批角色", textfont, Element.ALIGN_CENTER, 1, 1, true));
        table3.addCell(createCell("审批人/审核人", textfont, Element.ALIGN_CENTER, 1, 1, true));
        table3.addCell(createCell("审批意见", textfont, Element.ALIGN_CENTER, 1, 1, true));
        
        String assignee_1 = obj.getString("assignee_1");
        String assignee_2 = obj.getString("assignee_2"); 
        String address_assignee = obj.getString("address_assignee"); 
        int i_num = 1;
        for(int i=0;i<6;i++){
            boolean exam_boo = false;
            int size = tontentList.size();
            if(i==0){
                if(size>=1){
                    exam_boo = true;
                }
            }else if(i==1){
                if(!("chang").equals(assignee_2)){
                    if(size>=2){
                        exam_boo = true;
                    }
                }
            }else if(i==2){
                if(("chang").equals(assignee_2)){
                    if(size>=2){
                        exam_boo = true;
                    }
                }else{
                    if(size>=3){
                        exam_boo = true;
                    }
                }
            }else if(i==3){
                if(("chang").equals(assignee_2)){
                    if(size>=3){
                        exam_boo = true;
                    }
                }else{
                    if(size>=4){
                        exam_boo = true;
                    }
                }
            }else if(i==4){
                if(("chang").equals(assignee_2)){
                    if(size>=4){
                        exam_boo = true;
                    }
                }else{
                    if(size>=5){
                        exam_boo = true;
                    }
                }
            }else if(i==5){
                if(("chang").equals(assignee_2)){
                    if(size>=5){
                        exam_boo = true;
                    }
                }else{
                    if(size>=6){
                        exam_boo = true;
                    }
                }
            }
            
            if(exam_boo){
                int content_type = 0;
                String content_type_str = "";
                String content = "";
                String content_time = "";
                if(tontentList.size()>=i_num && tontentList.get(i_num-1)!=null){
                    content_type = tontentList.get(i_num-1).getContentType();
                    content_time = "审核时间: "+tontentList.get(i_num-1).getStrCreateTime();
                    if(tontentList.get(i_num-1).getContent()!=null){
                        content = tontentList.get(i_num-1).getContent();
                    }
                }
                if(content_type == 1){
                    content_type_str = "审批意见: 同意";
                }else if(content_type == 2){
                    content_type_str = "审批意见: 不同意";
                }else if(content_type == 3){
                    content_type_str = "审批意见: 不同意且继续";
                }
                
                String exam_role = "";
                if(i==0){
                    exam_role = "部门负责人";
                }else if(i==1){
                    exam_role = "分管合伙人";
                }else if(i==2){
                    exam_role = "财务岗1";
                }else if(i==3){
                    exam_role = "财务岗2";
                }else if(i==4){
                    exam_role = "公司CEO";
                }else if(i==5){
                    exam_role = "收文人";
                }
                
                String exam_assignee = "";
                if(i==0){
                    exam_assignee = assignee_1;
                }else if(i==1){
                    exam_assignee = assignee_2;
                }else if(i==2){
                    exam_assignee = "张瑞平";
                }else if(i==3){
                    exam_assignee = "蒋中杰";
                }else if(i==4){
                    exam_assignee = "常东亮";
                }else if(i==5){
                    exam_assignee = address_assignee;
                }
                
                if(i==0||i==1||i==5){
                    Map<String, Object> userMap=this.userService.getUserByLoginCode(exam_assignee);
                    exam_assignee = userMap.get("user_name").toString();
                }
                
                table3.addCell(createCell(i_num+"", textfont, Element.ALIGN_CENTER, 1, 1, true));
                table3.addCell(createCell(exam_role, textfont, Element.ALIGN_CENTER, 1, 1, true));
                table3.addCell(createCell(exam_assignee, textfont, Element.ALIGN_CENTER, 1, 1, true));
                table3.addCell(createCell(content_type_str+"    "+content+"    "+content_time, textfont, Element.ALIGN_CENTER, 1, 1, true));
                i_num +=1;
            }
            
        }
        document.add(table3);
        document.add(new Paragraph("\n",new Font(bfChinese, 4, Font.NORMAL)));
        
        // 5.关闭文档
        document.close();
        return new File(filepath);
    }

    @SuppressWarnings("unused")
    private PdfPTable createTable(int colNumber, boolean borderflg, Integer totalWidth) {
        int maxWidth = 520;
        PdfPTable table = new PdfPTable(colNumber);
        try {
            if (totalWidth != null) {
                table.setTotalWidth(totalWidth);
            } else {
                table.setTotalWidth(maxWidth);
            }
            table.setLockedWidth(true);
            table.setHorizontalAlignment(Element.ALIGN_CENTER);
            if (borderflg) {
                table.getDefaultCell().setBorder(1);
            } else {
                table.getDefaultCell().setBorder(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return table;
    }
    
    private PdfPTable createTable(float[] widths, boolean borderflg, Integer totalWidth) {
        int maxWidth = 520;
        PdfPTable table = new PdfPTable(widths);
        try {
            if (totalWidth != null) {
                table.setTotalWidth(totalWidth);
            } else {
                table.setTotalWidth(maxWidth);
            }
            table.setLockedWidth(true);
            table.setHorizontalAlignment(Element.ALIGN_CENTER);
            if (borderflg) {
                table.getDefaultCell().setBorder(1);
            } else {
                table.getDefaultCell().setBorder(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return table;
    }
    
    private PdfPCell createCell(String value, com.itextpdf.text.Font font, int align, int colspan, int rowspan,
            boolean boderFlag) {
        BaseColor tabBorderColor = new BaseColor(222,222,222);
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setColspan(colspan);
        cell.setRowspan(rowspan);
        cell.setPhrase(new Phrase(value, font));
        cell.setPadding(3f);
        cell.setBorderColor(tabBorderColor);
        if (!boderFlag) {
            cell.setBorder(0);
            // cell.setPaddingTop(15.0f);
            // cell.setPaddingBottom(8.0f);
        }
        return cell;
    }

 

posted @ 2016-11-07 13:08  残星  阅读(2461)  评论(0编辑  收藏  举报