先上个效果图
因为做的项目涉及到数据预测,其中有大量打印业务来支撑实体店的运营,因为注重的是数据,要求简洁,清晰,所以写了个很简单也很实用的工具类。
如果需要编写样式或者插入背景,都可以查阅itex官方文档,进行扩展。
这个工具是基于 itext 写的,主要作用是生成最简洁的表格,选用的jar包版本是:
com.lowagie
itext
2.1.7
废话就不多说了,直接贴代码 PDFConstants.class
importjava.awt.Color;importjava.util.List;importcom.lowagie.text.Element;importcom.lowagie.text.Font;importcom.lowagie.text.Paragraph;importcom.lowagie.text.pdf.PdfPCell;importcom.lowagie.text.pdf.PdfPTable;public classPDFConstants {/*** PDF大标题字体*/
public static Font PDFTITLEFONT = new Font(null, 16, Font.BOLD);/*** PDF小标题字体*/
public static Font PDFTITLEFONT1 = new Font(null, 13, Font.NORMAL);/*** 表格宽度百分比*/
public static Integer WIDTHPERCENTAGE = 98;/*** 表格标题字体*/
public static Font TITLEFONT = new Font(null, 12, Font.COURIER);/*** 翻页加载表头*/
public static Integer HEADERROWS = 1;/*** 翻页不加载表头*/
public static Integer NOHEADERROWS = 0;/*** 表格内容字体*/
public static Font CONTENTFONT = new Font(null, 9, Font.NORMAL);/*** PDF表格样式*/
private static PdfPCell cell = newPdfPCell();/*** 获取表格*/
public staticPdfPCell getCell() {//水平居中
cell.setHorizontalAlignment(Element.ALIGN_CENTER);//垂直居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//边距
cell.setPadding(1);//行高
cell.setMinimumHeight(22);//不换行//cell.setNoWrap(true);//颜色淡化
cell.setBorderColor(Color.decode("#EBEEF5"));returncell;
}/*** 获取表格并赋值*/
public staticPdfPCell getCell(Paragraph content) {
cell=getCell();//设置内容
cell.setPhrase(content);returncell;
}/*** @Description 生成PDF表格
*@paramtitleNum
* 列数
*@paramtableWidth
* 列宽
*@paramtitles
* 标题集合
*@paramcontents
* 内容集合
*@paramheaderRows
* 是否再次加载表头
*@return*@throwsException*/
public static PdfPTable getPDFTable(int titleNum, int[] tableWidth, String[] titles, List contents, int headerRows) throwsException {//创建表格对象//列数
PdfPTable table = newPdfPTable(titleNum);//表格宽度百分比
table.setWidthPercentage(WIDTHPERCENTAGE);//列宽百分比
if (tableWidth != null)
table.setWidths(tableWidth);//翻页加载表头
if (headerRows ==HEADERROWS)
table.setHeaderRows(HEADERROWS);//标题集合
String[] pdfTitles =titles;if (pdfTitles != null && pdfTitles.length > 0) {//标题
for(String pdfTitle : pdfTitles) {
PdfPCell title= getCell(newParagraph(pdfTitle, TITLEFONT));
table.addCell(title);
}
}//内容集合
List pdfContents =contents;if (pdfContents != null && pdfContents.size() > 0) {//内容
for(String pdfContent : pdfContents) {
PdfPCell content= getCell(newParagraph(pdfContent, CONTENTFONT));
table.addCell(content);
}
}
//撑行数,否则最后一行会消失
table.addCell("");
pleteRow();returntable;
}
}
分页工具类 PDFMaker.class
importcom.lowagie.text.pdf.BaseFont;importcom.lowagie.text.pdf.PdfContentByte;importcom.lowagie.text.pdf.PdfPageEventHelper;importcom.lowagie.text.pdf.PdfTemplate;importcom.lowagie.text.pdf.PdfWriter;/*** @Description 分页工具
*@authorry
* @date 7月12日*/
public class PDFMaker extendsPdfPageEventHelper {/**这个PdfTemplate实例用于保存总页数*/
publicPdfTemplate tpl;/**页码字体*/
publicBaseFont helv;
@Overridepublic voidonCloseDocument(PdfWriter writer, com.lowagie.text.Document arg1) {
tpl.beginText();
tpl.setFontAndSize(helv,12);
tpl.setTextMatrix(0, 0);
tpl.showText("" + (writer.getPageNumber() - 1));
tpl.endText();
}/** (non-Javadoc)
*
* @see
* com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf
* .PdfWriter, com.lowagie.text.Document)*/@Overridepublic voidonEndPage(PdfWriter writer, com.lowagie.text.Document document) {
PdfContentByte cb=writer.getDirectContent();
cb.saveState();
String text= " Page " + writer.getPageNumber() + " of ";float textSize = helv.getWidthPoint(text, 9);float textBase =document.bottom();
cb.beginText();
cb.setFontAndSize(helv,9);//for odd pagenumbers, show t
cb.setTextMatrix(document.left(), textBase);
cb.showText(text);
cb.endText();
cb.addTemplate(tpl, document.left()+textSize, textBase);
cb.restoreState();
}/** (non-Javadoc)
*
* @see
* com.lowagie.text.pdf.PdfPageEventHelper#onOpenDocument(com.lowagie.text
* .pdf.PdfWriter, com.lowagie.text.Document)*/@Overridepublic voidonOpenDocument(PdfWriter writer, com.lowagie.text.Document arg1) {try{//initialization of the template
tpl = writer.getDirectContent().createTemplate(100, 100);//tpl.setBoundingBox(new Rectangle(0, 0, 10, 10));//initialization of the font
helv = BaseFont.createFont("Helvetica", BaseFont.WINANSI, false);
}catch(Exception e) {
}
}
}
注意:网上大部分此工具类都会报错,生成PDF失败 Unbalanced save/restore state operators,这是因为调用了saveState()但是没调用restoreState(),这里我已经修正了
拿出部分业务代码做例子,使用这个工具是很简单的
首先打开一个文档
//定义文件路径 你可以完成过程后删掉这个临时文件 或者存在tmp里
File f = new File("xxxx/xxx.pdf");
FileOutputStream output= newFileOutputStream(f);//实例化文档对象
Document document = new Document(PageSize.A4, 0, 0, 0, 0);//创建 PdfWriter 对象 文件的输出路径+文件的实际名称
PdfWriter writer = PdfWriter.getInstance(document, output);
//设置分页
writer.setPageEvent(new PDFMaker());
document.open();//打开文档
Document有横向属性 使用方法是 PageSize.A4.rotate()这个rotate方法是个神奇的方法
后面四个数字对应的是边距 分别是 左,右,上,下
生成table几个传参的例子
//标题
String[] title = { "Min.", "SUN", "MON", "TUE", "WED", "THUR", "FRI", "SAT"};//列数
Integer titleNum = 8;//列宽
int tableWidth[] = { 15, 15, 15, 10, 10, 12, 12, 11};//内容
List contents = new ArrayList();//TODO 业务代码填充contens//获取PDFTable
PdfPTable table = PDFConstants.getPDFTable(titleNum, tableWidth, title, contents, 0);//表格上间距
table.setSpacingBefore(0);//添加进文档document.add(table);
//关闭文档document.close();
如果大家有什么不解,或意见,欢迎在下方留言,楼主看到就会回复的,谢谢。