iis服务器助手广告
返回顶部
首页 > 资讯 > 后端开发 > JAVA >Java导出PDF(itextpdf)-通俗易懂
  • 597
分享到

Java导出PDF(itextpdf)-通俗易懂

javapdf开发语言 2023-08-17 13:08:35 597人浏览 泡泡鱼
摘要

文章目录 前言一、itextpdf是什么?二、快速开始1.废话不多说,效果先上一波2.依赖引入3.模版加载3.1模版加载工具类PDF 4.PDF操作工具类方法5.完整代码5.1前端代码-Vue5.2控制层代码5.3业务层代码


前言

在java开发的过程中会遇到太多太多文档pdf导出,excle导出等业务场景,时隔三个月或半年来一次每一次遇到这样的业务场景对我都是非常痛苦的过程,本文旨在记录工具类使用方法和技术分享。


一、itextpdf是什么?

在这里插入图片描述
itextpdf是一个开源的Java库,用于创建和操作PDF文档。使用itextpdf,您可以创建新的PDF文档或修改现有文档,添加文本和图像,创建表单等等。该库提供了广泛的功能,并经常用于企业级应用程序中根据用户输入动态生成复杂的PDF文档。它具有各种许可证选项,具体取决于您的用例,范围从AGPL到商业许可证。

itextpdf库提供了大量的方法和功能,以下是一些常用的方法:

  1. 创建PDF文档和页面:使用PdfWriter和Document对象可以创建一个新的PDF文档并添加页面。
  2. 添加内容到PDF文档:使用Paragraph、Phrase和Chunk对象可以向PDF文档中添加文本内容。同时,也可以添加图片、表格等其它类型的内容。
  3. 创建表格:使用PdfPTable对象可以创建表格,并向其中添加行和单元格。
  4. 设置页眉页脚:使用HeaderFooter对象可以设置PDF文档的页眉和页脚,以及页码等信息。
  5. 创建书签:使用PdfOutline对象可以创建PDF文档的书签,方便用户在浏览器中导航。
  6. 导出PDF文档:使用PdfWriter对象可以将PDF文档导出为文件或流。

二、快速开始

1.废话不多说,效果先上一波

下列数据均为测试虚拟数据,不具有任何真实性

在这里插入图片描述

2.依赖引入

                    com.itextpdf            itextpdf            5.5.13                            com.itextpdf            itext-asian            5.2.0                            com.itextpdf.tool            xmlworker            5.5.11        

3.模版加载

在这里插入图片描述
rescources-templates-创建一个pdf模版,如果没有定制内容,直接为空白即可


3.1模版加载工具类PDF

