您的位置:首页 > 其它

用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生成报表时不支持中文,要想中文显示,得再加另一个包,下篇解释。


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: