IText基本模板
导入itext的maven依赖
com.itextpdf
itextpdf
5.5.7
写生成地址
创建文档
获取输出流
打开文档
添加内容
关闭文档
// 生成文件名和地址
public static final String RESULT="C:/Users/founder/Desktop/Itext/hello.pdf" ;
public static void main(String[] args) throws FileNotFoundException, DocumentException {
new HelloIText().createPdf(RESULT);
}
public void createPdf(String filename) throws FileNotFoundException, DocumentException {
// 创建实例文档
Document document = new Document();
// 获取pdfwriter实例
PdfWriter.getInstance(document,new FileOutputStream(filename));
// 打开文档
document.open();
// 添加内容
document.add(new Paragraph(“hello itext!”));
// 关闭文档
document.close();
}
输出中文
public class IText {
public static void main(String[] args) throws IOException {
PdfWriter writer = new PdfWriter("1.pdf");
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf);
PdfFont sysFont = PdfFontFactory.createFont("src/main/resources/font/kai.ttf", PdfEncodings.IDENTITY_H,true);
document.add(new Paragraph("这是我的").setFont(sysFont));
document.close();
}
}
生成pdf报表且输出中文
package com.guxin.study_demo.Itext;
import java.util.ArrayList;
import java.util.List;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.UnitValue;
public class test {
public static final String RESULT=“C:/Users/founder/Desktop/Itext/FirstTable.pdf” ;
public static void main(String[] args) throws Exception {
//构建容器
//生成的pdf文件名
PdfWriter writer = new PdfWriter(RESULT);
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf);
//使用系统本地字体,可以解决生成的pdf中无法显示中文问题,本处字体为宋体
//可以直接把例如: src/main/resources/font/kai.ttf字体所在的路径
//在创建字体时直接使用即可解决中文问题
PdfFont sysFont = PdfFontFactory.createFont("src/main/resources/font/kai.ttf", PdfEncodings.IDENTITY_H,true);
//添加文本
document.add(new Paragraph("Hello World!"));
//添加图片
//定义表格
Table table = new Table(4);
table.setWidth(UnitValue.createPercentValue(100));
//自定义的封装表格数据
formatData(table,getUsers(),sysFont);
document.add(table);
document.close();
}
/*
*自定义的一个数据源
*/
public static List<User> getUsers(){
List<User> list = new ArrayList<User>();
User user = null;
for(int i=0;i<5;i++){
user = new User();
user.setId(i);
user.setName("ss "+i);
user.setAdd("西安--"+i);
user.setMemo("这是备注"+i);
list.add(user);
}
return list;
}
/*
* 使用pdf表格工具,将list中数据封装进pdf的table中
* 传入的三个参数分别是,table对象,数据源list集合,创建的PdfFont对象
*/
public static void formatData(Table table,List<User> list,PdfFont font){
table.addCell(new Cell().add(new Paragraph(" 字段名称 ").setFont(font)));
table.addCell(new Cell().add(new Paragraph(" 数据类型 ").setFont(font)));
table.addCell(new Cell().add(new Paragraph(" 约束 ").setFont(font)));
table.addCell(new Cell().add(new Paragraph(" 备注 ").setFont(font)));
for(User user : list){
table.addCell(new Cell().add(new Paragraph(user.getId()+"")));
table.addCell(new Cell().add(new Paragraph(user.getName()).setFont(font)));
//测试数据中,add属性中含有中文,所以 需要设置字体
table.addCell(new Cell().add(new Paragraph(user.getAdd()).setFont(font)));
table.addCell(new Cell().add(new Paragraph(user.getMemo()).setFont(font)));
}
}
}
相较于之前转中文方式较为繁琐
可以直接 给document设置一个整体的font
Document document = new Document(pdf);
PdfFont sysFont = PdfFontFactory.createFont(“src/main/resources/font/kai.ttf”, PdfEncodings.IDENTITY_H,true);
document.setFont(sysFont);
package com.guxin.study_demo.Itext;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.color.Color;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import java.io.IOException;
public class IText {
public static void main(String[] args) throws IOException {
PdfWriter writer = new PdfWriter("1.pdf");
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf);
PdfFont sysFont = PdfFontFactory.createFont("src/main/resources/font/kai.ttf", PdfEncodings.IDENTITY_H,true);
document.setFont(sysFont);
document.add(new Paragraph("这是我的").setRelativePosition(pdf.getDefaultPageSize().getWidth()/2-100, 10, pdf.getDefaultPageSize().getWidth()/2,pdf.getDefaultPageSize().getHeight()).setFontSize(20));
Table table = new Table(4);
table.addCell(new Cell().add(new Paragraph("字段名称").setFontSize(15).setFontColor(Color.RED)));
table.addCell(new Cell().add(new Paragraph("数据类型").setFontSize(15).setFontColor(Color.RED)));
table.addCell(new Cell().add(new Paragraph("约束").setFontSize(15).setFontColor(Color.RED)));
table.addCell(new Cell().add(new Paragraph("备注").setFontSize(15).setFontColor(Color.RED)));
for (int i = 0; i <5 ; i++) {
table.addCell(new Cell().add(new Paragraph("字段名称"+i).setFontSize(15).setFontColor(Color.GRAY)));
table.addCell(new Cell().add(new Paragraph("数据类型"+i).setFontSize(15).setFontColor(Color.GRAY)));
table.addCell(new Cell().add(new Paragraph("约束"+i).setFontSize(15).setFontColor(Color.GRAY)));
table.addCell(new Cell().add(new Paragraph("备注"+i).setFontSize(15).setFontColor(Color.GRAY)));
}
document.add(table);
document.close();
}
}
在本地模拟数据库
package com.guxin.study_demo.Itext;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
public class DataBase {
private static Map<Integer,User> userMap=null;
static {
userMap=new HashMap<Integer,User>();
userMap.put(1,new User(1,“精灵1”,“笨蛋”,“绿色的”));
userMap.put(0,new User(0,“精灵1”,“笨蛋”,“绿色的”));
userMap.put(2,new User(2,“精灵1”,“笨蛋”,“绿色的”));
userMap.put(3,new User(3,“精灵1”,“笨蛋”,“绿色的”));
userMap.put(4,new User(4,“精灵1”,“笨蛋”,“绿色的”));
userMap.put(5,new User(5,“精灵1”,“笨蛋”,“绿色的”));
userMap.put(6,new User(6,“精灵1”,“笨蛋”,“绿色的”));
}
public Collection getUsers(){
return userMap.values();
}
public User getUserBy(Integer id){
return userMap.get(id);
}
}
通过itext访问生成报表
package com.guxin.study_demo.Itext;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.color.Color;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import java.io.IOException;
import java.util.Collection;
public class IText {
public static void main(String[] args) throws IOException {
PdfWriter writer = new PdfWriter("1.pdf");
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf);
PdfFont sysFont = PdfFontFactory.createFont("src/main/resources/font/kai.ttf", PdfEncodings.IDENTITY_H,true);
document.setFont(sysFont);
document.add(new Paragraph("这是我的").setRelativePosition(pdf.getDefaultPageSize().getWidth()/2-100, 10, pdf.getDefaultPageSize().getWidth()/2,pdf.getDefaultPageSize().getHeight()).setFontSize(20));
Table table = new Table(4);
table.addCell(new Cell().add(new Paragraph("字段名称").setFontSize(15).setFontColor(Color.RED)));
table.addCell(new Cell().add(new Paragraph("数据类型").setFontSize(15).setFontColor(Color.RED)));
table.addCell(new Cell().add(new Paragraph("约束").setFontSize(15).setFontColor(Color.RED)));
table.addCell(new Cell().add(new Paragraph("备注").setFontSize(15).setFontColor(Color.RED)));
Collection<User> users = new DataBase().getUsers();
for (int i = 0; i <users.size()-1 ; i++) {
User user = new DataBase().getUserBy(i);
table.addCell(new Cell().add(new Paragraph(user.getId()+"").setFontSize(15).setFontColor(Color.GRAY)));
table.addCell(new Cell().add(new Paragraph(user.getName()).setFontSize(15).setFontColor(Color.GRAY)));
table.addCell(new Cell().add(new Paragraph(user.getAdd()).setFontSize(15).setFontColor(Color.GRAY)));
table.addCell(new Cell().add(new Paragraph(user.getMemo()).setFontSize(15).setFontColor(Color.GRAY)));
}
document.add(table);
document.close();
}
}