PDF文檔生成(代碼組合)

一、 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>();
    //當(dāng)前時(shí)間
    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);
    //生成唯一標(biāo)識(shí)
    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));
        //設(shè)置字體
        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("合同明細(xì)", FontChinese24));
        cell11.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell11.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell11.setBorder(0);
        //設(shè)置每列寬度比例
        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("乙方:" + "個(gè)人", FontChinese12));
        PdfPCell cell23 = new PdfPCell(new Paragraph("法定代表人:" + "鴻濤", FontChinese12));
        PdfPCell cell24 = new PdfPCell(new Paragraph("姓名:" + "林德曼", FontChinese12));
        PdfPCell cell25 = new PdfPCell(new Paragraph("身份證號(hào):" + "110221200004010033", FontChinese12));
        PdfPCell cell26 = new PdfPCell(new Paragraph("身份證號(hào):" + "110221200004010092", FontChinese12));

        //設(shè)置邊框
        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("第二條 乙方在本合同授權(quán)范圍通過后,甲方頒發(fā)的執(zhí)業(yè)證書后開展業(yè)務(wù)。", FontChinese12));
        PdfPCell cell3004 = new PdfPCell(new Paragraph("第三條 甲方權(quán)利 ", FontChinese12));
        PdfPCell cell3005 = new PdfPCell(new Paragraph("  1.根據(jù)國(guó)家法律法規(guī)行政規(guī)章及公司規(guī)定,制定管理制度,并據(jù)此對(duì)乙方的行為進(jìn)行管理;  ", FontChinese11Normal));
        PdfPCell cell3006 = new PdfPCell(new Paragraph("  2.督查乙方代理業(yè)務(wù),確保符合國(guó)家相關(guān)規(guī)定;", FontChinese11Normal));
        PdfPCell cell3007 = new PdfPCell(new Paragraph("  3.按稅務(wù)機(jī)關(guān)規(guī)定代扣乙方應(yīng)繳納的稅款;", FontChinese11Normal));
        PdfPCell cell3008 = new PdfPCell(new Paragraph("第四條 乙方權(quán)利義務(wù)", FontChinese12));
        PdfPCell cell3009 = new PdfPCell(new Paragraph("  (一)乙方權(quán)利", FontChinese11));
        PdfPCell cell3010 = new PdfPCell(new Paragraph("  1.在本合同授權(quán)范圍內(nèi)開展代理業(yè)務(wù),并獲得代理費(fèi);", FontChinese11Normal));
        PdfPCell cell3011 = new PdfPCell(new Paragraph("  2.獲得甲方相關(guān)培訓(xùn)資源;", FontChinese11Normal));
        PdfPCell cell3012 = new PdfPCell(new Paragraph("  3.通過資質(zhì)考核后,取得執(zhí)業(yè)證、業(yè)務(wù)支持和宣傳材料等;", FontChinese11Normal));
        PdfPCell cell3013 = new PdfPCell(new Paragraph("  (二)乙方的義務(wù):", FontChinese11));
        PdfPCell cell3014 = new PdfPCell(new Paragraph("  1.參加甲方組織的培訓(xùn),滿足培訓(xùn)課程要求并通過資格考試;", FontChinese11Normal));
        PdfPCell cell3015 = new PdfPCell(new Paragraph("  2.承擔(dān)與代理活動(dòng)相關(guān)的一切費(fèi)用;", FontChinese11Normal));
        PdfPCell cell3016 = new PdfPCell(new Paragraph("  3.接受甲方檢查、監(jiān)督和指導(dǎo),并按甲方要求進(jìn)行整改;", FontChinese11Normal));
        PdfPCell cell3017 = new PdfPCell(new Paragraph("  4.遵守國(guó)家法律、法規(guī)、行政規(guī)章和甲方的管理制度,遵守職業(yè)操守,維護(hù)甲方及投保人、被保險(xiǎn)人和受益人的合法利益,維護(hù)甲方的社會(huì)形象;", FontChinese11Normal));
        PdfPCell cell3018 = new PdfPCell(new Paragraph("第八條 合同的變更、解除", FontChinese12));
        PdfPCell cell3019 = new PdfPCell(new Paragraph("  (一)在本合同有效期內(nèi),根據(jù)國(guó)家有關(guān)法律、法規(guī)和行政規(guī)章,甲乙雙方協(xié)商一致,可依法對(duì)合同內(nèi)容進(jìn)行變更,并訂立補(bǔ)充協(xié)議予以約定。", FontChinese11));
        PdfPCell cell3020 = new PdfPCell(new Paragraph("  (二)合同有效期內(nèi)乙方自行通過甲方系統(tǒng),通過注銷專用 ID解除本合同,注銷完成本合同解除。", FontChinese11));
        PdfPCell cell3021 = new PdfPCell(new Paragraph("  (三)乙方有下列情況之一的,甲方有權(quán)單方解除合同:", FontChinese11));
        PdfPCell cell3022 = new PdfPCell(new Paragraph("  1.違反國(guó)家法律法規(guī)行政規(guī)章或違反本合同第七條第三款規(guī)定;", FontChinese11Normal));
        PdfPCell cell3023 = new PdfPCell(new Paragraph("  2.違反本合同第四條第(二)款,給甲方造成損失的。", FontChinese11Normal));
        PdfPCell cell3024 = new PdfPCell(new Paragraph("  3.本合同期滿,乙方無意續(xù)約的;", FontChinese11Normal));
        PdfPCell cell3025 = new PdfPCell(new Paragraph("  4.乙方喪失民事行為能力且無監(jiān)護(hù)人或監(jiān)護(hù)人拒絕接受本合同的;", FontChinese11Normal));
        PdfPCell cell3026 = new PdfPCell(new Paragraph("  (四)本合同終止或解除后,乙方不得再以甲方的名義開展業(yè)務(wù)。", FontChinese11));
        PdfPCell cell3027 = new PdfPCell(new Paragraph("第九條 違約責(zé)任", FontChinese12));
        PdfPCell cell3028 = new PdfPCell(new Paragraph("  (一)乙方違反本合同約定,未形成損失的,甲方有權(quán)根據(jù)甲方管理規(guī)定對(duì)乙方做出相應(yīng)處罰;造成甲方損失的,除根據(jù)甲方管理規(guī)定處罰外,乙方仍須按照損失金額支付賠償金。", FontChinese11));
        PdfPCell cell3029 = new PdfPCell(new Paragraph("第十條 保證條款", FontChinese12));
        PdfPCell cell3030 = new PdfPCell(new Paragraph("  (一)乙方所屬營(yíng)業(yè)部或分公司為乙方保證人。", FontChinese11));
        PdfPCell cell3031 = new PdfPCell(new Paragraph("  (二)保證人同意對(duì)乙方履行本合同應(yīng)向甲方承擔(dān)的債務(wù),承擔(dān)連帶責(zé)任。", FontChinese11));
        PdfPCell cell3032 = new PdfPCell(new Paragraph("第十一條 爭(zhēng)議處理 甲乙雙方就本合同發(fā)生爭(zhēng)議時(shí),可協(xié)商解決。協(xié)商不成,可依法仲裁或提起訴訟。", FontChinese12));
        PdfPCell cell3033 = new PdfPCell(new Paragraph("第十二條 附則 本合同一式二份,甲乙雙方各執(zhí)一份。", 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.乙方身份證復(fù)印件", 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("公司負(fù)責(zé)人簽字:", 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;
    }
  }

二、生成不同的文件名編號(hào)

/**
 * 得到32位UUID
 * @return
 */
public static String getUUID32(){
  return UUID.randomUUID().toString().replace("-", "").toLowerCase();
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容