在B/S结构的项目中,经常需要将一些数据导出为PDF文档,项目中我们使用的就是iText包,该包可以从其官方网上下载: http://www.lowagie.com/iText/ 。
下面就来讲一下具体的使用方法。
1、首先我们建一个叫做DbgridColumn的Java类,用来表示标题信息,具体字段如图。
java 代码
- List columns = new ArrayList();
- DbgridColumn column = new DbgridColumn();
- 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 ArrayList();
- Map map = new HashMap();
- 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 ReportModal
- {
- public PdfReport(OutputStream out,DbgridModal modal)
- {
- this.out = out;
- this.modal = modal;
- }
- public PdfReport()
- { }
- public void out()
- {
- Document document = new Document();
- try
- {
- public class PdfReport extends ReportModal
- {
- public PdfReport(OutputStream out,DbgridModal modal)
- {
- this.out = out;
- this.modal = modal;
- }
- public PdfReport()
- {
- }
- /* 输出 */
- public void out()
- {
- Document document = new Document();
- try
- {
- PdfWriter.getInstance(document, out);
- document.open();
- document.add(DbgridToPdfTable(modal.columns,modal.collection));
- }
- catch (DocumentException e)
- {
- throw new Error(e.getMessage(), e);
- }
- catch (IOException e)
- {
- throw new Error(e.getMessage(), e);
- }
- if(document.isOpen())
- {
- document.close();
- }
- public PdfPTable DbgridToPdfTable(List columns,Collection collection)
- throws BadElementException,DocumentException,IOException
- {
- int columnsCount = columns.size();
- int rowsCount = collection.size();
- //创建Pdf Table
- PdfPTable table = new PdfPTable(columnsCount);
- //table总宽度
- table.setTotalWidth((float)getTotalWidth(columns));
- table.setWidths(getWidths(columns));
- //每列之间的间隔
- table.setSpacingBefore(Float.parseFloat("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);
- //创建单元格
- PdfPCell cell = new PdfPCell(new Phrase(dbgridColumn.getCaption(), FontChinese ));
- //标题的背景颜色
- cell.setBackgroundColor(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(} {
- String value = "";
- DbgridColumn dbgridColumn = (DbgridColumn)columns.get(i);
- //根据字段名取数据
- Object bean = map.get(dbgridColumn.getProperty());
- //getRealValue函数是对具体的一些数据类型(比如日期、double、integer等)进行格式化处理,如果为null,则返回空字符串
- value = getRealValue(bean,dbgridColumn);
- //创建单元格
- PdfPCell cell = new PdfPCell(new Phrase(value, FontChinese ));
- //对齐方式
- cell.setHorizontalAlignment(getCellAlign(dbgridColumn.getAlign()));
- cell.setVerticalAlignment(Element.ALIGN_TOP);
- table.addCell(cell);
- }
- }
- return table;
- }
- /* 把字符串型对齐方式 left,right,center 转化为pdf格式的对齐方式 */
- public int getCellAlign(String align)
- {
- if (align==null)
- {
- return Cell.ALIGN_LEFT;
- }
- if (align.toLowerCase().equals("left"))
- {
- return Cell.ALIGN_LEFT;
- }
- if (align.toLowerCase().equals("right"))
- {
- return Cell.ALIGN_RIGHT;
- }
- if (align.toLowerCase().equals("center"))
- {
- return Cell.ALIGN_CENTER;
- }
- return Cell.ALIGN_LEFT;
- /* 返回一个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 widths;
- }
- /* 返回表格的总宽度 */
- public int getTotalWidth(java.util.List cols)
- {
- int totalwidth = 0;
- for (int i=0;i2N {
- DbgridColumn dbgridColumn = (DbgridColumn)cols.get(i);
- totalwidth = totalwidth + dbgridColumn.getWidth();
- }
- return totalwidth;
- }
- PdfWriter.getInstance(document, out);
- document.open();
- document.add(DbgridToPdfTable(modal.columns,modal.collection));
- }
- catch (DocumentException e)
- {
- throw new Error(e.getMessage(), e);
- }
- catch (IOException e)
- {
- throw new Error(e.getMessage(), e);
- }
- if(document.isOpen())
- {
- document.close();
- }
- public PdfPTable DbgridToPdfTable(List columns,Collection collection)
- throws BadElementException,DocumentException,IOException
- {
- int columnsCount = columns.size();
- int rowsCount = collection.size();
- //创建Pdf Table
- PdfPTable table = new PdfPTable(columnsCount);
- //table总宽度
- table.setTotalWidth((float)getTotalWidth(columns));
- //每列的宽度
- table.setWidths(getWidths(columns));
- //每列之间的间隔
- table.setSpacingBefore(Float.parseFloat("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);
- //创建单元格
- PdfPCell cell = new PdfPCell(new Phrase(dbgridColumn.getCaption(), FontChinese ));
- //标题的背景颜色
- cell.setBackgroundColor(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}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);
- //创建单元格
- PdfPCell cell = new PdfPCell(new Phrase(value, FontChinese ));
- //对齐方式
- cell.setVerticalAlignment(Element.ALIGN_TOP);
- table.addCell(cell);
- return table;
- }
- /* 把字符串型对齐方式 left,right,center 转化为pdf格式的对齐方式 */
- public int getCellAlign(String align)
- {
- if (align==null)
- {
- return Cell.ALIGN_LEFT;
- }
- if (align.toLowerCase().equals("left"))
- {
- return Cell.ALIGN_LEFT;
- }
- if (align.toLowerCase().equals("right"))
- {
- return Cell.ALIGN_RIGHT;
- }
- if (align.toLowerCase().equals("center"))
- {
- return Cell.ALIGN_CENTER;
- }
- return Cell.ALIGN_LEFT;
- }
- /* 返回一个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 widths;
- }
- /* 返回表格的总宽度 */
- public int getTotalWidth(java.util.List cols)
- {
- int totalwidth = 0;
- for (int i=0;i2 {
- DbgridColumn dbgridColumn = (DbgridColumn)cols.get(i);
- totalwidth = totalwidth + dbgridColumn.getWidth();
- return totalwidth;
- }
4、通过Servlet生成pdf文档,并弹出下载对话框。
java 代码
- public class ReportServlet extends HttpServlet
- public ReportServlet()
- {
- }
- <