【pdf】使用itext生成pdf的,各种布局和使用itext导出pdf,图片、表格、背景图

一、使用itext生成pdf的,各种布局

代码如下,jar包为itext.jar,itextAsia.jar,最好都是最新的 ;2张图片也在最后贴出,把图片放到D盘可以直接生成制定格式的pdf。 
最后生成的pdf如下: 
这里写图片描述

代码如下:

package com.itext.test;

import java.io.FileOutputStream;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;


public class BaoXiaoDan {
    public static void main(String[] args) {

        try 
        {
             Document document = new Document(PageSize.A4.rotate()); 
             PdfWriter.getInstance(document, new FileOutputStream("D:\\Helloworld.PDF"));

            //设置字体
            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);   
            com.itextpdf.text.Font FontChinese24 = new com.itextpdf.text.Font(bfChinese, 24, com.itextpdf.text.Font.BOLD);
            com.itextpdf.text.Font FontChinese18 = new com.itextpdf.text.Font(bfChinese, 18, com.itextpdf.text.Font.BOLD); 
            com.itextpdf.text.Font FontChinese16 = new com.itextpdf.text.Font(bfChinese, 16, com.itextpdf.text.Font.BOLD);
            com.itextpdf.text.Font FontChinese12 = new com.itextpdf.text.Font(bfChinese, 12, com.itextpdf.text.Font.NORMAL);
            com.itextpdf.text.Font FontChinese11Bold = new com.itextpdf.text.Font(bfChinese, 11, com.itextpdf.text.Font.BOLD);
            com.itextpdf.text.Font FontChinese11 = new com.itextpdf.text.Font(bfChinese, 11, com.itextpdf.text.Font.ITALIC);
            com.itextpdf.text.Font FontChinese11Normal = new com.itextpdf.text.Font(bfChinese, 11, com.itextpdf.text.Font.NORMAL);

            document.open();
            //table1
            PdfPTable table1 = new PdfPTable(3);
            PdfPCell cell11 = new PdfPCell(new Paragraph("费用报销",FontChinese24));
            cell11.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell11.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell11.setBorder(0);
            String imagePath = "D:/alibaba.jpg";
            Image image1 = Image.getInstance(imagePath); 

            Image image2 = Image.getInstance(imagePath); 
            //设置每列宽度比例   
            int width11[] = {35,40,25};
            table1.setWidths(width11); 
            table1.getDefaultCell().setBorder(0);
            table1.addCell(image1);  
            table1.addCell(cell11);  
            table1.addCell(image2);
            document.add(table1);
            //加入空行
            Paragraph blankRow1 = new Paragraph(18f, " ", FontChinese18); 
            document.add(blankRow1);

            //table2
            PdfPTable table2 = new PdfPTable(2);
            //设置每列宽度比例   
            int width21[] = {2,98};
            table2.setWidths(width21); 
            table2.getDefaultCell().setBorder(0);
            PdfPCell cell21 = new PdfPCell(new Paragraph("报销概要",FontChinese16));
            String imagePath2 = "D:/boder.jpg";
            Image image21 = Image.getInstance(imagePath2); 
            cell21.setBorder(0);
            table2.addCell(image21);
            table2.addCell(cell21); 
            document.add(table2);
            //加入空行
            Paragraph blankRow2 = new Paragraph(18f, " ", FontChinese18); 
            document.add(blankRow2);

            //table3
            PdfPTable table3 = new PdfPTable(3);
            int width3[] = {40,35,25};
            table3.setWidths(width3); 
            PdfPCell cell31 = new PdfPCell(new Paragraph("申请人:"+"XXX",FontChinese11Normal));
            PdfPCell cell32 = new PdfPCell(new Paragraph("日期:"+"2011-11-11",FontChinese11Normal));
            PdfPCell cell33 = new PdfPCell(new Paragraph("报销单号:"+"123456789",FontChinese11Normal));
            cell31.setBorder(0);
            cell32.setBorder(0);
            cell33.setBorder(0);
            table3.addCell(cell31);
            table3.addCell(cell32);
            table3.addCell(cell33);
            document.add(table3);
            //加入空行
            Paragraph blankRow31 = new Paragraph(18f, " ", FontChinese11); 
            document.add(blankRow31);

            //table4
            PdfPTable table4 = new PdfPTable(2);
            int width4[] = {40,60};
            table4.setWidths(width4); 
            PdfPCell cell41 = new PdfPCell(new Paragraph("公司:"+"XXX",FontChinese11Normal));
            PdfPCell cell42 = new PdfPCell(new Paragraph("部门:"+"XXX",FontChinese11Normal));
            cell41.setBorder(0);
            cell42.setBorder(0);
            table4.addCell(cell41);
            table4.addCell(cell42);
            document.add(table4);
            //加入空行
            Paragraph blankRow41 = new Paragraph(18f, " ", FontChinese11); 
            document.add(blankRow41);

            //table5
            PdfPTable table5 = new PdfPTable(1);
            PdfPCell cell51 = new PdfPCell(new Paragraph("报销说明:"+"XXX",FontChinese11));
            cell51.setBorder(0);
            table5.addCell(cell51);
            document.add(table5);
            //加入空行
            Paragraph blankRow51 = new Paragraph(18f, " ", FontChinese18); 
            document.add(blankRow51);

            //table6
            PdfPTable table6 = new PdfPTable(2);
            table6.getDefaultCell().setBorder(0);
            table6.setWidths(width21); 
            PdfPCell cell61 = new PdfPCell(new Paragraph("报销明细",FontChinese16));
            cell61.setBorder(0);
            table6.addCell(image21);
            table6.addCell(cell61); 
            document.add(table6);
            //加入空行
            Paragraph blankRow4 = new Paragraph(18f, " ", FontChinese16); 
            document.add(blankRow4);

            //table7
            PdfPTable table7 = new PdfPTable(6);
            BaseColor lightGrey = new BaseColor(0xCC,0xCC,0xCC);
            int width7[] = {20,18,13,20,14,15};
            table7.setWidths(width7); 
            PdfPCell cell71 = new PdfPCell(new Paragraph("费用类型",FontChinese11Bold));
            PdfPCell cell72 = new PdfPCell(new Paragraph("费用发生时间",FontChinese11Bold));
            PdfPCell cell73 = new PdfPCell(new Paragraph("详细信息",FontChinese11Bold));
            PdfPCell cell74 = new PdfPCell(new Paragraph("消费金币/币种",FontChinese11Bold));
            PdfPCell cell75 = new PdfPCell(new Paragraph("报销汇率",FontChinese11Bold));
            PdfPCell cell76 = new PdfPCell(new Paragraph("报销金额",FontChinese11Bold));
            //表格高度
            cell71.setFixedHeight(25);
            cell72.setFixedHeight(25);
            cell73.setFixedHeight(25);
            cell74.setFixedHeight(25);
            cell75.setFixedHeight(25);
            cell76.setFixedHeight(25);
            //水平居中
            cell71.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell72.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell73.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell74.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell75.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell76.setHorizontalAlignment(Element.ALIGN_CENTER);
            //垂直居中
            cell71.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell72.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell73.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell74.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell75.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell76.setVerticalAlignment(Element.ALIGN_MIDDLE);
            //边框颜色
            cell71.setBorderColor(lightGrey);
            cell72.setBorderColor(lightGrey);
            cell73.setBorderColor(lightGrey);
            cell74.setBorderColor(lightGrey);
            cell75.setBorderColor(lightGrey);
            cell76.setBorderColor(lightGrey);
            //去掉左右边框
            cell71.disableBorderSide(8);
            cell72.disableBorderSide(4);
            cell72.disableBorderSide(8);
            cell73.disableBorderSide(4);
            cell73.disableBorderSide(8);
            cell74.disableBorderSide(4);
            cell74.disableBorderSide(8);
            cell75.disableBorderSide(4);
            cell75.disableBorderSide(8);
            cell76.disableBorderSide(4);
            table7.addCell(cell71);
            table7.addCell(cell72);
            table7.addCell(cell73);
            table7.addCell(cell74);
            table7.addCell(cell75);
            table7.addCell(cell76);
            document.add(table7);

                    //table8
                    PdfPTable table8 = new PdfPTable(6);
                    int width8[] = {20,18,13,20,14,15};
                    table8.setWidths(width8); 
                    PdfPCell cell81 = new PdfPCell(new Paragraph("差旅报销",FontChinese12));
                    PdfPCell cell82 = new PdfPCell(new Paragraph("2011-11-11",FontChinese12));
                    PdfPCell cell83 = new PdfPCell(new Paragraph("XXX",FontChinese12));
                    PdfPCell cell84 = new PdfPCell(new Paragraph("XXX",FontChinese12));
                    PdfPCell cell85 = new PdfPCell(new Paragraph("XXX",FontChinese12));
                    PdfPCell cell86 = new PdfPCell(new Paragraph("XXX",FontChinese12));
                    //表格高度
                    cell81.setFixedHeight(25);
                    cell82.setFixedHeight(25);
                    cell83.setFixedHeight(25);
                    cell84.setFixedHeight(25);
                    cell85.setFixedHeight(25);
                    cell86.setFixedHeight(25);
                    //水平居中
                    cell81.setHorizontalAlignment(Element.ALIGN_CENTER);
                    cell82.setHorizontalAlignment(Element.ALIGN_CENTER);
                    cell83.setHorizontalAlignment(Element.ALIGN_CENTER);
                    cell84.setHorizontalAlignment(Element.ALIGN_CENTER);
                    cell85.setHorizontalAlignment(Element.ALIGN_CENTER);
                    cell86.setHorizontalAlignment(Element.ALIGN_CENTER);
                    //垂直居中
                    cell81.setVerticalAlignment(Element.ALIGN_MIDDLE);
                    cell82.setVerticalAlignment(Element.ALIGN_MIDDLE);
                    cell83.setVerticalAlignment(Element.ALIGN_MIDDLE);
                    cell84.setVerticalAlignment(Element.ALIGN_MIDDLE);
                    cell85.setVerticalAlignment(Element.ALIGN_MIDDLE);
                    cell86.setVerticalAlignment(Element.ALIGN_MIDDLE);
                    //边框颜色
                    cell81.setBorderColor(lightGrey);
                    cell82.setBorderColor(lightGrey);
                    cell83.setBorderColor(lightGrey);
                    cell84.setBorderColor(lightGrey);
                    cell85.setBorderColor(lightGrey);
                    cell86.setBorderColor(lightGrey);
                    //去掉左右边框
                    cell81.disableBorderSide(8);
                    cell82.disableBorderSide(4);
                    cell82.disableBorderSide(8);
                    cell83.disableBorderSide(4);
                    cell83.disableBorderSide(8);
                    cell84.disableBorderSide(4);
                    cell84.disableBorderSide(8);
                    cell85.disableBorderSide(4);
                    cell85.disableBorderSide(8);
                    cell86.disableBorderSide(4);
                    table8.addCell(cell81);
                    table8.addCell(cell82);
                    table8.addCell(cell83);
                    table8.addCell(cell84);
                    table8.addCell(cell85);
                    table8.addCell(cell86);
                    document.add(table8);
              //加入空行
              Paragraph blankRow5 = new Paragraph(18f, " ", FontChinese18); 
              document.add(blankRow5);

            //table9
            PdfPTable table9 = new PdfPTable(3);
            int width9[] = {30,50,20};
            table9.setWidths(width9);
            PdfPCell cell91 = new PdfPCell(new Paragraph("",FontChinese12));
            PdfPCell cell92 = new PdfPCell(new Paragraph("收到的报销金额",FontChinese12));
            PdfPCell cell93 = new PdfPCell(new Paragraph("1000",FontChinese24));
            cell92.setHorizontalAlignment(Element.ALIGN_RIGHT);
            cell92.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell93.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell93.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell91.setBorder(0);
            cell92.setBorder(0);
            cell93.setBorder(0);
            table9.addCell(cell91); 
            table9.addCell(cell92); 
            table9.addCell(cell93); 
            document.add(table9);

             document.close();

        } catch (Exception ex) 
        {
          ex.printStackTrace();
        }
    }
}

