PDF 生成,添加图片

PDF工具类
maven配置

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13</version>
</dependency>
		
<!-- 导出PDF -->
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itext-asian</artifactId>
    <version>5.2.0</version>
</dependency>
<!-- 导出PDF -->

工具类
PDFUtils

package com.platform.common.utils;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;

import java.io.*;
import java.util.Date;

/**
 * @Author lihaibo
 * @Date 2023/9/12 10:23
 * @PackageName:com.platform.common.utils
 * @ClassName: PdfUtils
 * @Description: TODO
 * @Version 1.0
 */
public class PdfUtils {
    public static void main(String[] args) {
        System.out.println("===========start=============");
        try {
//            Document doc = createPdf("C:\\Users\\Administrator\\Desktop\\test1.pdf");
//            //生成  合同文件
//            createFile(doc,"测试","32413246546546543","测试","测试","山西省","测试市","测试","","","测试"
//                    ,"山西省","测试市","测试","","","测试","","13211113356","一般项目:教育咨询服务(不含涉许可审批的教育培训活动);信息咨询服务(不含许可类信息咨询服务);信息技术咨询服务;健康咨询服务(不含诊疗服务);人力资源服务(不含职业中介活动、劳务派遣服务);财务咨询",
//                    "2020年12月11日","2020年12月11日","测试有限公司");
//
            createFile1(doc,"个体经营户","32413246546546543","测试","测试","山西省","西安市","雁塔区","","测试","13211115623"
                    ,"空间的空间放得开的开发贷款是咖啡的苦涩的","测试管理公司","100","20","1");
//            doc.close();
//            String[] files = {"C:\\\\Users\\\\Administrator\\\\Desktop\\\\test3.pdf"};
//            String outputPath= "C:\\\\Users\\\\Administrator\\\\Desktop\\\\";
//            String outputFileName ="merge.pdf";
//            try {
//                PdfUtils.mergePDF(files,outputPath,outputFileName);
//            } catch (Exception e) {
//            }
//            addImage("C:\\Users\\Administrator\\Desktop\\test1.pdf","C:\\Users\\Administrator\\Desktop\\1111.jpg","C:\\Users\\Administrator\\Desktop\\test7.pdf",null);
            

        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("===========end=============");
    }

    /**
     * 创建一个pdf并打开
     * @param outpath  pdf路径
     */
    public static Document createPdf(String outpath) throws DocumentException, IOException{
        //页面大小
        //Rectangle rect = new Rectangle(PageSize.A4.rotate());//文档横方向
        Rectangle rect = new Rectangle(PageSize.A4);//文档竖方向
        //如果没有则创建
        File saveDir = new File(outpath);
        File dir = saveDir.getParentFile();
        if (!dir.exists()) {
            dir.mkdirs();
        }
        Document doc = new Document(rect);
        PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(outpath));
        //PDF版本(默认1.4)
        writer.setPdfVersion(PdfWriter.PDF_VERSION_1_2);
        //文档属性
//        doc.addTitle("Title@wpixel");
//        doc.addAuthor("Author@wpixel");
//        doc.addSubject("Subject@wpixel");
//        doc.addKeywords("Keywords@wpixel");
//        doc.addCreator("Creator@wpixel");
        //页边空白
        doc.setMargins(1, 1, 40, 40);
        //打开文档
        doc.open();
        return doc;
    }

    public static void createFile(Document doc,String title,String number,String addr,
                                  String person,String province,String city,String prefectures,
                                  String heung,String village,String street,String province1,String city1,String prefectures1,
                                  String heung1,String village1,String street1,String phone,String mobile,String business,
                                  String inception,String opening,String enterpriseName) throws DocumentException{
        doc.add(PdfFontUtils.getFont(1, title));
        PdfPTable table1 = new PdfPTable(new float[]{2,3});
        // 制表信息
        PdfPCell cell1;
        cell1 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "表号:321表")));
        cell1.setColspan(1);
        cell1.setBorder(0);
        table1.addCell(cell1);
        cell1 = new PdfPCell(new Phrase(PdfFontUtils.getFont(7, " ")));
        cell1.setColspan(1);
        cell1.setBorder(0);
        table1.addCell(cell1);
        cell1 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "制定机关:国家统计局国务院经济")));
        cell1.setColspan(2);
        cell1.setBorder(0);
        table1.addCell(cell1);
