首页 技术 正文
技术 2022年11月21日
0 收藏 840 点赞 4,329 浏览 1586 个字

最近正在做导出pdf文件的功能,所以查了了一些相关资料,发现不是很完善,这里做一些小小的感想,欢迎各位“猿”童鞋批评指正。

poi+itext,所需要的jar包有itext-2.1.7.jar,poi-3.14.jar,iTextAsian.jar:用于处理中文乱码问题,这是我所用的jar包,大家也可以自行配置。https://www.cnblogs.com/h–d/p/6150320.html,这个网址步骤写的挺好的,https://www.cnblogs.com/shuilangyizu/p/5760928.html 这个是画pdf比较详细

首先设置一下字体,看看pdf里有哪些需要的字体,这里先设置一个基础的

BaseFont bfChinese = null;
try {
bfChinese = BaseFont.createFont(“STSong-Light”, “UniGB-UCS2-H”, BaseFont.NOT_EMBEDDED);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();

}

Font FontChinese = new Font(bfChinese,13,Font.NORMAL);

Font FontChinese2 = new Font(bfChinese,13,Font.NORMAL|Font.UNDERLINE);这里设置需要的的字体类型,在初始的字体里面修改,保证一下统一以便字体结构清晰。

// 第一步,创建document对象
Rectangle rectPageSize = new Rectangle(PageSize.A4);//这里可以查查,有很多其他的
//下面代码设置页面横置

rectPageSize = rectPageSize.rotate();

//创建document对象并指定边距

Document document1 = new Document(rectPageSize);

// 将Document实例和文件输出流用PdfWriter类绑定在一起,从而完成向Document写,即写入PDF文档

PdfWriter.getInstance(document1,new FileOutputStream(“src/a.pdf”));//创建到哪个路径文件中
//打开文档
document1.open();

//向文档添加文字. 文档由段组成,这里只是做个别的说明

float[] widths = {40,80,150,50,50,100,60,50,70,70};数据存的是列宽,按照配比来的,不用在乎数据大小

PdfPTable table = new PdfPTable(widths);这个可以在页面画一个table,很方便并且比较好的方法

table.setWidthPercentage(100);设置占比
table.setSpacingBefore(10);设置上边距离上个Paragraph的距离

table.setSpacingAfter(10);设置下边距离下个Paragraph的距离

下面这个可以设置表格中同单元格存在不同的字体

Paragraph largeText = new Paragraph();
Chunk chunk1 = new Chunk(“text0”,FontChinese11);
Chunk chunk2 = new Chunk(text1,FontChinese1);
largeText.add(chunk1);
largeText.add(chunk2);

cell1.addElement(largeText);

document1.add(list+Paragraph+table等等之类的);

//关闭document
document1.close();

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,904
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,430
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,246
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,058
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,690
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,727