二、使用itext导出pdf,图片、表格、背景图
导出效果如下


代码

1、常用的几个方法放到了一个工具类中

[java]  view plain  copy
  1. import com.itextpdf.text.Element;  
  2. import com.itextpdf.text.Font;  
  3. import com.itextpdf.text.Paragraph;  
  4. import com.itextpdf.text.pdf.PdfPCell;  
  5.   
  6. /** 
  7.  * @Description: pdf工具类 
  8.  * @author: kylin 
  9.  * @create: 2018-01-10 16:22 
  10.  **/  
  11. public class PDFUtil {  
  12.   
  13.     /** 
  14.      * 获取指定内容与字体的单元格 
  15.      * @param string 
  16.      * @param font 
  17.      * @return 
  18.      */  
  19.     public static PdfPCell getPDFCell(String string, Font font)  
  20.     {  
  21.         //创建单元格对象,将内容与字体放入段落中作为单元格内容  
  22.         PdfPCell cell=new PdfPCell(new Paragraph(string,font));  
  23.   
  24.         cell.setHorizontalAlignment(Element.ALIGN_CENTER);  
  25.         cell.setVerticalAlignment(Element.ALIGN_MIDDLE);  
  26.   
  27.         //设置最小单元格高度  
  28.         cell.setMinimumHeight(25);  
  29.         return cell;  
  30.     }  
  31.   
  32.     /** 
  33.      * 合并行的静态函数 
  34.      * @param str 
  35.      * @param font 
  36.      * @param i 
  37.      * @return 
  38.      */  
  39.     public static PdfPCell mergeRow(String str,Font font,int i) {  
  40.   
  41.         //创建单元格对象,将内容及字体传入  
  42.         PdfPCell cell=new PdfPCell(new Paragraph(str,font));  
  43.         //设置单元格内容居中  
  44.         cell.setHorizontalAlignment(Element.ALIGN_CENTER);  
  45.         cell.setVerticalAlignment(Element.ALIGN_MIDDLE);  
  46.         //将该单元格所在列包括该单元格在内的i行单元格合并为一个单元格  
  47.         cell.setRowspan(i);  
  48.   
  49.         return cell;  
  50.     }  
  51.   
  52.     /** 
  53.      * 合并列的静态函数 
  54.      * @param str 
  55.      * @param font 
  56.      * @param i 
  57.      * @return 
  58.      */  
  59.     public static PdfPCell mergeCol(String str,Font font,int i) {  
  60.   
  61.         PdfPCell cell=new PdfPCell(new Paragraph(str,font));  
  62.         cell.setMinimumHeight(25);  
  63.         cell.setHorizontalAlignment(Element.ALIGN_CENTER);  
  64.         cell.setVerticalAlignment(Element.ALIGN_MIDDLE);  
  65.         //将该单元格所在行包括该单元格在内的i列单元格合并为一个单元格  
  66.         cell.setColspan(i);  
  67.   
  68.         return cell;  
  69.     }  
  70. }  
