用itext生成pdf报表上篇
2015-06-26 11:57
344 查看
ITEXT 生成PDF报表
这几天,项目中要生成报表,网上查了下,这个itext很简单,对数据字段少的表生成报表很好用。看了一天,自己做了个例子。
首先是下载它的jar包,我这用的是itext.2.1.7.jar,可以直接到我的CSDN中下载,免费的。
package com.tang.base;
import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Chapter;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Section;
import com.lowagie.text.pdf.PdfWriter;
public class testPDF {
public static void main(String[] args)
{
//1. 新建Document对象
// 第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
Document document = new Document(PageSize.A4,50,50,50,50);
//2.建立一个书写器(存放pdf的位置)
PdfWriter write = PdfWriter.getInstance(document,new FileOutputStream("D:\\ite1.pdf"));
//3.打开文档
document.open();
//4.向文档中添加内容
// 通过 com.lowagie.text.Paragraph 来添加文本。可以用文本及其默认的字体、颜色、大小等等设置来创建一个默认段落
document.addTitle("生成pdf文档");
document.add(new Paragraph("First page of document"));
document.add(new Paragraph("Some more text on the first page with different color and font type.", FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD, new Color(255, 150, 200))));
//标题
document.addTitle("生成pdf文档");
//作者
document.addAuthor("李洋");
//向文本中添加内容
document.newPage();
document.add(new Paragraph("\n");
document.add(new Paragraph("Some more text on the first page with different color and font type.", FontFactory.getFont(FontFactory.defaultEncoding, 10, Font.BOLD, new Color(0, 0, 0))));
Paragraph title1 = new Paragraph("Chapter 1", FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC, new Color(0, 0, 255)));
// 新建章节
Chapter chapter1 = new Chapter(title1, 1);
chapter1.setNumberDepth(0);
Paragraph title11 = new Paragraph("This is Section 1 in Chapter 1", FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD, new Color(255, 0, 0)));
Section section1 = chapter1.addSection(title11);
Paragraph someSectionText = new Paragraph("This text comes as part of section 1 of chapter 1.");
section1.add(someSectionText);
someSectionText = new Paragraph("Following is a 3 X 2 table.");
section1.add(someSectionText);
//添加章节
document.add(chapter1);
//创建一个有4列的表格,一般只定义列数
PdfPTable table = new PdfPTable(4);
//定义一个表格单元
PdfPCell cell = new PdfPCell(new Paragraph("项目立项详情",font));
//定义一个表格单元的跨度
cell.setColspan(4);
cell.setHorizontalAlignment(1);
//把单元加到表格中
table.addCell(cell);
//把下面这4项顺次的加入到表格中,当一行充满时候自动折行到下一行
cell = new PdfPCell(new Paragraph("建设单位111:"));
table.addCell(cell);
cell = new PdfPCell(new Paragraph("112",font));
table.addCell(cell);
cell = new PdfPCell(new Paragraph("联系人222:"));
table.addCell(cell);
cell = new PdfPCell(new Paragraph("112"));
table.addCell(cell);
document.add(table);//将表格添加到文档中
//5.关闭
document.close();
} }
这只是一个简单的例子。对于入门者来说最好理解的。当然它的功能远不止这些,这只是对初学者的一个例子。大家看到了这个中含有中文,大家运行后会发现PDF中没有中文,中文没显示出来。这是因为ITEXT生成报表时不支持中文,要想中文显示,得再加另一个包,下篇解释。
这几天,项目中要生成报表,网上查了下,这个itext很简单,对数据字段少的表生成报表很好用。看了一天,自己做了个例子。
首先是下载它的jar包,我这用的是itext.2.1.7.jar,可以直接到我的CSDN中下载,免费的。
package com.tang.base;
import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Chapter;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Section;
import com.lowagie.text.pdf.PdfWriter;
public class testPDF {
public static void main(String[] args)
{
//1. 新建Document对象
// 第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
Document document = new Document(PageSize.A4,50,50,50,50);
//2.建立一个书写器(存放pdf的位置)
PdfWriter write = PdfWriter.getInstance(document,new FileOutputStream("D:\\ite1.pdf"));
//3.打开文档
document.open();
//4.向文档中添加内容
// 通过 com.lowagie.text.Paragraph 来添加文本。可以用文本及其默认的字体、颜色、大小等等设置来创建一个默认段落
document.addTitle("生成pdf文档");
document.add(new Paragraph("First page of document"));
document.add(new Paragraph("Some more text on the first page with different color and font type.", FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD, new Color(255, 150, 200))));
//标题
document.addTitle("生成pdf文档");
//作者
document.addAuthor("李洋");
//向文本中添加内容
document.newPage();
document.add(new Paragraph("\n");
document.add(new Paragraph("Some more text on the first page with different color and font type.", FontFactory.getFont(FontFactory.defaultEncoding, 10, Font.BOLD, new Color(0, 0, 0))));
Paragraph title1 = new Paragraph("Chapter 1", FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC, new Color(0, 0, 255)));
// 新建章节
Chapter chapter1 = new Chapter(title1, 1);
chapter1.setNumberDepth(0);
Paragraph title11 = new Paragraph("This is Section 1 in Chapter 1", FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD, new Color(255, 0, 0)));
Section section1 = chapter1.addSection(title11);
Paragraph someSectionText = new Paragraph("This text comes as part of section 1 of chapter 1.");
section1.add(someSectionText);
someSectionText = new Paragraph("Following is a 3 X 2 table.");
section1.add(someSectionText);
//添加章节
document.add(chapter1);
//创建一个有4列的表格,一般只定义列数
PdfPTable table = new PdfPTable(4);
//定义一个表格单元
PdfPCell cell = new PdfPCell(new Paragraph("项目立项详情",font));
//定义一个表格单元的跨度
cell.setColspan(4);
cell.setHorizontalAlignment(1);
//把单元加到表格中
table.addCell(cell);
//把下面这4项顺次的加入到表格中,当一行充满时候自动折行到下一行
cell = new PdfPCell(new Paragraph("建设单位111:"));
table.addCell(cell);
cell = new PdfPCell(new Paragraph("112",font));
table.addCell(cell);
cell = new PdfPCell(new Paragraph("联系人222:"));
table.addCell(cell);
cell = new PdfPCell(new Paragraph("112"));
table.addCell(cell);
document.add(table);//将表格添加到文档中
//5.关闭
document.close();
} }
这只是一个简单的例子。对于入门者来说最好理解的。当然它的功能远不止这些,这只是对初学者的一个例子。大家看到了这个中含有中文,大家运行后会发现PDF中没有中文,中文没显示出来。这是因为ITEXT生成报表时不支持中文,要想中文显示,得再加另一个包,下篇解释。
相关文章推荐
- 剑指offer 例题
- 《开源框架那点事儿19》:特斯拉建“桩”与开源的生命力
- D题 Codeforces 325B Stadium And Games
- 图解MBR分区无损转换GPT分区+UEFI引导安装WIN8.1
- 网络编程API-下 (I/O复用函数)
- 图解MBR分区无损转换GPT分区+UEFI引导安装WIN8.1 分类: Windows-嵌入式 生活百科 2015-06-26 11:57 475人阅读 评论(2) 收藏
- Redis有序集内部实现原理分析(二)
- HTML 标签 <body>
- Maven详解之仓库------本地仓库、远程仓库
- 贝塞尔曲线 (Bézier curve) 理论及绘制方法
- matlab纵波动画
- Android之activity总结
- cocos2d-x 3.0rc1 创建project
- POJ 1741 Tree (树上点分治)(楼教主男人八题之一)
- 事件委托
- 更改Android应用程序的图标
- UIView及其子类围绕任意点旋转的方法
- 电脑全盘搜索软件 Everything 强烈推荐
- iOS7后导航栏的问题
- AngularJS 指令