您的位置:首页 > 其它

使用itext组件生成PDF文件

2012-04-26 13:20 701 查看
.

.

.

.

.

这几天需要做一个单据打印功能,没有找到好的办法,于是只能采用生成PDF文件,然后由客户端下载到本地进行打印,如果使用Chrome浏览器还能支持在线打印预览。

那么在这里笔者跟大家分享一下使用iText组件的方法,适用于从没有接触过iText的新手,老手请飘过。

这里纯属笔者从实践中所得的经验,如有错误或疏忽之处还请读者指正。

首先从iText的官网下载这个开源的小组件。

iText官方网站

Java版iText组件

Java版工具包

C#版iText组件

C#版工具包

这里笔者使用的是Java版itext-5.2.1。

将itext-5.2.1.zip压缩包解压缩后得到7个文件:itextpdf-5.2.1.jar(核心组件)、itextpdf-5.2.1-javadoc.jar(API文档)、itextpdf-5.2.1-sources.jar(源代码)、itext-xtra-5.2.1.jar、itext-xtra-5.2.1-javadoc.jar、itext-xtra-5.2.1-sources.jar

使用5步即可生成一个简单的PDF文档。

// 1.创建 Document 对象
Document _document = new Document();
// 2.创建书写器,通过书写器将文档写入磁盘
PdfWriter _pdfWriter = PdfWriter.getInstance(_document, new FileOutputStream("生成文件的路径"));
// 3.打开文档
_document.open();
// 4.向文档中添加内容
_document.add(new Paragraph("Hi"));
// 5.关闭文档
_document.close();


OK,搞定,不出问题的话就会在你指定的路径中生成一个PDF文档,内容是纯文本的“Hi”。

可是这样并不能完全满足我们的需求,因为通常我们要生成的PDF文件不一定是纯文本格式的,比如我现在要实现打印销售单的功能,那么最起码需要绘制表格才行,怎么办呢?且跟笔者继续向下研究。

在iText中,有专门的表格类,即PdfPTable类。笔者做了一个简单的表格示例,请先看代码:

1 OutTradeList _otl = this.getOtlBiz().findOutTradeListById(this.getOtlid());
String _fileName = _otl.getOtlId() + ".pdf";

// iText 处理中文
BaseFont _baseFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", true);
// 1.创建 Document 对象
Document _document = new Document(PageSize.A4);

HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("application/pdf; charset=ISO-8859-1");
response.setHeader("Content-Disposition", "inline; filename=" + new String(_fileName.getBytes(), "iso8859-1"));

// 2.创建书写器,通过书写器将文档写入磁盘
PdfWriter _pdfWriter = null;
try {
_pdfWriter = PdfWriter.getInstance(_document, response.getOutputStream());
} catch (Exception e) {
this.setMessage("单据生成失败,请检查服务器目录权限配置是否正确");
e.printStackTrace();
System.out.println("2.挂了");
//   return INPUT;
return null;
}
if(_pdfWriter == null) {
this.setMessage("单据生成失败,请检查服务器目录权限配置是否正确");
System.out.println("3.挂了");
//   return INPUT;
return null;
}

// 3.打开文档
_document.open();

// 4.创建需要填入文档的元素
PdfPTable _table = new PdfPTable(4);
PdfPCell _cell = null;

_table.addCell(new Paragraph("单据号", new Font(_baseFont)));
_cell = new PdfPCell(new Paragraph(_otl.getOtlId()));
_cell.setColspan(3);
_table.addCell(_cell);

_table.addCell(new Paragraph("客户名称", new Font(_baseFont)));
_cell = new PdfPCell(new Paragraph(_otl.getClients().getName(), new Font(_baseFont)));
_cell.setColspan(3);
_table.addCell(_cell);

_table.addCell(new Paragraph("销售日期", new Font(_baseFont)));
_cell = new PdfPCell(new Paragraph(_otl.getOutDate().toString()));
_cell.setColspan(3);
_table.addCell(_cell);

_cell = new PdfPCell();
_cell.setColspan(4);
PdfPTable _tabGoods = new PdfPTable(7);
// 添加标题行
_tabGoods.setHeaderRows(1);
_tabGoods.addCell(new Paragraph("序号", new Font(_baseFont)));
_tabGoods.addCell(new Paragraph("商品名称", new Font(_baseFont)));
_tabGoods.addCell(new Paragraph("自定义码", new Font(_baseFont)));
_tabGoods.addCell(new Paragraph("规格", new Font(_baseFont)));
_tabGoods.addCell(new Paragraph("数量", new Font(_baseFont)));
_tabGoods.addCell(new Paragraph("单价", new Font(_baseFont)));
_tabGoods.addCell(new Paragraph("小计", new Font(_baseFont)));
Object[] _outTrades = _otl.getOutTrades().toArray();
66 // 将商品销售详细信息加入表格
for(int i = 0; i < _outTrades.length;) {
if((_outTrades[i] != null) && (_outTrades[i] instanceof OutTrade)) {
OutTrade _ot = (OutTrade) _outTrades[i];
Goods _goods = _ot.getGoods();
_tabGoods.addCell(String.valueOf((++i)));
_tabGoods.addCell(new Paragraph(_goods.getName(), new Font(_baseFont)));
_tabGoods.addCell(_goods.getUserCode());
_tabGoods.addCell(_goods.getEtalon());
_tabGoods.addCell(String.valueOf(_ot.getNum()));
_tabGoods.addCell(String.valueOf(_ot.getPrice()));
_tabGoods.addCell(String.valueOf((_ot.getNum() * _ot.getPrice())));
}
}
_cell.addElement(_tabGoods);
_table.addCell(_cell);

_table.addCell(new Paragraph("总计", new Font(_baseFont)));
_cell = new PdfPCell(new Paragraph(_otl.getAllPrice().toString()));
_cell.setColspan(3);
_table.addCell(_cell);

_table.addCell(new Paragraph("操作员", new Font(_baseFont)));
_cell = new PdfPCell(new Paragraph(_otl.getProcure()));
_cell.setColspan(3);
_table.addCell(_cell);

// 5.向文档中添加内容,将表格加入文档中
_document.add(_table);

// 6.关闭文档
_document.close();
System.out.println(_fileName);
this.setPdfFilePath(_fileName);
System.out.println("3.搞定");
// return SUCCESS;
return null;


以上代码是写在 Struts2 的 Action 中的,当用户发送了请求之后直接将生成的PDF文件用输出流写入到客户端,浏览器收到服务器的响应之后就会询问用户打开方式。

当然,我们也可以将文件写入磁盘等等。

以下是效果图:



虽然磕碜了点,不过最基本表格已经按照我们预设的样子出现了。

好了,先写到这里,有什么问题大家可以多查查API文档,剩下的样式问题自己可以慢慢调整。

iText 还有很多强大的功能,由于笔者也是边学边用,在这里也只是把学习的结果记录下来给小菜们作为一些参考,如果有什么不正确的地方,还请各位不吝赐教。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: