您的位置:首页 > 编程语言 > Java开发

springmvc使用itext生存pdf文件

2017-07-07 09:20 246 查看
最近做项目,需要生成PDF格式的日报,由于之前没有做过,研究了好几天,中途遇到了一些问题,现在把它记录先来,方便以后查看.

先贴代码:

Document document = new Document(PageSize.A4); 

PdfWriter.getInstance(document, new FileOutputStream(fileName));

BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);

// 标题字体        

Font f30 = new Font(bfChinese, 30, Font.NORMAL, Color.BLACK);

// 正文字体        

Font f8 = new Font(bfChinese, 15, Font.NORMAL, Color.BLACK);//小标题字体                

Font f9 = new Font(bfChinese, 15, Font.NORMAL, Color.RED);//填充内容字体        

document.open();                

Paragraph title = new Paragraph("日报", f30);        

title.setAlignment(1); 

// 标题        

document.add(title);        

// 换行        

document.add(new Chunk("\n"));        

// 添加table实例,共有4列        

PdfPTable table = new PdfPTable(4);        

table.setWidthPercentage(100);        

table.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);        

PdfPCell cell;

  // 表格标题和数据

     cell = new PdfPCell(new Paragraph("工程名称", f8));

     cell.setMinimumHeight(25);

cell.setHorizontalAlignment(1);

     table.addCell(cell);

     cell = new PdfPCell(new Paragraph("工程一", f9)); 

     cell.setColspan(3);

     cell.setMinimumHeight(25);

cell.setHorizontalAlignment(1);

     table.addCell(cell);

     cell = new PdfPCell(new Paragraph("信息化监管单位", f8)); 

     cell.setMinimumHeight(25);

cell.setHorizontalAlignment(1);

     table.addCell(cell);

cell = new PdfPCell(new Paragraph("人民群众", f9));

     cell.setMinimumHeight(25);

     cell.setHorizontalAlignment(1);

     table.addCell(cell);

     cell = new PdfPCell(new Paragraph("监管日期", f8));

     cell.setHorizontalAlignment(1);

     table.addCell(cell);

     cell = new PdfPCell(new Paragraph("2016-05-12", f9));

     cell.setMinimumHeight(25);

     cell.setHorizontalAlignment(1);

     table.addCell(cell);

  document.add(table);



生成的样式如下,以上我想一般都不会有什么问题.

但是在设置子标题的时候,出现了合并单元格的问题.就是itex虽然有单元格合并的方法,但是行合并和列合并不能同时使用,否则生成的样式就会错位,但是有时候又必须这么做,

那么我自己的解决方式通过创建多个PdfPTable来实现.比如下图



这里我共有6列,但是在其他地方其实不是6列,可能是8列,如果整个文章只用一个table的话那么就需要列合并,如果里面没有行合并,那么完全可以,但是现在有行合并,

用同一个table就比较麻烦,当然也是可以通过隐藏表格边框线来实现单元格合并,我也试过,但缺点也很明显,那就是里面的内容不容易居中,调起来很麻烦,所以我就选择了使用

多个PdfPTable的方式来实现.

当遇到同时需要行合并和列合并的时候,就创建一个PdfPTable,列树就是明细列数,比如我上图,列数应该是6而不是4,这样我行合并和列合并就可以分开,一个单元格就不会出现同时需要行合并和列合并,代码是:

 PdfPTable table2 = new PdfPTable(6);

        table2.setWidthPercentage(100);

        table2.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);

 cell = new PdfPCell(new Paragraph("标段", f8));

        cell.setRowspan(2);

        cell.setHorizontalAlignment(1);

        cell.setVerticalAlignment(5);

        table2.addCell(cell);

        

        cell = new PdfPCell(new Paragraph("混合料", f8));

        cell.disableBorderSide(2);

        cell.setRowspan(2);

        cell.setVerticalAlignment(5);

        cell.setHorizontalAlignment(1);

        table2.addCell(cell);

        

        cell = new PdfPCell(new Paragraph("开始碾压桩号", f8));

        cell.setColspan(2);

        cell.setHorizontalAlignment(1);

        cell.setMinimumHeight(25);

        table2.addCell(cell);

        

        cell = new PdfPCell(new Paragraph("最后碾压桩号", f8));

        cell.setColspan(2);

        cell.setHorizontalAlignment(1);

        cell.setMinimumHeight(25);

        table2.addCell(cell);

        

        cell = new PdfPCell(new Paragraph("欠压率(%)", f8));

        cell.setHorizontalAlignment(1);

        cell.setMinimumHeight(25);

        table2.addCell(cell);

        

        cell = new PdfPCell(new Paragraph("超压率(%)", f8));

        cell.setHorizontalAlignment(1);

        cell.setMinimumHeight(25);

        table2.addCell(cell);

        

        cell = new PdfPCell(new Paragraph("欠压率(%)", f8));

        cell.setHorizontalAlignment(1);

        cell.setMinimumHeight(25);

        table2.addCell(cell);

        

        cell = new PdfPCell(new Paragraph("超压率(%)", f8));

        cell.setHorizontalAlignment(1);

        cell.setMinimumHeight(25);

        table2.addCell(cell);

        

        cell = new PdfPCell(new Paragraph("AP1", f9));

        cell.setHorizontalAlignment(1);

        cell.setVerticalAlignment(1);

        cell.setMinimumHeight(25);

        table2.addCell(cell);

        

        cell = new PdfPCell(new Paragraph("AC-20C", f9));

        cell.setHorizontalAlignment(1);

        cell.setMinimumHeight(25);

        table2.addCell(cell);

        

        cell = new PdfPCell(new Paragraph("2.2", f9));

        cell.setHorizontalAlignment(1);

        cell.setMinimumHeight(25);

        table2.addCell(cell);

        

        cell = new PdfPCell(new Paragraph("0", f9));

        cell.setHorizontalAlignment(1);

        cell.setMinimumHeight(25);

        table2.addCell(cell);

        

        cell = new PdfPCell(new Paragraph("2.2", f9));

        cell.setHorizontalAlignment(1);

        cell.setMinimumHeight(25);

        table2.addCell(cell);

        

        cell = new PdfPCell(new Paragraph("0", f9));

        cell.setHorizontalAlignment(1);

        cell.setMinimumHeight(25);

        table2.addCell(cell);

        

        cell = new PdfPCell(new Paragraph("AP1", f9));

        cell.setHorizontalAlignment(1);

        cell.setMinimumHeight(25);

        table2.addCell(cell);

        

        cell = new PdfPCell(new Paragraph("AC-25C", f9));

        cell.setHorizontalAlignment(1);

        cell.setMinimumHeight(25);

        table2.addCell(cell);

        

        cell = new PdfPCell(new Paragraph("2.2", f9));

        cell.setHorizontalAlignment(1);

        cell.setMinimumHeight(25);

        table2.addCell(cell);

        

        cell = new PdfPCell(new Paragraph("0", f9));

        cell.setHorizontalAlignment(1);

        cell.setMinimumHeight(25);

        table2.addCell(cell);

        

        cell = new PdfPCell(new Paragraph("2.2", f9));

        cell.setHorizontalAlignment(1);

        cell.setMinimumHeight(25);

        table2.addCell(cell);

        

        cell = new PdfPCell(new Paragraph("0", f9));

        cell.setHorizontalAlignment(1);

        cell.setMinimumHeight(25);

        table2.addCell(cell);

        

        document.add(table2);

我遇到的的第二个问题是往单元格里插入图片.

一开始我是

 Image image2 = Image.getInstance (imagePath2);

 cell = new PdfPCell(image2 );

这样添加进去的,但是样式出现了问题



他会把表格的边框覆盖度掉.然后我尝试通过 image.scaleAbsolute(520,150)来调整图片大小,但是始终不行.无意中发现了PdfPCell 有一个setImage(Image) 方法,

尝试了一下,完美达标,效果如下,



 

所以如果在表格里插入图片,则调用cell的setImage方法.代码如下

 Image image2 = Image.getInstance (imagePath2);

        cell = new PdfPCell();

        cell.setColspan(7);

        cell.setImage(image2);

        table3.addCell(cell);  

都不需要设置图片大小,它会自动填充整个表格.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: