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

jar:iText-2.1.4.jar,iTextAsian.jar
Java代码 复制代码  收藏代码
  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. }  
package com.zjhcsoft.sim.util;  

import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import com.zjhcsoft.sim.device.bean.DeviceItems;
import com.zjhcsoft.sim.si.bean.SiPollrpt;

/***
 * <p>Title: </p> 
 *
 * <p>Description: </p> 
 *
 * <p>Author:wangcheng </p> 
 *
 * <p>Copyright: Copyright (c) 2013 </p>
 * 
 * <p>Company: 浙江鸿程</p>
 *
 */
public class PollRptPdf {
	Logger logger = Logger.getLogger(PollRptPdf.class);
	public void downPdf( HttpServletRequest request,
            HttpServletResponse response,SiPollrpt siPollrpt,String filePath){
		  try {
		   List deviceList = siPollrpt.getDeviceItemList();
		   // 创建一个Document对象
		   com.lowagie.text.Document document = new com.lowagie.text.Document(PageSize.A4, 36, 36, 36, 36);
		   ByteArrayOutputStream ba = new ByteArrayOutputStream();
		   //设置宋体
		   BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", true); 
		   Font fontChinese =new Font(bfChinese,15,Font.BOLD,Color.BLACK);
		   Font normalfont =new Font(bfChinese,13,Font.NORMAL,Color.BLACK);
		   
		   BaseFont bfChinese2 = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);//设置中文字体
		   Font headFont2 = new Font(bfChinese2, 13, Font.NORMAL);//设置字体大小 
		   
		   try { 
		    //添加一些信息
		    document.addTitle("平台巡检报告");
		    PdfWriter writer = PdfWriter.getInstance(document, ba);
		    document.open();
			Paragraph paragraphTitle1 = new Paragraph("平台巡检报告--("+siPollrpt.getAPPNAME()+")\n",fontChinese);
			paragraphTitle1.setAlignment(1);//1.中间,2.右边
		    
		    Paragraph paragraphTitle2 = new Paragraph("操作员:"+siPollrpt.getUSERNAME()+" ("+siPollrpt.getYEAR()+"年"+siPollrpt.getQUARTERNAME()+")\n",normalfont);
		    paragraphTitle2.setAlignment(2);//1.中间,2.右边
		    
			StringBuffer content1 = new StringBuffer("\n 1.设备运行情况检查总结");
			content1.append("\n\t").append(siPollrpt.getRPTDESC());
			
			StringBuffer content2 = new StringBuffer("\n 2.系统网络结构\n\t");
			 Image image = Image.getInstance(request.getRealPath("\\") + siPollrpt.getFILEPATH()); 
			StringBuffer content3 = new StringBuffer("\n 3.设备具体检查情况");
			
			
			Paragraph paragraphContent1 = new Paragraph(content1.toString(),normalfont);
			Paragraph paragraphContent2 = new Paragraph(content2.toString(),normalfont);
			Paragraph paragraphContent3 = new Paragraph(content3.toString(),normalfont);
			
			document.add(paragraphTitle1);
			document.add(paragraphTitle2);
			
			document.add(paragraphContent1);
			document.add(paragraphContent2);
			document.add(image);
			document.add(paragraphContent3);
			
			
			
			DeviceItems items  = null;
			for(int i=0;i<deviceList.size();i++){
				float[] widths = {70f, 180f, 500f};//设置表格的列宽
				PdfPTable table = new PdfPTable(widths);
				table.setTotalWidth(750);//设置表格的宽度   
				table.setSpacingBefore(40f);//设置表格上面空白宽度
				
				items = (DeviceItems)deviceList.get(i);
				PdfPCell cellHType = new PdfPCell(new Paragraph(items.getSERVERTYPENAME(),headFont2));
				cellHType.setColspan(3);
				table.addCell(cellHType);
				
				PdfPCell cellH = new PdfPCell(new Paragraph("编号",headFont2));
				cellH.setHorizontalAlignment(Element.ALIGN_CENTER);
				table.addCell(cellH);
				cellH= new PdfPCell(new Paragraph("检查项目",headFont2));
				cellH.setHorizontalAlignment(Element.ALIGN_CENTER);
				table.addCell(cellH);
				cellH = new PdfPCell(new Paragraph("检查结果",headFont2));
				cellH.setHorizontalAlignment(Element.ALIGN_CENTER);
				table.addCell(cellH);
				
				PdfPCell cellContent = null;
				for(int j=1;j<5;j++){
					if(j==1){
						cellContent = new PdfPCell(new Paragraph((i+1)+"."+j,headFont2));
						table.addCell(cellContent);
						cellContent = new PdfPCell(new Paragraph("CPU占用率检查",headFont2));
						table.addCell(cellContent);
						cellContent = new PdfPCell(new Paragraph(items.getCPU(),headFont2));
						table.addCell(cellContent);
					}else if(j==2){
						cellContent = new PdfPCell(new Paragraph((i+1)+"."+j,headFont2));
						table.addCell(cellContent);
						cellContent = new PdfPCell(new Paragraph("内存检查",headFont2));
						table.addCell(cellContent);
						cellContent = new PdfPCell(new Paragraph(items.getMEM(),headFont2));
						table.addCell(cellContent);
					}else if(j==3){
						cellContent = new PdfPCell(new Paragraph((i+1)+"."+j,headFont2));
						table.addCell(cellContent);
						cellContent = new PdfPCell(new Paragraph("本地硬盘容量检查",headFont2));
						table.addCell(cellContent);
						cellContent = new PdfPCell(new Paragraph(items.getDISK(),headFont2));
						table.addCell(cellContent);
					}else if(j==4){
						cellContent = new PdfPCell(new Paragraph((i+1)+"."+j,headFont2));
						table.addCell(cellContent);
						cellContent = new PdfPCell(new Paragraph("进程检查",headFont2));
						table.addCell(cellContent);
						cellContent = new PdfPCell(new Paragraph(items.getPROCESS(),headFont2));
						table.addCell(cellContent);
					}
				}
				document.add(table);
			}
			
		   } catch (DocumentException de) {
		    de.printStackTrace();
		   }
		   document.close();
		   response.setHeader("Content-disposition", "attachment; filename=" + new String(filePath.getBytes("GBK"), "iso8859-1"));
		   response.setContentType("application/pdf");
		   response.setContentLength(ba.size());
		   ServletOutputStream out = response.getOutputStream();
		   ba.writeTo(out);
		   out.flush();
		   out.close();
		  } catch (Exception e) {
		   e.printStackTrace();
		  }
	}

}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值