最新项目中客户要求把一些表格导成PDF发邮件形式发送给特定的用户,本文是利用iText生成PDF,采用策略模式解藕。
所需要引入的包:iText-2.0.8.jar
PdfUtilsBean接口类代码如下:
/**
*@author liudonggan Email:ldg123456@sina.com
*@date 2011-7-18 上午10:47:53
*@version v1.0
**/
public interface PdfUtilsBean {
public void CreatePdf(String fileName,String picPath,int id);
}
PdfRoadKbUtilsBean实现类(implements PdfUtilsBean)代码如下:
package com.ys.product.bean;
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
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.ys.product.service.ISgKbRoadDetailService;
import com.ys.util.Utils;
/**
*@author liudonggan Email:ldg123456@sina.com
*@date 2011-7-18 上午10:53:31
*@version v1.0
**/
public class PdfRoadKbUtilsBean implements PdfUtilsBean {
private SgKbRoadDetail sgKbRoadDetail=new SgKbRoadDetail();
private ISgKbRoadDetailService sgKbRoadDetailService;
public PdfRoadKbUtilsBean(ISgKbRoadDetailService sgKbRoadDetailService)
{
this.sgKbRoadDetailService=sgKbRoadDetailService;
}
private final int CELLHIGHT=30;//行高
public void CreatePdf(String fileName,String picPath,int id) {
// TODO Auto-generated method stub
File file=new File(fileName);
FileOutputStream out = null;
sgKbRoadDetail=sgKbRoadDetailService.findKbRoad(id);
try
{
Document document=new Document(PageSize.A4, 50, 50, 50, 50);
out = new FileOutputStream(file);
PdfWriter writer = PdfWriter.getInstance(document, out);
document.open();
Image img=Image.getInstance(picPath+"\\gdcd.jpg");
img.scalePercent(70f);
img.setAlignment(Element.ALIGN_CENTER);
document.add(img);
Paragraph title1 = new Paragraph("\n");
document.add(title1);
img=Image.getInstance(picPath+"\\gdcd1.jpg");
img.scalePercent(70f);
img.setAlignment(Element.ALIGN_CENTER);
document.add(img);
Font font = FontFactory.getFont(FontFactory.HELVETICA,
18, Font.BOLD, new Color(0, 0, 0));
BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);//设置中文字体
//中文大小为20,加粗
font = new Font(bfChinese, 20, Font.BOLD);
Paragraph title = new Paragraph("XX标题",font);
title.setAlignment(Element.ALIGN_CENTER);
document.add(title);
title1 = new Paragraph("\n");
document.add(title1);
//创建表格对象
PdfPTable table = new PdfPTable(6);
int[] cellsWidth = {10,10,10,10,10,10};
table.setWidths(cellsWidth);
table.setWidthPercentage(100);
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
table.getDefaultCell().setFixedHeight(CELLHIGHT);
//设置表格边框颜色
table.getDefaultCell().setBackgroundColor(new Color(0, 0, 0));
//设置单元格的边距间隔等
table.getDefaultCell().setPadding(0);
table.getDefaultCell().setBorderWidth(0);
//单元格对象
PdfPCell cell = new PdfPCell();
Font font1 = new Font(bfChinese, 9, Font.HELVETICA);
Paragraph cel = new Paragraph("填报单位:"+sgKbRoadDetail.getUnitName(),font1);
cel.setAlignment(Element.ALIGN_CENTER);
cell=new PdfPCell(cel);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setColspan(6);
cell.setBorder(0);
table.addCell(cell);
cel = new Paragraph("事故基本情况",font1);
cel.setAlignment(Element.ALIGN_CENTER);
cell=new PdfPCell(cel);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置内容水平居中显示
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setColspan(6);
cell.setFixedHeight(CELLHIGHT);
table.addCell(cell);
cel = new Paragraph("事故发生时间",font1);
cel.setAlignment(Element.ALIGN_CENTER);
cell=new PdfPCell(cel);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置内容水平居中显示
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setColspan(1);
cell.setFixedHeight(CELLHIGHT);
table.addCell(cell);
cel = new Paragraph(Utils.getMinute2(sgKbRoadDetail.getFssj()),font1);
cel.setAlignment(Element.ALIGN_CENTER);
cell=new PdfPCell(cel);
cell.setColspan(3);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setNoWrap(true);
table.addCell(cell);
cel = new Paragraph("天气情况",font1);
cel.setAlignment(Element.ALIGN_CENTER);
cell=new PdfPCell(cel);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置内容水平居中显示
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setColspan(1);
table.addCell(cell);
cel = new Paragraph(sgKbRoadDetail.getTqqk(),font1);
cel.setAlignment(Element.ALIGN_CENTER);
cell=new PdfPCell(cel);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setColspan(1);
table.addCell(cell);
cel = new Paragraph("事故地点",font1);
cel.setAlignment(Element.ALIGN_CENTER);
cell=new PdfPCell(cel);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置内容水平居中显示
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setColspan(1);
cell.setFixedHeight(CELLHIGHT);
table.addCell(cell);
cel = new Paragraph(sgKbRoadDetail.getSgdd(),font1);
cel.setAlignment(Element.ALIGN_CENTER);
cell=new PdfPCell(cel);
cell.setColspan(3);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setFixedHeight(CELLHIGHT);
cell.setNoWrap(true);
table.addCell(cell);
//将表格对象添加到小节对象中
document.add(table);
document.close();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
if (out != null){
try {
//关闭输出文件流
out.close();
} catch (IOException e1) {
}
}
}
}
}
PdfUtils类 代码如下
import java.io.File;
/**
*@author liudonggan Email:ldg123456@sina.com
*@date 2011-7-18 上午10:50:48
*@version v1.0
**/
public class PdfUtils {
private PdfUtilsBean pdfUtilsBean=null;
private String fileName="";
private String picPath="";
private int id=-1;
public PdfUtils(PdfUtilsBean pdfUtilsBean,String fileName,String picPath,int id)
{
this.pdfUtilsBean=pdfUtilsBean;
this.fileName=fileName;
this.picPath=picPath;
this.id=id;
}
public void CreatePdf()
{
this.pdfUtilsBean.CreatePdf(fileName,picPath,id);
}
public void DeletePdf() throws Exception
{
File file=new File(fileName);
if(file.exists())
{
file.delete();
}
}
}