一、iText简介

   iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。

使用iText非常方便,引入jar包,程序中就可以使用iText类库了。iText.jar包下载地址:http://www.itextpdf.com/download.php

如果生成的PDF文件中需要出现中文、日文、韩文字符,则同样的地址,下载extrajars-2.3.zip扩展包,里面包括itext-asian.jar等扩展工具包。

二、功能介绍

在企业的信息系统中,报表处理一直占比较重要的作用,iText组件通过在服务器端使用Jsp 或JavaBean生成PDF报表,客户端采用超级连接显示或下载得到生成的报表,这样就很好的解决了B/S系统的报表处理问题。

适合使用IText的需求:

Typically, iText is used in projects that have one ofthe following requirements:

The content isn't available in advance: it'scalculated based on user input or real-time database information.

The PDF files can't be produced manually due to the massivevolume of content: a large number of pages or documents.

Documents need to be created in unattended mode, in abatch process.

The content needs to be customized or personalized;for instance, the name of the end user has to be stamped on a number of pages.

Often you'll encounter these requirements in webapplications, where content needs to be served dynamically to a browser.Normally, you'd serve this information in the form of HTML, but for somedocuments, PDF is preferred over HTML for better printing quality, foridentical presentation on a variety of platforms, for security reasons, or toreduce the file size.

通常,iText用于具有下列条件之一的项目:

内容不固定,它是基于用户输入或实时数据库信息计算。

由于页数多或者文件较大而造成的内容过多而使得PDF文件不能手动生成,。

文件需要在无人值守模式下创建的,使用批处理过程。

内容需要自定义或个性化;例如,最终用户的名字需要被印在某一页中。

通常你会在Web应用程序中遇到的这些要求,其中的内容对于浏览者来说必须是动态。通常,你会以HTML的形式提供这些信息,但对于一些文档,PDF格式在印刷质量上是优于HTML的,同样,在各种平台上,出于安全原因,或减少文件大小的考虑,PDF都优于HTML。

三、demo演示,一个最简单的使用IText转化为PDF的例子

用iText生成PDF文档需要5个步骤:

  ①建立com.itextpdf.text.Document对象的实例。

Document document= new Document();

  ②建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。

PDFWriter.getInstance(document,new FileOutputStream("ITextTest.pdf"));

  ③打开文档。

document.open();

  ④向文档中添加内容。

document.add(newParagraph("IText  Test"));

  ⑤关闭文档。

document.close();

  通过上面的5个步骤,就能产生一个ITextTest.PDF的文件,文件内容为"ITextTest"。

具体代码如下:

 package com.wh;  

 importjava.io.FileOutputStream;
importcom.itextpdf.text.BaseColor;
importcom.itextpdf.text.Document;
importcom.itextpdf.text.Element;
importcom.itextpdf.text.Font;
importcom.itextpdf.text.Paragraph;
importcom.itextpdf.text.Rectangle;
importcom.itextpdf.text.pdf.BaseFont;
importcom.itextpdf.text.pdf.PdfPTable;
importcom.itextpdf.text.pdf.PdfWriter; public class ToPDF{
// 表头
public static final String[] tableHeader= { "姓名", "性别", "年龄",
"学院", "专业", "年级"}; // 数据表字段数
private static final int colNumber = 6; // 表格的设置
private static final int spacing = 2; // 表格的设置
private static final int padding = 2; // 导出Pdf文挡
public static void exportPdfDocument() {
// 创建文Pdf文挡50, 50, 50,50左右上下距离
Document document = newDocument(new Rectangle(1500, 2000), 50, 50, 50,
50);
try {
//使用PDFWriter进行写文件操作
PdfWriter.getInstance(document,new FileOutputStream(
"d:\\学生信息.pdf"));
document.open();
// 中文字体
BaseFont bfChinese =BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
Font fontChinese = newFont(bfChinese, 12, Font.NORMAL);
// 创建有colNumber(6)列的表格
PdfPTable datatable = newPdfPTable(colNumber);
//定义表格的宽度
int[] cellsWidth = { 8, 2,2, 8, 5, 3 };
datatable.setWidths(cellsWidth);
// 表格的宽度百分比
datatable.setWidthPercentage(100);
datatable.getDefaultCell().setPadding(padding);
datatable.getDefaultCell().setBorderWidth(spacing);
//设置表格的底色
datatable.getDefaultCell().setBackgroundColor(BaseColor.GREEN);
datatable.getDefaultCell().setHorizontalAlignment(
Element.ALIGN_CENTER);
// 添加表头元素
for (int i = 0; i <colNumber; i++) {
datatable.addCell(newParagraph(tableHeader[i], fontChinese));
}
// 添加子元素
for (int i = 0; i <colNumber; i++) {
datatable.addCell(newParagraph(tableHeader[i], fontChinese));
}
document.add(datatable);
} catch (Exception e) {
e.printStackTrace();
}
document.close();
} public static void main(String[] args)throws Exception {
exportPdfDocument();
} }

