一、 PDF生成主体代码
/**
* @param createFilePath 生成文件路径
* @author : HongTao
* @version : v1.0
* @description TODO PDF生成
* @date : 2021/7/8 23:53
*/
public Map<String, String> creatPDF(String createFilePath) {
//返回值定义
Map<String, String> fileMap = new HashMap<String, String>();
//当前时间
Calendar cal = Calendar.getInstance();
int file_year = cal.get(Calendar.YEAR);
int file_month = cal.get(Calendar.MONTH) + 1;
int file_date = cal.get(Calendar.DATE);
//生成唯一标识
String uuid = FileOperation.getUUID32();
//文件名称
String FileName = uuid;
//pdf文件路径
String PDFFilePath = createFilePath + "\\" + FileName + ".pdf";
try {
Document document = new Document(PageSize.A4);
PdfWriter.getInstance(document, new FileOutputStream(PDFFilePath));
//设置字体
BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.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 FontChinese13 = new com.itextpdf.text.Font(bfChinese, 13, 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.NORMAL);
com.itextpdf.text.Font FontChinese11Normal = new com.itextpdf.text.Font(bfChinese, 10, com.itextpdf.text.Font.NORMAL);
//打开新文档
document.open();
//table1
PdfPTable table1 = new PdfPTable(1);
PdfPCell cell11 = new PdfPCell(new Paragraph("合同明细", FontChinese24));
cell11.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell11.setHorizontalAlignment(Element.ALIGN_CENTER);
cell11.setBorder(0);
//设置每列宽度比例
int width11[] = {100};
table1.setWidths(width11);
table1.getDefaultCell().setBorder(0);
table1.addCell(cell11);
document.add(table1);
//加入空行
Paragraph blankRow1 = new Paragraph(18f, " ", FontChinese18);
document.add(blankRow1);
//table2
PdfPTable table2 = new PdfPTable(2);
int width21[] = {55, 45};
table2.setWidths(width21);
PdfPCell cell21 = new PdfPCell(new Paragraph("甲方:" + "非途", FontChinese12));
PdfPCell cell22 = new PdfPCell(new Paragraph("乙方:" + "个人", FontChinese12));
PdfPCell cell23 = new PdfPCell(new Paragraph("法定代表人:" + "鸿涛", FontChinese12));
PdfPCell cell24 = new PdfPCell(new Paragraph("姓名:" + "林德曼", FontChinese12));
PdfPCell cell25 = new PdfPCell(new Paragraph("身份证号:" + "110221200004010033", FontChinese12));
PdfPCell cell26 = new PdfPCell(new Paragraph("身份证号:" + "110221200004010092", FontChinese12));
//设置边框
cell21.setBorder(0);
cell22.setBorder(0);
cell23.setBorder(0);
cell24.setBorder(0);
cell25.setBorder(0);
cell26.setBorder(0);
table2.addCell(cell21);
table2.addCell(cell22);
table2.addCell(cell23);
table2.addCell(cell24);
table2.addCell(cell25);
table2.addCell(cell26);
document.add(table2);
//加入空行
Paragraph blankRow2 = new Paragraph(18f, " ", FontChinese18);
document.add(blankRow2);
//table3
PdfPTable table3 = new PdfPTable(1);
int width3[] = {90};
table3.setWidths(width3);
PdfPCell cell3001 = new PdfPCell(new Paragraph(" ", FontChinese18));
PdfPCell cell3002 = new PdfPCell(new Paragraph("第一条 本合同自甲乙双方及保证人签字或盖章之日起生效。", FontChinese12));
PdfPCell cell3003 = new PdfPCell(new Paragraph("第二条 乙方在本合同授权范围通过后,甲方颁发的执业证书后开展业务。", FontChinese12));
PdfPCell cell3004 = new PdfPCell(new Paragraph("第三条 甲方权利 ", FontChinese12));
PdfPCell cell3005 = new PdfPCell(new Paragraph(" 1.根据国家法律法规行政规章及公司规定,制定管理制度,并据此对乙方的行为进行管理; ", FontChinese11Normal));
PdfPCell cell3006 = new PdfPCell(new Paragraph(" 2.督查乙方代理业务,确保符合国家相关规定;", FontChinese11Normal));
PdfPCell cell3007 = new PdfPCell(new Paragraph(" 3.按税务机关规定代扣乙方应缴纳的税款;", FontChinese11Normal));
PdfPCell cell3008 = new PdfPCell(new Paragraph("第四条 乙方权利义务", FontChinese12));
PdfPCell cell3009 = new PdfPCell(new Paragraph(" (一)乙方权利", FontChinese11));
PdfPCell cell3010 = new PdfPCell(new Paragraph(" 1.在本合同授权范围内开展代理业务,并获得代理费;", FontChinese11Normal));
PdfPCell cell3011 = new PdfPCell(new Paragraph(" 2.获得甲方相关培训资源;", FontChinese11Normal));
PdfPCell cell3012 = new PdfPCell(new Paragraph(" 3.通过资质考核后,取得执业证、业务支持和宣传材料等;", FontChinese11Normal));
PdfPCell cell3013 = new PdfPCell(new Paragraph(" (二)乙方的义务:", FontChinese11));
PdfPCell cell3014 = new PdfPCell(new Paragraph(" 1.参加甲方组织的培训,满足培训课程要求并通过资格考试;", FontChinese11Normal));
PdfPCell cell3015 = new PdfPCell(new Paragraph(" 2.承担与代理活动相关的一切费用;", FontChinese11Normal));
PdfPCell cell3016 = new PdfPCell(new Paragraph(" 3.接受甲方检查、监督和指导,并按甲方要求进行整改;", FontChinese11Normal));
PdfPCell cell3017 = new PdfPCell(new Paragraph(" 4.遵守国家法律、法规、行政规章和甲方的管理制度,遵守职业操守,维护甲方及投保人、被保险人和受益人的合法利益,维护甲方的社会形象;", FontChinese11Normal));
PdfPCell cell3018 = new PdfPCell(new Paragraph("第八条 合同的变更、解除", FontChinese12));
PdfPCell cell3019 = new PdfPCell(new Paragraph(" (一)在本合同有效期内,根据国家有关法律、法规和行政规章,甲乙双方协商一致,可依法对合同内容进行变更,并订立补充协议予以约定。", FontChinese11));
PdfPCell cell3020 = new PdfPCell(new Paragraph(" (二)合同有效期内乙方自行通过甲方系统,通过注销专用 ID解除本合同,注销完成本合同解除。", FontChinese11));
PdfPCell cell3021 = new PdfPCell(new Paragraph(" (三)乙方有下列情况之一的,甲方有权单方解除合同:", FontChinese11));
PdfPCell cell3022 = new PdfPCell(new Paragraph(" 1.违反国家法律法规行政规章或违反本合同第七条第三款规定;", FontChinese11Normal));
PdfPCell cell3023 = new PdfPCell(new Paragraph(" 2.违反本合同第四条第(二)款,给甲方造成损失的。", FontChinese11Normal));
PdfPCell cell3024 = new PdfPCell(new Paragraph(" 3.本合同期满,乙方无意续约的;", FontChinese11Normal));
PdfPCell cell3025 = new PdfPCell(new Paragraph(" 4.乙方丧失民事行为能力且无监护人或监护人拒绝接受本合同的;", FontChinese11Normal));
PdfPCell cell3026 = new PdfPCell(new Paragraph(" (四)本合同终止或解除后,乙方不得再以甲方的名义开展业务。", FontChinese11));
PdfPCell cell3027 = new PdfPCell(new Paragraph("第九条 违约责任", FontChinese12));
PdfPCell cell3028 = new PdfPCell(new Paragraph(" (一)乙方违反本合同约定,未形成损失的,甲方有权根据甲方管理规定对乙方做出相应处罚;造成甲方损失的,除根据甲方管理规定处罚外,乙方仍须按照损失金额支付赔偿金。", FontChinese11));
PdfPCell cell3029 = new PdfPCell(new Paragraph("第十条 保证条款", FontChinese12));
PdfPCell cell3030 = new PdfPCell(new Paragraph(" (一)乙方所属营业部或分公司为乙方保证人。", FontChinese11));
PdfPCell cell3031 = new PdfPCell(new Paragraph(" (二)保证人同意对乙方履行本合同应向甲方承担的债务,承担连带责任。", FontChinese11));
PdfPCell cell3032 = new PdfPCell(new Paragraph("第十一条 争议处理 甲乙双方就本合同发生争议时,可协商解决。协商不成,可依法仲裁或提起诉讼。", FontChinese12));
PdfPCell cell3033 = new PdfPCell(new Paragraph("第十二条 附则 本合同一式二份,甲乙双方各执一份。", FontChinese12));
cell3001.setBorder(0);
cell3002.setBorder(0);
cell3003.setBorder(0);
cell3004.setBorder(0);
cell3005.setBorder(0);
cell3006.setBorder(0);
cell3007.setBorder(0);
cell3008.setBorder(0);
cell3009.setBorder(0);
cell3010.setBorder(0);
cell3011.setBorder(0);
cell3012.setBorder(0);
cell3013.setBorder(0);
cell3014.setBorder(0);
cell3015.setBorder(0);
cell3016.setBorder(0);
cell3017.setBorder(0);
cell3018.setBorder(0);
cell3019.setBorder(0);
cell3020.setBorder(0);
cell3021.setBorder(0);
cell3022.setBorder(0);
cell3023.setBorder(0);
cell3024.setBorder(0);
cell3025.setBorder(0);
cell3026.setBorder(0);
cell3027.setBorder(0);
cell3028.setBorder(0);
cell3029.setBorder(0);
cell3030.setBorder(0);
cell3031.setBorder(0);
cell3032.setBorder(0);
cell3033.setBorder(0);
table3.addCell(cell3001);
table3.addCell(cell3002);
table3.addCell(cell3003);
table3.addCell(cell3004);
table3.addCell(cell3005);
table3.addCell(cell3006);
table3.addCell(cell3007);
table3.addCell(cell3008);
table3.addCell(cell3009);
table3.addCell(cell3010);
table3.addCell(cell3011);
table3.addCell(cell3012);
table3.addCell(cell3013);
table3.addCell(cell3014);
table3.addCell(cell3015);
table3.addCell(cell3016);
table3.addCell(cell3017);
table3.addCell(cell3018);
table3.addCell(cell3019);
table3.addCell(cell3020);
table3.addCell(cell3021);
table3.addCell(cell3022);
table3.addCell(cell3023);
table3.addCell(cell3024);
table3.addCell(cell3025);
table3.addCell(cell3026);
table3.addCell(cell3027);
table3.addCell(cell3028);
table3.addCell(cell3029);
table3.addCell(cell3030);
table3.addCell(cell3031);
table3.addCell(cell3032);
table3.addCell(cell3033);
document.add(table3);
//加入空行
Paragraph blankRow31 = new Paragraph(18f, " ", FontChinese11);
Paragraph blankRow32 = new Paragraph(18f, " ", FontChinese11);
Paragraph blankRow33 = new Paragraph(18f, " ", FontChinese11);
Paragraph blankRow34 = new Paragraph(18f, " ", FontChinese11);
Paragraph blankRow35 = new Paragraph(18f, " ", FontChinese11);
Paragraph blankRow36 = new Paragraph(18f, " ", FontChinese11);
Paragraph blankRow37 = new Paragraph(18f, " ", FontChinese11);
document.add(blankRow31);
document.add(blankRow32);
document.add(blankRow33);
document.add(blankRow34);
document.add(blankRow35);
document.add(blankRow36);
document.add(blankRow37);
//table4
PdfPTable table4 = new PdfPTable(2);
int width4[] = {65, 35};
table4.setWidths(width4);
PdfPCell cell41 = new PdfPCell(new Paragraph("附件:1.乙方身份证复印件", FontChinese12));
PdfPCell cell42 = new PdfPCell(new Paragraph(" ", FontChinese12));
PdfPCell cell43 = new PdfPCell(new Paragraph("甲方盖章: ", FontChinese12));
PdfPCell cell44 = new PdfPCell(new Paragraph("乙方签字: ", FontChinese12));
cell41.setBorder(0);
cell42.setBorder(0);
cell43.setBorder(0);
cell44.setBorder(0);
table4.addCell(cell41);
table4.addCell(cell42);
table4.addCell(cell43);
table4.addCell(cell44);
document.add(table4);
//加入空行
Paragraph blankRow41 = new Paragraph(18f, " ", FontChinese11);
Paragraph blankRow42 = new Paragraph(18f, " ", FontChinese11);
Paragraph blankRow43 = new Paragraph(18f, " ", FontChinese11);
Paragraph blankRow44 = new Paragraph(18f, " ", FontChinese11);
document.add(blankRow41);
document.add(blankRow42);
document.add(blankRow43);
document.add(blankRow44);
//table5
PdfPTable table5 = new PdfPTable(1);
int width5[] = {100};
table5.setWidths(width5);
PdfPCell cell51 = new PdfPCell(new Paragraph("公司负责人签字:", FontChinese12));
cell51.setBorder(0);
table5.addCell(cell51);
document.add(table5);
//加入空行
Paragraph blankRow51 = new Paragraph(18f, " ", FontChinese18);
Paragraph blankRow52 = new Paragraph(18f, " ", FontChinese18);
Paragraph blankRow53 = new Paragraph(18f, " ", FontChinese18);
document.add(blankRow51);
document.add(blankRow52);
document.add(blankRow53);
//table6
PdfPTable table6 = new PdfPTable(1);
int width6[] = {100};
table6.setWidths(width6);
PdfPCell cell61 = new PdfPCell(new Paragraph("签订日期:" + file_year + " 年 " + file_month + " 月 " + file_date + " 日 ", FontChinese12));
cell61.setBorder(0);
cell61.setHorizontalAlignment(Element.ALIGN_RIGHT);
table6.addCell(cell61);
document.add(table6);
//加入空行
Paragraph blankRow61 = new Paragraph(18f, " ", FontChinese18);
document.add(blankRow61);
document.close();
//封装返回值
fileMap.put("PDFFilePath", PDFFilePath);
fileMap.put("FileName", FileName);
return fileMap;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
二、生成不同的文件名编号
/**
* 得到32位UUID
* @return
*/
public static String getUUID32(){
return UUID.randomUUID().toString().replace("-", "").toLowerCase();
}