如何运用Java组件itext生成pdf

 我来答
育知同创教育
2016-03-29 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
展开全部
  首先从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 // 1.创建 Document 对象
  2 Document _document = new Document();
  3 // 2.创建书写器,通过书写器将文档写入磁盘
  4 PdfWriter _pdfWriter = PdfWriter.getInstance(_document, new FileOutputStream("生成文件的路径"));
  5 // 3.打开文档
  6 _document.open();
  7 // 4.向文档中添加内容
  8 _document.add(new Paragraph("Hi"));
  9 // 5.关闭文档
  10 _document.close();
  复制代码
  OK,搞定,不出问题的话就会在你指定的路径中生成一个PDF文档,内容是纯文本的“Hi”。

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

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

  复制代码
  1 OutTradeList _otl = this.getOtlBiz().findOutTradeListById(this.getOtlid());
  2 String _fileName = _otl.getOtlId() + ".pdf";
  3
  4 // iText 处理中文
  5 BaseFont _baseFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", true);
  6 // 1.创建 Document 对象
  7 Document _document = new Document(PageSize.A4);
  8
  9 HttpServletResponse response = ServletActionContext.getResponse();
  10 response.setContentType("application/pdf; charset=ISO-8859-1");
  11 response.setHeader("Content-Disposition", "inline; filename=" + new String(_fileName.getBytes(), "iso8859-1"));
  12
  13 // 2.创建书写器,通过书写器将文档写入磁盘
  14 PdfWriter _pdfWriter = null;
  15 try {
  16 _pdfWriter = PdfWriter.getInstance(_document, response.getOutputStream());
  17 } catch (Exception e) {
  18 this.setMessage("单据生成失败,请检查服务器目录权限配置是否正确");
  19 e.printStackTrace();
  20 System.out.println("2.挂了");
  21 // return INPUT;
  22 return null;
  23 }
  24 if(_pdfWriter == null) {
  25 this.setMessage("单据生成失败,请检查服务器目录权限配置是否正确");
  26 System.out.println("3.挂了");
  27 // return INPUT;
  28 return null;
  29 }
  30
  31 // 3.打开文档
  32 _document.open();
  33
  34 // 4.创建需要填入文档的元素
  35 PdfPTable _table = new PdfPTable(4);
  36 PdfPCell _cell = null;
  37
  38 _table.addCell(new Paragraph("单据号", new Font(_baseFont)));
  39 _cell = new PdfPCell(new Paragraph(_otl.getOtlId()));
  40 _cell.setColspan(3);
  41 _table.addCell(_cell);
  42
  43 _table.addCell(new Paragraph("客户名称", new Font(_baseFont)));
  44 _cell = new PdfPCell(new Paragraph(_otl.getClients().getName(), new Font(_baseFont)));
  45 _cell.setColspan(3);
  46 _table.addCell(_cell);
  47
  48 _table.addCell(new Paragraph("销售日期", new Font(_baseFont)));
  49 _cell = new PdfPCell(new Paragraph(_otl.getOutDate().toString()));
  50 _cell.setColspan(3);
  51 _table.addCell(_cell);
  52
  53 _cell = new PdfPCell();
  54 _cell.setColspan(4);
  55 PdfPTable _tabGoods = new PdfPTable(7);
  56 // 添加标题行
  57 _tabGoods.setHeaderRows(1);
  58 _tabGoods.addCell(new Paragraph("序号", new Font(_baseFont)));
  59 _tabGoods.addCell(new Paragraph("商品名称", new Font(_baseFont)));
  60 _tabGoods.addCell(new Paragraph("自定义码", new Font(_baseFont)));
  61 _tabGoods.addCell(new Paragraph("规格", new Font(_baseFont)));
  62 _tabGoods.addCell(new Paragraph("数量", new Font(_baseFont)));
  63 _tabGoods.addCell(new Paragraph("单价", new Font(_baseFont)));
  64 _tabGoods.addCell(new Paragraph("小计", new Font(_baseFont)));
  65 Object[] _outTrades = _otl.getOutTrades().toArray();
  66 // 将商品销售详细信息加入表格
  67 for(int i = 0; i < _outTrades.length;) {
  68 if((_outTrades[i] != null) && (_outTrades[i] instanceof OutTrade)) {
  69 OutTrade _ot = (OutTrade) _outTrades[i];
  70 Goods _goods = _ot.getGoods();
  71 _tabGoods.addCell(String.valueOf((++i)));
  72 _tabGoods.addCell(new Paragraph(_goods.getName(), new Font(_baseFont)));
  73 _tabGoods.addCell(_goods.getUserCode());
  74 _tabGoods.addCell(_goods.getEtalon());
  75 _tabGoods.addCell(String.valueOf(_ot.getNum()));
  76 _tabGoods.addCell(String.valueOf(_ot.getPrice()));
  77 _tabGoods.addCell(String.valueOf((_ot.getNum() * _ot.getPrice())));
  78 }
  79 }
  80 _cell.addElement(_tabGoods);
  81 _table.addCell(_cell);
  82
  83 _table.addCell(new Paragraph("总计", new Font(_baseFont)));
  84 _cell = new PdfPCell(new Paragraph(_otl.getAllPrice().toString()));
  85 _cell.setColspan(3);
  86 _table.addCell(_cell);
  87
  88 _table.addCell(new Paragraph("操作员", new Font(_baseFont)));
  89 _cell = new PdfPCell(new Paragraph(_otl.getProcure()));
  90 _cell.setColspan(3);
  91 _table.addCell(_cell);
  92
  93 // 5.向文档中添加内容,将表格加入文档中
  94 _document.add(_table);
  95
  96 // 6.关闭文档
  97 _document.close();
  98 System.out.println(_fileName);
  99 this.setPdfFilePath(_fileName);
  100 System.out.println("3.搞定");
  101 // return SUCCESS;
  102 return null;
  复制代码
  以上代码是写在 Struts2 的 Action 中的,当用户发送了请求之后直接将生成的PDF文件用输出流写入到客户端,浏览器收到服务器的响应之后就会询问用户打开方式。
 当然,也可以将文件写入磁盘等等。
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式