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

jar:iText-2.1.4.jar,iTextAsian.jar

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();
}
}

}




如有疑问,欢迎加入群:283948248 找群主
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值