2、为每一页都加上背景图

[java]  view plain  copy
  1. import com.itextpdf.text.BadElementException;  
  2. import com.itextpdf.text.Document;  
  3. import com.itextpdf.text.DocumentException;  
  4. import com.itextpdf.text.Image;  
  5. import com.itextpdf.text.pdf.PdfPageEventHelper;  
  6. import com.itextpdf.text.pdf.PdfWriter;  
  7.   
  8. import java.io.IOException;  
  9.   
  10. /** 
  11.  * @Description: 为每一页pdf都生成背景图片 
  12.  * @author: kylin 
  13.  * @create: 2018-01-08 18:00 
  14.  **/  
  15. public class BackGroundImage extends PdfPageEventHelper {  
  16.     private String picPath = "";  
  17.   
  18.     public String getPicPath() {  
  19.         return picPath;  
  20.     }  
  21.   
  22.     public void setPicPath(String picPath) {  
  23.         this.picPath = picPath;  
  24.     }  
  25.   
  26.     public BackGroundImage(){  
  27.   
  28.     }  
  29.   
  30.     public BackGroundImage(String path){  
  31.         this.picPath = path;  
  32.     }  
  33.   
  34.     @Override  
  35.     public void onStartPage(PdfWriter pdfWriter, Document document) {  
  36.         try {  
  37.             Image image = Image.getInstance(picPath);  
  38.             image.setAlignment(image.UNDERLYING);  
  39.             image.setAbsolutePosition(0,0);  
  40.             image.scaleAbsolute(595,842);  
  41.             document.add(image);  
  42.         } catch (BadElementException e) {  
  43.             e.printStackTrace();  
  44.         } catch (IOException e) {  
  45.             e.printStackTrace();  
  46.         } catch (DocumentException e) {  
  47.             e.printStackTrace();  
  48.         }  
  49.   
  50.         super.onStartPage(pdfWriter, document);  
  51.     }  
  52. }  