public static BaseFont bfChinese;        public static Font titlefont;    public static Font titleSecondFont;    public static Font headfont;    public static Font secondHeadfont;    public static Font textfont;    public static Font secondTitleFont;    public static Font itemFont;    public static Font paragraphFont;    public static Font tableCellFont;    public static Font accessoryFont;        public static String shortSpacing = "      ";    public static String mediumSpacing = "            ";    public static String longSpacing = "                        ";    public static String maxLongSpacing = "        ";    public static String shortSlash = "   /   ";    public static String mediumSlash = "      /      ";    public static String longSlash = "            /            ";    public static String maxLongSlash = "                  /                  ";        public static String ht;    public static String scan;    public static String htScan;    public static String over;    public static String htSign;    public static String signatureHt;    public static File htFile;    public static File overFile;    public static File htSignFile;        static {        try {            // 不同字体(这里定义为同一种字体:包含不同字号、不同style)            bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);            titlefont = new Font(bfChinese, 16, Font.NORMAL);            titleSecondFont = new Font(bfChinese, 14, Font.NORMAL);            headfont = new Font(bfChinese, 14, Font.BOLD);            secondHeadfont = new Font(bfChinese, 15, Font.BOLD);            textfont = new Font(bfChinese, 16, Font.NORMAL);            secondTitleFont = new Font(bfChinese,18,Font.BOLD);            itemFont = new Font(bfChinese,16,Font.BOLD);            paragraphFont = new Font(bfChinese,11,Font.NORMAL);            accessoryFont = new Font(bfChinese,13,Font.NORMAL);            tableCellFont = new Font(bfChinese,9,Font.NORMAL);                        String osName = System.getProperty("os.name").toLowerCase();            if (osName.contains("win")||osName.contains("Mac")) {                ht = getResourceBasePath() + "\\templates\\ht.pdf";                htFile = new File(ht);            } else { //todo: linux或unbunt                ht = "/mnt/jar/spring-cloud/ht.pdf";                htFile = new File(ht);            }        } catch (Exception e) {            e.printStackTrace();        }    }        public static String getResourceBasePath() {        // 获取根目录        File path = null;        try {            path = new File(ResourceUtils.getURL("classpath:").getPath());        } catch (FileNotFoundException e) {        }        if (path == null || !path.exists()) {            path = new File("");        }//todo: 就获取第三步中模版所在的位置,请看上列中 “3.模版加载里的内容”        File file = new File(path.getAbsolutePath() + "\\templates");        if (!file.exists()){            return path.getAbsolutePath().replace("xx","xx");        }        return path.getAbsolutePath();    }

4.PDF操作工具类方法

public static PdfPTable createTable() throws DocumentException {        PdfPTable table = new PdfPTable(2);        // 设置总宽度        table.setTotalWidth(490);        table.setLockedWidth(true);        // 设置每一列宽度        table.setTotalWidth(new float[] {240,240});        table.setLockedWidth(true);        return table;    }    public PdfPTable createTable(int numColumns) {        PdfPTable table = new PdfPTable(numColumns);        // 设置总宽度        table.setTotalWidth(490);        table.setLockedWidth(true);        return table;    }    public static PdfPTable createTitleTable(int numColumns,Font font, String ... titles) {        PdfPTable table = new PdfPTable(numColumns);        // 设置总宽度        table.setTotalWidth(490);        table.setLockedWidth(true);        for (String title : titles) {            addTitleCell(table,title,1,font);        }        return table;    }        public static void addTitle(Document document, String text, int alignment) throws DocumentException {        Paragraph paragraph = new Paragraph(text, new Font(bfChinese, 16, Font.NORMAL));        paragraph.setAlignment(alignment); //设置文字居中 0靠左   1,居中     2,靠右        paragraph.setIndentationLeft(0); //设置左缩进        paragraph.setIndentationRight(0); //设置右缩进        paragraph.setFirstLineIndent(0); //设置首行缩进        paragraph.setLeading(10f); //行间距        paragraph.setSpacingBefore(10f); //设置段落上空白        paragraph.setSpacingAfter(10f); //设置段落下空白        document.add(paragraph);    }        public static void addFirstTitle(Document document,String text,int alignment) throws DocumentException {        Paragraph paragraph = new Paragraph(text, new Font(bfChinese, 14, Font.NORMAL));        paragraph.setAlignment(alignment); //设置文字居中 0靠左   1,居中     2,靠右        paragraph.setIndentationLeft(0); //设置左缩进        paragraph.setIndentationRight(0); //设置右缩进        paragraph.setFirstLineIndent(0); //设置首行缩进        paragraph.setLeading(7f); //行间距        paragraph.setSpacingBefore(20f); //设置段落上空白        paragraph.setSpacingAfter(7f); //设置段落下空白        document.add(paragraph);    }        public static void addSecondTitle(Document document,String text,Font font,int alignment) throws DocumentException {        Paragraph paragraph = new Paragraph(text, new Font(bfChinese, 12, Font.NORMAL));        paragraph.setAlignment(alignment); //设置文字居中 0靠左   1,居中     2,靠右        paragraph.setIndentationLeft(9); //设置左缩进        paragraph.setIndentationRight(0); //设置右缩进        paragraph.setFirstLineIndent(9); //设置首行缩进        paragraph.setLeading(15f); //行间距        paragraph.setSpacingBefore(10f); //设置段落上空白        paragraph.setSpacingAfter(5f); //设置段落下空白        document.add(paragraph);    }        public static void addLine(Document document) throws DocumentException {        LineSeparator ls = new LineSeparator();        ls.setLineWidth(1);        ls.setLineColor(new BaseColor(179,180,164));        Chunk chunk = new Chunk(ls);        document.add(chunk);    }    public static void addNoBorderCell(PdfPTable table,String text) {        PdfPCell cell = new PdfPCell(new Paragraph(text,paragraphFont));        cell.setBorderWidth(0);        cell.setLeading(24,0); //行间距        table.addCell(cell);    }    public static void addTitleCell(PdfPTable table,String text,float borderWidth,Font font) {        Paragraph paragraph = new Paragraph(text, font);        paragraph.setAlignment(1);        paragraph.setLeading(5f);        PdfPCell cell = new PdfPCell(paragraph);        cell.setBorderWidth(borderWidth);        cell.setBackgroundColor(new BaseColor(245,247,251));        cell.setHorizontalAlignment(1);        cell.setVerticalAlignment(1);        table.addCell(cell);    }    public static void addCell(PdfPTable table,String text,float borderWidth,Font font) {        if (StringUtils.isBlank(text)) {            text = " ";        }        Paragraph paragraph = new Paragraph(text, font);        paragraph.setAlignment(1);        PdfPCell cell = new PdfPCell(paragraph);        //水平居中和垂直居中        cell.setHorizontalAlignment(Element.ALIGN_CENTER);        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);        cell.setUseAscender(true);        table.addCell(cell);    }    public static void addCell(PdfPTable table, BigDecimal amount, float borderWidth, Font font) {        Paragraph paragraph = null;        if (Objects.isNull(amount)) {            paragraph = new Paragraph(" ",font);        } else {            paragraph = new Paragraph(amount.toString(),font);        }        paragraph.setAlignment(1);        PdfPCell cell = new PdfPCell(paragraph);        cell.setBorderWidth(borderWidth);        //水平居中和垂直居中        cell.setHorizontalAlignment(Element.ALIGN_CENTER);        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);        cell.setUseAscender(true);        table.addCell(cell);    }        public void addSecondTitle(Document document,String text,int alignment,float spacingBefore,float spacingAfter) throws DocumentException {        Paragraph paragraph10 = new Paragraph(text, secondTitleFont);        paragraph10.setAlignment(alignment); //设置文字居中 0靠左   1,居中     2,靠右        paragraph10.setIndentationLeft(12); //设置左缩进        paragraph10.setIndentationRight(12); //设置右缩进        paragraph10.setFirstLineIndent(32); //设置首行缩进        paragraph10.setLeading(30f); //行间距        paragraph10.setSpacingBefore(1f); //设置段落上空白        paragraph10.setSpacingAfter(15f); //设置段落下空白        document.add(paragraph10);    }        public void addItemTitle(Document document,String text) throws DocumentException {        Paragraph paragraph = new Paragraph(text, itemFont);        paragraph.setAlignment(0); //设置文字居中 0靠左   1,居中     2,靠右        paragraph.setIndentationLeft(12); //设置左缩进        paragraph.setIndentationRight(12); //设置右缩进        paragraph.setFirstLineIndent(32); //设置首行缩进        paragraph.setLeading(30f); //行间距        paragraph.setSpacingBefore(1f); //设置段落上空白        paragraph.setSpacingAfter(1f); //设置段落下空白        document.add(paragraph);    }        public void addTextParagraph(Document document,String text) throws DocumentException {        Paragraph paragraph = new Paragraph(text,paragraphFont);        paragraph.setAlignment(0); //设置文字居中 0靠左   1,居中     2,靠右        paragraph.setIndentationLeft(12); //设置左缩进        paragraph.setIndentationRight(12); //设置右缩进        paragraph.setFirstLineIndent(32); //设置首行缩进        paragraph.setLeading(30f); //行间距        document.add(paragraph);    }        public Paragraph createTextParagraph() throws DocumentException {        Paragraph paragraph = new Paragraph("",paragraphFont);        paragraph.setAlignment(0); //设置文字居中 0靠左   1,居中     2,靠右        paragraph.setIndentationLeft(12); //设置左缩进        paragraph.setIndentationRight(12); //设置右缩进        paragraph.setFirstLineIndent(32); //设置首行缩进        paragraph.setLeading(30f); //行间距        return paragraph;    }        public Paragraph createTextParagraph(String text) throws DocumentException {        Paragraph paragraph = new Paragraph(text,paragraphFont);        paragraph.setAlignment(0); //设置文字居中 0靠左   1,居中     2,靠右        paragraph.setIndentationLeft(12); //设置左缩进        paragraph.setIndentationRight(12); //设置右缩进        paragraph.setFirstLineIndent(32); //设置首行缩进        paragraph.setLeading(30f); //行间距        return paragraph;    }        public Paragraph createTextParagraph(String text,int alignment) {        Paragraph paragraph = new Paragraph(text,paragraphFont);        paragraph.setAlignment(alignment); //设置文字居中 0靠左   1,居中     2,靠右        paragraph.setIndentationLeft(12); //设置左缩进        paragraph.setIndentationRight(12); //设置右缩进        paragraph.setFirstLineIndent(32); //设置首行缩进        paragraph.setLeading(30f); //行间距        return paragraph;    }    public void appendParagraph(Document document,Paragraph paragraph,String text) throws DocumentException {        paragraph.add(text);        document.add(paragraph);    }        public Paragraph addUnderLine(Paragraph paragraph,String text){        if (StringUtils.isBlank(text)){            text = shortSpacing;        } else {            text = StringUtils.join(" ",text," ");        }        Chunk sigUnderline = new Chunk(text);        sigUnderline.setUnderline(0.1f, -2f);        paragraph.add(sigUnderline);        return paragraph;    }        public void addUnderLine(Document document,Paragraph paragraph,String text) throws DocumentException {        if (StringUtils.isBlank(text)){            text = shortSpacing;        }        Chunk sigUnderline = new Chunk(StringUtils.join(" ",text," "));        sigUnderline.setUnderline(0.1f, -2f);        paragraph.add(sigUnderline);        document.add(paragraph);    }        public File mergePdf(String [] files,String savePath){        PdfReader pdfReader = null;        Document document = null;        try {            //创建一个与a.pdf相同纸张大小的document            pdfReader = new PdfReader(files[0]);            document = new Document(pdfReader.getPageSize(1));            PdfCopy copy = new PdfCopy(document, new FileOutputStream(savePath));            document.open();            for (int i = 0; i < files.length; i++) {                //一个一个的遍历现有的PDF                PdfReader reader = new PdfReader(files[i]);                int n = reader.getNumberOfPages();//PDF文件总共页数                System.out.println("n:"+n);                for (int j = 1; j <= n; j++) {                    document.newPage();                    PdfImportedPage page = copy.getImportedPage(reader, j);                    copy.addPage(page);                }                reader.close();            }            document.close();            pdfReader.close();            return new File(savePath);        } catch (IOException e) {            e.printStackTrace();        } catch (DocumentException e) {            e.printStackTrace();        } finally {            document.close();            pdfReader.close();        }        return null;    }        public Paragraph addUnderLine(Boolean check,Paragraph paragraph,String text1,String text2){        Chunk sigUnderline = null;        if (check){            if (Objects.isNull(text1)){                text1 = shortSpacing;            } else {                text1 = StringUtils.join(" ",text1," ");            }            sigUnderline = new Chunk(text1);        } else {            if (Objects.isNull(text2)){                text2 = shortSpacing;            } else {                text2 = StringUtils.join(" ",text2," ");            }            sigUnderline = new Chunk(text2);        }        sigUnderline.setUnderline(0.1f, -2f);        paragraph.add(sigUnderline);        return paragraph;    }        public void addUnderLine(Document document,Boolean check,Paragraph paragraph,String text1,String text2) throws DocumentException {        Chunk sigUnderline = null;        if (check){            if (Objects.isNull(text1)){                text1 = shortSpacing;            } else {                text1 = StringUtils.join(" ",text1," ");            }            sigUnderline = new Chunk(text1);        } else {            if (Objects.isNull(text2)){                text2 = shortSpacing;            } else {                text2 = StringUtils.join(" ",text2," ");            }            sigUnderline = new Chunk(text2);        }        sigUnderline.setUnderline(0.1F, -2.0F);        paragraph.add(sigUnderline);        document.add(paragraph);    }        public Chunk createUnderLineChunk(String text) {        if (Objects.isNull(text)){            text = shortSpacing;        } else {            text = StringUtils.join(" ",text," ");        }        Chunk sigUnderline = new Chunk(text);        sigUnderline.setUnderline(0.1f, -2f);        return sigUnderline;    }    private Document createDocument() throws IOException {        // 1.新建document对象 建立一个Document对象        Document document = new Document(PageSize.A4);        //        PdfUtils.htFile.createNewFile();        // 3.打开文档        document.open();        document.addTitle("销售合同");// 标题        document.addAuthor("chuz");// 作者        document.addSubject("Subject@iText pdf sample");// 主题        document.addKeyWords("Keywords@iTextpdf");// 关键字        document.addCreator("chuz develop");// 创建者        return document;    }        public Chunk createUnderLineChunk(Boolean check,String text1,String text2) throws DocumentException {        Chunk sigUnderline = null;        if (check){            if (Objects.isNull(text1)){                text1 = shortSpacing;            } else {                text1 = StringUtils.join(" ",text1," ");            }            sigUnderline = new Chunk(text1);        } else {            if (Objects.isNull(text2)){                text2 = shortSpacing;            } else {                text2 = StringUtils.join(" ",text2," ");            }            sigUnderline = new Chunk(text2);        }        sigUnderline.setUnderline(0.1f, -2f);        return sigUnderline;    }        public Chunk createChunk(String text) throws DocumentException {        if (StringUtils.isBlank(text)){            text = shortSpacing;        }        text = StringUtils.join(" ",text," ");        return new Chunk(text);    }    //    public void addCheck(Document document,Boolean check,String text) throws Exception {//        Paragraph paragraph = createTextParagraph("");//        if (check){//            paragraph.add(new Chunk(checkPng,0,0,true));//        } else {//            paragraph.add(new Chunk(unCheckPng,0,0,true));//        }//        Chunk chunk = new Chunk(StringUtils.join(" ",text),textfont);//        paragraph.add(chunk);//        document.add(paragraph);//    }    //    public void addCheck(Paragraph paragraph,Boolean check,String text) throws Exception {//        if (check){//            paragraph.add(new Chunk(checkPng,0,0,true));//        } else {//            paragraph.add(new Chunk(unCheckPng,0,0,true));//        }//        Chunk chunk = new Chunk(StringUtils.join(" ",text),textfont);//        paragraph.add(chunk);//    }    //    public Paragraph addCheck(Boolean check,String text) throws Exception {//        Paragraph paragraph = createTextParagraph("");//        if (check){//            paragraph.add(new Chunk(checkPng,0,0,true));//        } else {//            paragraph.add(new Chunk(unCheckPng,0,0,true));//        }//        Chunk chunk = new Chunk(StringUtils.join(" ",text),textfont);//        paragraph.add(chunk);//        return paragraph;//    }    //    public Paragraph appendCheck(Paragraph paragraph,Boolean check,String text) throws Exception {//        if (check){//            paragraph.add(new Chunk(checkPng,0,0,true));//        } else {//            paragraph.add(new Chunk(unCheckPng,0,0,true));//        }//        Chunk chunk = new Chunk(StringUtils.join(" ",text),textfont);//        paragraph.add(chunk);//        return paragraph;//    }    public void packageDateParagraph(Document document, Paragraph paragraph, Date date) throws DocumentException {        if (Objects.nonNull(date)){            Calendar calendar = Calendar.getInstance();            calendar.setTime(date);            int year = calendar.get(Calendar.YEAR);            int month = calendar.get(Calendar.MONTH) + 1;            int day = calendar.get(Calendar.DAY_OF_MONTH);            addUnderLine(paragraph,StringUtils.join(year));            paragraph.add("年");            addUnderLine(paragraph,StringUtils.join(month));            paragraph.add("月");            addUnderLine(paragraph,StringUtils.join(day));            paragraph.add("日");        } else {            addUnderLine(paragraph,StringUtils.join(shortSlash));            paragraph.add("年");            addUnderLine(paragraph,StringUtils.join(shortSlash));            paragraph.add("月");            addUnderLine(paragraph,StringUtils.join(shortSlash));            paragraph.add("日");        }        document.add(paragraph);    }    public MultipartFile fileToMultipartFile(File file) {        FileItem fileItem = createFileItem(file);        MultipartFile multipartFile = new CommonsMultipartFile(fileItem);        return multipartFile;    }    public static FileItem createFileItem(File file) {        FileItemFactory factory = new DiskFileItemFactory(16, null);        FileItem item = factory.createItem("textField", "text/plain", true, file.getName());        int bytesRead = 0;        byte[] buffer = new byte[8192];        try {            FileInputStream fis = new FileInputStream(file);            OutputStream os = item.getOutputStream();            while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) {                os.write(buffer, 0, bytesRead);            }            os.close();            fis.close();        } catch (IOException e) {            e.printStackTrace();        }        return item;    }    public String precision(BigDecimal number){        if (Objects.nonNull(number)){            return number.setScale(0,BigDecimal.ROUND_DOWN).toString();        }        return null;    }

5.完整代码

5.1前端代码-Vue

downloadPdf(){      api.downloadPdf({        fileName: this.data.contractName+'.pdf',        params: {type:'0',contractId:this.data.id}      })    },

5.2控制层代码

@GetMapping(value = "create")    public void create(@ApiParam(value = "0:销售合同,1:第三方合同") @RequestParam(name = "type") String type,                       @RequestParam(name = "contractId") String contractId,                       httpservletResponse response) throws IOException {        File pdfFile = null;        if ("0".equals(type)) {                                  //到业务方法 xx代替,这里主要是根据条件查询从表信息            List 1 =getXXX();            //设计到业务方法 xx代替,这里主要是根据条件查询从表信息            List 1 =getXXX();            // 仪表经验计费条款            List 1 =getXXX();                        List 1 =getXXX();//导出方法            pdfFile = contractPdfService.createContractInfo(createContractInfo);        }         response.setContentType("application/pdf");        if (pdfFile.exists()) {            FileInputStream in = new FileInputStream(pdfFile);            OutputStream out = response.getOutputStream();            byte[] b = new byte[1024 * 5];            int n;            while ((n = in.read(b)) != -1) {                out.write(b, 0, n);            }            out.flush();            in.close();            out.close();        }    }

5.3业务层代码

public File createContractInfo(CreateContractInfo contractInfo){        try {            // 1.新建document对象 建立一个Document对象            Document document = new Document(PageSize.A4);            PdfUtils.htFile.createNewFile();            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(PdfUtils.htFile));            // 3.打开文档            document.open();            document.addTitle(contractInfo.getContractName());// 标题            document.addAuthor("chuz");// 作者            document.addSubject("Subject@iText pdf sample");// 主题            document.addKeywords("Keywords@iTextpdf");// 关键字            document.addCreator("chuz develop");// 创建者            // 4.向文档中添加内容            generatePDF(document,contractInfo);            // 5.关闭文档            document.close();            writer.close();            return PdfUtils.htFile;        } catch (Exception e) {            e.printStackTrace();        }        return PdfUtils.htFile;    }// 生成PDF文件    public void generatePDF(Document document,CreateContractInfo contractInfo) throws Exception {        //添加总标题,        PdfUtils.addTitle(document,contractInfo.getContractName(),1);        // 基本信息-一级标题        PdfUtils.addFirstTitle(document,"(一)基本信息",0);        PdfPTable table = PdfUtils.createTable();        PdfUtils.addNoBorderCell(table,StringUtils.join("合同编号:",contractInfo.getContractNo()));        PdfUtils.addNoBorderCell(table,StringUtils.join("出租方:",contractInfo.getFirstPartyName()));        PdfUtils.addNoBorderCell(table,StringUtils.join("出租方:",contractInfo.getFirstPartyName()));        PdfUtils.addNoBorderCell(table,StringUtils.join("租赁方:",contractInfo.getPartyBName()));        PdfUtils.addNoBorderCell(table,StringUtils.join("经办人:",contractInfo.getHandledByName()));        PdfUtils.addNoBorderCell(table,StringUtils.join("签订日期:", DateFormatUtil.dateToString(DateFormatUtil.yyyy_MM_dd,contractInfo.getSigningTime())));        PdfUtils.addNoBorderCell(table,StringUtils.join("业态:",contractInfo.getContractTypeName()));        PdfUtils.addNoBorderCell(table,StringUtils.join("意向来源:",dictTrans("customer_source",contractInfo.getContractSource())));        PdfUtils.addNoBorderCell(table,StringUtils.join("合同标签:",contractInfo.getContractLabel()));        PdfUtils.addNoBorderCell(table,StringUtils.join("租赁日期:",StringUtils.join(                DateFormatUtil.dateToString(DateFormatUtil.yyyy_MM_dd,contractInfo.getStartTime()),                "~",                DateFormatUtil.dateToString(DateFormatUtil.yyyy_MM_dd,contractInfo.getEndTime()))));        document.add(table);        PdfUtils.addLine(document);        // 合同条款-一级标题        PdfUtils.addFirstTitle(document,"(二)合同条款",0);        PdfUtils.addSecondTitle(document,"合同计费条款",PdfUtils.titleSecondFont,0);        PdfPTable itemTable = PdfUtils.createTitleTable(5,PdfUtils.tableCellFont,"资源","收费项","计费方式","计费周期","计算方式");        for (FreeContractClauseDto contractClauseDto : contractInfo.getContractClauseDtos()) {            PdfUtils.addCell(itemTable,contractClauseDto.getHouseName(),1,PdfUtils.tableCellFont);            PdfUtils.addCell(itemTable,contractClauseDto.getSetName(),1,PdfUtils.tableCellFont);            PdfUtils.addCell(itemTable,dictTrans("free_calculate_way",contractClauseDto.getCalculateWay()),1,PdfUtils.tableCellFont);            PdfUtils.addCell(itemTable,dictTrans("charging_cycle",contractClauseDto.getCharginGCycle()),1,PdfUtils.tableCellFont);            PdfUtils.addCell(itemTable,contractClauseDto.getFreeRemarks(),1,PdfUtils.tableCellFont);        }        document.add(itemTable);        PdfUtils.addSecondTitle(document,"仪表/经营计费条款",PdfUtils.titleSecondFont,0);        PdfPTable deviceTable = PdfUtils.createTitleTable(4,PdfUtils.tableCellFont,"资源","收费项","计费方式","仪表/录数项");        for (FreeContractClauseDto deviceContractClauseDto : contractInfo.getDeviceContractClauseDtos()) {            PdfUtils.addCell(deviceTable,deviceContractClauseDto.getHouseName(),1,PdfUtils.tableCellFont);            PdfUtils.addCell(deviceTable,deviceContractClauseDto.getSetName(),1,PdfUtils.tableCellFont);            PdfUtils.addCell(deviceTable,deviceContractClauseDto.getChargeStandard(),1,PdfUtils.tableCellFont);            PdfUtils.addCell(deviceTable,deviceContractClauseDto.getMeterNo(),1,PdfUtils.tableCellFont);        }        document.add(deviceTable);        PdfUtils.addSecondTitle(document,"其他费用添加",PdfUtils.titleSecondFont,0);        PdfPTable elseFreeTable = PdfUtils.createTitleTable(8,PdfUtils.tableCellFont,"资源","收费项","应收(元)","实收(元)","欠收(元)","应收日期","账期","备注");        for (FreeBillDetailVo elseFreeBillDetailVo : contractInfo.getElseFreeBillDetailVos()) {            PdfUtils.addCell(elseFreeTable,elseFreeBillDetailVo.getHouseName(),1,PdfUtils.tableCellFont);            PdfUtils.addCell(elseFreeTable,"收费项",1,PdfUtils.tableCellFont);            PdfUtils.addCell(elseFreeTable,elseFreeBillDetailVo.getOughtAmount(),1,PdfUtils.tableCellFont);            PdfUtils.addCell(elseFreeTable,elseFreeBillDetailVo.getPracticalAmount(),1,PdfUtils.tableCellFont);            PdfUtils.addCell(elseFreeTable,elseFreeBillDetailVo.getOweAmount(),1,PdfUtils.tableCellFont);            PdfUtils.addCell(elseFreeTable,DateFormatUtil.dateToString(DateFormatUtil.yyyy_MM_dd,elseFreeBillDetailVo.getOughtDate()),1,PdfUtils.tableCellFont);            PdfUtils.addCell(elseFreeTable,elseFreeBillDetailVo.getPeriod(),1,PdfUtils.tableCellFont);            PdfUtils.addCell(elseFreeTable,elseFreeBillDetailVo.getRemarks(),1,PdfUtils.tableCellFont);        }        document.add(elseFreeTable);        PdfUtils.addLine(document);        // 其它约定        PdfUtils.addFirstTitle(document,"(三)其它约定",0);        PdfPTable elseTable = PdfUtils.createTable();        PdfUtils.addNoBorderCell(elseTable,"设施/服务:设施/服务");        PdfUtils.addNoBorderCell(elseTable,"特殊约定:特殊约定");        PdfUtils.addNoBorderCell(elseTable,"违约说明:违约说明");        PdfUtils.addNoBorderCell(elseTable,"");        document.add(elseTable);        PdfUtils.addLine(document);        // 合同账单-自定义        Paragraph paragraph = new Paragraph("(四)合同账单", new Font(PdfUtils.bfChinese, 14, Font.NORMAL));        paragraph.setAlignment(0); //设置文字居中 0靠左   1,居中     2,靠右        paragraph.setIndentationLeft(0); //设置左缩进        paragraph.setIndentationRight(0); //设置右缩进        paragraph.setFirstLineIndent(0); //设置首行缩进        paragraph.setLeading(7f); //行间距        paragraph.setSpacingBefore(20f); //设置段落上空白        paragraph.setSpacingAfter(25f); //设置段落下空白        document.add(paragraph);        PdfPTable billTable = PdfUtils.createTitleTable(10,PdfUtils.tableCellFont,"费用项目","费用类型","费用标识","账期","起止时间","应收金额(元)","实收金额(元)","欠费金额(元)","缴费时间","状态");        List freeBillDetailVos = contractInfo.getFreeBillDetailVos();        for (FreeBillDetailVo freeBillDetailVo : freeBillDetailVos) {            PdfUtils.addCell(billTable,"租金",1,PdfUtils.tableCellFont);            PdfUtils.addCell(billTable,"租金",1,PdfUtils.tableCellFont);            PdfUtils.addCell(billTable,"周期性收费",1,PdfUtils.tableCellFont);            PdfUtils.addCell(billTable,freeBillDetailVo.getPeriod(),1,PdfUtils.tableCellFont);            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");            String dateTime=sdf.format(freeBillDetailVo.getBeginTime())+"~"+sdf.format(freeBillDetailVo.getEndTime());            PdfUtils.addCell(billTable,dateTime,1,PdfUtils.tableCellFont);            PdfUtils.addCell(billTable,freeBillDetailVo.getPracticalAmount(),1,PdfUtils.tableCellFont);            PdfUtils.addCell(billTable,freeBillDetailVo.getOughtAmount(),1,PdfUtils.tableCellFont);            PdfUtils.addCell(billTable,freeBillDetailVo.getOweAmount(),1,PdfUtils.tableCellFont);            PdfUtils.addCell(billTable,DateFormatUtil.dateToString(DateFormatUtil.yyyy_MM_dd_HHmmss,freeBillDetailVo.getPayTime()),1,PdfUtils.tableCellFont);            PdfUtils.addCell(billTable,"未结清",1,PdfUtils.tableCellFont);        }        document.add(billTable);    }

5.4PDF工具类完整代码

public class PdfUtils {    public static BaseFont bfChinese;        public static Font titlefont;    public static Font titleSecondFont;    public static Font headfont;    public static Font secondHeadfont;    public static Font textfont;    public static Font secondTitleFont;    public static Font itemFont;    public static Font paragraphFont;    public static Font tableCellFont;    public static Font accessoryFont;    //    public static Image checkPng;//    public static Image unCheckPng;        public static String shortSpacing = "      ";    public static String mediumSpacing = "            ";    public static String longSpacing = "                        ";    public static String maxLongSpacing = "        ";    public static String shortSlash = "   /   ";    public static String mediumSlash = "      /      ";    public static String longSlash = "            /            ";    public static String maxLongSlash = "                  /                  ";        public static String ht;    public static String scan;    public static String htScan;    public static String over;    public static String htSign;    public static String signatureHt;    public static File htFile;    public static File overFile;    public static File htSignFile;        static {        try {            // 不同字体(这里定义为同一种字体:包含不同字号、不同style)            bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);            titlefont = new Font(bfChinese, 16, Font.NORMAL);            titleSecondFont = new Font(bfChinese, 14, Font.NORMAL);            headfont = new Font(bfChinese, 14, Font.BOLD);            secondHeadfont = new Font(bfChinese, 15, Font.BOLD);            textfont = new Font(bfChinese, 16, Font.NORMAL);            secondTitleFont = new Font(bfChinese,18,Font.BOLD);            itemFont = new Font(bfChinese,16,Font.BOLD);            paragraphFont = new Font(bfChinese,11,Font.NORMAL);            accessoryFont = new Font(bfChinese,13,Font.NORMAL);            tableCellFont = new Font(bfChinese,9,Font.NORMAL);                        String osName = System.getProperty("os.name").toLowerCase();            if (osName.contains("win")||osName.contains("mac")) {//                checkPng = Image.getInstance(getResourceBasePath() + "\\templates\\check.png");//                checkPng.scaleAbsolute(14,12);//                unCheckPng = Image.getInstance(getResourceBasePath() + "\\templates\\unCheck.png");//                unCheckPng.scaleAbsolute(14,12);                ht = getResourceBasePath() + "\\templates\\ht.pdf";                scan = getResourceBasePath() + "\\templates\\scan.pdf";                htScan = getResourceBasePath() + "\\templates\\htScan.pdf";                over = getResourceBasePath() + "\\templates\\over.pdf";                htSign = getResourceBasePath() + "\\templates\\htSign.pdf";                htFile = new File(ht);                overFile = new File(over);                htSignFile = new File(htSign);            } else { //todo: linux或unbunt//                checkPng = Image.getInstance("/mnt/jar/chuz-cloud-school/check.png");//                checkPng.scaleAbsolute(14,12);//                unCheckPng = Image.getInstance("/mnt/jar/chuz-cloud-school/unCheck.png");//                unCheckPng.scaleAbsolute(14,12);                ht = "/mnt/jar/chuz-cloud-school/ht.pdf";                scan = "/mnt/jar/chuz-cloud-school/scan.pdf";                htScan = "/mnt/jar/chuz-cloud-school/htScan.pdf";                over = "/mnt/jar/chuz-cloud-school/over.pdf";                htSign = "/mnt/jar/chuz-cloud-school/htSign.pdf";                htFile = new File(ht);                overFile = new File(over);                htSignFile = new File(htSign);            }        } catch (Exception e) {            e.printStackTrace();        }    }        public static String getResourceBasePath() {        // 获取根目录        File path = null;        try {            path = new File(ResourceUtils.getURL("classpath:").getPath());        } catch (FileNotFoundException e) {        }        if (path == null || !path.exists()) {            path = new File("");        }        File file = new File(path.getAbsolutePath() + "\\templates");        if (!file.exists()){            return path.getAbsolutePath().replace("chuz-cloud-module\\chuz-cloud-school-start","chuz-boot-module-school");        }        return path.getAbsolutePath();    }    public static PdfPTable createTable() throws DocumentException {        PdfPTable table = new PdfPTable(2);        // 设置总宽度        table.setTotalWidth(490);        table.setLockedWidth(true);        // 设置每一列宽度        table.setTotalWidth(new float[] {240,240});        table.setLockedWidth(true);        return table;    }    public PdfPTable createTable(int numColumns) {        PdfPTable table = new PdfPTable(numColumns);        // 设置总宽度        table.setTotalWidth(490);        table.setLockedWidth(true);        return table;    }    public static PdfPTable createTitleTable(int numColumns,Font font, String ... titles) {        PdfPTable table = new PdfPTable(numColumns);        // 设置总宽度        table.setTotalWidth(490);        table.setLockedWidth(true);        for (String title : titles) {            addTitleCell(table,title,1,font);        }        return table;    }        public static void addTitle(Document document, String text, int alignment) throws DocumentException {        Paragraph paragraph = new Paragraph(text, new Font(bfChinese, 16, Font.NORMAL));        paragraph.setAlignment(alignment); //设置文字居中 0靠左   1,居中     2,靠右        paragraph.setIndentationLeft(0); //设置左缩进        paragraph.setIndentationRight(0); //设置右缩进        paragraph.setFirstLineIndent(0); //设置首行缩进        paragraph.setLeading(10f); //行间距        paragraph.setSpacingBefore(10f); //设置段落上空白        paragraph.setSpacingAfter(10f); //设置段落下空白        document.add(paragraph);    }        public static void addFirstTitle(Document document,String text,int alignment) throws DocumentException {        Paragraph paragraph = new Paragraph(text, new Font(bfChinese, 14, Font.NORMAL));        paragraph.setAlignment(alignment); //设置文字居中 0靠左   1,居中     2,靠右        paragraph.setIndentationLeft(0); //设置左缩进        paragraph.setIndentationRight(0); //设置右缩进        paragraph.setFirstLineIndent(0); //设置首行缩进        paragraph.setLeading(7f); //行间距        paragraph.setSpacingBefore(20f); //设置段落上空白        paragraph.setSpacingAfter(7f); //设置段落下空白        document.add(paragraph);    }        public static void addSecondTitle(Document document,String text,Font font,int alignment) throws DocumentException {        Paragraph paragraph = new Paragraph(text, new Font(bfChinese, 12, Font.NORMAL));        paragraph.setAlignment(alignment); //设置文字居中 0靠左   1,居中     2,靠右        paragraph.setIndentationLeft(9); //设置左缩进        paragraph.setIndentationRight(0); //设置右缩进        paragraph.setFirstLineIndent(9); //设置首行缩进        paragraph.setLeading(15f); //行间距        paragraph.setSpacingBefore(10f); //设置段落上空白        paragraph.setSpacingAfter(5f); //设置段落下空白        document.add(paragraph);    }        public static void addLine(Document document) throws DocumentException {        LineSeparator ls = new LineSeparator();        ls.setLineWidth(1);        ls.setLineColor(new BaseColor(179,180,164));        Chunk chunk = new Chunk(ls);        document.add(chunk);    }    public static void addNoBorderCell(PdfPTable table,String text) {        PdfPCell cell = new PdfPCell(new Paragraph(text,paragraphFont));        cell.setBorderWidth(0);        cell.setLeading(24,0); //行间距        table.addCell(cell);    }    public static void addTitleCell(PdfPTable table,String text,float borderWidth,Font font) {        Paragraph paragraph = new Paragraph(text, font);        paragraph.setAlignment(1);        paragraph.setLeading(5f);        PdfPCell cell = new PdfPCell(paragraph);        cell.setBorderWidth(borderWidth);        cell.setBackgroundColor(new BaseColor(245,247,251));        cell.setHorizontalAlignment(1);        cell.setVerticalAlignment(1);        table.addCell(cell);    }    public static void addCell(PdfPTable table,String text,float borderWidth,Font font) {        if (StringUtils.isBlank(text)) {            text = " ";        }        Paragraph paragraph = new Paragraph(text, font);        paragraph.setAlignment(1);        PdfPCell cell = new PdfPCell(paragraph);        //水平居中和垂直居中        cell.setHorizontalAlignment(Element.ALIGN_CENTER);        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);        cell.setUseAscender(true);        table.addCell(cell);    }    public static void addCell(PdfPTable table, BigDecimal amount, float borderWidth, Font font) {        Paragraph paragraph = null;        if (Objects.isNull(amount)) {            paragraph = new Paragraph(" ",font);        } else {            paragraph = new Paragraph(amount.toString(),font);        }        paragraph.setAlignment(1);        PdfPCell cell = new PdfPCell(paragraph);        cell.setBorderWidth(borderWidth);        //水平居中和垂直居中        cell.setHorizontalAlignment(Element.ALIGN_CENTER);        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);        cell.setUseAscender(true);        table.addCell(cell);    }        public void addSecondTitle(Document document,String text,int alignment,float spacingBefore,float spacingAfter) throws DocumentException {        Paragraph paragraph10 = new Paragraph(text, secondTitleFont);        paragraph10.setAlignment(alignment); //设置文字居中 0靠左   1,居中     2,靠右        paragraph10.setIndentationLeft(12); //设置左缩进        paragraph10.setIndentationRight(12); //设置右缩进        paragraph10.setFirstLineIndent(32); //设置首行缩进        paragraph10.setLeading(30f); //行间距        paragraph10.setSpacingBefore(1f); //设置段落上空白        paragraph10.setSpacingAfter(15f); //设置段落下空白        document.add(paragraph10);    }        public void addItemTitle(Document document,String text) throws DocumentException {        Paragraph paragraph = new Paragraph(text, itemFont);        paragraph.setAlignment(0); //设置文字居中 0靠左   1,居中     2,靠右        paragraph.setIndentationLeft(12); //设置左缩进        paragraph.setIndentationRight(12); //设置右缩进        paragraph.setFirstLineIndent(32); //设置首行缩进        paragraph.setLeading(30f); //行间距        paragraph.setSpacingBefore(1f); //设置段落上空白        paragraph.setSpacingAfter(1f); //设置段落下空白        document.add(paragraph);    }        public void addTextParagraph(Document document,String text) throws DocumentException {        Paragraph paragraph = new Paragraph(text,paragraphFont);        paragraph.setAlignment(0); //设置文字居中 0靠左   1,居中     2,靠右        paragraph.setIndentationLeft(12); //设置左缩进        paragraph.setIndentationRight(12); //设置右缩进        paragraph.setFirstLineIndent(32); //设置首行缩进        paragraph.setLeading(30f); //行间距        document.add(paragraph);    }        public Paragraph createTextParagraph() throws DocumentException {        Paragraph paragraph = new Paragraph("",paragraphFont);        paragraph.setAlignment(0); //设置文字居中 0靠左   1,居中     2,靠右        paragraph.setIndentationLeft(12); //设置左缩进        paragraph.setIndentationRight(12); //设置右缩进        paragraph.setFirstLineIndent(32); //设置首行缩进        paragraph.setLeading(30f); //行间距        return paragraph;    }        public Paragraph createTextParagraph(String text) throws DocumentException {        Paragraph paragraph = new Paragraph(text,paragraphFont);        paragraph.setAlignment(0); //设置文字居中 0靠左   1,居中     2,靠右        paragraph.setIndentationLeft(12); //设置左缩进        paragraph.setIndentationRight(12); //设置右缩进        paragraph.setFirstLineIndent(32); //设置首行缩进        paragraph.setLeading(30f); //行间距        return paragraph;    }        public Paragraph createTextParagraph(String text,int alignment) {        Paragraph paragraph = new Paragraph(text,paragraphFont);        paragraph.setAlignment(alignment); //设置文字居中 0靠左   1,居中     2,靠右        paragraph.setIndentationLeft(12); //设置左缩进        paragraph.setIndentationRight(12); //设置右缩进        paragraph.setFirstLineIndent(32); //设置首行缩进        paragraph.setLeading(30f); //行间距        return paragraph;    }    public void appendParagraph(Document document,Paragraph paragraph,String text) throws DocumentException {        paragraph.add(text);        document.add(paragraph);    }        public Paragraph addUnderLine(Paragraph paragraph,String text){        if (StringUtils.isBlank(text)){            text = shortSpacing;        } else {            text = StringUtils.join(" ",text," ");        }        Chunk sigUnderline = new Chunk(text);        sigUnderline.setUnderline(0.1f, -2f);        paragraph.add(sigUnderline);        return paragraph;    }        public void addUnderLine(Document document,Paragraph paragraph,String text) throws DocumentException {        if (StringUtils.isBlank(text)){            text = shortSpacing;        }        Chunk sigUnderline = new Chunk(StringUtils.join(" ",text," "));        sigUnderline.setUnderline(0.1f, -2f);        paragraph.add(sigUnderline);        document.add(paragraph);    }        public File mergePdf(String [] files,String savePath){        PdfReader pdfReader = null;        Document document = null;        try {            //创建一个与a.pdf相同纸张大小的document            pdfReader = new PdfReader(files[0]);            document = new Document(pdfReader.getPageSize(1));            PdfCopy copy = new PdfCopy(document, new FileOutputStream(savePath));            document.open();            for (int i = 0; i < files.length; i++) {                //一个一个的遍历现有的PDF                PdfReader reader = new PdfReader(files[i]);                int n = reader.getNumberOfPages();//PDF文件总共页数                System.out.println("n:"+n);                for (int j = 1; j <= n; j++) {                    document.newPage();                    PdfImportedPage page = copy.getImportedPage(reader, j);                    copy.addPage(page);                }                reader.close();            }            document.close();            pdfReader.close();            return new File(savePath);        } catch (IOException e) {            e.printStackTrace();        } catch (DocumentException e) {            e.printStackTrace();        } finally {            document.close();            pdfReader.close();        }        return null;    }        public Paragraph addUnderLine(Boolean check,Paragraph paragraph,String text1,String text2){        Chunk sigUnderline = null;        if (check){            if (Objects.isNull(text1)){                text1 = shortSpacing;            } else {                text1 = StringUtils.join(" ",text1," ");            }            sigUnderline = new Chunk(text1);        } else {            if (Objects.isNull(text2)){                text2 = shortSpacing;            } else {                text2 = StringUtils.join(" ",text2," ");            }            sigUnderline = new Chunk(text2);        }        sigUnderline.setUnderline(0.1f, -2f);        paragraph.add(sigUnderline);        return paragraph;    }        public void addUnderLine(Document document,Boolean check,Paragraph paragraph,String text1,String text2) throws DocumentException {        Chunk sigUnderline = null;        if (check){            if (Objects.isNull(text1)){                text1 = shortSpacing;            } else {                text1 = StringUtils.join(" ",text1," ");            }            sigUnderline = new Chunk(text1);        } else {            if (Objects.isNull(text2)){                text2 = shortSpacing;            } else {                text2 = StringUtils.join(" ",text2," ");            }            sigUnderline = new Chunk(text2);        }        sigUnderline.setUnderline(0.1F, -2.0F);        paragraph.add(sigUnderline);        document.add(paragraph);    }        public Chunk createUnderLineChunk(String text) {        if (Objects.isNull(text)){            text = shortSpacing;        } else {            text = StringUtils.join(" ",text," ");        }        Chunk sigUnderline = new Chunk(text);        sigUnderline.setUnderline(0.1f, -2f);        return sigUnderline;    }    private Document createDocument() throws IOException {        // 1.新建document对象 建立一个Document对象        Document document = new Document(PageSize.A4);        //        PdfUtils.htFile.createNewFile();        // 3.打开文档        document.open();        document.addTitle("销售合同");// 标题        document.addAuthor("chuz");// 作者        document.addSubject("Subject@iText pdf sample");// 主题        document.addKeywords("Keywords@iTextpdf");// 关键字        document.addCreator("chuz develop");// 创建者        return document;    }        public Chunk createUnderLineChunk(Boolean check,String text1,String text2) throws DocumentException {        Chunk sigUnderline = null;        if (check){            if (Objects.isNull(text1)){                text1 = shortSpacing;            } else {                text1 = StringUtils.join(" ",text1," ");            }            sigUnderline = new Chunk(text1);        } else {            if (Objects.isNull(text2)){                text2 = shortSpacing;            } else {                text2 = StringUtils.join(" ",text2," ");            }            sigUnderline = new Chunk(text2);        }        sigUnderline.setUnderline(0.1f, -2f);        return sigUnderline;    }        public Chunk createChunk(String text) throws DocumentException {        if (StringUtils.isBlank(text)){            text = shortSpacing;        }        text = StringUtils.join(" ",text," ");        return new Chunk(text);    }    //    public void addCheck(Document document,Boolean check,String text) throws Exception {//        Paragraph paragraph = createTextParagraph("");//        if (check){//            paragraph.add(new Chunk(checkPng,0,0,true));//        } else {//            paragraph.add(new Chunk(unCheckPng,0,0,true));//        }//        Chunk chunk = new Chunk(StringUtils.join(" ",text),textfont);//        paragraph.add(chunk);//        document.add(paragraph);//    }    //    public void addCheck(Paragraph paragraph,Boolean check,String text) throws Exception {//        if (check){//            paragraph.add(new Chunk(checkPng,0,0,true));//        } else {//            paragraph.add(new Chunk(unCheckPng,0,0,true));//        }//        Chunk chunk = new Chunk(StringUtils.join(" ",text),textfont);//        paragraph.add(chunk);//    }    //    public Paragraph addCheck(Boolean check,String text) throws Exception {//        Paragraph paragraph = createTextParagraph("");//        if (check){//            paragraph.add(new Chunk(checkPng,0,0,true));//        } else {//            paragraph.add(new Chunk(unCheckPng,0,0,true));//        }//        Chunk chunk = new Chunk(StringUtils.join(" ",text),textfont);//        paragraph.add(chunk);//        return paragraph;//    }    //    public Paragraph appendCheck(Paragraph paragraph,Boolean check,String text) throws Exception {//        if (check){//            paragraph.add(new Chunk(checkPng,0,0,true));//        } else {//            paragraph.add(new Chunk(unCheckPng,0,0,true));//        }//        Chunk chunk = new Chunk(StringUtils.join(" ",text),textfont);//        paragraph.add(chunk);//        return paragraph;//    }    public void packageDateParagraph(Document document, Paragraph paragraph, Date date) throws DocumentException {        if (Objects.nonNull(date)){            Calendar calendar = Calendar.getInstance();            calendar.setTime(date);            int year = calendar.get(Calendar.YEAR);            int month = calendar.get(Calendar.MONTH) + 1;            int day = calendar.get(Calendar.DAY_OF_MONTH);            addUnderLine(paragraph,StringUtils.join(year));            paragraph.add("年");            addUnderLine(paragraph,StringUtils.join(month));            paragraph.add("月");            addUnderLine(paragraph,StringUtils.join(day));            paragraph.add("日");        } else {            addUnderLine(paragraph,StringUtils.join(shortSlash));            paragraph.add("年");            addUnderLine(paragraph,StringUtils.join(shortSlash));            paragraph.add("月");            addUnderLine(paragraph,StringUtils.join(shortSlash));            paragraph.add("日");        }        document.add(paragraph);    }    public MultipartFile fileToMultipartFile(File file) {        FileItem fileItem = createFileItem(file);        MultipartFile multipartFile = new CommonsMultipartFile(fileItem);        return multipartFile;    }    public static FileItem createFileItem(File file) {        FileItemFactory factory = new DiskFileItemFactory(16, null);        FileItem item = factory.createItem("textField", "text/plain", true, file.getName());        int bytesRead = 0;        byte[] buffer = new byte[8192];        try {            FileInputStream fis = new FileInputStream(file);            OutputStream os = item.getOutputStream();            while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) {                os.write(buffer, 0, bytesRead);            }            os.close();            fis.close();        } catch (IOException e) {            e.printStackTrace();        }        return item;    }    public String precision(BigDecimal number){        if (Objects.nonNull(number)){            return number.setScale(0,BigDecimal.ROUND_DOWN).toString();        }        return null;    }}

总结

解决每一个bug,开发每一个功能都是对程序员能力上、心里上的修炼。既然翻不过浪浪山,那就要适应浪浪山。
加油 希望我们都能变成最好的自己。
sunshine
2023年04月13日

来源地址:https://blog.csdn.net/weixin_45116026/article/details/130122045

--结束END--

本文标题: Java导出PDF(itextpdf)-通俗易懂

本文链接: https://www.lsjlt.com/news/372867.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Java导出PDF(itextpdf)-通俗易懂
    文章目录 前言一、itextpdf是什么?二、快速开始1.废话不多说,效果先上一波2.依赖引入3.模版加载3.1模版加载工具类PDF 4.PDF操作工具类方法5.完整代码5.1前端代码-Vue5.2控制层代码5.3业务层代码...
    99+
    2023-08-17
    java pdf 开发语言
  • Java 之 PropertyDescriptor[通俗易懂]
    PropertyDescriptor是Java中的一个类,用于描述一个Java bean类的属性。PropertyDescripto...
    99+
    2023-09-20
    Java
  • Java反射(通俗易懂)
    目录 1、反射介绍 2、反射API 2.1 获取类对应的字节码的对象(三种) 2.2 常用方法 3、反射的应用 3.1 创建 : 测试物料类 3.2 获取类对象 3.3 获取成员变量 3.4 通过字节码对象获取类的成员方法 3.5 通过字...
    99+
    2023-09-02
    java spring
  • Java通俗易懂讲解泛型
    目录1.什么是泛型2.引出泛型3.泛型类的语法4.裸类型5.泛型如何编译的5.1 擦除机制5.2.泛型数组为什么不能实例化6.泛型的上界7.通配符7.1.通配符能用来干嘛7.2.通配...
    99+
    2024-04-02
  • java 可变参数 详解(通俗易懂)
    目录 一、概述: 二、格式: 三、注意事项(使用规范): 四、代码演示:         演示规范①~③:         演示规范④:         演示规范⑤:         课堂练习:         代码演示:         ...
    99+
    2023-09-06
    java 开发语言 经验分享 后端
  • 通俗易懂讲解JsonWebToken(JWT)
    目录前言基于session认证所显露的问题组成headerplayloadsignature验证过程基于token的鉴权机制优点总结前言 之前只是了解JWT是对cookie和sess...
    99+
    2022-12-08
    Json Web Token (JWT) Json Web Token
  • AjaxPro使用说明[通俗易懂]
    AjaxPro是一个用于简化Ajax开发的工具库,使开发人员能够更方便地使用Ajax技术进行数据交互。使用AjaxPro,你只需要几...
    99+
    2023-09-20
    AjaxPro
  • Zookeeper选举机制(通俗易懂)
    一. zk的选举机制中的概念: SID:服务器ID。用来唯一标识一台ZooKeeper集群中的机器,每台机器不能重复,和myid一致。 ZXID:事务ID。ZXID是一个事务ID,用来标识一次服务器状态的变更。在某一时刻,集群中的每台机器的...
    99+
    2023-08-19
    zookeeper 服务器 分布式
  • Nagios安装与部署[通俗易懂]
    Nagios是一款开源的网络监控工具,用于监控网络设备、服务器和应用程序的运行状态。安装和部署Nagios可以帮助管理员及时发现并解...
    99+
    2023-10-12
    Nagios
  • Activity 工作流引擎[通俗易懂]
    工作流引擎是一种软件工具,用于管理和自动化组织内的工作流程。它可以帮助组织更高效地处理和跟踪任务、流程和项目。工作流引擎将工作流程中...
    99+
    2023-10-12
    Activity
  • 一文给你通俗易懂的讲解Java异常
    什么是异常? 最简单的,看一个代码示例: public static void main(String[] args) { int a = 1; ...
    99+
    2024-04-02
  • windows XP虚拟机安装[通俗易懂]
    虚拟机是一种软件,可以在现有的操作系统中模拟另一个操作系统。在安装Windows XP虚拟机之前,你需要先下载一个虚拟机软件,比如V...
    99+
    2023-09-20
    windows
  • 轻松实现Android3D效果通俗易懂
    目录一、先看看聊天(需求)二、实现效果三、实现1.通过getSystemService获得SensorManager实例对象2.通过SensorManager实例对象获得想要的传感器...
    99+
    2024-04-02
  • Java链表超详细讲解(通俗易懂,含源码)
    目录概念链表的分类链表的结构代码实现链表1.创建节点类2.创建链表3.打印链表:public void display()4.查找是否包含关键字key是否在单链表当中:public ...
    99+
    2024-04-02
  • 一文让你彻底搞懂AQS(通俗易懂的AQS)
    一文让你彻底搞懂AQS(通俗易懂的AQS) 一、什么是AQS AQS是一个用来构建锁和同步器的框架,使用AQS能简单且高效地构造出应用广泛的大量的同步器,比如我们提到的ReentrantLock,Se...
    99+
    2023-09-04
    java 开发语言
  • OAuth2.0详细介绍与实践(通俗易懂)
    一、OAuth2.0介绍 1.1 概述 OAUTH协议为用户资源的授权提供了一个安全的、开放而又简易的标准。与以往的授权方式不同之处是OAUTH的授权不会使第三方触及到用户的帐号信息(如用户名与密码)...
    99+
    2023-09-16
    java
  • matlab中wavedec2,wavedec2函数详解[通俗易懂]
    wavedec2函数是Matlab中用于对二维信号进行小波分解的函数。这个函数的输入参数包括三个:- x:要进行小波分解的二维信号-...
    99+
    2023-09-20
    matlab
  • Java通俗易懂系列设计模式之代理模式
    目录前言介绍实现一.静态代理二.Jdk动态代理三.Cglib动态代理总结前言 国内程序员好像普遍对百度都没好感,而且百度近些年产生了不少负面的新闻,像16年的魏则西事件,近期的导演吴...
    99+
    2024-04-02
  • Java通俗易懂系列设计模式之策略模式
    目录介绍实例类图总结介绍 策略设计模式是行为设计模式之一。当我们为特定任务使用多个算法时,使用策略模式,客户端决定在运行时使用的实际实现。 策略模式的最佳示例之一是Collecti...
    99+
    2024-04-02
  • Java通俗易懂系列设计模式之模板模式
    目录介绍实现总结实际开发中常常会遇到,代码骨架类似甚至相同,只是具体的实现不一样的场景。例如:流程都有开启、编辑、驳回、结束。每个流程都包含这几个步骤,不同的是不同的流程实例它们的内...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作