大致有俩种方案:
1. 就是创建模板; 动态给模板赋值;导出
使用模板导出
https://www.cnblogs.com/wangpeng00700/p/8418594.html
2. iText API 导出: jar 包;
iText-2.1.7.jar
iTextAsian.jar
iText系列(一) Itext简单介绍
https://blog.csdn.net/jarniyy/article/details/51612812
Java使用iText实现对PDF文件的操作(水印)
https://blog.csdn.net/weixin_36380516/article/details/76984283
iText简介与前期准备-API
https://blog.csdn.net/microfhu/article/details/6996647
1.-----iText---表格
https://blog.csdn.net/jarniyy/article/details/51612910
使用itext生成pdf的,各种布局
https://blog.csdn.net/tanqian351/article/details/51201690
PDF笔记(一):Itext 的基础知识
https://blog.csdn.net/u012228718/article/details/40706499
通过PdfPCell的方法可以隐藏单元格的边框。
https://blog.csdn.net/cfup_less/article/details/82686426
导出的初步样式:
图片+ 表格
开发步骤:
// 步骤 1: 创建一个document对象
Document document = new Document();
try {
// 步骤 2:
// 我们为document创建一个监听,并把PDF流写到文件中
PdfWriter.getInstance(document, new FileOutputStream("c:\\MyFirstTable.pdf"));
// 步骤 3:打开文档
document.open();
// 4. 创建表格 创建一个有3列的表格
PdfPTable table = new PdfPTable(3);
//定义一个表格单元
PdfPCell cell = new PdfPCell(new Paragraph("header with colspan 3"));
table.addCell(cell);
//增加到文档中
// document 添加 表格
document.add(table);
} catch (DocumentException de) {
System.err.println(de.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
}finally{
// 步骤 5:关闭文档
document.close();
}
步骤1: 创建 Document() 对象 ;
if(null == pageSize){
pageSize = PageSize.A4;
}
int length=getLength(centerColumn);
if(length==centerColumn.length&&length>15){
pageSize = PageSize.A4.rotate();
}
2. 图片的设置 :
早些时候按照这种方法写;以失败告终, 不好控制 样式 ;
/*Image img = Image.getInstance("src/image/bus365.jpg");
img.setAlignment(Image.RIGHT);
img.setBorder(Image.BOX);
img.setBorderWidth(10);
//img.setBorderColor(BaseColor.WHITE);
//img.scaleToFit(1000, 72);// 大小
//img.setRotationDegrees(-30);// 旋转
// 设置图片宽高
//img.scaleToFit(signRect.getWidth(), signRect.getHeight());
//img.scaleAbsolute(2, 1);
// 设置图片位置
//img.setAbsolutePosition(x, y);// 左边距、底边距
//doc.setPageSize(img);
//doc.newPage();
img.setAbsolutePosition(0, doc.top(20));
doc.add(img);*/
后来把图片放入表格里, 利于排版布局;
3.设置单元格子
PdfPCell cell23 = new PdfPCell(new Paragraph("报销单号:"+"123456789",FontChinese11Normal));
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
Font FontChinese11Normal = new Font(bfChinese, 11, Font.NORMAL);
字体类型, 字号, 字体样式,
利用段落对象, Paragraph (); 设置字体;字号;内容;
4、循环写入:
//把单元加到表格中
int[] width = new int[columnENList.size()];// 表格宽度设置
for (int i = 0; i <columnCNList.size(); i++) {
String column = columnCNList.get(i);
if(i==0){
width[i]=10;
}else{
width[i]=25;
}
// 这样 段落的方式赋值; 居中
PdfPCell basePdfPCell = new PdfPCell(new Paragraph(column,FontChinese12));
//定义单元格的背景颜色
basePdfPCell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
//水平居中
basePdfPCell.setHorizontalAlignment(Element.ALIGN_CENTER);
basePdfPCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
basePdfPCell.setFixedHeight(35);
basePdfPCell.setBorder(0);
tableTitle.addCell(basePdfPCell);
}
// list集合中数据:
// 先循环list集合;
for (Map<String, String> map : travelDetailInfo) {
// 再在 list每一行字段进行遍历;
for (String column : columnENList) {
PdfPCell basePdfPCell = new PdfPCell(new Paragraph(map.get(column),FontChinese12));
basePdfPCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
basePdfPCell.setHorizontalAlignment(Element.ALIGN_CENTER);
basePdfPCell.setFixedHeight(25);
// 设置边框颜色为灰色
basePdfPCell.setBorderColor(new Color(0xC0, 0xC0, 0xC0));
tableList.addCell(basePdfPCell);
}
}
5、总结:
1. 统一布局到table 格子里; 不容易乱; 剩下的就是每一块对应的API 文档进行对应的操作。
2. 水印的操作没有试, 提炼成模板utils;
3、 下划线:当时弄了弄了好久, 最后是以 cell 的下边框作为 下划线的;