1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > PDF格式报表生成(IText技术)

PDF格式报表生成(IText技术)

时间:2019-10-16 16:47:30

相关推荐

PDF格式报表生成(IText技术)

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");}

效果图

祝你幸福

送你一首歌《送别》 朴树 词 李叔同 电影《厨子戏子痞子》片尾曲

附图:宁波博物馆 李叔同展览 弘一法师

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。