下载示例代码及jar包请点击: 下载

IText简介及示例的更多相关文章

  1. Android查缺补漏(IPC篇)-- 进程间通讯之Socket简介及示例

    本文作者:CodingBlock 文章链接:http://www.cnblogs.com/codingblock/p/8425736.html 进程间通讯篇系列文章目录: Android查缺补漏(IP ...

  2. IdentityServer4 中文文档 -6- (简介)示例服务器和测试

    IdentityServer4 中文文档 -6- (简介)示例服务器和测试 原文:http://docs.identityserver.io/en/release/intro/test.html 目 ...

  3. IText PDF简单示例

    package com.exe.learn.demo.itextpdf; import java.io.ByteArrayInputStream; import java.io.File; impor ...

  4. Ruby简介,附带示例程序

    Ruby语言是日本人松本行弘于1993年器开始着手研发,经历2年时间,发布了Ruby语言的第一个版本:0.95版.     Ruby是一种非常简介的解释性语言,一种纯粹的面向对象编程语言,甚至比Jav ...

  5. Unity 3(一):简介与示例

    本文关注以下方面(环境为VS2012..Net Framework 4.5以及Unity 3): Ioc/DI简介: Unity简单示例 一.Ioc/DI简介 IoC 即 Inversion of C ...

  6. JavaScript简介及示例

    JavaScript简介及使用 一.简介 JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛 ...

  7. Lambda表达式 简介 语法 示例

    Lambda 表达式也称为闭包,是匿名类的简短形式.Lambda 表达式简化了[单一抽象方法声明接口]的使用,因此 lambda 表达式也称为功能接口. 在 Java SE 7 中,单一方法接口可使用 ...

  8. Spring IO Platform简介及示例

    什么是Spring IO Platform Spring IO Platform,简单的可以认为是一个依赖维护平台,该平台将相关依赖汇聚到一起,针对每个依赖,都提供了一个版本号: 这些版本对应的依赖都 ...

  9. Lambda表达式 简介 语法 示例 匿名内部类

    在AS中使用 Lambda 表达式 Demo地址:https://github.com/baiqiantao/MultiTypeTest.git Gradle(Project级别)中添加classpa ...

随机推荐

  1. VSFTP服务配置

    FTP连接及传输模式控制连接:TCP 21 ,用于发送FTP命令信息数据连接:TCP 20 ,用于上传.下载数据数据连接的建立类型:主动模式.被动模式 主动模式:服务器主动发起数据连接·首先由客户端向 ...

  2. PHP ueditor编辑器使用(TP5)

    百度搜索ueditor,下载,解压,把需要的JS文件放到对应的目录里,根据框架的不同目录也不一样 在文件中引入需要的JS文件 HTML代码 <div> <!-- 编辑器 --> ...

  3. 一分钟使用Docker快速搭建Wordpress

    1. apt install docker.io -y 2. pip install docker-compose 3. vim wordpress_stack.yml version: '3.1' ...

  4. React Natived打包报错java.io.IOException: Could not delete path '...\android\support\v7'解决

    问题详情 React Native打包apk时在第二次编译时候报错: java.io.IOException: Could not delete path 'D:\mycode\reactnative ...

  5. foreach遍历数组、数组的转置与方阵的迹

    public class Copy1 { public static void main(String[] args) { array1(); //如果不初始化元素,默认为0 int [][] a = ...

  6. npm http-server ubuntu

    Node.js中http-server的使用 使用阿里的npm镜像 国外的npm太慢了.查看一下自己使用的源: npm config get registry 1 应该显示https://regist ...

  7. Buildroot Savedefconfig

    /********************************************************************************* * Buildroot Saved ...

  8. Angular版本

    Index of (-1.6) 没有Angular 3,下一个Angular主版本将是Angular 4

  9. (17)模型层 -ORM之msql 单表的增、删、改、查 及其他操作

    单表操作-增.删.改.查 ret=models.User.objects.filter(id=1)  #这里的结果是一个queryset对象 ret=modles.User.Objects.filte ...

  10. LG2634 [国家集训队]聪聪可可

    题意 题目描述 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)--遇到这种问题,一般情况下石头剪刀布就好了,可是 ...