1.添加maven依赖 最新iText7 涉及商业收费 用5就行
官网
old 4.x- 不建议使用
<!-- 导入iText报表 -->
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>4.2.1</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
new 5.x+ 建议使用这个
<!-- 导入iText报表 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.9</version>
</dependency>
<!-- 导入iText报表 中文包 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
- 步骤 很简单
1 创建Document文档
2 创建输出位置
3 打开文档
4 写入内容
5 关闭文档
- Demo1
public static void main(String[] args) throws Exception{
// 1 创建一个文档
Document document = new Document();
// 2 设置输出位置
PdfWriter.getInstance(document,new FileOutputStream(new File("d:\\a.pdf")));
// 3 打开文档
document.open();
// 4 写入内容
document.add(new Paragraph("HelloWorld 你好世界"));
//5 关闭文档
document.close();
}
注 中文是无法生成到pdf的
需要设置字体(设置可以支持中文的字库 【操作系统】 , 【导入itext-asian的jar包】)
<!-- 导入iText报表 中文包 刚才添加依赖已经有了 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
- Demo2
public static void main(String[] args) throws Exception {
//1 创建pdf对象 导包注意 import com.itextpdf.text.Document;
Document document = new Document();
//2 设置输出位置
// 第一个参数:文档对象
// 第二个参数:输出位置
PdfWriter.getInstance(document,new FileOutputStream(new File("d:\\a.pdf")));
//3 打开文档
document.open();
//4 写入内容
// 创建BaseFont 基础字体对象
// String name, 字体名字:宋体,楷体,隶书 AsianFontMapper.ChineseSimplifiedFont : STSong-Light
// String encoding:编码 + 布局方式
// boolean embedded:是否内嵌字体 true 导出的时候将字体一并导出去 false 不导出字体
BaseFont baseFont = BaseFont.createFont(AsianFontMapper.ChineseSimplifiedFont, AsianFontMapper.ChineseSimplifiedEncoding_H, false);
//BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);
// 通过基础字体创建字体
/**
* 第一个参数:baseFont
* 2 :字体大小
* 3 :字体样式 加粗 切斜 加粗倾斜 正常.....
* 4 :字体颜色
*/
Font font = new Font(baseFont,16,Font.BOLDITALIC,BaseColor.BLUE);
document.add(new Paragraph("HelloWorld 往后余生 祝你幸福 R",font));
//5 关闭文档
document.close();
}
打印效果
- Demo3 进阶 插入表格
public static void main(String[] args) throws Exception{
//准备数
List<String[]> list = new ArrayList<String[]>();
list.add(new String[]{"七月","1000","1100"});
list.add(new String[]{"八月","950","1000"});
list.add(new String[]{"九月","1000","1200"});
//创建文档对象
/**
* 第一个参数:PageSize:设置纸张的大小:A1,A2,A3,A4(默认值),A5
* 第2--5个参数:左右上下:纸张的边距
*/
Document document = new Document(PageSize.A4, 20, 20, 20, 20);
//设置输出位置
PdfWriter.getInstance(document, new FileOutputStream(new File("d://a.pdf")));
//打开文档
document.open();
//写入内容
//字体得宋体,默认情况下,iText技术不支持中文的输出,得导入亚洲语言包
//创建亚洲中文字体
/**
* 第一个参数:当前的字体:宋体、楷体...
* 第二个参数:编码
* 第三个参数:是否以内嵌的样式显示,值是boolean类型
* true:以内嵌的方式显示,比较占用资源
* false:不适用内嵌的方式显示,(更常用)
*/
BaseFont baseFont = BaseFont.createFont(AsianFontMapper.ChineseSimplifiedFont,
AsianFontMapper.ChineseSimplifiedEncoding_H, BaseFont.NOT_EMBEDDED);
/**********大标题的输出***********/
//设置字体样式
/**
* 第一个参数:字体
* 第二个参数:字体大小
* 第三个参数:加粗、倾斜..
* 第四个参数:颜色
*/
Font bigTitleFont = new Font(baseFont, 30f, Font.BOLD, BaseColor.PINK);
//设置内容
/**
* 第一个参数:内容
* 第二个参数:字体格式
*/
Paragraph bigTitleParagraph = new Paragraph("出货表", bigTitleFont);
//设置对其方式
bigTitleParagraph.setAlignment(Paragraph.ALIGN_CENTER);
//添加至文档中
document.add(bigTitleParagraph);
/**********作者的输出***********/
//设置字体样式
Font authorFont = new Font(baseFont, 20f, Font.NORMAL, BaseColor.BLACK);
//设置内容
Paragraph authorParagraph = new Paragraph("乐子hzw", authorFont);
//设置对其方式
authorParagraph.setAlignment(Paragraph.ALIGN_RIGHT);
//添加至文档中
document.add(authorParagraph);
/**********表格的创建***********/
//参数:表格的列树
PdfPTable table = new PdfPTable(3);
//设置当前面table的上边距
table.setSpacingBefore(20f);
/**********表格标题的输出***********/
//设置字体样式
Font titleFont = new Font(baseFont, 15f, Font.BOLD, BaseColor.GREEN);
//设置内容
//第一个参数:内容
//第二个参数:字体样式
table.addCell(new PdfPCell(new Phrase("月份", titleFont)));
table.addCell(new PdfPCell(new Phrase("去年销量", titleFont)));
table.addCell(new PdfPCell(new Phrase("今年销量", titleFont)));
/**********表格内容的输出***********/
//设置字体样式
Font contentFont = new Font(baseFont, 15f, Font.NORMAL, BaseColor.GREEN);
//设置内容
for(String[] values:list){
table.addCell(new PdfPCell(new Phrase(values[0], contentFont)));
table.addCell(new PdfPCell(new Phrase(values[1], contentFont)));
table.addCell(new PdfPCell(new Phrase(values[2], contentFont)));
}
//将表格添加至document中
document.add(table);
//关闭文档
document.close();
System.out.println("ok");
}
效果图
祝你幸福
送你一首歌《送别》 朴树 词 李叔同 电影《厨子戏子痞子》片尾曲
附图:宁波博物馆 李叔同展览 弘一法师