IText 生成pdf,处理table cell列跨页缺失的问题

 /**
     * 创建(table)PDF,处理cell 跨页处理
     * @param savePath(需要保存的pdf路径)
     * @param pmbs (数据库查询的数据)
     * @return

  *http://www.cnblogs.com/qgc88/
     */
    private String createPDFTable(String savePath, LinkedList<ProjectManageBase> pmbs){
        try{
        Document document = new Document();
       // String separator = System.getProperties().getProperty("file.separator");
        FileOutputStream out = new FileOutputStream(savePath);
       
        PdfWriter.getInstance(document, out);
        document.open();

        PdfPTable table = new PdfPTable(7);
       table.setWidthPercentage(100);
       
       table.getDefaultCell().setPaddingLeft(1);
       table.getDefaultCell().setPaddingRight(1);
       table.getDefaultCell().setPaddingTop(2);
       table.getDefaultCell().setPaddingBottom(2);
       table.getDefaultCell().setVerticalAlignment(Element.ALIGN_CENTER);
      // cell 跨页处理:
       table.setSplitLate(false);
       table.setSplitRows(true);
       PdfPCell cell = new PdfPCell();
       
       cell.setColspan(7);
       cell.setUseAscender(true);
       cell.setHorizontalAlignment(Element.ALIGN_CENTER);
       cell.setVerticalAlignment(Element.ALIGN_TOP);
      // 标题居中处理:

       BaseFont fontChinese = null;
    try {
        fontChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
                BaseFont.NOT_EMBEDDED);// 设置中文字体
    } catch (Exception e) {
        e.printStackTrace();
    }
      Font chinese = new Font(fontChinese, 18, Font.BOLD);
       Paragraph title = new Paragraph("组评审结果清单", chinese);
       title.setAlignment(Element.ALIGN_CENTER);
       cell.addElement(title);
       cell.setBorderWidth(0);
       table.addCell(cell);
       
       //列的标题
       Font chinese_cellTitle = new Font(fontChinese, 12, Font.BOLD);
       for (int k = 0; k < 7; k++) {
           String celCount="";
           if(k==0){
               celCount="申报项目";
           }else if(k==1){
               celCount="申报单位";
           }else if(k==2){
               celCount="申报经费\n(万元)";
           }else if(k==3){
               celCount="综合评估\n分数";
           }else if(k==4){
               celCount="综合评估\n名次";
           }else if(k==5){
               celCount="立项等级";
           }else if(k==6){
               celCount="建议经费\n(万元)";
           }
           
           Paragraph count = new Paragraph(celCount, chinese_cellTitle);//支持中文
           count.setAlignment(Element.ALIGN_CENTER);
           cell.setPhrase(count);
           //cell.addElement(count)
           table.addCell(count);
       }
       
       Font chinese2 = new Font(fontChinese, 12, Font.NORMAL);
           for (int j = 0; j <pmbs.size(); j++) {//数据(List)
               for (int k = 0; k < 7; k++) {//内容的列
                   String celCount="";
                   if(k==0){
                       celCount=pmbs.get(j).getProjectName();
                   }else if(k==1){
                           celCount=pmbs.get(j).getSuoshudanwei();
                   }else if(k==2){
                       celCount=pmbs.get(j).getChiefAuditNames();
                   }else if(k==3){
                       celCount=pmbs.get(j).getCreateYear();
                   }else if(k==4){
                       celCount=pmbs.get(j).getCreUserId();
                   }else if(k==5){
                       celCount=pmbs.get(j).getShenhejieguo();
                   }else if(k==6){
                       celCount=pmbs.get(j).getBeizhu();
                   }
                   Phrase count = new Phrase(celCount, chinese2);//支持中文
                 //  count.setAlignment(Element.ALIGN_CENTER);
                   cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                   cell.addElement(count);
                   table.addCell(count);
               }
        
    }
           
           
      document.add(table);
      
      SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
      Paragraph date = new Paragraph(sdf.format(new Date()),chinese2);   
      date.setAlignment(Element.ALIGN_RIGHT);   
      document.add(date);
      
      document.close();
      System.gc();
      return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }
   

posted on 2014-08-18 11:59  qgc  阅读(2660)  评论(0编辑  收藏  举报

导航