3、生成pdf(图片那块由于我是从数据库取出的byte[],所以处理的有点繁琐,可以按自己的方式简化)

[java]  view plain  copy
  1. import java.io.IOException;  
  2. import java.io.OutputStream;  
  3. import java.util.List;  
  4. import java.util.regex.Matcher;  
  5. import java.util.regex.Pattern;  
  6.   
  7. import com.itextpdf.text.*;  
  8. import com.itextpdf.text.pdf.*;  
  9. import com.kunbo.modules.station.entity.SysPassRecordEntity;  
  10. import org.apache.commons.codec.binary.Base64;  
  11.   
  12. /** 
  13.  * 创建pdf 
  14.  * @Author kylin 
  15.  * 2018.1.8 
  16.  */  
  17. public class GeneratePDF {  
  18.     /** 
  19.      * 生成绿通车辆数据pdf 
  20.      * @param os 
  21.      * @param prList 
  22.      */  
  23.     public static void createPDF1(OutputStream os, List<SysPassRecordEntity> prList) {  
  24.   
  25.         // 新建document对象  
  26.         Document document = new Document(PageSize.A4,36,36,126,36);  
  27.         try {  
  28.             //添加中文字体  
  29.             BaseFont bfChinese=BaseFont.createFont("STSong-Light""UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);  
  30.             //设置字体样式  
  31.             Font textFont = new Font(bfChinese,12,Font.NORMAL);//正常  
  32.             Font boldFont = new Font(bfChinese,11,Font.BOLD); //加粗  
  33.             Font firsetTitleFont = new Font(bfChinese,22,Font.BOLD); //一级标题  
  34.             Font secondTitleFont = new Font(bfChinese,15,Font.BOLD); //二级标题  
  35.             Font underlineFont = new Font(bfChinese,11,Font.UNDERLINE); //下划线斜体  
  36.   
  37.             //创建输出流  
  38.             PdfWriter pdfWriter = PdfWriter.getInstance(document, os);  
  39.             pdfWriter.setPageEvent(new BackGroundImage("classPath:static/images/bgImg.jpg"));  
  40.             document.open();  
  41.             for (int i = 0; i <prList.size() ; i++) {  
  42.                 Paragraph p0 = new Paragraph("  ",textFont);  
  43.                 p0.setLeading(60);  
  44.   
  45.                 String str = "时间:" + prList.get(i).getRecordDate().substring(0,16)+ "        查验人(签字):" + prList.get(i).getAuUsername()+ "       " + prList.get(i).getChannelNum()+ "车道";  
  46.                 Paragraph ph1 = new Paragraph(str,textFont);  
  47.                 ph1.setAlignment(Element.ALIGN_CENTER);  
  48.                 p0.add(ph1);  
  49.                 Paragraph ph2 = new Paragraph("  ",textFont);  
  50.                 p0.add(ph2);  
  51.   
  52.                 PdfPTable imgTable = new PdfPTable(4);  
  53.                 imgTable.setSpacingBefore(10);  
  54.                 imgTable.setHorizontalAlignment(Element.ALIGN_CENTER);  
  55.                 imgTable.setTotalWidth(new float[]{ 124124124124 }); //设置列宽  
  56.                 imgTable.setLockedWidth(true); //锁定列宽  
  57.                 imgTable.setSpacingAfter(30);  
  58.   
  59.                 //取出二进制并转为base64字符串  
  60.                 String base64Str1 = Base64.encodeBase64String(prList.get(i).getPhoto1());  
  61.                 String base64Str2 = Base64.encodeBase64String(prList.get(i).getPhoto2());  
  62.                 String base64Str3 = Base64.encodeBase64String(prList.get(i).getPhoto3());  
  63.                 String base64Str4 = Base64.encodeBase64String(prList.get(i).getPhoto4());  
  64.   
  65.                 //base64解密  
  66.                 String base64Img1 = Base64Helper.getFromBase64(base64Str1);  
  67.                 String base64Img2 = Base64Helper.getFromBase64(base64Str2);  
  68.                 String base64Img3 = Base64Helper.getFromBase64(base64Str3);  
  69.                 String base64Img4 = Base64Helper.getFromBase64(base64Str4);  
  70.   
  71.                 //字符串处理  利用正则表达式找到首次出现“,”的索引  
  72.                 Matcher matcher1= Pattern.compile(",").matcher(base64Img1);  
  73.                 Matcher matcher2= Pattern.compile(",").matcher(base64Img2);  
  74.                 Matcher matcher3= Pattern.compile(",").matcher(base64Img3);  
  75.                 Matcher matcher4= Pattern.compile(",").matcher(base64Img4);  
  76.   
  77.                 //转为二进制  
  78.                 byte[] bImg1 = null;  
  79.                 byte[] bImg2 = null;  
  80.                 byte[] bImg3 = null;  
  81.                 byte[] bImg4 = null;  
  82.                 if(matcher1.find()){  
  83.                     bImg1 = Base64.decodeBase64(base64Img1.substring(matcher1.start()+1));  
  84.                 }  
  85.                 if(matcher2.find()){  
  86.                     bImg2 = Base64.decodeBase64(base64Img2.substring(matcher2.start()+1));  
  87.                 }  
  88.                 if(matcher3.find()){  
  89.                     bImg3 = Base64.decodeBase64(base64Img3.substring(matcher3.start()+1));  
  90.                 }  
  91.                 if(matcher4.find()){  
  92.                     bImg4 = Base64.decodeBase64(base64Img4.substring(matcher4.start()+1));  
  93.                 }  
  94.   
  95.                 Image img1 = Image.getInstance(bImg1);  
  96.                 Image img2 = Image.getInstance(bImg2);  
  97.                 Image img3 = Image.getInstance(bImg3);  
  98.                 Image img4 = Image.getInstance(bImg4);  
  99.                 img1.scaleToFit(120,220);  
  100.                 img2.scaleToFit(120,220);  
  101.                 img3.scaleToFit(120,220);  
  102.                 img4.scaleToFit(120,220);  
  103.                 //填充到table  
  104.                 PdfPCell pdfPCell1 = new PdfPCell(img1);  
  105. //                pdfPCell1.setFixedHeight(180);  
  106.                 pdfPCell1.setHorizontalAlignment(Element.ALIGN_CENTER);  
  107.                 pdfPCell1.setBorder(PdfPCell.NO_BORDER);  
  108.   
  109.                 PdfPCell pdfPCell2 = new PdfPCell(img2);  
  110. //                pdfPCell2.setFixedHeight(180);  
  111.                 pdfPCell2.setHorizontalAlignment(Element.ALIGN_CENTER);  
  112.                 pdfPCell2.setBorder(PdfPCell.NO_BORDER);  
  113.   
  114.                 PdfPCell pdfPCell3 = new PdfPCell(img3);  
  115. //                pdfPCell3.setFixedHeight(180);  
  116.                 pdfPCell3.setHorizontalAlignment(Element.ALIGN_CENTER);  
  117.                 pdfPCell3.setBorder(PdfPCell.NO_BORDER);  
  118.   
  119.                 PdfPCell pdfPCell4 = new PdfPCell(img4);  
  120. //                pdfPCell4.setFixedHeight(180);  
  121.                 pdfPCell4.setHorizontalAlignment(Element.ALIGN_CENTER);  
  122.                 pdfPCell4.setBorder(PdfPCell.NO_BORDER);  
  123.   
  124.                 PdfPCell titleCell1 = new PdfPCell(new Paragraph(prList.get(i).getVehicleNum(),textFont));  
  125.                 PdfPCell titleCell2 = new PdfPCell(new Paragraph(prList.get(i).getAxleCount()+"轴",textFont));  
  126.                 PdfPCell titleCell3 = new PdfPCell(new Paragraph(prList.get(i).getGoodsType(),textFont));  
  127.                 PdfPCell titleCell4 = new PdfPCell(new Paragraph(prList.get(i).getWeight()+"吨",textFont));  
  128.                 titleCell1.setHorizontalAlignment(Element.ALIGN_CENTER);  
  129.                 titleCell2.setHorizontalAlignment(Element.ALIGN_CENTER);  
  130.                 titleCell3.setHorizontalAlignment(Element.ALIGN_CENTER);  
  131.                 titleCell4.setHorizontalAlignment(Element.ALIGN_CENTER);  
  132.                 titleCell1.setBorder(PdfPCell.NO_BORDER);  
  133.                 titleCell2.setBorder(PdfPCell.NO_BORDER);  
  134.                 titleCell3.setBorder(PdfPCell.NO_BORDER);  
  135.                 titleCell4.setBorder(PdfPCell.NO_BORDER);  
  136.   
  137.                 imgTable.addCell(pdfPCell1);  
  138.                 imgTable.addCell(pdfPCell2);  
  139.                 imgTable.addCell(pdfPCell3);  
  140.                 imgTable.addCell(pdfPCell4);  
  141.                 imgTable.addCell(titleCell1);  
  142.                 imgTable.addCell(titleCell2);  
  143.                 imgTable.addCell(titleCell3);  
  144.                 imgTable.addCell(titleCell4);  
  145.   
  146.                 p0.add(imgTable);  
  147.                 document.add(p0);  
  148.             }  
  149.   
  150.   
  151.         } catch (DocumentException e) {  
  152.             e.printStackTrace();  
  153.         } catch (IOException e) {  
  154.             e.printStackTrace();  
  155.         } catch (Exception e) {  
  156.             e.printStackTrace();  
  157.         }  
  158.   
  159.         // 关闭文档  
  160.         document.close();  
  161.     }  
  162.   
  163.     /** 
  164.      * 生成非绿通车辆数据pdf 
  165.      * @param os 
  166.      * @param prList 
  167.      */  
  168.     public static void createPDF0(OutputStream os,List<SysPassRecordEntity> prList){  
  169.   
  170.         Document document = new Document(PageSize.A4,36,36,126,36);  
  171.   
  172.         try {  
  173.             BaseFont bfChinese=BaseFont.createFont("STSong-Light""UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);  
  174.             //设置字体样式  
  175.             Font textFont = new Font(bfChinese,12,Font.NORMAL);//正常  
  176.             Font boldFont = new Font(bfChinese,11,Font.BOLD); //加粗  
  177.             Font firsetTitleFont = new Font(bfChinese,22,Font.BOLD); //一级标题  
  178.             Font secondTitleFont = new Font(bfChinese,15,Font.BOLD); //二级标题  
  179.             Font underlineFont = new Font(bfChinese,11,Font.UNDERLINE); //下划线斜体  
  180.   
  181.             //创建输出流  
  182.             PdfWriter pdfWriter = PdfWriter.getInstance(document, os);  
  183.             pdfWriter.setPageEvent(new BackGroundImage("classPath:static/images/bgImg0.jpg"));  
  184.             document.open();  
  185.             for (int i = 0; i <prList.size() ; i++) {  
  186.                 Paragraph p0 = new Paragraph();  
  187.   
  188.                 Paragraph p1 = new Paragraph("收费站名称:" + prList.get(i).getExitStation(),textFont);  
  189.                 p1.setLeading(40);  
  190.                 p0.add(p1);  
  191.   
  192.                 //创建table对象  
  193.                 PdfPTable table = new PdfPTable(7);  
  194.                 table.setSpacingBefore(10);  
  195.                 table.setHorizontalAlignment(Element.ALIGN_CENTER);  
  196.                 table.setTotalWidth(new float[]{ 30,8080808080 ,100 }); //设置列宽  
  197.                 table.setLockedWidth(true); //锁定列宽  
  198.   
  199.                 PdfPCell cell = new PdfPCell();  
  200.   
  201.                 //添加表格内容  
  202.                 table.addCell(PDFUtil.mergeCol("时间",textFont,2));  
  203.                 table.addCell(PDFUtil.mergeCol(prList.get(i).getRecordDate().substring(0,16), textFont, 3));  
  204.   
  205.                 table.addCell(PDFUtil.getPDFCell("查验人",textFont));  
  206.                 table.addCell(PDFUtil.getPDFCell(prList.get(i).getAuUsername(), textFont));  
  207.   
  208.                 table.addCell(PDFUtil.mergeCol("车牌号",textFont,2));  
  209.                 table.addCell(PDFUtil.getPDFCell(prList.get(i).getVehicleNum(), textFont));  
  210.                 table.addCell(PDFUtil.getPDFCell("入口站名",textFont));  
  211.                 table.addCell(PDFUtil.getPDFCell(prList.get(i).getEntrStation(), textFont));  
  212.                 table.addCell(PDFUtil.getPDFCell("记录人",textFont));  
  213.                 table.addCell(PDFUtil.getPDFCell(prList.get(i).getCreator(), textFont));  
  214.   
  215.                 table.addCell(PDFUtil.mergeRow("出口通行信息", textFont, 2));  
  216.   
  217.                 table.addCell(PDFUtil.getPDFCell("车道",textFont));  
  218.                 table.addCell(PDFUtil.mergeCol("出口" + prList.get(i).getChannelNum() + "车道", textFont, 3));  
  219.   
  220.                 table.addCell(PDFUtil.getPDFCell("收费员工号",textFont));  
  221.                 table.addCell(PDFUtil.getPDFCell(prList.get(i).getToUsername(), textFont));  
  222.   
  223.                 table.addCell(PDFUtil.getPDFCell("车型",textFont));  
  224.                 table.addCell(PDFUtil.mergeCol(String.valueOf(prList.get(i).getAxleCount())+"轴", textFont,3));  
  225.   
  226.                 table.addCell(PDFUtil.getPDFCell("追缴金额",textFont));  
  227.                 table.addCell(PDFUtil.getPDFCell(String.valueOf(prList.get(i).getAmount()) + "¥", textFont));  
  228.   
  229.                 cell = PDFUtil.mergeCol("查验情况:  假冒原因:" + prList.get(i).getSpurTypeName(),textFont,7);  
  230.                 cell.setFixedHeight(280);  
  231.                 cell.setVerticalAlignment(Element.ALIGN_TOP);  
  232.                 cell.setHorizontalAlignment(Element.ALIGN_LEFT);  
  233.                 cell.setPaddingTop(10);  
  234.                 cell.setPaddingLeft(10);  
  235.                 table.addCell(cell);  
  236.   
  237.                 //取出二进制并转为base64字符串  
  238.                 String base64Str1 = Base64.encodeBase64String(prList.get(i).getPhoto1());  
  239.                 String base64Str2 = Base64.encodeBase64String(prList.get(i).getPhoto2());  
  240.                 String base64Str3 = Base64.encodeBase64String(prList.get(i).getPhoto3());  
  241.                 String base64Str4 = Base64.encodeBase64String(prList.get(i).getPhoto4());  
  242.   
  243.                 //base64解密  
  244.                 String base64Img1 = Base64Helper.getFromBase64(base64Str1);  
  245.                 String base64Img2 = Base64Helper.getFromBase64(base64Str2);  
  246.                 String base64Img3 = Base64Helper.getFromBase64(base64Str3);  
  247.                 String base64Img4 = Base64Helper.getFromBase64(base64Str4);  
  248.   
  249.                 //字符串处理  利用正则表达式找到首次出现“,”的索引  
  250.                 Matcher matcher1= Pattern.compile(",").matcher(base64Img1);  
  251.                 Matcher matcher2= Pattern.compile(",").matcher(base64Img2);  
  252.                 Matcher matcher3= Pattern.compile(",").matcher(base64Img3);  
  253.                 Matcher matcher4= Pattern.compile(",").matcher(base64Img4);  
  254.   
  255.                 //转为二进制  
  256.                 byte[] bImg1 = null;  
  257.                 byte[] bImg2 = null;  
  258.                 byte[] bImg3 = null;  
  259.                 byte[] bImg4 = null;  
  260.                 if(matcher1.find()){  
  261.                     bImg1 = Base64.decodeBase64(base64Img1.substring(matcher1.start()+1));  
  262.                 }  
  263.                 if(matcher2.find()){  
  264.                     bImg2 = Base64.decodeBase64(base64Img2.substring(matcher2.start()+1));  
  265.                 }  
  266.                 if(matcher3.find()){  
  267.                     bImg3 = Base64.decodeBase64(base64Img3.substring(matcher3.start()+1));  
  268.                 }  
  269.                 if(matcher4.find()){  
  270.                     bImg4 = Base64.decodeBase64(base64Img4.substring(matcher4.start()+1));  
  271.                 }  
  272.   
  273.                 Image img1 = Image.getInstance(bImg1);  
  274.                 Image img2 = Image.getInstance(bImg2);  
  275.                 Image img3 = Image.getInstance(bImg3);  
  276.                 Image img4 = Image.getInstance(bImg4);  
  277.                 img1.scaleToFit(120,260);  
  278.                 img2.scaleToFit(120,260);  
  279.                 img3.scaleToFit(120,260);  
  280.                 img4.scaleToFit(120,260);  
  281.                 img1.setAbsolutePosition(40,340);  
  282.                 img2.setAbsolutePosition(170,340);  
  283.                 img3.setAbsolutePosition(300,340);  
  284.                 img4.setAbsolutePosition(430,340);  
  285.   
  286.                 cell = PDFUtil.mergeCol("处理结果:追缴金额" + prList.get(i).getAmount() + "元",textFont,7);  
  287.                 cell.setFixedHeight(200);  
  288.                 cell.setVerticalAlignment(Element.ALIGN_TOP);  
  289.                 cell.setHorizontalAlignment(Element.ALIGN_LEFT);  
  290.                 cell.setPaddingTop(10);  
  291.                 cell.setPaddingLeft(10);  
  292.                 table.addCell(cell);  
  293.   
  294.                 Paragraph p2 = new Paragraph("被调查人签字:                                                                          站长签字:",textFont);  
  295.   
  296.                 p0.add(table);  
  297.                 document.add(p0);  
  298.                 document.add(p2);  
  299.                 document.add(img1);  
  300.                 document.add(img2);  
  301.                 document.add(img3);  
  302.                 document.add(img4);  
  303.             }  
  304.   
  305.   
  306.         } catch (DocumentException e) {  
  307.             e.printStackTrace();  
  308.         } catch (IOException e) {  
  309.             e.printStackTrace();  
  310.         } catch (Exception e) {  
  311.             e.printStackTrace();  
  312.         }  
  313.   
  314.         // 关闭文档  
  315.         document.close();  
  316.     }  
  317. }  
4、controller里面调用

[java]  view plain  copy
  1. /** 
  2.  * 导出数据 生成pdf 
  3.  * @param recordIds 
  4.  * @param isGreen 
  5.  * @param res 
  6.  */  
  7. @RequiresPermissions("sys:passRecord:export")  
  8. @RequestMapping("/export/{recordIds}/{isGreen}")  
  9. public void exportData(@PathVariable Long[] recordIds, @PathVariable int isGreen ,HttpServletResponse res) {  
  10.     List<SysPassRecordEntity> list = queryListByIds(recordIds,isGreen);  
  11.     try {  
  12.         OutputStream os = res.getOutputStream();  
  13.         res.setCharacterEncoding("utf-8");  
  14.         res.setHeader("content-type""application/octet-stream");  
  15.   
  16.         String date = DateUtils.format(new Date(),DateUtils.DATE_PATTERN);  
  17.   
  18.         if(isGreen == 1){  
  19.             String fileName = URLEncoder.encode("绿通车辆资料" + date + ".pdf","UTF-8");  
  20.             res.setHeader("Content-Disposition""attachment;filename=" + fileName);  
  21.             GeneratePDF.createPDF1(os,list);  
  22.         }  
  23.         if(isGreen == 0){  
  24.             String fileName = URLEncoder.encode("假冒绿通车辆资料" + date + ".pdf","UTF-8");  
  25.             res.setHeader("Content-Disposition""attachment;filename=" + fileName);  
  26.             GeneratePDF.createPDF0(os,list);  
  27.         }  
  28.   
  29.   
  30.   
  31.         os.flush();  
  32.         os.close();  
  33.     } catch (IOException e) {  
  34.         e.printStackTrace();  
  35.     }  
  36. }  


猜你喜欢

转载自blog.csdn.net/wjx_jasin/article/details/80591531