在B/S结构的项目中,经常需要将一些数据导出为PDF文档,项目中我们使用的就是iText包,该包可以从其官方网上下载: http://www.lowagie.com/iText/ 。
下面就来讲一下具体的使用方法。
1、首先我们建一个叫做DbgridColumn的Java类,用来表示标题信息,具体字段如图。
java 代码
List columns = new
DbgridColumn column = new
column.setProperty = "DM";
column.setCaption = "代码";
column.setWidth = 35;
columns.add(column);
column.setProperty = "MC";
column.setCaption = "名称";
column.setWidth = 175;
columns.add(column);
2、我们需要构建演示数据,数据列表与Java构建函数。
java 代码
Collection collection = new
Map map = new
map.put("DM",new String("01");
map.put("MC",new String("设置成功");
collection.add(map);
map.put("DM",new String("02");
map.put("MC",new String("中继命令没有返回");
collection.add(map);
map.put("DM",new String("03");
map.put("MC",new String("设置内容非法");
collection.add(map);
map.put("DM",new String("04");
map.put("MC",new String("密码权限不足");
collection.add(map);
map.put("DM",new String("05");
map.put("MC",new String("无此数据项");
collection.add(map);
map.put("DM",new String("06");
map.put("MC",new String("命令时间失效");
collection.add(map);
map.put("DM",new String("07");
map.put("MC",new String("目标地址不存在");
collection.add(map);
map.put("DM",new String("08");
map.put("MC",new String("发送失败");
collection.add(map);
map.put("DM",new String("09");
map.put("MC",new String("短消息帧太长");
collection.add(map);
3、将上面格式的数据转换为iText的PdfPTable格式。
java 代码
public class PdfReport extends
{
public
{
this.out = out;
this.modal = modal;
}
public
{ }
public void
{
Document document = new
try
{
public class PdfReport extends
{
public
{
this.out = out;
this.modal = modal;
}
public
{
}
/* 输出 */
public void
{
new
try
{
PdfWriter.getInstance(document, out);
document.open();
document.add(DbgridToPdfTable(modal.columns,modal.collection));
}
catch
{
throw new
}
catch
{
throw new
}
if(document.isOpen())
{
document.close();
}
public
throws
{
int
int
//创建Pdf Table
new
//table总宽度
table.setTotalWidth((float)getTotalWidth(columns));
table.setWidths(getWidths(columns));
//每列之间的间隔
table.setSpacingBefore(Float.parseFloat("1.0"));
//使用中文字符,要支持中文字符,还需要在其官方网站下载iTextAsian.jar和iTextAsianCmaps.jar 两个包
"STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
new com.lowagie.text.Font(bfChinese, 9, com.lowagie.text.Font.NORMAL);
//开始写标题
for (int i=0;i
{
DbgridColumn dbgridColumn = (DbgridColumn)columns.get(i);
//创建单元格
new PdfPCell(new
//标题的背景颜色
new Color(212,208,200));
//左右对齐方式
cell.setHorizontalAlignment(getCellAlign(dbgridColumn.getAlign()));
//垂直对齐方式默认为中间对齐
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
}
//写表格数据
Object[] objects = (Object[])collection.toArray();
for (int j=0;j,@gdE} {
Map map = (Map)objects[j];
for (int i=0;i&Uo(} {
"";
DbgridColumn dbgridColumn = (DbgridColumn)columns.get(i);
//根据字段名取数据
Object bean = map.get(dbgridColumn.getProperty());
//getRealValue函数是对具体的一些数据类型(比如日期、double、integer等)进行格式化处理,如果为null,则返回空字符串
value = getRealValue(bean,dbgridColumn);
//创建单元格
new PdfPCell(new
//对齐方式
cell.setHorizontalAlignment(getCellAlign(dbgridColumn.getAlign()));
cell.setVerticalAlignment(Element.ALIGN_TOP);
table.addCell(cell);
}
}
return
}
/* 把字符串型对齐方式 left,right,center 转化为pdf格式的对齐方式 */
public int
{
if (align==null)
{
return
}
if (align.toLowerCase().equals("left"))
{
return
}
if (align.toLowerCase().equals("right"))
{
return
}
if (align.toLowerCase().equals("center"))
{
return
}
return
/* 返回一个int型的数组,存放每列的宽度 */
public int[] getWidths(java.util.List cols)
{
int widths[]=new int[cols.size()];
for (int i=0;i
{
DbgridColumn dbgridColumn = (DbgridColumn)cols.get(i);
widths[i] = dbgridColumn.getWidth();
}
return
}
/* 返回表格的总宽度 */
public int
{
int totalwidth = 0;
for (int i=0;i2N {
DbgridColumn dbgridColumn = (DbgridColumn)cols.get(i);
totalwidth = totalwidth + dbgridColumn.getWidth();
}
return
}
PdfWriter.getInstance(document, out);
document.open();
document.add(DbgridToPdfTable(modal.columns,modal.collection));
}
catch
{
throw new
}
catch
{
throw new
}
if(document.isOpen())
{
document.close();
}
public
throws
{
int
int
//创建Pdf Table
new
//table总宽度
float)getTotalWidth(columns));
//每列的宽度
//每列之间的间隔
"1.0"));
//使用中文字符,要支持中文字符,还需要在其官方网站下载iTextAsian.jar和iTextAsianCmaps.jar 两个包
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
com.lowagie.text.Font FontChinese = new com.lowagie.text.Font(bfChinese, 9, com.lowagie.text.Font.NORMAL);
//开始写标题
for (int i=0;i
{
DbgridColumn dbgridColumn = (DbgridColumn)columns.get(i);
//创建单元格
new PdfPCell(new
//标题的背景颜色
new Color(212,208,200));
//左右对齐方式
cell.setHorizontalAlignment(getCellAlign(dbgridColumn.getAlign()));
//垂直对齐方式默认为中间对齐
table.addCell(cell);
}
//写表格数据
Object[] objects = (Object[])collection.toArray();
for (int j=0;j,@gdE}Guest {
Map map = (Map)objects[j]; for (int i=0; {
String value = "";
DbgridColumn dbgridColumn = (DbgridColumn)columns.get(i);
//根据字段名取数据
Object bean = map.get(dbgridColumn.getProperty());
//getRealValue函数是对具体的一些数据类型(比如日期、double、integer等)进行格式化处理,如果为null,则返回空字符串
value = getRealValue(bean,dbgridColumn);
//创建单元格
new PdfPCell(new
//对齐方式
cell.setVerticalAlignment(Element.ALIGN_TOP);
table.addCell(cell);
return
}
/* 把字符串型对齐方式 left,right,center 转化为pdf格式的对齐方式 */
public int
{
if (align==null)
{
return
}
if (align.toLowerCase().equals("left"))
{
return
}
if (align.toLowerCase().equals("right"))
{
return
}
if (align.toLowerCase().equals("center"))
{
return
}
return
}
/* 返回一个int型的数组,存放每列的宽度 */
public int[] getWidths(java.util.List cols)
{
int widths[]=new int[cols.size()];
for (int i=0;i
{
DbgridColumn dbgridColumn = (DbgridColumn)cols.get(i);
widths[i] = dbgridColumn.getWidth();
}
return
}
/* 返回表格的总宽度 */
public int
{
int totalwidth = 0;
for (int i=0;i2 {
DbgridColumn dbgridColumn = (DbgridColumn)cols.get(i);
totalwidth = totalwidth + dbgridColumn.getWidth();
return
}
4、通过Servlet生成pdf文档,并弹出下载对话框。
java 代码
public class ReportServlet extends
public
{
}
<