java代码生成PDF文件-表格和图片

时间:2023-02-10 23:25:13
jar:iText-2.1.4.jar,iTextAsian.jar
Java代码 java代码生成PDF文件-表格和图片  java代码生成PDF文件-表格和图片java代码生成PDF文件-表格和图片
  1. package com.zjhcsoft.sim.util;     
  2.   
  3. import java.awt.Color;   
  4. import java.io.ByteArrayOutputStream;   
  5. import java.util.List;   
  6.   
  7. import javax.servlet.ServletOutputStream;   
  8. import javax.servlet.http.HttpServletRequest;   
  9. import javax.servlet.http.HttpServletResponse;   
  10.   
  11. import org.apache.log4j.Logger;   
  12.   
  13. import com.lowagie.text.DocumentException;   
  14. import com.lowagie.text.Element;   
  15. import com.lowagie.text.Font;   
  16. import com.lowagie.text.Image;   
  17. import com.lowagie.text.PageSize;   
  18. import com.lowagie.text.Paragraph;   
  19. import com.lowagie.text.pdf.BaseFont;   
  20. import com.lowagie.text.pdf.PdfPCell;   
  21. import com.lowagie.text.pdf.PdfPTable;   
  22. import com.lowagie.text.pdf.PdfWriter;   
  23. import com.zjhcsoft.sim.device.bean.DeviceItems;   
  24. import com.zjhcsoft.sim.si.bean.SiPollrpt;   
  25.   
  26. /***  
  27.  * <p>Title: </p>   
  28.  *  
  29.  * <p>Description: </p>   
  30.  *  
  31.  * <p>Author:wangcheng </p>   
  32.  *  
  33.  * <p>Copyright: Copyright (c) 2013 </p>  
  34.  *   
  35.  * <p>Company: 浙江鸿程</p>  
  36.  *  
  37.  */  
  38. public class PollRptPdf {   
  39.     Logger logger = Logger.getLogger(PollRptPdf.class);   
  40.     public void downPdf( HttpServletRequest request,   
  41.             HttpServletResponse response,SiPollrpt siPollrpt,String filePath){   
  42.           try {   
  43.            List deviceList = siPollrpt.getDeviceItemList();   
  44.            // 创建一个Document对象   
  45.            com.lowagie.text.Document document = new com.lowagie.text.Document(PageSize.A4, 36363636);   
  46.            ByteArrayOutputStream ba = new ByteArrayOutputStream();   
  47.            //设置宋体   
  48.            BaseFont bfChinese = BaseFont.createFont("STSongStd-Light""UniGB-UCS2-H"true);    
  49.            Font fontChinese =new Font(bfChinese,15,Font.BOLD,Color.BLACK);   
  50.            Font normalfont =new Font(bfChinese,13,Font.NORMAL,Color.BLACK);   
  51.               
  52.            BaseFont bfChinese2 = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);//设置中文字体  
  53.            Font headFont2 = new Font(bfChinese2, 13, Font.NORMAL);//设置字体大小   
  54.               
  55.            try {    
  56.             //添加一些信息   
  57.             document.addTitle("平台巡检报告");   
  58.             PdfWriter writer = PdfWriter.getInstance(document, ba);   
  59.             document.open();   
  60.             Paragraph paragraphTitle1 = new Paragraph("平台巡检报告--("+siPollrpt.getAPPNAME()+")\n",fontChinese);   
  61.             paragraphTitle1.setAlignment(1);//1.中间,2.右边  
  62.                
  63.             Paragraph paragraphTitle2 = new Paragraph("操作员:"+siPollrpt.getUSERNAME()+" ("+siPollrpt.getYEAR()+"年"+siPollrpt.getQUARTERNAME()+")\n",normalfont);   
  64.             paragraphTitle2.setAlignment(2);//1.中间,2.右边  
  65.                
  66.             StringBuffer content1 = new StringBuffer("\n 1.设备运行情况检查总结");   
  67.             content1.append("\n\t").append(siPollrpt.getRPTDESC());   
  68.                
  69.             StringBuffer content2 = new StringBuffer("\n 2.系统网络结构\n\t");   
  70.              Image image = Image.getInstance(request.getRealPath("\\") + siPollrpt.getFILEPATH());    
  71.             StringBuffer content3 = new StringBuffer("\n 3.设备具体检查情况");   
  72.                
  73.                
  74.             Paragraph paragraphContent1 = new Paragraph(content1.toString(),normalfont);   
  75.             Paragraph paragraphContent2 = new Paragraph(content2.toString(),normalfont);   
  76.             Paragraph paragraphContent3 = new Paragraph(content3.toString(),normalfont);   
  77.                
  78.             document.add(paragraphTitle1);   
  79.             document.add(paragraphTitle2);   
  80.                
  81.             document.add(paragraphContent1);   
  82.             document.add(paragraphContent2);   
  83.             document.add(image);   
  84.             document.add(paragraphContent3);   
  85.                
  86.                
  87.                
  88.             DeviceItems items  = null;   
  89.             for(int i=0;i<deviceList.size();i++){   
  90.                 float[] widths = {70f, 180f, 500f};//设置表格的列宽  
  91.                 PdfPTable table = new PdfPTable(widths);   
  92.                 table.setTotalWidth(750);//设置表格的宽度     
  93.                 table.setSpacingBefore(40f);//设置表格上面空白宽度  
  94.                    
  95.                 items = (DeviceItems)deviceList.get(i);   
  96.                 PdfPCell cellHType = new PdfPCell(new Paragraph(items.getSERVERTYPENAME(),headFont2));   
  97.                 cellHType.setColspan(3);   
  98.                 table.addCell(cellHType);   
  99.                    
  100.                 PdfPCell cellH = new PdfPCell(new Paragraph("编号",headFont2));   
  101.                 cellH.setHorizontalAlignment(Element.ALIGN_CENTER);   
  102.                 table.addCell(cellH);   
  103.                 cellH= new PdfPCell(new Paragraph("检查项目",headFont2));   
  104.                 cellH.setHorizontalAlignment(Element.ALIGN_CENTER);   
  105.                 table.addCell(cellH);   
  106.                 cellH = new PdfPCell(new Paragraph("检查结果",headFont2));   
  107.                 cellH.setHorizontalAlignment(Element.ALIGN_CENTER);   
  108.                 table.addCell(cellH);   
  109.                    
  110.                 PdfPCell cellContent = null;   
  111.                 for(int j=1;j<5;j++){   
  112.                     if(j==1){   
  113.                         cellContent = new PdfPCell(new Paragraph((i+1)+"."+j,headFont2));   
  114.                         table.addCell(cellContent);   
  115.                         cellContent = new PdfPCell(new Paragraph("CPU占用率检查",headFont2));   
  116.                         table.addCell(cellContent);   
  117.                         cellContent = new PdfPCell(new Paragraph(items.getCPU(),headFont2));   
  118.                         table.addCell(cellContent);   
  119.                     }else if(j==2){   
  120.                         cellContent = new PdfPCell(new Paragraph((i+1)+"."+j,headFont2));   
  121.                         table.addCell(cellContent);   
  122.                         cellContent = new PdfPCell(new Paragraph("内存检查",headFont2));   
  123.                         table.addCell(cellContent);   
  124.                         cellContent = new PdfPCell(new Paragraph(items.getMEM(),headFont2));   
  125.                         table.addCell(cellContent);   
  126.                     }else if(j==3){   
  127.                         cellContent = new PdfPCell(new Paragraph((i+1)+"."+j,headFont2));   
  128.                         table.addCell(cellContent);   
  129.                         cellContent = new PdfPCell(new Paragraph("本地硬盘容量检查",headFont2));   
  130.                         table.addCell(cellContent);   
  131.                         cellContent = new PdfPCell(new Paragraph(items.getDISK(),headFont2));   
  132.                         table.addCell(cellContent);   
  133.                     }else if(j==4){   
  134.                         cellContent = new PdfPCell(new Paragraph((i+1)+"."+j,headFont2));   
  135.                         table.addCell(cellContent);   
  136.                         cellContent = new PdfPCell(new Paragraph("进程检查",headFont2));   
  137.                         table.addCell(cellContent);   
  138.                         cellContent = new PdfPCell(new Paragraph(items.getPROCESS(),headFont2));   
  139.                         table.addCell(cellContent);   
  140.                     }   
  141.                 }   
  142.                 document.add(table);   
  143.             }   
  144.                
  145.            } catch (DocumentException de) {   
  146.             de.printStackTrace();   
  147.            }   
  148.            document.close();   
  149.            response.setHeader("Content-disposition""attachment; filename=" + new String(filePath.getBytes("GBK"), "iso8859-1"));   
  150.            response.setContentType("application/pdf");   
  151.            response.setContentLength(ba.size());   
  152.            ServletOutputStream out = response.getOutputStream();   
  153.            ba.writeTo(out);   
  154.            out.flush();   
  155.            out.close();   
  156.           } catch (Exception e) {   
  157.            e.printStackTrace();   
  158.           }   
  159.     }   
  160.   
  161. }