Java 生成PDF表格 itext5 例子 下载

导入jar包或Maven坐标

  1. Maven坐标
    在项目中(pom)输入
<!-- pdf模板-->
		<dependency>
			     <groupId>com.itextpdf</groupId>
			     <artifactId>itextpdf</artifactId>
			     <version>5.5.9</version>
		</dependency>

		<dependency>
			     <groupId>com.itextpdf</groupId>
			     <artifactId>itext-asian</artifactId>
			     <version>5.2.0</version>
		</dependency>
  1. itext5jar包下载
    https://download.csdn.net/download/weixin_43877551/12499903

itext5的使用

  1. 新建文档对象
//设置纸张大小(横A4)
Rectangle pageSize = new Rectangle(842.0F,595.0F);
//新建文档对象,页大小为A4纸,然后设置4个边距
Document document = new Document(pageSize,20,20,10,30);

PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("这里写输出文件路径比如 D:\\123.pdf"));
       document.open();
       //字体文件
       String fontPath = "D:\\QrpdfRes\\simsun.ttf";
       //创建字体
       BaseFont baseFont = BaseFont.createFont(fontPath ,BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
       //字体对象
       Font size9font = new Font(baseFont,9,Font.NORMAL);  //大小为9的正常字体
       Font size16font = new Font(baseFont,16,Font.BOLD);  //大小为16的粗体
       Font size9fontB = new Font(baseFont,10,Font.BOLD); //大小为10的粗体
  1. 添加二维码
          //生成二维码对象
          BarcodeQRCode barcodeQRCode = new BarcodeQRCode(“二维码内容”, 800, 800, null);
          //二维码图片文件
          Image codeQrImage = barcodeQRCode.getImage();
          codeQrImage.scaleAbsolute(80, 80);
          //写入文档对象
          document.add(codeQrImage); 
  1. 添加图片(水印)
        //水印
        Image chapterImg = Image.getInstance("水印文件路径如 D:\\123.jpg");// 插入水印
        // 设置图片水印的位置。
        chapterImg.setAbsolutePosition(630, 450);
        // 设置图片水印的大小。
        chapterImg.scaleAbsolute(130,130);
        //写入文档对象
        document.add(chapterImg);
  1. 添加内容
        //添加内容
        PdfPTable tableName = new PdfPTable(1);
        tableName.setWidthPercentage(98);  //设置标题长度占纸张比例
        PdfPCell pdfPCell  = new PdfPCell(new Phrase("内容",size16font));
        pdfPCell.setMinimumHeight(25);
        pdfPCell.setUseAscender(true); // 设置可以居中
        pdfPCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); // 设置水平居中
        pdfPCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); // 设置垂直居中 
		//颜色代码 RGB
       pdfPCell.setBackgroundColor(new BaseColor(217,217,217));
       PdfPCell pdfPCellTitle = pdfPCell;  
        pdfPCellTitle.setVerticalAlignment(PdfPCell.ALIGN_TOP);
        tableName.addCell(pdfPCellTitle);
        document.add(tableName);

额,不想写了 上代码

在这里插入图片描述

package com.ruoyi.qr.utils.pdf;


import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.qr.domain.SocialSecurity;
import com.ruoyi.system.domain.SysEmp;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.io.FileOutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


public class CreatePDFUtils2 {

    SysEmp emp;

    //字体文件
    String fontPath = "D:\\QrpdfRes\\simsun.ttf";

    //水印文件
    String watermark="D:\\QrpdfRes\\chapter.png";




