java导出pdf


      //导出 

         public void ScoringAnnouncementdownLoad() throws MalformedURLException, IOException, DocumentException, FileStoreException {
ActionContext act = ActionContext.getContext();
AclUserBean logonUser = (AclUserBean) act.getSession().get(Constant.LOGON_USER);
String acnt=logonUser.getAcnt();
ResponseWrapper response = act.getResponse();
OutputStream os = null;
String fileName = "特约店综合评分.pdf";
fileName = new String(fileName.getBytes("GB2312"), "ISO8859-1");
Document document=new Document(PageSize.A0, 10, 10,10, 10);
    Rectangle pageRect=document.getPageSize(); 
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfWriter writer = PdfWriter.getInstance(document, baos);
         Paragraph emptyLine10 = new Paragraph(10,"");
         BaseFont bf= BaseFont.createFont("C:/WINDOWS/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
    Font fontChinese10 = new Font(bf,10, Font.NORMAL); 
      //页尾
         document.setFooter(createFooter(fontChinese10));  
            //页头
         document.setHeader(createHeader(fontChinese10)); 
            //水印
         Watermark W =new Watermark(acnt);
         writer.setPageEvent(W);
            //打开
         document.open(); 
            //添加空行
         document.add(emptyLine10);  
            //加数据
         document.add(createTab(fontChinese10));  
            //创建新页
         document.newPage();  
            //关闭
document.close();
//生成是否保存
       response.addHeader("Content-Disposition", "attachment;filename=" + fileName); 
       response.setContentType("application/pdf");
       response.setContentLength(baos.size()); 
       os = response.getOutputStream();
       baos.writeTo(os);
       //关闭流
os.flush();    
       os.close(); 
}
         private HeaderFooter createFooter(Font fontChinese10){  
        String footStr1 = "页尾 ";  
        HeaderFooter footer=new HeaderFooter(new Phrase(footStr1,fontChinese10),new Phrase(" ",fontChinese10));    
        footer.setAlignment(Element.ALIGN_RIGHT);  
        footer.setBottom(0);
        return footer;  
   }  
private HeaderFooter createHeader(Font fontChinese10){  
        String footStr1 = "页头 ";  
        HeaderFooter footer=new HeaderFooter(new Phrase(footStr1,fontChinese10), false) ;  
        footer.setAlignment(Element.ALIGN_CENTER); 
        footer.setTop(0);
        footer.setLeft(0);
        return footer;  
    }  

 //数据
private PdfPTable createTab(Font fontChinese12){  
       try {
           PdfPTable tab = new PdfPTable(27);//多少列  
           //tab.setTotalWidth(100); //固定宽度  
           tab.setWidths(new int[]{20,20,40,50,50,50,50,45,45,40,40,60,90,55,75,50,40,40,78,55,50,50,60,50,50,35,50}); //每列的宽度, 只针对固     定宽度有效;如果表格使用百分比设置总宽度的话,那这里也就是使用百分比.  
           tab.setWidthPercentage(100);// 表格宽度设置为100%  
           
           List<Map<String, Object>> data = tabData();  
           PdfPCell cell_temp =  null;
           
           cell_temp = new PdfPCell(new Paragraph("排名", fontChinese12)); 
           tab.addCell(cell_temp);  
             
           cell_temp = new PdfPCell(new Paragraph("大区", fontChinese12));  
           tab.addCell(cell_temp);  
             
           cell_temp = new PdfPCell(new Paragraph("省份", fontChinese12));  
           tab.addCell(cell_temp);  
             
           cell_temp = new PdfPCell(new Paragraph("特约店简称", fontChinese12));  
           tab.addCell(cell_temp);  
             
           cell_temp = new PdfPCell(new Paragraph("特约店代码", fontChinese12));  
           tab.addCell(cell_temp); 
           cell_temp = new PdfPCell(new Paragraph("开业时间", fontChinese12));  
           tab.addCell(cell_temp); 
           cell_temp = new PdfPCell(new Paragraph("提车达成率12%", fontChinese12));  
           tab.addCell(cell_temp); 
           cell_temp = new PdfPCell(new Paragraph("登录达成率12%", fontChinese12));  
           tab.addCell(cell_temp); 
           cell_temp = new PdfPCell(new Paragraph("提车贡献率  10%", fontChinese12));  
           tab.addCell(cell_temp); 
           
           cell_temp = new PdfPCell(new Paragraph("市占率6%", fontChinese12));  
           tab.addCell(cell_temp); 
           
           cell_temp = new PdfPCell(new Paragraph("销售满意度6%", fontChinese12));  
           tab.addCell(cell_temp); 
           
           cell_temp = new PdfPCell(new Paragraph("客流目标达成率 6%", fontChinese12));  
           tab.addCell(cell_temp); 
           
           cell_temp = new PdfPCell(new Paragraph("电话网络到店目标达成率2%", fontChinese12));  
           tab.addCell(cell_temp); 
           
           cell_temp = new PdfPCell(new Paragraph("二手车目标达成率6%", fontChinese12));  
           tab.addCell(cell_temp); 
           
           cell_temp = new PdfPCell(new Paragraph("电话网络到店目标达成率2%", fontChinese12));  
           tab.addCell(cell_temp); 
           
           cell_temp = new PdfPCell(new Paragraph("售后满意度8%", fontChinese12));  
           tab.addCell(cell_temp); 
           
           cell_temp = new PdfPCell(new Paragraph("人员培训4%", fontChinese12));  
           tab.addCell(cell_temp); 
           
           cell_temp = new PdfPCell(new Paragraph("保养达成率8%", fontChinese12));  
           tab.addCell(cell_temp); 
           
           cell_temp = new PdfPCell(new Paragraph("管理内用户再次回店保养率4%", fontChinese12));  
           tab.addCell(cell_temp); 
           
           cell_temp = new PdfPCell(new Paragraph("保险产品 达成率8%", fontChinese12));  
           tab.addCell(cell_temp); 
           
           cell_temp = new PdfPCell(new Paragraph("用品单车产值4%", fontChinese12));  
           tab.addCell(cell_temp); 
           
           cell_temp = new PdfPCell(new Paragraph("保养单车达成率8%", fontChinese12));  
           tab.addCell(cell_temp); 
           
           cell_temp = new PdfPCell(new Paragraph("售后有责投诉(扣分项)", fontChinese12));  
           tab.addCell(cell_temp); 
           
           cell_temp = new PdfPCell(new Paragraph("售后得分", fontChinese12));  
           tab.addCell(cell_temp); 
           
           cell_temp = new PdfPCell(new Paragraph("总分", fontChinese12));  
           tab.addCell(cell_temp); 
           
           cell_temp = new PdfPCell(new Paragraph("是否参评", fontChinese12));  
           tab.addCell(cell_temp); 
           
           cell_temp = new PdfPCell(new Paragraph("取消参评原因", fontChinese12));  
           tab.addCell(cell_temp); 
           if(data!=null){
for (int i = 0; i < data.size(); i++) {
cell_temp = new PdfPCell(new Paragraph(data.get(i).get("RANKING")+"", fontChinese12));  
               tab.addCell(cell_temp);  
               cell_temp = new PdfPCell(new Paragraph(data.get(i).get("REGION_NAME")+"", fontChinese12));  
               tab.addCell(cell_temp);  
               cell_temp = new PdfPCell(new Paragraph(data.get(i).get("PROVINCE")+"", fontChinese12));  
               tab.addCell(cell_temp);  
               cell_temp = new PdfPCell(new Paragraph(data.get(i).get("DEALER_SHORTNAME")+"", fontChinese12));  
               tab.addCell(cell_temp);  
               cell_temp = new PdfPCell(new Paragraph(data.get(i).get("DEALER_CODE")+"", fontChinese12));  
               tab.addCell(cell_temp);
               
               cell_temp = new PdfPCell(new Paragraph(data.get(i).get("OPEN_DATE")+"", fontChinese12));  
               tab.addCell(cell_temp);
               
               cell_temp = new PdfPCell(new Paragraph(data.get(i).get("V1001")+"", fontChinese12));  
               tab.addCell(cell_temp);
               cell_temp = new PdfPCell(new Paragraph(data.get(i).get("V1002")+"", fontChinese12));  
               tab.addCell(cell_temp);
               
               cell_temp = new PdfPCell(new Paragraph(data.get(i).get("V1003")+"", fontChinese12));  
               tab.addCell(cell_temp);
               
               cell_temp = new PdfPCell(new Paragraph(data.get(i).get("V1004")+"", fontChinese12));  
               tab.addCell(cell_temp);
               
               cell_temp = new PdfPCell(new Paragraph(data.get(i).get("V1005")+"", fontChinese12));  
               tab.addCell(cell_temp);
               
               cell_temp = new PdfPCell(new Paragraph(data.get(i).get("V1006")+"", fontChinese12));  
               tab.addCell(cell_temp);
               
               cell_temp = new PdfPCell(new Paragraph(data.get(i).get("V1007")+"", fontChinese12));  
               tab.addCell(cell_temp);
               
               cell_temp = new PdfPCell(new Paragraph(data.get(i).get("V1008")+"", fontChinese12));  
               tab.addCell(cell_temp);
               
               cell_temp = new PdfPCell(new Paragraph(data.get(i).get("V1999")+"", fontChinese12));  
               tab.addCell(cell_temp);
               
               cell_temp = new PdfPCell(new Paragraph(data.get(i).get("V2001")+"", fontChinese12));  
               tab.addCell(cell_temp);
               
               cell_temp = new PdfPCell(new Paragraph(data.get(i).get("V2002")+"", fontChinese12));  
               tab.addCell(cell_temp);
               
               cell_temp = new PdfPCell(new Paragraph(data.get(i).get("V2003")+"", fontChinese12));  
               tab.addCell(cell_temp);
               
               cell_temp = new PdfPCell(new Paragraph(data.get(i).get("V2004")+"", fontChinese12));  
               tab.addCell(cell_temp);
               cell_temp = new PdfPCell(new Paragraph(data.get(i).get("V2005")+"", fontChinese12));  
               tab.addCell(cell_temp);
               cell_temp = new PdfPCell(new Paragraph(data.get(i).get("V2006")+"", fontChinese12));  
               tab.addCell(cell_temp);
               
               cell_temp = new PdfPCell(new Paragraph(data.get(i).get("V2007")+"", fontChinese12));  
               tab.addCell(cell_temp);
               cell_temp = new PdfPCell(new Paragraph(data.get(i).get("V2008")+"", fontChinese12));  
               tab.addCell(cell_temp);
               
               cell_temp = new PdfPCell(new Paragraph(data.get(i).get("V2999")+"", fontChinese12));  
               tab.addCell(cell_temp);
               
               cell_temp = new PdfPCell(new Paragraph(data.get(i).get("V9999")+"", fontChinese12));  
               tab.addCell(cell_temp);
if(data.get(i).get("IS_EVALUATING")=="10041002"){
cell_temp = new PdfPCell(new Paragraph("否", fontChinese12));  
               tab.addCell(cell_temp);
}else{
cell_temp = new PdfPCell(new Paragraph("是", fontChinese12));  
               tab.addCell(cell_temp);
}
cell_temp = new PdfPCell(new Paragraph(data.get(i).get("CANCEL_REASON")+"", fontChinese12));  
               tab.addCell(cell_temp);
}
           }
           return tab;  
       } catch (Exception e) {  
           e.printStackTrace();  
           return null;  
       }  
   } 
private List<Map<String, Object>> tabData(){  
 
   String DEPT_NAME=CommonUtils.checkNull(reqUtil.getString("DEPT_NAME"));//区域
String DEALER_ID=CommonUtils.checkNull(reqUtil.getString("DEALER_ID"));//店代码
String DEALER_NAME=CommonUtils.checkNull(reqUtil.getString("DEALER_NAME"));//店简称
String year = CommonUtils.checkNull(reqUtil.getString("year"));//年度
String monthBegin = CommonUtils.checkNull(reqUtil.getString("monthBegin"));//月份
Map<String, String> passMap = new HashMap<String, String>();
passMap.put("DEPT_NAME", DEPT_NAME);
passMap.put("DEALER_ID", DEALER_ID);
passMap.put("year", year);
passMap.put("DEALER_NAME", DEALER_NAME);
passMap.put("monthBegin", monthBegin);
PageResult<Map<String, Object>> ps = dao.assessmentRangeSearch(passMap, Constant.PAGE_SIZE_ALL, 1);
List<Map<String, Object>> records =  ps.getRecords();
return records;  
   }  

 //水印

public class Watermark extends PdfPageEventHelper  {
Font FONT = new Font("",24, Font.BOLD, new GrayColor(0.75f));
    private String waterCont;//水印内容  
    public Watermark() {  
  
    }  
    public Watermark(String waterCont) {  
        this.waterCont = waterCont;  
    }  
  @Override
    public void onEndPage(PdfWriter writer, Document document) {  
        for(int i=0 ; i<20; i++) {  
            for(int j=0; j<28; j++) {  
                ColumnText.showTextAligned(writer.getDirectContentUnder(),  
                                           Element.ALIGN_CENTER,  
                                           new Phrase(this.waterCont == null ? "HELLO WORLD" : this.waterCont, FONT),  
                                           (50.5f+i*180),  
                                           (40.0f+j*130),  
                                           writer.getPageNumber() % 2 == 1 ? 45 : -45);  
            }  
        }  
    }  
}
原文地址:https://www.cnblogs.com/liuhx/p/8287932.html