用itext生成pdf报表上篇
来源:互联网 发布:淘宝宝贝上架时间表 编辑:程序博客网 时间:2024/05/21 14:28
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生成pdf报表上篇
- Struts+iText生成Pdf报表
- Struts+iText生成Pdf报表
- Struts+iText生成Pdf报表
- 利用iText生成PDF报表
- Struts+iText生成Pdf报表
- 利用iText生成pdf报表
- iText生成PDF报表文件
- 用itext生成pdf报表下篇(解决中文问题)
- 使用ITEXT与JAVA生成PDF报表
- (例)Java生成PDF报表 iText
- Java使用itext生成PDF报表
- 用iText生成PDF文件
- 用Java的iText实现PDF报表
- java用itext 做pdf报表
- 如何利用iText在JSP中生成PDF报表- -
- 利用iText在JSP中生成PDF报表
- 如何利用iText在JSP中生成PDF报表
- 递归与分治:归并排序法
- 基于飞思卡尔imx6 sabrelite开发板的android lvds屏幕驱动调试
- linux配置防火墙详细步骤及参数说明
- 简单粗暴的图片压缩,可以压到100kb以内
- Java工程导入Eclipse
- 用itext生成pdf报表上篇
- hdu 1455 sticks(经典深搜+剪枝技巧)
- linux 系统启动服务setup设置
- HTML特殊字符大全
- centos性能监控系列二:Collectl初解
- JAX-RS入门 八: HTTP响应
- Nginx安装
- Android中的dispatchTouchEvent()、onInterceptTouchEvent()和onTouchEvent()
- svn常用命令