PDF生成方案之iText
前言
有一段时间没有更新了,最近有点懒了,在此立下flag,督促自己能够常更新吧,话不多说,进入主题,今天介绍的是电子凭证(pdf)生成的解决方案,会从几个常用的工具来介绍,也会对比一下几者之间的性能。
完整代码下载地址: http://download.youkuaiyun.com/download/u010695794/9855688
iText是什么?
在官网中 http://itextpdf.com/描述:
iText, the world’s preferred PDF library,iText is a software developer toolkit that allows users to integrate PDF functionalities within their applications, processes or products
iText,是世界上首选的PDF库,iText是一个软件开发人员工具包,允许用户将PDF功能集成到其他应用程序,流程或者产品中。
其特点有:
准备工作
在使用iTex时,我们需要添加Maven依赖,如下:
1 2 3 4 5 |
<dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.11</version> </dependency> |
入门实例
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
/** * 生成pdf文件 */ @Test public void testCreatePdf(){ try{ // 1. new Document Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(DEST)); // 2. 打开document document.open(); // 3. 添加内容 document.add(new Paragraph("hello world!")); // 4. 关闭 (如果未关闭则会生成无效的pdf文件) document.close(); }catch(DocumentException ex){ ex.printStackTrace(); }catch (FileNotFoundException ex){ ex.printStackTrace(); } } |