//        cell1 = new PdfPCell(new Phrase(PdfFontUtils.getFont(7, " ")));
//        cell1.setColspan(1);
//        cell1.setBorder(0);
//        table1.addCell(cell1);
        cell1 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "文号:国统字〔2023〕77号")));
        cell1.setColspan(1);
        cell1.setBorder(0);
        table1.addCell(cell1);
        cell1 = new PdfPCell(new Phrase(PdfFontUtils.getFont(7, " ")));
        cell1.setColspan(1);
        cell1.setBorder(0);
        table1.addCell(cell1);
        cell1 = new PdfPCell(new Phrase(PdfFontUtils.getFont(7, "有效期至:2024年6月")));
        cell1.setColspan(1);
        cell1.setBorder(0);
        table1.addCell(cell1);
        cell1 = new PdfPCell(new Phrase(PdfFontUtils.getFont(7, "")));
        cell1.setColspan(1);
        cell1.setBorder(0);
        table1.addCell(cell1);
        String str = "测试";
        cell1 = new PdfPCell(new Phrase(PdfFontUtils.getFont(7, str)));
        cell1.setColspan(2);
        cell1.setLeading(10,0.5f);
        cell1.setBorder(1);
        table1.addCell(cell1);
        doc.add(table1);
        PdfPTable table = new PdfPTable(8);
        table.getDefaultCell().setBorder(PdfPCell.ALIGN_BASELINE);
        PdfPCell cell;
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(7, "01")));
        cell.setColspan(1);
        cell.setBorder(1);
        cell.setBottom(0);
        cell.setTop(0);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "单位类型")));
        cell.setColspan(0);
        cell.setBorder(PdfPCell.BOX);
        cell.disableBorderSide(2);
        cell.disableBorderSide(8);
        cell.setLeft(1);
        cell.setRight(0);
        table.addCell(cell);
        cell = new PdfPCell(PdfFontUtils.getTable("1"));
        cell.setColspan(1);
        cell.setBorder(PdfPCell.BOX);
        cell.setPaddingBottom(2);
        cell.disableBorderSide(2);
        cell.disableBorderSide(4);
        cell.disableBorderSide(8);
        cell.setPaddingLeft(0);
        cell.setPaddingTop(2);
        cell.setPaddingRight(40);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "(已视同法人单位的分支机构,请填写“1”)")));
        cell.setColspan(5);
        cell.setBorder(PdfPCell.BOX);
        cell.disableBorderSide(4);
        cell.setLeft(0);
        cell.disableBorderSide(2);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(7, " ")));
        cell.setColspan(1);
        cell.setBorder(0);
        cell.setBottom(0);
        cell.setTop(0);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(7, "1 法人单位 2 产业活动单位\n普查机构填写:如为视同法人单位,请勾选 □")));
        cell.setColspan(7);
        cell.setBorder(PdfPCell.BOX);
        cell.disableBorderSide(1);
        cell.setBottom(0);cell.setLeft(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(7, "02")));
        cell.setColspan(1);
        cell.setBorder(1);
        cell.setBottom(0);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(7, "统一社会信用代码 (没有统一社会信用代码的,由普查机构填写统计用临时代码)")));
        cell.setColspan(7);
        cell.setBorder(PdfPCell.BOX);
        cell.disableBorderSide(2);
        cell.setBottom(0);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(7, " ")));
        cell.setColspan(1);
        cell.setBorder(0);
        cell.setTop(0);
        table.addCell(cell);
        cell = new PdfPCell(PdfFontUtils.getTable(number));
        cell.setColspan(7);
        cell.setBorder(PdfPCell.BOX);
        cell.disableBorderSide(1);
        cell.setTop(0);
        cell.setPaddingBottom(5);
        cell.setPaddingTop(5);
        cell.setPaddingLeft(5);
        cell.setPaddingRight(200);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "03")));
        cell.setColspan(1);
        cell.setBorder(1);
        cell.setBottom(1);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(8, "单位详细名称   -"+enterpriseName)));
        cell.setColspan(7);
        cell.setBorder(PdfPCell.BOX);
        cell.setPaddingBottom(5);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "04")));
        cell.setColspan(1);
        cell.setBorder(1);
        cell.setBottom(1);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(8, "法定代表人(单位负责人)    -"+person)));
        cell.setColspan(7);
        cell.setBorder(PdfPCell.BOX);
        cell.setPaddingBottom(5);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "05")));
        cell.setColspan(1);
        cell.setBorder(1);
        cell.setBottom(1);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "运营状态")));
        cell.setColspan(1);
        cell.setBorder(PdfPCell.BOX);
        cell.disableBorderSide(8);
        cell.disableBorderSide(2);
        table.addCell(cell);
        cell = new PdfPCell(PdfFontUtils.getTable("1"));
        cell.setColspan(1);
        cell.setBorder(PdfPCell.BOX);
        cell.setPaddingBottom(2);
        cell.disableBorderSide(2);
        cell.disableBorderSide(4);
        cell.disableBorderSide(8);
        cell.setPaddingLeft(1);
        cell.setPaddingTop(2);
        cell.setPaddingRight(40);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "(季节性生产 3 个月以上或临时性停产的企业,请填写“1”)")));
        cell.setColspan(5);
        cell.setBorder(PdfPCell.BOX);
        cell.setPaddingLeft(0);
        cell.disableBorderSide(2);
        cell.disableBorderSide(4);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        cell.setColspan(1);
        cell.setBorder(0);
        cell.setBottom(1);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "1 正常运营 2 停业(歇业) 3 筹建 4 当年关闭 5 当年破产 6 当年注销 7 当年撤(吊)销 9 其他")));
        cell.setColspan(7);
        cell.setBorder(PdfPCell.BOX);
        cell.disableBorderSide(1);
        cell.setBottom(1);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "06")));
        cell.setColspan(1);
        cell.setBorder(1);
        cell.setBottom(1);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(9, "单位所在地区划及详细地址 (请精确、具体地描述本单位目前从事主要业务活动所在地详细地址," +
                "详细填写街(路)、门牌号,例如:北京西路 12 号楼 710 房间)\n -"+province+"省(自治区、直辖市)"+city+"地(市、州、盟)"+prefectures+"县(市、区、旗)"+heung+
                "乡 ( 镇 、 街 道 )"+village+"村 ( 居 ) 委 会"+street+"街(路)、门牌号\n#普查机构填写:区划代码 □ □ □ □ □ □ □ □ □ □ □ □\n普查小区代码 □ □ □")));
        cell.setColspan(7);
        cell.setLeading(10,0.5f);
        cell.setBorder(PdfPCell.BOX);
        cell.setPaddingBottom(5);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "07")));
        cell.setColspan(1);
        cell.setBorder(1);
        cell.setBottom(1);
        cell.setTop(1);
        table.addCell(cell);
        cell.setBorder(PdfPCell.BOX);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(9, "单位注册地区划及详细地址\n是否与单位所在地详细地址一致: □1 是,2 否\n  -"+province1+
                        "省(自治区、直辖市)"+city1+"地(市、州、盟)"+prefectures1+"县(市、区、旗)"+heung1+"乡 ( 镇 、 街 道 )"+village1+"村 ( 居 ) 委 会"+street1+"街(路)、门牌号\n" +
                        "#普查机构填写:区划代码 □ □ □ □ □ □ □ □ □ □ □ □")));
        cell.setColspan(7);
        cell.setLeading(10,0.5f);
        cell.setBorder(PdfPCell.BOX);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "08")));
        cell.setColspan(1);
        cell.setBorder(1);
        cell.setBottom(1);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "联系电话长途区号 □ □ □ □ □\n固定电话 "+phone+"\n移动电话 "+mobile)));
        cell.setColspan(7);
        cell.setBorder(PdfPCell.BOX);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "09")));
        cell.setColspan(1);
        cell.setBorder(1);
        cell.setBottom(1);
        cell.setTop(1);
        table.addCell(cell);
        StringBuffer sb = new StringBuffer();
        int index = business.indexOf("一般项目");
        if (index>-1){
            business = business.substring(5,business.length());
        }
        String[] businesss = business.split(";");
        for (int i = 0; i < businesss.length; i++) {
            if (i<5){
                sb.append((i+1)+":"+businesss[i]);
            }
        }
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(9, "行业类别 (请按照“动词+名词”或“名词+动词”的形式,详细描述本单位目前从事的主要业务活动。从事多种活动的," +
                "请按重要程度或者营业收入比重依次填写 1到3 种活动)\n主要业务活动 -"+sb.toString()+"#\n普查机构填写:行业代码 □ □ □ □")));
        cell.setColspan(7);
        cell.setLeading(10,0.5f);
        cell.setBorder(PdfPCell.BOX);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "10")));
        cell.setColspan(1);
        cell.setBorder(1);
        cell.setBottom(1);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "机构类型")));
        cell.setColspan(1);
        cell.setBorder(PdfPCell.BOX);
        cell.disableBorderSide(8);
        cell.disableBorderSide(2);
        table.addCell(cell);
        cell = new PdfPCell(PdfFontUtils.getTableText("10"));
        cell.setColspan(1);
        cell.setBorder(PdfPCell.BOX);
        cell.setPaddingBottom(2);
        cell.disableBorderSide(2);
        cell.disableBorderSide(4);
        cell.disableBorderSide(8);
        cell.setPaddingLeft(1);
        cell.setPaddingTop(2);
        cell.setPaddingRight(40);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        cell.setColspan(5);
        cell.setBorder(PdfPCell.BOX);
        cell.disableBorderSide(2);
        cell.disableBorderSide(4);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        cell.setColspan(1);
        cell.setBorder(0);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "10 企业 20 事业单位 30 机关 40 社会团体 51 民办非企业单位52 基金会53 居委会 54 村委会 55 农民专业合作社 56 农村集体经济组织90 其他组织机构")));
        cell.setColspan(7);
        cell.setBorder(PdfPCell.BOX);
        cell.disableBorderSide(1);
        cell.setBottom(1);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "11")));
        cell.setColspan(1);
        cell.setBorder(1);
        cell.setBottom(1);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "登记注册统计类别")));
        cell.setColspan(2);
        cell.setBorder(PdfPCell.BOX);
        cell.disableBorderSide(2);
        cell.disableBorderSide(8);
        table.addCell(cell);
        cell = new PdfPCell(PdfFontUtils.getTableText("119"));
        cell.setColspan(1);
        cell.setBorder(PdfPCell.BOX);
        cell.setPaddingBottom(2);
        cell.disableBorderSide(2);
        cell.disableBorderSide(4);
        cell.disableBorderSide(8);
        cell.setPaddingLeft(1);
        cell.setPaddingTop(2);
        cell.setPaddingRight(35);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        cell.setColspan(4);
        cell.setBorder(PdfPCell.BOX);
        cell.disableBorderSide(2);
        cell.disableBorderSide(4);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        cell.setColspan(1);
        cell.setBorder(0);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "内资企业\n111 国有独资公司 112 私营有限责任公司 119 其他有限责任公司\n121 私营股份有限公司 129 其他股份有限公司\n" +
                "131 全民所有制企业(国有企业) 132 集体所有制企业(集体企业) 133 股份合作企业134 联营企业\n140 个人独资企业 150 合伙企业 190 其他内资企业\n港澳台投资企业\n210 港澳台投资有限责任公司 220 港澳台投资股份有限公司 230 港澳台投资合伙企业\n" +
                "290 其他港澳台投资企业\n外商投资企业\n310 外商投资有限责任公司 320 外商投资股份有限公司 330 外商投资合伙企业\n390 其他外商投资企业\n400 农民专业合作社(联合社) 500 个体工商户 900 其他市场主体")));
        cell.setColspan(7);
        cell.setBorder(PdfPCell.BOX);
        cell.disableBorderSide(1);
        cell.setBottom(1);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "12")));
        cell.setColspan(1);
        cell.setBorder(1);
        cell.setBottom(1);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(10, "成立时间 -"+inception+"#(请参照营业执照或登记证书中的成立日期填写)\n开业时间 -"+opening+"#(请企业根据实际开业时间填写)")));
        cell.setColspan(7);
        cell.setBorder(PdfPCell.BOX);
        cell.setLeading(10,0.5f);
        cell.setPaddingBottom(5);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "请法人单位填写:")));
        cell.setColspan(8);
        cell.setBorder(1);
        cell.setBottom(1);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "13")));
        cell.setColspan(1);
        cell.setBorder(1);
        cell.setBottom(1);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "执行会计标准类别")));
        cell.setColspan(2);
        cell.setBorder(PdfPCell.BOX);
        cell.disableBorderSide(2);
        cell.disableBorderSide(8);
        table.addCell(cell);
        cell = new PdfPCell(PdfFontUtils.getTableText("1"));
        cell.setColspan(1);
        cell.setBorder(PdfPCell.BOX);
        cell.setPaddingBottom(2);
        cell.disableBorderSide(2);
        cell.disableBorderSide(4);
        cell.disableBorderSide(8);
        cell.setPaddingLeft(1);
        cell.setPaddingTop(2);
        cell.setPaddingRight(45);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        cell.setColspan(4);
        cell.setBorder(PdfPCell.BOX);
        cell.disableBorderSide(2);
        cell.disableBorderSide(4);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        cell.setColspan(1);
        cell.setBorder(0);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "1 企业会计准则制度 2 政府会计准则制度 4 民间非营利组织会计制度9 其他")));
        cell.setColspan(7);
        cell.setBorder(PdfPCell.BOX);
        cell.disableBorderSide(1);
        cell.setBottom(1);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "14")));
        cell.setColspan(1);
        cell.setBorder(1);
        cell.setBottom(0);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "如有下属产业活动单位(不包括视同法人单位),请填写:\n" +
                "本单位本部(总部、本店、本所等)和其他下属产业活动单位(分部、分厂、分店、支所等)共 0 个。\n" +
                "请填写法人单位本部和其他下属产业活动单位的基本情况:\n")));
        cell.setColspan(7);
        cell.setBorder(PdfPCell.BOX);
        cell.setBottom(0);
        cell.setTop(1);
        cell.setLeft(1);
        cell.setRight(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        cell.setColspan(1);
        cell.setBorder(0);
        cell.setBottom(1);
        cell.setTop(0);
        table.addCell(cell);
        PdfPTable table2 = new PdfPTable(9);
        PdfPCell pdfPCell2 ;
        pdfPCell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "序号:")));
        pdfPCell2.setBorder(PdfPCell.BOX);
        table2.addCell(pdfPCell2);
        pdfPCell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "单位类别(1 本部 2 分支机构)")));
        pdfPCell2.setBorder(PdfPCell.BOX);
        table2.addCell(pdfPCell2);
        pdfPCell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "统一社会信用代码")));
        pdfPCell2.setBorder(PdfPCell.BOX);
        table2.addCell(pdfPCell2);
        pdfPCell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "单位详细名称")));
        pdfPCell2.setBorder(PdfPCell.BOX);
        table2.addCell(pdfPCell2);
        pdfPCell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "详细地址")));
        pdfPCell2.setBorder(PdfPCell.BOX);
        table2.addCell(pdfPCell2);
        pdfPCell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "区划代码(普查机构填写省、地市、县 6 位代码)")));
        pdfPCell2.setBorder(PdfPCell.BOX);
        table2.addCell(pdfPCell2);
        pdfPCell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "联系电话")));
        pdfPCell2.setBorder(PdfPCell.BOX);
        table2.addCell(pdfPCell2);
        pdfPCell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "主要业务活动")));
        pdfPCell2.setBorder(PdfPCell.BOX);
        table2.addCell(pdfPCell2);
        pdfPCell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "行业代码(普查机构填写)")));
        pdfPCell2.setBorder(PdfPCell.BOX);
        table2.addCell(pdfPCell2);
        //
        pdfPCell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        pdfPCell2.setBorder(PdfPCell.BOX);
        table2.addCell(pdfPCell2);
        pdfPCell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        pdfPCell2.setBorder(PdfPCell.BOX);
        table2.addCell(pdfPCell2);
        pdfPCell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        pdfPCell2.setBorder(PdfPCell.BOX);
        table2.addCell(pdfPCell2);
        pdfPCell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        pdfPCell2.setBorder(PdfPCell.BOX);
        table2.addCell(pdfPCell2);
        pdfPCell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        pdfPCell2.setBorder(PdfPCell.BOX);
        table2.addCell(pdfPCell2);
        pdfPCell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        pdfPCell2.setBorder(PdfPCell.BOX);
        table2.addCell(pdfPCell2);
        pdfPCell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        pdfPCell2.setBorder(PdfPCell.BOX);
        table2.addCell(pdfPCell2);
        pdfPCell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        pdfPCell2.setBorder(PdfPCell.BOX);
        table2.addCell(pdfPCell2);
        pdfPCell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        pdfPCell2.setBorder(PdfPCell.BOX);
        table2.addCell(pdfPCell2);
        pdfPCell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        pdfPCell2.setBorder(PdfPCell.BOX);
        table2.addCell(pdfPCell2);
        pdfPCell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        pdfPCell2.setBorder(PdfPCell.BOX);
        table2.addCell(pdfPCell2);
        pdfPCell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        pdfPCell2.setBorder(PdfPCell.BOX);
        table2.addCell(pdfPCell2);
        pdfPCell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        pdfPCell2.setBorder(PdfPCell.BOX);
        table2.addCell(pdfPCell2);
        pdfPCell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        pdfPCell2.setBorder(PdfPCell.BOX);
        table2.addCell(pdfPCell2);
        pdfPCell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        pdfPCell2.setBorder(PdfPCell.BOX);
        table2.addCell(pdfPCell2);
        pdfPCell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        pdfPCell2.setBorder(PdfPCell.BOX);
        table2.addCell(pdfPCell2);
        pdfPCell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        pdfPCell2.setBorder(PdfPCell.BOX);
        table2.addCell(pdfPCell2);
        pdfPCell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        pdfPCell2.setBorder(PdfPCell.BOX);
        table2.addCell(pdfPCell2);
        pdfPCell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        pdfPCell2.setBorder(PdfPCell.BOX);
        table2.addCell(pdfPCell2);
        pdfPCell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        pdfPCell2.setBorder(PdfPCell.BOX);
        table2.addCell(pdfPCell2);
        pdfPCell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        pdfPCell2.setBorder(PdfPCell.BOX);
        table2.addCell(pdfPCell2);
        pdfPCell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        pdfPCell2.setBorder(PdfPCell.BOX);
        table2.addCell(pdfPCell2);
        pdfPCell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        pdfPCell2.setBorder(PdfPCell.BOX);
        table2.addCell(pdfPCell2);
        pdfPCell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        pdfPCell2.setBorder(PdfPCell.BOX);
        table2.addCell(pdfPCell2);
        pdfPCell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        pdfPCell2.setBorder(PdfPCell.BOX);
        table2.addCell(pdfPCell2);
        pdfPCell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        pdfPCell2.setBorder(PdfPCell.BOX);
        table2.addCell(pdfPCell2);
        pdfPCell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        pdfPCell2.setBorder(PdfPCell.BOX);
        table2.addCell(pdfPCell2);
        cell = new PdfPCell(table2);
        cell.setColspan(7);
        cell.setBorder(PdfPCell.BOX);
        cell.setBottom(1);
        cell.setTop(0);
        cell.setRight(1);
        cell.setLeft(1F);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "请产业活动单位填写:")));
        cell.setColspan(8);
        cell.setBorder(PdfPCell.TOP);
        cell.setBottom(1);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "15")));
        cell.setColspan(1);
        cell.setBorder(1);
        cell.setBottom(1);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "单位类别")));
        cell.setColspan(1);
        cell.setBorder(PdfPCell.BOX);
        cell.disableBorderSide(2);
        cell.disableBorderSide(8);
        table.addCell(cell);
        cell = new PdfPCell(PdfFontUtils.getTableText("1"));
        cell.setColspan(1);
        cell.setBorder(PdfPCell.BOX);
        cell.setPaddingBottom(2);
        cell.disableBorderSide(2);
        cell.disableBorderSide(4);
        cell.disableBorderSide(8);
        cell.setPaddingLeft(1);
        cell.setPaddingTop(2);
        cell.setPaddingRight(45);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        cell.setColspan(5);
        cell.setBorder(PdfPCell.BOX);
        cell.disableBorderSide(2);
        cell.disableBorderSide(4);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        cell.setColspan(1);
        cell.setBorder(0);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "1 本部 (多产业法人单位去除下设分支机构后剩余的部分,如总部、本店、本所等) 2 分支机构 (多产业法人单位下设的分支机构,如分部、分厂、分店、支所等)")));
        cell.setColspan(7);
        cell.setBorder(PdfPCell.BOX);
        cell.disableBorderSide(1);
        cell.disableBorderSide(2);
        cell.setBottom(1);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        cell.setColspan(1);
        cell.setBorder(0);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "统一社会信用代码 ")));
        cell.setColspan(2);
        cell.setBorder(PdfPCell.BOX);
        cell.disableBorderSide(1);
        cell.disableBorderSide(2);
        cell.setBottom(1);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(PdfFontUtils.getTable(number));
        cell.setColspan(5);
        cell.setBorder(PdfPCell.BOX);
        cell.disableBorderSide(1);
        cell.disableBorderSide(2);
        cell.setBottom(1);
        cell.setTop(1);
        cell.setPaddingRight(20);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        cell.setColspan(1);
        cell.setBorder(0);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(10, "单位详细名称 -"+enterpriseName+" \n#单位详细地址 -"+addr+"#\n普查机构填写:区划代码□ □ □ □ □ □")));
        cell.setColspan(7);
        cell.setBorder(PdfPCell.BOX);
        cell.disableBorderSide(1);
        cell.setBottom(1);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "系统标识(调查对象免填)")));
        cell.setColspan(8);
        cell.setBorder(1);
        cell.setBottom(1);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "16")));
        cell.setColspan(1);
        cell.setBorder(1);
        cell.setBottom(1);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "底册唯一标识码\n资料来源")));
        cell.setColspan(7);
        cell.setBorder(PdfPCell.BOX);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "单位受访人:"+person)));
        cell.setColspan(2);
        cell.setBorder(1);
        cell.setBottom(1);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "联系电话:"+mobile)));
        cell.setColspan(3);
        cell.setBorder(0);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "报出日期:"+DateUtils.formatDate(new Date(),"yyyy年MM月dd日"))));
        cell.setColspan(3);
        cell.setBorder(0);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "说明:1.统计范围:辖区内全部法人单位和产业活动单位。\n" +
                "2.报送日期及方式:普查员入户采集,省级普查机构在 2023 年 11 月 10 日 24 时前完成数据审核、验收、上报。\n" +
                "3.数据填报要求:本表中部分信息根据清查底册导入,应当据实修改。")));
        cell.setColspan(8);
        cell.setBorder(0);
        table.addCell(cell);
        doc.add(table);
    }

    public static void createFile1(Document doc,String title,String number,
                                  String person,String province,String city,String prefectures,
                                  String heung,String village,String street,String mobile,String business,
                                  String enterpriseName,String pnum,String wnum,String inceipty) throws DocumentException{
        doc.add(PdfFontUtils.getFont(1, title));
        PdfPTable table1 = new PdfPTable(new float[]{2,3});
        // 制表信息
        PdfPCell cell1;
        cell1 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "表号:622表")));
        cell1.setColspan(1);
        cell1.setBorder(0);
        table1.addCell(cell1);
        cell1 = new PdfPCell(new Phrase(PdfFontUtils.getFont(7, " ")));
        cell1.setColspan(1);
        cell1.setBorder(0);
        table1.addCell(cell1);
        cell1 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "制定机关:国家统计局国务院测试")));
        cell1.setColspan(2);
        cell1.setBorder(0);
        table1.addCell(cell1);