    /**
     *新建以下两个方法,创建表格内的字体和样式的方法
     * @param str 内容
     * @param font 字体对象
     * @param high 表格高度
     * @Param alignCenter 是否水平居中
     * @Param alignMidde  是否垂直居中
     * @return
     */
    private static PdfPCell  mircoSoftFont(String str,Font font,int high,boolean alignCenter,boolean alignMidde){
        PdfPCell pdfPCell  = new PdfPCell(new Phrase(str,font));
        pdfPCell.setMinimumHeight(high);
        pdfPCell.setUseAscender(true); // 设置可以居中
        if (alignCenter){
            pdfPCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); // 设置水平居中
        }
        if (alignMidde){
            pdfPCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); // 设置垂直居中
        }
        return pdfPCell;
    }

    private static String  floatInString(float indata){
        long a=(long) indata;
        if (indata==0f){
            return "0";
        }
        if(indata>0f&&indata<1f){
            return String.valueOf(indata);
        }
        if (indata/a==1){
            return String.valueOf(a);
        }else
            return  String.valueOf(indata);

    }


    /**
     *
     * @param str 字符串
     * @param font 字体
     * @param high 表格高度
     * @Param alignCenter 是否水平居中
     * @Param alignMidde  是否垂直居中
     * @Param haveColor 是否有背景色(灰色)
     * @return
     */
    private static PdfPCell  mircoSoftFont(String str,Font font,int high,boolean alignCenter,boolean alignMidde,boolean haveColor){
        PdfPCell pdfPCell  = new PdfPCell(new Phrase(str,font));
        pdfPCell.setMinimumHeight(high);
        pdfPCell.setUseAscender(true); // 设置可以居中
        if (alignCenter){
            pdfPCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); // 设置水平居中
        }
        if (alignMidde){
            pdfPCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); // 设置垂直居中
        }
        if (haveColor){
            //颜色代码 RGB
            pdfPCell.setBackgroundColor(new BaseColor(217,217,217));
        }
        return pdfPCell;
    }

    /**
     *
     * @param str 字符串
     * @param font 字体
     * @param high 表格高度
     * @Param alignCenter 是否水平居中
     * @Param alignMidde  是否垂直居中
     * @Param haveColor 是否有背景色(灰色)
     * @Param border 是否无边框
     * @return
     */
    private static PdfPCell  mircoSoftFont(String str,Font font,int high,boolean alignCenter,boolean alignMidde,boolean haveColor ,boolean border){
        PdfPCell pdfPCell  = new PdfPCell(new Phrase(str,font));
        pdfPCell.setMinimumHeight(high);
        pdfPCell.setUseAscender(true); // 设置可以居中
        if (alignCenter){
            pdfPCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); // 设置水平居中
        }
        if (alignMidde){
            pdfPCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); // 设置垂直居中
        }
        if (haveColor){
            //颜色代码 RGB
            pdfPCell.setBackgroundColor(new BaseColor(217,217,217));
        }
        if (border){
            //是否无边框
            pdfPCell.setBorder(Rectangle.NO_BORDER);
        }
        return pdfPCell;
    }

    /**
     * 计算月份差
     * @return
     */
    public static int inPool(String indata,String endata) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        Calendar bef = Calendar.getInstance();
        Calendar aft = Calendar.getInstance();
        bef.setTime(sdf.parse(indata));
        aft.setTime(sdf.parse(endata));
        int surplus = aft.get(Calendar.DATE) - bef.get(Calendar.DATE);
        int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
        int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
        return  (Math.abs(month + result) + surplus+1);
    }


    public  void createHardwarePDF(String outputPath, String downloadPath)throws Exception{
        //设置纸张大小(横A4)
        Rectangle pageSize = new Rectangle(842.0F,595.0F);
        //新建文档对象,页大小为A4纸,然后设置4个边距
        Document document = new Document(pageSize,20,20,10,30);

        PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream(outputPath));
        document.open();
         fontPath = "D:\\QrpdfRes\\simsun.ttf";
        //创建字体
        BaseFont baseFont = BaseFont.createFont(fontPath ,BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
        //字体对象
        Font size9font = new Font(baseFont,9,Font.NORMAL);  //大小为9的正常字体
        Font size16font = new Font(baseFont,16,Font.BOLD);  //大小为16的粗体
        Font size9fontB = new Font(baseFont,10,Font.BOLD); //大小为10的粗体


/************************************************************生成二维码************************************************************************************/
            //生成二维码
            BarcodeQRCode barcodeQRCode = new BarcodeQRCode(downloadPath, 800, 800, null);
            Image codeQrImage = barcodeQRCode.getImage();
            codeQrImage.scaleAbsolute(80, 80);
            document.add(codeQrImage);
/************************************************************公章水印************************************************************************************/
        //公章水印
        Image chapterImg = Image.getInstance(watermark);// 插入水印
        // 设置图片水印的位置。
        chapterImg.setAbsolutePosition(630, 450);
        // 设置图片水印的大小。
        chapterImg.scaleAbsolute(130,130);
        document.add(chapterImg);

/************************************************************标题************************************************************************************/
        //添加标题
        PdfPTable tableName = new PdfPTable(1);
        tableName.setWidthPercentage(98);  //设置标题长度占纸张比例
        PdfPCell pdfPCellTitle = mircoSoftFont("社会保险个人参保缴费证明", size16font, 25, true, true, false, true);
        pdfPCellTitle.setVerticalAlignment(PdfPCell.ALIGN_TOP);
        tableName.addCell(pdfPCellTitle);
        document.add(tableName);




/************************************************************个人信息************************************************************************************/
        //添加第二行
        PdfPTable table2 = new PdfPTable(3);
        //这里的数组长度是上面创建的列数,数组的总和为1,就是按比例划分的意思
        table2.setTotalWidth(new float[]{0.3f,0.42f,0.28f});
        table2.setWidthPercentage(98);  //设置标题长度占纸张比例
        if(emp!=null){
            PdfPCell pdfPCell = mircoSoftFont("姓名:"+emp.getEmpName(), size9font, 35, true, true, false, true);
            //左对齐
            pdfPCell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
            table2.addCell(pdfPCell);
            table2.addCell(mircoSoftFont("身份证号:"+emp.getEmpNo(),size9font,35,true,true,false,true));
            table2.addCell(mircoSoftFont("个人编号:"+emp.getEmpCode(),size9font,35,true,true,false,true));
        }else {
            PdfPCell pdfPCell = mircoSoftFont("姓名:", size9font, 35, true, true, false, true);
            //左对齐
            pdfPCell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
            table2.addCell(pdfPCell);
            table2.addCell(mircoSoftFont("身份证号:",size9font,35,true,true,false,true));
            table2.addCell(mircoSoftFont("个人编号:",size9font,35,true,true,false,true));
        }

        document.add(table2);
/************************************************************表头************************************************************************************/
        //添加第三行
        PdfPTable table3 = new PdfPTable(13);
        //这里的数组长度是上面创建的列数,数组的总和为1,就是按比例划分的意思
        table3.setTotalWidth(new float[] {0.14f,0.1f,0.1f,0.1f,0.1f,0.1f,0.1f,0.1f,0.1f,0.1f,0.1f,0.1f,0.3f});
        //设置标题长度占纸张比例
        table3.setWidthPercentage(99);


        PdfPCell pdfPCell1 = mircoSoftFont("缴费起止年月", size9font, 30, true, true);
        // 设置跨两行
        pdfPCell1.setRowspan(2);
        table3.addCell(pdfPCell1);

        PdfPCell pdfPCell3 = mircoSoftFont("企业基本养老保险", size9font, 20, true, true);
        //设置占用列数
        pdfPCell3.setColspan(3);
        table3.addCell(pdfPCell3);

        PdfPCell pdfPCell4 = mircoSoftFont("基本医疗保险", size9font, 20, true, true);
        //设置占用列数
        pdfPCell4.setColspan(3);
        table3.addCell(pdfPCell4);

        PdfPCell pdfPCell5 = mircoSoftFont("失业保险", size9font, 20, true, true);
        //设置占用列数
        pdfPCell5.setColspan(3);
        table3.addCell(pdfPCell5);


        table3.addCell(mircoSoftFont("工伤保险",size9font,20,true,true));
        table3.addCell(mircoSoftFont("生育保险",size9font,20,true,true));

        PdfPCell pdfPCell2 = mircoSoftFont("缴费单位名称", size9font, 30, true, true);
        // 设置跨两行
        pdfPCell2.setRowspan(2);
        table3.addCell(pdfPCell2);


        table3.addCell(mircoSoftFont("实缴月数",size9font,20,true,true));
        table3.addCell(mircoSoftFont("缴费基数",size9font,20,true,true));
        table3.addCell(mircoSoftFont("个人缴纳",size9font,20,true,true));

        table3.addCell(mircoSoftFont("实缴月数",size9font,20,true,true));
        table3.addCell(mircoSoftFont("缴费基数",size9font,20,true,true));
        table3.addCell(mircoSoftFont("个人缴纳",size9font,20,true,true));

        table3.addCell(mircoSoftFont("实缴月数",size9font,20,true,true));
        table3.addCell(mircoSoftFont("缴费基数",size9font,20,true,true));
        table3.addCell(mircoSoftFont("个人缴纳",size9font,20,true,true));

        table3.addCell(mircoSoftFont("实缴月数",size9font,20,true,true));
        table3.addCell(mircoSoftFont("实缴月数",size9font,20,true,true));

/***************************************************************数据填充*****************************************************************************/
        //以上表头已制作完,下面开始填充数据
        long aa = 0,bb=0,cc=0,dd=0,ee=0,ff=0;
        int i=0;
//        for (SocialSecurity security : list) {
//            i++;
//            //判断是否到第二页要填表头
//            if (list.size()>12&&i==13){
//                //添加第三行
//                PdfPTable tablen = new PdfPTable(13);
//                //这里的数组长度是上面创建的列数,数组的总和为1,就是按比例划分的意思
//                table3.setTotalWidth(new float[] {0.14f,0.1f,0.1f,0.1f,0.1f,0.1f,0.1f,0.1f,0.1f,0.1f,0.1f,0.1f,0.3f});
//                //设置标题长度占纸张比例
//                table3.setWidthPercentage(99);
//
//
//                PdfPCell pdfPCelln1 = mircoSoftFont("缴费起止年月", size9font, 30, true, true);
//                // 设置跨两行
//                pdfPCelln1.setRowspan(2);
//                table3.addCell(pdfPCelln1);
//
//                PdfPCell pdfPCelln2 = mircoSoftFont("企业基本养老保险", size9font, 20, true, true);
//                //设置占用列数
//                pdfPCelln2.setColspan(3);
//                table3.addCell(pdfPCelln2);
//
//                PdfPCell pdfPCelln3 = mircoSoftFont("基本医疗保险", size9font, 20, true, true);
//                //设置占用列数
//                pdfPCelln3.setColspan(3);
//                table3.addCell(pdfPCelln3);
//
//                PdfPCell pdfPCelln4 = mircoSoftFont("失业保险", size9font, 20, true, true);
//                //设置占用列数
//                pdfPCelln4.setColspan(3);
//                table3.addCell(pdfPCelln4);
//
//
//                table3.addCell(mircoSoftFont("工伤保险",size9font,20,true,true));
//                table3.addCell(mircoSoftFont("生育保险",size9font,20,true,true));
//
//                PdfPCell pdfPCelln5 = mircoSoftFont("缴费单位名称", size9font, 30, true, true);
//                // 设置跨两行
//                pdfPCelln5.setRowspan(2);
//                table3.addCell(pdfPCelln5);
//
//
//                table3.addCell(mircoSoftFont("实缴月数",size9font,20,true,true));
//                table3.addCell(mircoSoftFont("缴费基数",size9font,20,true,true));
//                table3.addCell(mircoSoftFont("个人缴纳",size9font,20,true,true));
//
//                table3.addCell(mircoSoftFont("实缴月数",size9font,20,true,true));
//                table3.addCell(mircoSoftFont("缴费基数",size9font,20,true,true));
//                table3.addCell(mircoSoftFont("个人缴纳",size9font,20,true,true));
//
//                table3.addCell(mircoSoftFont("实缴月数",size9font,20,true,true));
//                table3.addCell(mircoSoftFont("缴费基数",size9font,20,true,true));
//                table3.addCell(mircoSoftFont("个人缴纳",size9font,20,true,true));
//
//                table3.addCell(mircoSoftFont("实缴月数",size9font,20,true,true));
//                table3.addCell(mircoSoftFont("实缴月数",size9font,20,true,true));
//            }
//
//            if (i>13&&(i-13)%17==0){
//                //添加第三行
//                PdfPTable tablen = new PdfPTable(13);
//                //这里的数组长度是上面创建的列数,数组的总和为1,就是按比例划分的意思
//                table3.setTotalWidth(new float[] {0.14f,0.1f,0.1f,0.1f,0.1f,0.1f,0.1f,0.1f,0.1f,0.1f,0.1f,0.1f,0.3f});
//                //设置标题长度占纸张比例
//                table3.setWidthPercentage(99);
//
//
//                PdfPCell pdfPCelln1 = mircoSoftFont("缴费起止年月", size9font, 30, true, true);
//                // 设置跨两行
//                pdfPCelln1.setRowspan(2);
//                table3.addCell(pdfPCelln1);
//
//                PdfPCell pdfPCelln2 = mircoSoftFont("企业基本养老保险", size9font, 20, true, true);
//                //设置占用列数
//                pdfPCelln2.setColspan(3);
//                table3.addCell(pdfPCelln2);
//
//                PdfPCell pdfPCelln3 = mircoSoftFont("基本医疗保险", size9font, 20, true, true);
//                //设置占用列数
//                pdfPCelln3.setColspan(3);
//                table3.addCell(pdfPCelln3);
//
//                PdfPCell pdfPCelln4 = mircoSoftFont("失业保险", size9font, 20, true, true);
//                //设置占用列数
//                pdfPCelln4.setColspan(3);
//                table3.addCell(pdfPCelln4);
//
//
//                table3.addCell(mircoSoftFont("工伤保险",size9font,20,true,true));
//                table3.addCell(mircoSoftFont("生育保险",size9font,20,true,true));
//
//                PdfPCell pdfPCelln5 = mircoSoftFont("缴费单位名称", size9font, 30, true, true);
//                // 设置跨两行
//                pdfPCelln5.setRowspan(2);
//                table3.addCell(pdfPCelln5);
//
//
//                table3.addCell(mircoSoftFont("实缴月数",size9font,20,true,true));
//                table3.addCell(mircoSoftFont("缴费基数",size9font,20,true,true));
//                table3.addCell(mircoSoftFont("个人缴纳",size9font,20,true,true));
//
//                table3.addCell(mircoSoftFont("实缴月数",size9font,20,true,true));
//                table3.addCell(mircoSoftFont("缴费基数",size9font,20,true,true));
//                table3.addCell(mircoSoftFont("个人缴纳",size9font,20,true,true));
//
//                table3.addCell(mircoSoftFont("实缴月数",size9font,20,true,true));
//                table3.addCell(mircoSoftFont("缴费基数",size9font,20,true,true));
//                table3.addCell(mircoSoftFont("个人缴纳",size9font,20,true,true));
//
//                table3.addCell(mircoSoftFont("实缴月数",size9font,20,true,true));
//                table3.addCell(mircoSoftFont("实缴月数",size9font,20,true,true));
//            }
//
//            String format="yyyyMM";
//            //开始时间
//            Date dateB = security.getbBate();
//
//            //结束时间
//            Date dateE = security.geteBate();
//            String b = DateUtils.parseDateToStr(format, dateB);
//            String e = DateUtils.parseDateToStr(format, dateE);
//            //缴费起止年月总和
//            //aa=aa+inPool(b,e);
//            //企业基金养老保险总和
//            bb=bb + security.getEeiPaidMonths();
//            //基本医疗保险总和
//            cc=cc+security.getBmiPaidMonths();
//            //失业保险总和
//            dd=dd+security.getUiPaidMonths();
//            //工伤保险总和
//            ee=ee+security.getEiiPaidMonths();
//            //生育保险总和
//            ff=ff+security.getMiPaidMonths();
//
//
//            //缴费起止年月
//            table3.addCell(mircoSoftFont(b+"-"+e,size9font,30,true,true));
//            //企业基金养老保险-实缴月数
//            table3.addCell(mircoSoftFont(String.valueOf(security.getEeiPaidMonths()),size9font,30,true,true));
//            //企业基金养老保险-缴费基数
           解决 0.0 -- 0  的问题
//            table3.addCell(mircoSoftFont(floatInString(security.getEeiPaymentBase()),size9font,30,true,true));
//            //企业基金养老保险-个人缴纳
//            table3.addCell(mircoSoftFont(floatInString(security.getEeiIndividualPay()),size9font,30,true,true));
//
//            //基本医疗保险-实缴月数
//            table3.addCell(mircoSoftFont(String.valueOf(security.getBmiPaidMonths()),size9font,30,true,true));
//            // 基本医疗保险-缴费基数
//            table3.addCell(mircoSoftFont(floatInString(security.getBmiPaymentBase()),size9font,30,true,true));
//            // 基本医疗保险-个人缴纳
//            table3.addCell(mircoSoftFont(floatInString(security.getBmiIndividualPay()),size9font,30,true,true));
//
//            //失业保险-实缴月数 */
//            table3.addCell(mircoSoftFont(String.valueOf(security.getUiPaidMonths()),size9font,30,true,true));
//            /// 失业保险-缴费基数 */
//            table3.addCell(mircoSoftFont(floatInString(security.getUiPaymentBase()),size9font,30,true,true));
//            // 失业保险-个人缴纳 */
//            table3.addCell(mircoSoftFont(floatInString(security.getUiIndividualPay()),size9font,30,true,true));
//
//            //工伤保险-实缴月数 */
//            table3.addCell(mircoSoftFont(String.valueOf(security.getEiiPaidMonths()),size9font,30,true,true));
//
//            //生育保险-实缴月数
//            table3.addCell(mircoSoftFont(String.valueOf(security.getMiPaidMonths()),size9font,30,true,true));
//            //缴费单位名称
//            table3.addCell(mircoSoftFont(security.getPayerName(),size9font,30,true,true));
//        }
        //写入第三列
        document.add(table3);


/***********************************************************下列是累计月数*************************************************************************/

        /*if (list!=null&&((list.size()-13)%17>12))
        {
            //生成下一页
            document.newPage();
        }*/

        //添加第四列
        PdfPTable table4 = new PdfPTable(13);
        //这里的数组长度是上面创建的列数,数组的总和为1,就是按比例划分的意思
        table4.setTotalWidth(new float[] {0.14f,0.1f,0.1f,0.1f,0.1f,0.1f,0.1f,0.1f,0.1f,0.1f,0.1f,0.1f,0.3f});
        //设置标题长度占纸张比例
        table4.setWidthPercentage(99);

        PdfPCell pdfPCell6 = mircoSoftFont("累计缴费月份", size9font, 20, true, true);
        // 设置跨两行
        pdfPCell6.setRowspan(2);
        table4.addCell(pdfPCell6);

        PdfPCell pdfPCell7 = mircoSoftFont("", size9font, 20, true, true);
        //设置占用列数
        pdfPCell7.setColspan(3);
        table4.addCell(pdfPCell7);

        PdfPCell pdfPCell8 = mircoSoftFont("", size9font, 20, true, true);
        //设置占用列数
        pdfPCell8.setColspan(3);
        table4.addCell(pdfPCell8);

        PdfPCell pdfPCell9 = mircoSoftFont("", size9font, 20, true, true);
        //设置占用列数
        pdfPCell9.setColspan(3);
        table4.addCell(pdfPCell9);

        table4.addCell(mircoSoftFont("",size9font,20,true,true));
        table4.addCell(mircoSoftFont("",size9font,20,true,true));

        PdfPCell pdfPCell10 = mircoSoftFont("", size9font, 20, true, true);
        // 设置跨两行
        pdfPCell10.setRowspan(2);


        //表格中的斜杠
        Image xieImg = Image.getInstance("D:\\QrpdfRes\\xie.png");// 插入水印
        // 设置图片水印的位置。
        xieImg.setAbsolutePosition(0 ,0);
        // 设置图片水印的大小。
        //chapterImg.scaleAbsolute(130,130);
        pdfPCell10.addElement(xieImg);
        table4.addCell(pdfPCell10);

        //写入第四列
        document.add(table4);

/************************************************************注意事项**********************************************************************************/
        //添加第五列
        PdfPTable table5 = new PdfPTable(1);
        //这里的数组长度是上面创建的列数,数组的总和为1,就是按比例划分的意思
        table5.setTotalWidth(new float[] {1f});
        //设置长度占纸张比例
        table5.setWidthPercentage(99);
        PdfPCell pdfPCell11 = mircoSoftFont("注意事项", size9fontB, 15, true, true);
        //隐藏下边框
        pdfPCell11.disableBorderSide(PdfPCell.BOTTOM);
        table5.addCell(pdfPCell11);

        //写入第五列
        document.add(table5);

        //添加第六列
        PdfPTable table6 = new PdfPTable(2);
        //这里的数组长度是上面创建的列数,数组的总和为1,就是按比例划分的意思
        table6.setTotalWidth(new float[] {0.15f,0.85f});
        //设置长度占纸张比例
        table6.setWidthPercentage(99);
        PdfPCell pdfPCell17 = mircoSoftFont("", size9font, 30, false, true);
        //隐藏右、上边框
        pdfPCell17.disableBorderSide(9);
        table6.addCell(pdfPCell17);

        PdfPCell pdfPCell12 = mircoSoftFont("1.\t本证明采用电子签章方式,不再加盖红色公章,提供内容以实缴划账为准。\n" +
                "2.\t查验证明真伪请扫描左上角的二维码,查询有效期为本证明开具日期起一年内。\n" +
                "3.\t为保证信息安全,请妥善保管个人参保缴费证明。\n" +
                "4.\t本证明复印件有效,二维码验证可多次使用。\n" +
                "5.\t本参保证明只打印呼和浩特市正常参保记录,外阜转入或失地农民一次性补录信息无法生成打印数据,请去相关窗口查询。", size9font, 70, false, true);
        pdfPCell12.setHorizontalAlignment(PdfPCell.ALIGN_LEFT); // 设置左对齐
        //隐藏左和上边框
        pdfPCell12.disableBorderSide(5);
        table6.addCell(pdfPCell12);
        //写入第6列
        document.add(table6);
/**************************************************************单位和打印时间*********************************************************************************/
        //添加第7列
        PdfPTable table7 = new PdfPTable(2);
        //这里的数组长度是上面创建的列数,数组的总和为1,就是按比例划分的意思
        table7.setTotalWidth(new float[] {0.7f,0.3f});
        //设置长度占纸张比例
        table7.setWidthPercentage(99);
        table7.addCell(mircoSoftFont("", size9font, 25, true, true,false,true));

        PdfPCell pdfPCell13 = mircoSoftFont("\t\t\t\t五险核定稽审中心", size9font, 25, true, true,false,true);
        pdfPCell13.setHorizontalAlignment(PdfPCell.ALIGN_LEFT); // 左对齐
        table7.addCell(pdfPCell13);

        table7.addCell(mircoSoftFont("", size9font, 25, false, true, false, true));

        PdfPCell pdfPCell14 = mircoSoftFont(DateFormatUtils.format(new Date(), "打印时间:yyyy年MM月dd日"), size9font, 25, false, true, false, true);
        pdfPCell14.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); // 设置居中
        table7.addCell(pdfPCell14);
        //写入第7列
        document.add(table7);

        document.close();
        writer.close();

        System.out.println(downloadPath);
    }


    //使用例子
    public static void main(String[] args)throws Exception{


        new CreatePDFUtils2().createHardwarePDF("C:\\Users\\Administrator\\Desktop\\test.pdf","123");

        //uuid去-
        /*String s = UUID.randomUUID().toString().replace("-", "").toLowerCase();
        System.out.println(s);*/

    }

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值