//        cell1 = new PdfPCell(new Phrase(PdfFontUtils.getFont(7, " ")));
//        cell1.setColspan(1);
//        cell1.setBorder(0);
//        table1.addCell(cell1);
        cell1 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "文号:国统字〔2023〕77号")));
        cell1.setColspan(1);
        cell1.setBorder(0);
        table1.addCell(cell1);
        cell1 = new PdfPCell(new Phrase(PdfFontUtils.getFont(7, " ")));
        cell1.setColspan(1);
        cell1.setBorder(0);
        table1.addCell(cell1);
        cell1 = new PdfPCell(new Phrase(PdfFontUtils.getFont(7, "有效期至:2024年6月")));
        cell1.setColspan(1);
        cell1.setBorder(0);
        table1.addCell(cell1);
        cell1 = new PdfPCell(new Phrase(PdfFontUtils.getFont(7, " ")));
        cell1.setColspan(1);
        cell1.setBorder(0);
        table1.addCell(cell1);
        String str = "测试";
        cell1 = new PdfPCell(new Phrase(PdfFontUtils.getFont(7, str)));
        cell1.setColspan(2);
        cell1.setBorder(1);
        cell1.setLeading(10,0.5f);
        table1.addCell(cell1);
        doc.add(table1);
        PdfPTable table = new PdfPTable(8);
        table.getDefaultCell().setLeading(20, 1);
        table.getDefaultCell().setBorder(PdfPCell.ALIGN_BASELINE);
        PdfPCell cell;
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(7, "01")));
        cell.setMinimumHeight(20);
        cell.setColspan(1);
        cell.setBorder(1);
        cell.setBottom(1);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(8, "个体经营户名称:-"+enterpriseName)));
        cell.setColspan(7);
        cell.setBorder(PdfPCell.BOX);
        cell.setPaddingBottom(5);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(7, "02")));
        cell.setColspan(1);
        cell.setBorder(1);
        cell.setBottom(1);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(8, "个体经营户经营者姓名:-"+person)));
        cell.setColspan(7);
        cell.setBorder(PdfPCell.BOX);
        cell.setPaddingBottom(5);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "03")));
        cell.setColspan(1);
        cell.setBorder(1);
        cell.setBottom(1);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(7, "有无营业执照:")));
        cell.setColspan(2);
        cell.setBorder(PdfPCell.BOX);
        cell.disableBorderSide(8);
        cell.disableBorderSide(2);
        table.addCell(cell);
        cell = new PdfPCell(PdfFontUtils.getTableText("1"));
        cell.setColspan(1);
        cell.setBorder(PdfPCell.BOX);
        cell.setPaddingBottom(1);
        cell.setPaddingTop(5);
        cell.setPaddingRight(20);
        cell.setPaddingLeft(20);
        cell.disableBorderSide(1);
        cell.disableBorderSide(2);
        cell.disableBorderSide(4);
        cell.disableBorderSide(8);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(7, "1 有 2 无")));
        cell.setColspan(4);
        cell.setBorder(PdfPCell.BOX);
        cell.disableBorderSide(4);
        cell.disableBorderSide(2);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        cell.setColspan(1);
        cell.setBorder(0);
        cell.setBottom(1);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "统一社会信用代码(如果没有则无须填写)")));
        cell.setColspan(4);
        cell.setBorder(PdfPCell.BOX);
        cell.disableBorderSide(8);
        cell.disableBorderSide(1);
        table.addCell(cell);
        cell = new PdfPCell(PdfFontUtils.getTable(number));
        cell.setColspan(3);
        cell.setBorder(PdfPCell.BOX);
        cell.setPaddingRight(10);
        cell.setPaddingLeft(10);
        cell.setPaddingBottom(2);
        cell.setPaddingTop(2);
        cell.disableBorderSide(1);
        cell.disableBorderSide(2);
        cell.disableBorderSide(4);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "04")));
        cell.setColspan(1);
        cell.setBorder(1);
        cell.setBottom(1);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "联系电话(固定电话或者移动电话):"+mobile)));
        cell.setColspan(7);
        cell.setBorder(PdfPCell.BOX);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "05")));
        cell.setColspan(1);
        cell.setBorder(1);
        cell.setBottom(1);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(9, "个体经营户所在地区划及详细地址\n-"+province+"省(自治区、直辖市)"+city+"地(市、州、盟)"+prefectures+"县(市、区、旗)"+heung+
                "乡 ( 镇 、 街 道 )"+village+"村 ( 居 ) 委 会"+street+"街(路)、门牌号\n#\n普查机构填写:区划代码 □ □ □ □ □ □ □ □ □ □ □ □\n普查小区代码 □□□")));
        cell.setColspan(7);
        cell.setBorder(PdfPCell.BOX);
        cell.setLeading(10,0.5f);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "06")));
        cell.setColspan(1);
        cell.setBorder(1);
        cell.setBottom(1);
        cell.setTop(1);
        table.addCell(cell);
        cell.setBorder(PdfPCell.BOX);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(9, "行业类别(请按照“动词+名词”或者“名词+动词”的形式,详细描述目前从事的主要业务活动)。\n-"+business+"\n#普查机构填写:行业代码(中类) □□□")));
        cell.setColspan(7);
        cell.setBorder(PdfPCell.BOX);
        cell.setLeading(10,0.5f);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "07")));
        cell.setColspan(1);
        cell.setBorder(1);
        cell.setBottom(1);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "从业人员期末人数 "+pnum+"人,其中女性 "+wnum+"人(指2023 年6 月30 日参加经营活动的所有人员,包括业主、雇员以及参加经营活动的其他人员,如家庭成员、帮手和学徒,2023年7月 1 日以后开业的,按照受访当日情况填写)。")));
        cell.setColspan(7);
        cell.setBorder(PdfPCell.BOX);
        cell.setLeading(10,0.5f);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "08")));
        cell.setColspan(1);
        cell.setBorder(1);
        cell.setBottom(1);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "预计今年营业收入为:")));
        cell.setColspan(2);
        cell.setBorder(PdfPCell.BOX);
        cell.disableBorderSide(8);
        cell.disableBorderSide(2);
        table.addCell(cell);
        cell = new PdfPCell(PdfFontUtils.getTableText(inceipty));
        cell.setColspan(5);
        cell.setBorder(PdfPCell.BOX);
        cell.setPaddingTop(5);
        cell.setPaddingBottom(5);
        cell.setPaddingLeft(10);
        cell.setPaddingRight(270);
        cell.disableBorderSide(2);
        cell.disableBorderSide(4);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, " ")));
        cell.setColspan(1);
        cell.setBorder(0);
        cell.setBottom(1);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "①营业收入<200 万元 ②200 万元≤营业收入<500 万元 ③500 万元≤营业收入<1000 万元④1000 万元≤营业收入<2000 万元 ⑤营业收入≥2000 万元")));
        cell.setColspan(7);
        cell.setBorder(PdfPCell.BOX);
        cell.disableBorderSide(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "受访人:"+person)));
        cell.setColspan(4);
        cell.setBorder(1);
        cell.setBottom(1);
        cell.setTop(1);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "报出日期:"+DateUtils.getDate())));
        cell.setColspan(4);
        cell.setBorder(0);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "说明:1.统计范围:辖区内从事第二产业和第三产业活动的个体经营户。\n" +
                "2.报送日期及方式:普查员入户采集,省级普查机构在 2023 年 11 月10 日24 时前完成数据审核、验收、上报。")));
        cell.setColspan(8);
        cell.setBorder(0);
        table.addCell(cell);
        doc.add(table);
    }

    /**
     * 将多个PDF合并成一个PDF
     * @param files 源PDF路径
     * @param outputPath 合并后输出的PDF路径
     * @param outputFileName 合并后输出的PDF文件名
     */
    public static void mergePDF(String[] files, String outputPath, String outputFileName)  throws Exception{
        String sep = File.separator;
        Document document = null;
        PdfCopy copy = null;
        PdfReader reader = null;
        try {
            //首先验证输出文件是否存在,若不存在创建文件
            String outFilePath=outputPath + sep +outputFileName;
            //先验证是否存在输出文件夹,不存在则创建
            File fileDir = new File(outputPath);
            if (!fileDir.exists()) {
                fileDir.mkdirs();
            }
            //验证 合并后的文件是否存在若 不存在则新建文件
            File outFile=new File(outFilePath);
            if(outFile.exists()){
                outFile.createNewFile();
            }
            document = new Document(new PdfReader(files[0]).getPageSize(1));
            copy = new PdfCopy(document, new FileOutputStream(outFilePath));
            document.open();
            //循环导入合并pdf文件
            for (int i = 0; i < files.length; i++) {
                reader = new PdfReader(files[i]);
                int numberOfPages = reader.getNumberOfPages();
                for (int j = 1; j <= numberOfPages; j++) {
                    document.newPage();
                    PdfImportedPage page = copy.getImportedPage(reader, j);
                    copy.addPage(page);
                    document.add(PdfFontUtils.getFont(1, "其他"));
                    copy.addDocument(new PdfReader("其他2222"));
                }
            }
        } catch (Exception e) {
            throw e;
        } finally {
            if (document != null)
                document.close();
            if (reader != null)
                reader.close();
            if (copy != null)
                copy.close();
        }
    }

    /***
     * 添加图片到PDF
     * @param pdfPath
     * @param signturePath
     * @param outPath
     * @param type
     */
    public static void addImage(String pdfPath,String signturePath,String outPath,Integer type){
        try {
            //pdf文件存放在E盘
            File file = new File(pdfPath);
            //合成后的文件
            FileOutputStream outputStream = new FileOutputStream(outPath);
            PdfReader reader = new PdfReader(new FileInputStream(file));
            PdfStamper stamper = new PdfStamper(reader, outputStream);
            //将签名图片放在pdf文件的第4页
            PdfContentByte over ;
            if (type != null && !type.equals(1)){
                over = stamper.getOverContent(1);
            }else {
                over = stamper.getOverContent(2);
            }
            //签名图片
            Image contractSealImg = Image.getInstance(signturePath);
            over.saveState();
            PdfGState pdfGState = new PdfGState();
            pdfGState.setFillOpacity(0.8F);  //给图片设置透明度
            over.setGState(pdfGState);
            if (type != null && type.equals(1)){
                contractSealImg.setAbsolutePosition(325,70);  //设置图片位置
            }else {
                contractSealImg.setAbsolutePosition(325,70);  //设置图片位置
            }
            contractSealImg.scaleAbsolute(180, 100);  //设置图片大小
            over.addImage(contractSealImg);  //将图片添加到pdf文件
            over.restoreState();
            stamper.setFormFlattening(true);
            stamper.close();
            reader.close();
            outputStream.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

PdfFontUtils类

package com.platform.common.utils;

import java.io.IOException;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;

public class PdfFontUtils {

    // 字体
    private static BaseFont baseFont = null;

    static{
        try {
            /**
             * 设置字体
             *
             * windows路径字体
             * FONT_TYPE=C:/Windows/fonts/simsun.ttc
             * linux路径字体 宋体 (如果没有这个字体文件,就将windows的字体传上去)
             * FONT_TYPE=/usr/share/fonts/win/simsun.ttc
             */
            //可以用配置文件读取
            //获取配置
            //PropertiesLoader pl = new PropertiesLoader("/config/config.properties");
            //拼接文件web访问路径
            //String FONT_TYPE = pl.getProperty("FONT_TYPE");
            //解决中文问题  幼圆
            baseFont = BaseFont.createFont("C:/Windows/fonts/simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 文档超级  排版
     * @param type 1-标题 2-标题一  3-标题二 4-标题三  5-正文  6-左对齐
     */
    public static Paragraph getFont(int type, String text){
        Font font = new Font(baseFont);
        if(1 == type){//1-标题
            font.setSize(16f);
            font.setStyle(Font.BOLD);
        } else if(2 == type){//2-标题一
            font.setSize(16f);
            font.setStyle(Font.BOLD);
        } else if(3 == type){//3-标题二
            font.setSize(14f);
            font.setStyle(Font.BOLD);
        } else if(4 == type){//4-标题三
            font.setSize(14f);
        } else if(5 == type){//5-正文
            font.setSize(10.5f);
        } else if(6 == type){//6-左对齐
            font.setSize(10.5f);
        } else {
            font.setSize(10.5f);//默认大小
            font.setStyle("text-shadow");
        }
        //注: 字体必须和 文字一起new
        Paragraph paragraph = new Paragraph(text, font);
        if(1 == type){
            paragraph.setAlignment(Paragraph.ALIGN_CENTER);//居中
            paragraph.setSpacingBefore(10f);//上间距
            paragraph.setSpacingAfter(10f);//下间距
        } else if(2 == type){//2-标题一
            paragraph.setAlignment(Element.ALIGN_JUSTIFIED); //默认
            paragraph.setSpacingBefore(2f);//上间距
            paragraph.setSpacingAfter(2f);//下间距
        } else if(3 == type){
            paragraph.setSpacingBefore(2f);//上间距
            paragraph.setSpacingAfter(1f);//下间距
        } else if(4 == type){//4-标题三
            //paragraph.setAlignment(Element.ALIGN_RIGHT);//右对齐
            paragraph.setSpacingBefore(2f);//上间距
            paragraph.setSpacingAfter(2f);//下间距
        } else if(5 == type){
            paragraph.setAlignment(Element.ALIGN_JUSTIFIED);
            paragraph.setFirstLineIndent(24);//首行缩进
            paragraph.setSpacingBefore(1f);//上间距
            paragraph.setSpacingAfter(1f);//下间距
            paragraph.setLeading(paragraph.getTotalLeading()+40);
        } else if(6 == type){//左对齐
            paragraph.setAlignment(Element.ALIGN_LEFT);
            paragraph.setSpacingBefore(1f);//上间距
            paragraph.setSpacingAfter(1f);//下间距
        } else if(7 == type){//左对齐
            paragraph.setAlignment(Element.ALIGN_RIGHT);
            paragraph.setFirstLineIndent(24);//首行缩进
            paragraph.setSpacingBefore(50);//上间距
            paragraph.setSpacingAfter(5);//下间距
            paragraph.setAlignment(21);
            paragraph.setLeading(50,0);
            paragraph.setMultipliedLeading(20);
        }else if(8 == type){//左对齐
            paragraph = new Paragraph(text.split("-")[0], font);
            paragraph.setAlignment(Element.ALIGN_RIGHT);
            paragraph.setFirstLineIndent(24);//首行缩进
            paragraph.setSpacingBefore(1f);//上间距
            paragraph.setSpacingAfter(1f);//下间距
            paragraph.setAlignment(2);
//            Chunk sigUnderline1 = new Chunk(text.split("-")[0]);
//            sigUnderline1.setUnderline(0f, 0f);
//            paragraph.add(sigUnderline1);
            Chunk sigUnderline = new Chunk(text.split("-")[1]);
            sigUnderline.setUnderline(0.1f, -2f);
            paragraph.add(sigUnderline);
            paragraph.setLeading(25,0);
        }else if(9 == type){//左对齐
            paragraph = new Paragraph(text.split("-")[0], font);
            paragraph.setAlignment(Element.ALIGN_RIGHT);
            paragraph.setFirstLineIndent(54);//首行缩进
            paragraph.setSpacingBefore(150);//上间距
            paragraph.setSpacingAfter(150);//下间距
            paragraph.setAlignment(32);
            paragraph.setLeading(paragraph.getTotalLeading()+40);
            Chunk sigUnderline = new Chunk(text.split("-")[1].split("#")[0]);
            sigUnderline.setUnderline(0.01f, -1f);
            paragraph.add(sigUnderline);
            paragraph.add(text.split("#")[1]);
            paragraph.setLeading(25,0);
        }else if(10 == type){//左对齐
            paragraph = new Paragraph(text.split("-")[0], font);
            paragraph.setAlignment(Element.ALIGN_RIGHT);
            paragraph.setFirstLineIndent(24);//首行缩进
            paragraph.setSpacingBefore(150);//上间距
            paragraph.setSpacingAfter(150);//下间距
            paragraph.setAlignment(22);
            Chunk sigUnderline = new Chunk(text.split("-")[1].split("#")[0]);
            sigUnderline.setUnderline(0.01f, -1f);
            paragraph.add(sigUnderline);
            paragraph.add(text.split("#")[1].split("-")[0]);
            if (text.split("#")[1].split("-").length>1){
                sigUnderline = new Chunk(text.split("#")[1].split("-")[1]);
                sigUnderline.setUnderline(0.01f, -1f);
                paragraph.add(sigUnderline);
            }
            paragraph.add(text.split("#")[2]);
            paragraph.setLeading(25,0);
        }
        //paragraph.setIndentationLeft(50);//整体缩进左边
        //paragraph.setFirstLineIndent(40);//首行缩进
        return paragraph;
    }
    public static PdfPTable getTable(String text){
        char[] chars = text.toCharArray();
        PdfPTable pdfPTable = new PdfPTable(chars.length);
        for (int i = 0; i < chars.length; i++) {
            PdfPCell cell = new PdfPCell(getFont(7,chars[i]+""));
            pdfPTable.addCell(cell);
        }
        return pdfPTable;
    }
    public static PdfPTable getTableText(String text){
        PdfPTable pdfPTable = new PdfPTable(1);
        PdfPCell cell = new PdfPCell(getFont(7,text));
        pdfPTable.addCell(cell);
        return pdfPTable;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值