java导出pdf(水印、页眉、表格、背景样式、页脚、饼图、柱图)

news/2024/5/10 1:59:01

这里写自定义目录标题

  • controller层
  • pdf模板RiskReportTemplate1(页脚、水印)
  • 多线程导出pdf数据
  • pdf导出工具类(页眉、图片底纹、柱图、饼图、表格、一级二级标题)
  • 实体类
  • pdf工具类(根据html生成pdf文件)
  • 页脚工具类
  • 柱图工具类
  • 饼图工具类

controller层

@PostMapping("/queryRiskReport1")public Object queryRiskReport1(@RequestBody RiskBo riskBo)throws Exception {String corpKey = riskBo.getCorpKey();String reportNo = "18073201";String beginDate=riskBo.getBeginDate();String endDate=riskBo.getEndDate();SysUser user = SecurityUtils.getLoginUser().getUser();String deptName = user.getDept().getDeptName();ChangeRiskIntoVo changeRiskIntoVo = new ChangeRiskIntoVo();String ancestors = user.getDept().getAncestors();if(ancestors.contains(",")){String[] split = ancestors.split(",");changeRiskIntoVo.setDeptPid(split[0]);}else{changeRiskIntoVo.setDeptPid(ancestors);}changeRiskIntoVo.setCorpKey(corpKey);Boolean aBoolean = riskHistoryInfoService.queryAuth(changeRiskIntoVo);String queryDate = DateUtils.getLastYears(1) + "至"+ DateUtils.getLastYears(0);//查询数据期限String currentTime = DateUtils.getDateFormatter(new Date());long currentTimeMillis = System.currentTimeMillis();String path = pdfResourceFolder + "risk_report" + File.separator;String pdfTemp = tempPdfFolder + "risk_report" + File.separator;File file = new File(path);if (!file.exists()) file.mkdirs();File file1 = new File(pdfTemp);if (!file1.exists()) file1.mkdirs();String pathUrl = path + user.getUserId() + "-" + currentTimeMillis + ".PDF";String tmpPath1 = pdfTemp + user.getUserId() + "-" + currentTimeMillis + "1.PDF";String tmpPath2 = pdfTemp + user.getUserId() + "-" + currentTimeMillis + "2.PDF";String tmpPath3 = pdfTemp + user.getUserId() + "-" + currentTimeMillis + "3.PDF";String tmpPath4 = pdfTemp + user.getUserId() + "-" + currentTimeMillis + "4.PDF";RiskReportTemplate1 t=new RiskReportTemplate1();String htmlStr= t.createReportTemplate(riskReportService.queryRiskReport1(corpKey,beginDate,endDate), queryDate, user.getUserName(), deptName, reportNo,aBoolean,beginDate,endDate).toString();PdfUtils.htmp2pdf("客户风险分析报告", htmlStr,tmpPath1, false, false);ReportUtils2.addImageWatermark(tmpPath1, tmpPath2, 1, 0);ReportUtils2.pdf_yemei(deptName, tmpPath2, tmpPath3);t.waterMark(tmpPath3,tmpPath4, user.getUserName(), currentTime, user.getDept().getDeptName());t.addPageNum(tmpPath4, pathUrl);return Result.success(pathUrl);}

pdf模板RiskReportTemplate1(页脚、水印)

package com.ruoyi.business.bankInterface.controller.risk.model1;import com.alibaba.fastjson2.JSONObject;
import com.itextpdf.text.*;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import com.ruoyi.business.bankInterface.ConstantConfig;
import com.ruoyi.business.bankInterface.controller.risk.PdfUtils;
import com.ruoyi.business.bankInterface.controller.risk.utils.ReportUtils2;
import com.ruoyi.business.bankInterface.controller.tax.utils.DateUtils;
import com.ruoyi.business.bankInterface.controller.tax.utils.pdf.PdfNumPageEventTax;
import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceRGB;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.Font;
import java.awt.image.BufferedImage;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;@Slf4j
public class RiskReportTemplate1 {public static void main(String[]args)throws IOException {StringBuffer buffer = new StringBuffer();BufferedReader bf = new BufferedReader(new FileReader("D:\\test\\RiskReport1.json"));String s = null;while((s = bf.readLine()) != null){ buffer.append(s.trim()); } bf.close();RiskModel2 model = JSONObject.parseObject(buffer.toString(),RiskModel2.class);RiskReportTemplate1 t=new RiskReportTemplate1();String htmlStr= t.createReportTemplate(model, "2021-01-01 "+"至"+" 2024-01-31", "周伯通","中国银行苏州分行","18073201",true,"","").toString();PdfUtils.htmp2pdf("njfh", htmlStr,"D:\\test\\2123456789001.pdf", false, false);ReportUtils2.addImageWatermark("D:\\test\\2123456789001.pdf", "D:\\test\\2123456789002.pdf", 1, 0);ReportUtils2.pdf_yemei("中国银行苏州分行", "D:\\test\\2123456789002.pdf", "D:\\test\\2123456789.pdf");waterMark("D:\\test\\2123456789.pdf","D:\\test\\21234567891.pdf", "测试用户", "2024-03-29", "中国银行");addPageNum("D:\\test\\21234567891.pdf", "D:\\test\\212345678915.pdf");}/** 征信报告-征信报告模板生成 */public StringBuffer createReportTemplate(RiskModel2 riskModel2, String authDate, String person, String bankName, String reportNo,Boolean aBoolean,String beginDate,String endDate) {String begin = authDate.split("至")[0];String end = authDate.split("至")[1];StringBuffer template = new StringBuffer();template.append("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><title>客户风险分析报告</title></head>");template.append("<body><div>");//获取当前时间String curentDateStr = DateUtils.getDateFormatter(new Date());//首页template.append(getFirstHtml(riskModel2.getK1().getQymc(), riskModel2.getK1().getTyshxydm(),curentDateStr, authDate, bankName, person, reportNo,beginDate,endDate));/** 1.企业概览 */template.append(ReportUtils2.title("企业概览", 1));/** 1.1.基本信息 */template.append(K1.getHtml(riskModel2.getK1()));/** 1.2.企业信用评分 */template.append(K1.score(riskModel2.getK1().getScore()));/** 2.公司治理 */template.append(ReportUtils2.title("公司治理", 1));template.append(K3.getHtml1(riskModel2.getK3List()));template.append(ReportUtils2.title("风险信息", 1));template.append(K4.getHtml1(riskModel2.getK4List()));template.append(K5.getHtml(riskModel2.getK5List()));/** 3.1.经营风险 */template.append(K4.getHtml2(riskModel2.getK4_1List()));/** 3.1.1.经营资质 */template.append(K3.getHtml2(riskModel2.getK3_1List()));/** 3.1.2.运营风险 */template.append(K6.getHtml1(riskModel2.getK6List(),aBoolean));/** 3.1.3.司法风险 */template.append(K7.getHtml(riskModel2.getK7List(), riskModel2.getK7_1List(),aBoolean));/** 3.1.4.行政处罚 */template.append(K8.getHtml(riskModel2.getK8List(),aBoolean));/** 3.2.财务风险 */template.append(K4.getHtml3(riskModel2.getK4_2List()));/** 3.2.1.偿债能力 */template.append(K6.getHtml2(riskModel2.getK6_1List(), "偿债能力", "本风险监测时段中,暂未监测到本企业存在异常。",aBoolean));/** 3.2.2.盈利能力 */template.append(K6.getHtml2(riskModel2.getK6_2List(), "盈利能力", "本风险监测时段中,暂未监测到本企业存在异常。",aBoolean));/** 3.2.3.营运能力 */template.append(K6.getHtml2(riskModel2.getK6_3List(), "营运能力", "本风险监测时段中,暂未监测到本企业存在异常。",aBoolean));/** 3.3.授用信风险 */template.append(ReportUtils2.title("授用信风险", 2));/** 3.3.1.履约风险 */template.append(K6.getHtml2(riskModel2.getK6_4List(), "履约风险", "本风险监测时段中,暂未监测到本企业存在异常。",aBoolean));/** 3.3.2.过度授信风险 */template.append(K6.getHtml2(riskModel2.getK6_5List(), "过度授信风险", "本风险监测时段中,暂未监测到本企业存在异常。",aBoolean));/** 3.3.3.企业非银融资 */template.append(K6.getHtml2(riskModel2.getK6_6List(), "企业非银融资", "本风险监测时段中,暂未监测到本企业存在异常。",aBoolean));/** 3.3.4.其他授信异常 */template.append(K6.getHtml2(riskModel2.getK6_7List(), "其他授信异常", "本风险监测时段中,暂未监测到本企业存在异常。",aBoolean));/** 3.4.关联风险 */template.append(ReportUtils2.title("关联风险", 2));/** 3.4.1.工商信息监测 */template.append(K6.getHtml2(riskModel2.getK6_8List(), "工商信息监测", "本风险监测时段中,未监测到本企业存在企业关联人(企业)发生破产清算注销风险。",aBoolean));/** 3.4.2.司法信息监测 */template.append(K9.getHtml(riskModel2.getK9List(),aBoolean));/** 3.4.3.履约风险(征信) */template.append(K6.getHtml2(riskModel2.getK6_9List(), "履约风险(征信)", "本风险监测时段中,未监测到本企业存在关联征信五级不良、关联企业在他行未结清业务发生逾期、关联企业非银机构授信异常风险。",aBoolean));/** 3.5.名单监测 */template.append(ReportUtils2.title("名单监测", 2));/** 3.5.1.黑名单 *///template.append(K10.getHtml(riskModel2.getK10List()));/** 3.6.系统性风险 */template.append(ReportUtils2.title("系统性风险", 2));/** 3.6.1.行业风险 */template.append(K6.getHtml3(riskModel2.getK6_10List(),aBoolean));template.append("</div>");template.append("</body>");template.append("</html>");return template;}/***  首页* @param companyName              企业名称* @param companyCode              统一社会信用代码* @param time                      报告生成日期,如:2021-01-01* @param dataRetrievalCycle      数据周期,如:2021-01-01 至 2024-01-31* @param bankName                 金融机构名称* @param person                   查询人员* @param reportNo                 报告编号*/public static StringBuffer getFirstHtml(String companyName, String companyCode, String time, String dataRetrievalCycle, String bankName, String person, String reportNo,String beginDate,String endDate) {StringBuffer firstHtml = new StringBuffer();firstHtml.append("<div align=\"left\"><img style=\"height:30px;width:280px\" src=\"").append(ConstantConfig.RISK_IMAGE + "苏州征信-logo.png").append("\"/></div>");firstHtml.append("<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>");//查询人员信息firstHtml.append("<div>");firstHtml.append("<div align=\"right\" style=\"font-size:30px; font-weight: 900; color:#005BAB;\">").append(companyName).append("</div><br/>");firstHtml.append("<div align=\"right\" style=\"font-size:18px; font-weight: 500; color:#808080;\">统一社会信用代码:").append(companyCode).append("</div>");firstHtml.append("</div>");firstHtml.append("<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>");firstHtml.append("<div>");firstHtml.append("<div align=\"left\" style=\"font-size:16px; font-weight: 100; color:#808080;\">查询人员: ").append(person).append("</div>");firstHtml.append("<div align=\"left\" style=\"font-size:16px; font-weight: 100; color:#808080;\">查询机构: ").append(bankName).append("</div>");firstHtml.append("<div align=\"left\" style=\"font-size:16px; font-weight: 100; color:#808080;\">风险周期: ").append(beginDate+"至"+endDate).append("</div>");firstHtml.append("<div align=\"left\" style=\"font-size:16px; font-weight: 100; color:#808080;\">报告生成时间: ").append(time).append("</div>");firstHtml.append("</div><br/><br/><br/><br/><br/>");firstHtml.append("<div>");firstHtml.append("<div align=\"right\" style=\"font-size:20px; font-weight: 500; color:#4169E1;\">报告编号:").append(reportNo).append("</div>");firstHtml.append("</div><br/><br/><br/>");return firstHtml;}/*** 生成页脚* @param srcPdfPath 源文件* @param tagetPdfPath  加工后文件*/public static void addPageNum(String srcPdfPath, String tagetPdfPath) {try {// 输出文件 流FileOutputStream fos = new FileOutputStream(tagetPdfPath);// 读取 源PDF文件,进行一页一页复制,才能触发 添加页码的  页面监听事件PdfReader reader = new PdfReader(srcPdfPath);// 获取 源文件总页数int num = reader.getNumberOfPages();// 新建文档,默认A4大小Document document = new Document(PageSize.A4);PdfWriter writer = PdfWriter.getInstance(document, fos);// 设置页面监听事件,必须在open方法前writer.setPageEvent(new PdfNumPageEventTax(num));document.open();// PDF内容体PdfContentByte pdfContent = writer.getDirectContent();//System.out.println("总页数:" + num);// 页面数是从1开始的for (int i = 1; i <= num; i++) {document.newPage();// 设置空页码进行展示writer.setPageEmpty(false);PdfImportedPage page = writer.getImportedPage(reader, i);// 复制好的页面,添加到内容去,触发事件监听pdfContent.addTemplate(page, 0, 3);}document.close();reader.close();}catch (Exception e){e.printStackTrace();}}private static int interval = -5;//给pdf加水印public static void waterMark(String inputFile, String outputFile, String userName, String time,String bankName) {PdfReader reader = null;PdfStamper stamper = null;FileOutputStream outputStream = null;try {String waterMarkName = "长三角征信链";reader = new PdfReader(inputFile);outputStream = new FileOutputStream(outputFile);stamper = new PdfStamper(reader, outputStream);BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",   BaseFont.EMBEDDED);Rectangle pageRect = null;PdfGState gs = new PdfGState();gs.setFillOpacity(0.15f);//控制水印深浅gs.setStrokeOpacity(0.4f);int total = reader.getNumberOfPages() + 1;JLabel label = new JLabel();FontMetrics metrics;int textH = 0;int textW = 0;label.setText(waterMarkName);metrics = label.getFontMetrics(label.getFont());textH = metrics.getHeight();textW = metrics.stringWidth(label.getText());PdfContentByte under;for (int i = 1; i < total; i++) {if(i==1 || i==total-1) continue;pageRect = reader.getPageSizeWithRotation(i);under = stamper.getOverContent(i);under.saveState();under.setGState(gs);under.beginText();under.setFontAndSize(base, 18);//控制水印字体大小// 水印文字成30度角倾斜// 获取每一页的高度、宽度float pageHeight = pageRect.getHeight();float pageWidth = pageRect.getWidth();// 根据纸张大小多次添加, 水印文字成30度角倾斜for (int height = interval + textH; height < pageHeight; height = height + textH * 8) {for (int width = interval + textW; width < pageWidth + textW; width = width + textW * 6) {// 将分段的字段进行输出编写under.setFontAndSize(base, 12);//控制水印字体大小under.showTextAligned(Element.ALIGN_LEFT, "查询人员:"+userName, width - textW, height -textH , 30);under.setFontAndSize(base, 12);//控制水印字体大小under.showTextAligned(Element.ALIGN_LEFT, "查询时间:"+time, width - textW+12, height -textH*2 , 30);under.setFontAndSize(base, 12);//控制水印字体大小under.showTextAligned(Element.ALIGN_LEFT, "查询机构:"+bankName, width - textW+24, height -textH*3 , 30);under.setFontAndSize(base, 12);//控制水印字体大小under.showTextAligned(Element.ALIGN_LEFT, "苏州企业征信服务有限公司", width - textW+36, height -textH*4 , 30);}}under.endText();}} catch (Exception e) {e.printStackTrace();}finally {try {if(stamper != null){stamper.close();}if(outputStream != null){outputStream.close();}if(reader != null){reader.close();}}catch (Exception e){e.printStackTrace();}}}//    /** 文本内容 content:文本内容;fontSize:字体大小;color:字体颜色;backgroundColor:背景色 */
//    public static StringBuffer title(String content, int fontSize, int fontWeight){
//        StringBuffer description = new StringBuffer();
//        description.append("<div align=\"left\" style=\"");
//        if(fontWeight > 0) description.append("font-weight:").append(fontWeight).append(";");
//        if(fontSize > 0) description.append("font-size:").append(fontSize).append("px;");
//        description.append("line-height:").append(1.8).append(";");
//        description.append("letter-spacing:").append(1.8).append("px;");
//        description.append("\">");
//        description.append(content).append("</div>");
//        return description;
//    }//    /** 文本内容 content:文本内容;fontSize:字体大小;color:字体颜色;backgroundColor:背景色 */
//    public static StringBuffer description(String content, int fontSize, String color, String backgroundColor, int fontWeight){
//        StringBuffer description = new StringBuffer();
        description.append("<br/>");
//        description.append("<div align=\"left\" style=\"");
//        if(fontWeight > 0) description.append("font-weight:").append(fontWeight).append(";");
//        if(fontSize > 0) description.append("font-size:").append(fontSize).append("px;");
//        if(StringUtils.isNotBlank(color)) description.append("color:").append(color).append(";");
//        if(StringUtils.isNotBlank(color)) description.append("padding-left:12px;padding-right:12px;padding-bottom:12px;background-color:").append(backgroundColor).append(";");
//        description.append("line-height:").append(1.8).append(";");
//        description.append("letter-spacing:").append(1.8).append("px;");
//        description.append("\">");
//        description.append("&nbsp;&nbsp;");
//        description.append(content).append("</div>");
        description.append("<br/>");
//        return description;
//    }
//
//    /**
//     * 加载图片(柱状图/折线图)
//     */
//    public static StringBuffer getImgOneHtml(String title, List<String> xData, List<Double> yDataList, List<Color> colorList, String yUnit, int type,
//                                             int width, int height, double SpaceFill, StringBuffer tempHtml) {
//        if (xData == null || xData.size() == 0) return tempHtml;
//        String path = new HistogramChar<>().getChart2(title, xData, yDataList, colorList, yUnit, type,
//                width, height, SpaceFill);
//        tempHtml.append("<div align=\"center\"><img style=\"height:262px;width:700px\" src=\"").append(path).append("\"/></div>");
//        return tempHtml;
//    }
//
//    /**
//     * 加载图片(扇形图)
//     */
//    public static StringBuffer getImgPieHtml(List<String> keyList, List<Double> rDataList, Boolean isTitleVisible, String title,
//                                             Boolean isLegendVisible, RectangleEdge rectangleEdge, int flag, StringBuffer tempHtml) {
//        if (rDataList == null || rDataList.size() == 0) return tempHtml;
//        tempHtml.append("<div align=\"center\">");
//        String path = Pie2Chart.pieChart(keyList, rDataList, isTitleVisible, title, isLegendVisible, rectangleEdge, flag);
//        tempHtml.append("<img style=\"width:700px\" src=\"").append(path).append("\"/>");
//        tempHtml.append("</div>");
//        return tempHtml;
//    }
}

多线程导出pdf数据

public RiskModel2 queryRiskReport1(String corpKey,String beginDate,String endDate){RiskModel2 riskModel2 = new RiskModel2();/** 1.基本信息 */CompletableFuture<K1> k1 = CompletableFuture.supplyAsync(() ->{K1 k_1 = riskReportMapper.queryK1(corpKey);k_1.setTyshxydm(corpKey);return k_1;}, creditReportExecutor);/** 2.公司治理 */CompletableFuture<List<K3>> k3List = CompletableFuture.supplyAsync(() ->{List<K3> k_3List = riskReportMapper.queryK3List(corpKey, Arrays.asList("R001","R002","R003"),beginDate,endDate);return k_3List;}, creditReportExecutor);/** 3.风险信息:1级风险类型分布 */CompletableFuture<List<K4>> k4List = CompletableFuture.supplyAsync(() ->{List<K4> k_4List = riskReportMapper.queryK4List(corpKey,beginDate,endDate);return k_4List;}, creditReportExecutor);/** 3.风险信息:1级风险类型分布 */CompletableFuture<List<K5>> k5List = CompletableFuture.supplyAsync(() ->{List<K5> k_5List = riskReportMapper.queryK5List(corpKey,beginDate,endDate);return k_5List;}, creditReportExecutor);/** 3.1.经营风险 */CompletableFuture<List<K4>> k4_1List = CompletableFuture.supplyAsync(() ->{List<K4> k_4List = riskReportMapper.queryK4_1List(corpKey, "经营风险",beginDate,endDate);return k_4List;}, creditReportExecutor);/** 3.1.1.经营资质 */CompletableFuture<List<K3>> k3_1List = CompletableFuture.supplyAsync(() ->{List<K3> k_3List = riskReportMapper.queryK3List(corpKey, Arrays.asList("R004","R005"),beginDate,endDate);return k_3List;}, creditReportExecutor);/** 3.1.2.运营风险 */CompletableFuture<List<K6>> k6List = CompletableFuture.supplyAsync(() ->{List<K6> k_6List = riskReportMapper.queryK6List(corpKey, "运营风险",beginDate,endDate);return k_6List;}, creditReportExecutor);/** 3.1.3.司法风险 */CompletableFuture<List<K7>> k7_1List = CompletableFuture.supplyAsync(() ->{List<K7> k_7List = riskReportMapper.queryK7_1List(corpKey, "司法风险",beginDate,endDate);return k_7List;}, creditReportExecutor);CompletableFuture<List<K7>> k7List = CompletableFuture.supplyAsync(() ->{List<K7> k_7List = riskReportMapper.queryK7List(corpKey, "司法风险",beginDate,endDate);return k_7List;}, creditReportExecutor);/** 3.1.4.行政处罚 */CompletableFuture<List<K8>> k8List = CompletableFuture.supplyAsync(() ->{List<K8> k_8List = riskReportMapper.queryK8List(corpKey, "行政处罚",beginDate,endDate);return k_8List;}, creditReportExecutor);/** 3.2 财务风险 */CompletableFuture<List<K4>> k4_2List = CompletableFuture.supplyAsync(() ->{List<K4> k_4List = riskReportMapper.queryK4_1List(corpKey, "财务风险",beginDate,endDate);return k_4List;}, creditReportExecutor);/** 3.2.1 偿债能力 */CompletableFuture<List<K6>> k6_1List = CompletableFuture.supplyAsync(() ->{List<K6> k_6List = riskReportMapper.queryK6List(corpKey, "偿债能力",beginDate,endDate);return k_6List;}, creditReportExecutor);/** 3.2.2盈利能力 */CompletableFuture<List<K6>> k6_2List = CompletableFuture.supplyAsync(() ->{List<K6> k_6List = riskReportMapper.queryK6List(corpKey, "盈利能力",beginDate,endDate);return k_6List;}, creditReportExecutor);/** 3.2.3营运能力 */CompletableFuture<List<K6>> k6_3List = CompletableFuture.supplyAsync(() ->{List<K6> k_6List = riskReportMapper.queryK6List(corpKey, "营运能力",beginDate,endDate);return k_6List;}, creditReportExecutor);/** 3.3.1履约风险 */CompletableFuture<List<K6>> k6_4List = CompletableFuture.supplyAsync(() ->{List<K6> k_6List = riskReportMapper.queryK6List(corpKey, "履约风险",beginDate,endDate);return k_6List;}, creditReportExecutor);/** 3.3.2 过度授信风险 */CompletableFuture<List<K6>> k6_5List = CompletableFuture.supplyAsync(() ->{List<K6> k_6List = riskReportMapper.queryK6List(corpKey, "过度授信风险",beginDate,endDate);return k_6List;}, creditReportExecutor);/** 3.3.3 企业非银融资 */CompletableFuture<List<K6>> k6_6List = CompletableFuture.supplyAsync(() ->{List<K6> k_6List = riskReportMapper.queryK6List(corpKey, "企业非银融资",beginDate,endDate);return k_6List;}, creditReportExecutor);/** 3.3.4 其他授信异常 */CompletableFuture<List<K6>> k6_7List = CompletableFuture.supplyAsync(() ->{List<K6> k_6List = riskReportMapper.queryK6List(corpKey, "其他授信异常",beginDate,endDate);return k_6List;}, creditReportExecutor);/** 3.4.1 工商信息监测(暂无数据) *//** 3.4.2 司法信息监测 */CompletableFuture<List<K9>> k9List = CompletableFuture.supplyAsync(() ->{List<K9> k9_1List = riskReportMapper.queryK9List(corpKey, "司法信息监测",beginDate,endDate);return k9_1List;}, creditReportExecutor);/** 3.4.3 履约风险(征信) */CompletableFuture<List<K6>> k6_9List = CompletableFuture.supplyAsync(() ->{List<K6> k_6List = riskReportMapper.queryK6List(corpKey, "履约风险(征信)",beginDate,endDate);return k_6List;}, creditReportExecutor);/** 3.5.1 黑名单 */CompletableFuture<List<K10>> k10List = CompletableFuture.supplyAsync(() ->{List<K10> k10_1List = riskReportMapper.queryK10List(corpKey, "黑名单",beginDate,endDate);return k10_1List;}, creditReportExecutor);/** 3.6 系统性风险 *//** 3.6 系统性风险 */CompletableFuture<List<K6>> k6_10List = CompletableFuture.supplyAsync(() ->{List<K6> k_6List = riskReportMapper.queryK6List(corpKey, "行业风险",beginDate,endDate);return k_6List;}, creditReportExecutor);CompletableFuture<Void> allOf = CompletableFuture.allOf(k1, k3List, k4List, k5List, k3_1List, k4_1List, k6List, k7_1List, k7List, k4_2List, k8List, k6_1List, k6_2List, k6_3List, k6_4List, k6_5List, k6_6List, k6_7List, k9List, k6_9List, k10List, k6_10List);try {allOf.get();riskModel2 = RiskModel2.builder().k1(k1.get()).k3List(k3List.get()).k4List(k4List.get()).k5List(k5List.get()).k3_1List(k3_1List.get()).k4_1List(k4_1List.get()).k6List(k6List.get()).k7_1List(k7_1List.get()).k7List(k7List.get()).k4_2List(k4_2List.get()).k8List(k8List.get()).k6_1List(k6_1List.get()).k6_1List(k6_1List.get()).k6_2List(k6_2List.get()).k6_3List(k6_3List.get()).k6_4List(k6_4List.get()).k6_5List(k6_5List.get()).k6_6List(k6_6List.get()).k6_7List(k6_7List.get()).k9List(k9List.get()).k6_9List(k6_9List.get()).k6_10List(k6_10List.get()).k10List(k10List.get()).build();} catch (InterruptedException |ExecutionException e) {e.printStackTrace();log.error("查询企业{}风险报告出错",corpKey);}return riskModel2;}

pdf导出工具类(页眉、图片底纹、柱图、饼图、表格、一级二级标题)

package com.ruoyi.business.bankInterface.controller.risk.utils;import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.ruoyi.business.bankInterface.ConstantConfig;
import com.ruoyi.business.bankInterface.controller.risk.utils.chart.HistogramChar;
import com.ruoyi.business.bankInterface.controller.risk.utils.chart.Pie2Chart;
import com.ruoyi.business.bankInterface.controller.tax.utils.annotation.ReportField;
import com.ruoyi.business.bankInterface.controller.tax.utils.annotation.ReportFieldAnnotation;
import org.apache.commons.lang3.StringUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceRGB;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.jfree.ui.RectangleEdge;import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.*;
import java.util.List;public class ReportUtils2 {private static String baseUrl = ConstantConfig.IMAGE_TEMP_FOLDER;//列表数据public static StringBuffer listHtml(Class<?> clazz, String jsonStr, StringBuffer template, int tableType, int dataType){if(StringUtils.isBlank(jsonStr) || "{}".equals(jsonStr) || "[]".equals(jsonStr)) return template;Map<String, Object> data = null;if(dataType == 1){/** 数据类型是list */data = ReportUtils2.changeObjectList(clazz, jsonStr);}else{/** 数据类型是对象 */data = ReportUtils2.changeObject(clazz, jsonStr);}if(data == null || data.size() == 0) return template;List<ReportField> reportFieldList = (List<ReportField>)data.get("nameList");List<Map<String,Object>> valueList = (List<Map<String,Object>>)data.get("valueList");if(tableType == 1){//列表排列template.append(ReportUtils2.listHtml(reportFieldList, valueList));}else if(tableType == 3){//横着单列标题排列template.append(ReportUtils2.oneTitleHtml(reportFieldList, valueList));}else{//横着两列标题排列template.append(ReportUtils2.twoTitleHtml(reportFieldList, valueList));}return template;}//jsonArrStr支持的对象中可以有key:value和最多两个List<String>,且其中一个List<String>为yearListpublic static Map<String, Object> changeObjectList(Class<?> clazz, String jsonArrStr) {// 获取带有特定注解的字段Set<Field> annotatedFields = getAnnotatedFields(clazz, ReportFieldAnnotation.class);List<ReportField> reportFieldList = new ArrayList<>();List<JSONObject> objectList = JSONArray.parseArray(jsonArrStr, JSONObject.class);List<Map<String, String>> valueList = new ArrayList<>(); Map<String, String> value = null;int flag = 0;for(JSONObject jsonObject : objectList){value = new HashMap<>();for (Field field : annotatedFields) {ReportField reportField = new ReportField();ReportFieldAnnotation annotation = field.getAnnotation(ReportFieldAnnotation.class);int dataType = annotation.dataType();//0:字符串,1:List<String>String fieldName = field.getName();Object vObj = jsonObject.get(field.getName());if(dataType == 0){//字符串类型if(flag == 0) {reportField.setDesc(annotation.desc()); reportField.setName(fieldName); reportField.setSort(annotation.sort());reportField.setWidth(annotation.width()); reportField.setNum(annotation.num()); reportFieldList.add(reportField);}//value.put(fieldName, Objects.isNull(vObj) ? "" : vObj.toString());value.put(fieldName, Objects.isNull(vObj) ? "" : vObj.toString().replaceAll("<", "").replaceAll(">", ""));}else if(dataType == 1){//List<String>类型的时候字段名取yearList的值if("yearList".equals(field.getName())) continue;Object yearListObj = jsonObject.get("yearList");if(Objects.isNull(yearListObj) || Objects.isNull(vObj)) continue;List<String> yearList = JSONArray.parseArray(JSONObject.toJSONString(yearListObj), String.class);List<String> vList = JSONArray.parseArray(JSONObject.toJSONString(vObj), String.class);for(int i=0; i<yearList.size(); i++){String year = yearList.get(i);if(flag == 0) {reportField = new ReportField(); reportField.setDesc(year); reportField.setName(year); reportField.setSort(annotation.sort()); reportField.setWidth(annotation.width()); reportField.setNum(annotation.num()); reportFieldList.add(reportField);}String v = vList.get(i);value.put(year, v == null ? "" : v.replaceAll("<", "").replaceAll(">", ""));}}}valueList.add(value); flag++;}reportFieldList.sort((t1, t2) -> { Integer counts = t1.getSort(); Integer counts1 = t2.getSort(); return counts.compareTo(counts1); });Map<String, Object> res = new HashMap<>();res.put("nameList", reportFieldList);res.put("valueList", valueList);return res;}//jsonStr只支持key:valuepublic static Map<String, Object> changeObject(Class<?> clazz, String jsonStr) {//获取带有特定注解的字段Set<Field> annotatedFields = getAnnotatedFields(clazz, ReportFieldAnnotation.class);List<ReportField> reportFieldList = new ArrayList<>();JSONObject jsonObject = JSONObject.parseObject(jsonStr);List<Map<String, String>> valueList = new ArrayList<>(); Map<String, String> value = new HashMap<>();for (Field field : annotatedFields) {ReportField reportField = new ReportField();ReportFieldAnnotation annotation = field.getAnnotation(ReportFieldAnnotation.class);int dataType = annotation.dataType();//0:字符串,1:List<String>String fieldName = field.getName();Object vObj = jsonObject.get(fieldName);if(dataType == 0){//字符串类型reportField.setDesc(annotation.desc()); reportField.setName(fieldName); reportField.setSort(annotation.sort()); reportField.setWidth(annotation.width()); reportField.setNum(annotation.num()); reportFieldList.add(reportField);value.put(fieldName, Objects.isNull(vObj) ? "" : vObj.toString().replaceAll("<", "").replaceAll(">", ""));}else if(dataType == 1){//List<String>类型的时候字段名取yearList的值Object yearListObj = jsonObject.get("yearList");if(Objects.isNull(yearListObj) || Objects.isNull(vObj)) continue;List<String> yearList = JSONArray.parseArray(JSONObject.toJSONString(yearListObj), String.class);List<String> vList = JSONArray.parseArray(JSONObject.toJSONString(vObj), String.class);for(int i=0; i<yearList.size(); i++){String year = yearList.get(i); String v = vList.get(i);reportField.setDesc(year); reportField.setName(year); reportField.setSort(annotation.sort()); reportField.setWidth(annotation.width()); reportField.setNum(annotation.num()); reportFieldList.add(reportField);value.put(year, v == null ? "" : v.replaceAll("<", "").replaceAll(">", ""));}}}valueList.add(value);Map<String, Object> res = new HashMap<>();reportFieldList.sort((t1, t2) -> { Integer counts = t1.getSort(); Integer counts1 = t2.getSort(); return counts.compareTo(counts1); });res.put("nameList", reportFieldList);res.put("valueList", valueList);return res;}/** 获取有该注解的字段 */public static Set<Field> getAnnotatedFields(Class<?> clazz, Class<? extends Annotation> annotation) {Set<Field> fields = new HashSet<>();Class<?> superClass = clazz;while (superClass != Object.class) {for (Field field : superClass.getDeclaredFields()) {if (field.isAnnotationPresent(annotation)) {fields.add(field);}}superClass = superClass.getSuperclass();}return fields;}/** 个性化数据-列表数据展示 reportFieldList:标题行 value:标题行对应的字段对应的值 */public static StringBuffer listHtml(List<ReportField> reportFieldList, List<Map<String,Object>> value){List<String> keyList = new ArrayList<>();//存放标题行对应的字段StringBuffer tempHtml= new StringBuffer();tempHtml.append("<div class=\"table_box_b\">");tempHtml.append("<table width=\"100%\" cellspacing=\"0\">");tempHtml.append("<tbody>");/** -----------------------------标题列start----------------------------------- */tempHtml.append("<tr>"); int j=1; int size = reportFieldList.size();for(ReportField reportField : reportFieldList){if(j==size){tempHtml.append("<th height=\"30px\" width=\"").append(reportField.getWidth()).append("%\" nowrap=\"nowrap\" align=\"center\" style=\"").append(ReportLabelUtils.TH_FONT).append("border-bottom: 1px solid #F4F4F6;border-top:1px solid #F4F4F6;border-right:1px solid #F4F4F6;\">").append(reportField.getDesc()).append("</th>");} else if(j==1){tempHtml.append("<th height=\"30px\" width=\"").append(reportField.getWidth()).append("%\" nowrap=\"nowrap\" align=\"center\" style=\"").append(ReportLabelUtils.TH_FONT).append("border-top:1px solid #F4F4F6;border-left: 1px solid #F4F4F6;border-bottom: 1px solid #F4F4F6;border-right:1px solid #FFFFFF;\">").append(reportField.getDesc()).append("</th>");}else{tempHtml.append("<th height=\"30px\" width=\"").append(reportField.getWidth()).append("%\" nowrap=\"nowrap\" align=\"center\" style=\"").append(ReportLabelUtils.TH_FONT).append("border-bottom: 1px solid #F4F4F6;border-right: 1px solid #FFFFFF;border-top:1px solid #F4F4F6;\">").append(reportField.getDesc()).append("</th>");}keyList.add(reportField.getName()); j++;}tempHtml.append("</tr>");/** -----------------------------标题列end----------------------------------- *//** -----------------------------数据列start----------------------------------- */if(value != null && value.size() > 0){for(Map<String, Object> vMap : value) {tempHtml.append("<tr>"); int keySize = keyList.size(); int flag = 1;for (String key : keyList) {if(keySize==flag){tempHtml.append("<td height=\"30px\" nowrap=\"nowrap\" align=\"center\" style=\"").append(ReportLabelUtils.TD_FONT).append("border-left:1px solid #F4F4F6; border-bottom:1px solid #F4F4F6;border-right:1px solid #F4F4F6;\">").append(vMap.get(key)).append("</td>");}else{tempHtml.append("<td height=\"30px\" nowrap=\"nowrap\" align=\"center\" style=\"").append(ReportLabelUtils.TD_FONT).append("border-left:1px solid #F4F4F6; border-bottom:1px solid #F4F4F6;\">").append(vMap.get(key)).append("</td>");}flag++;}tempHtml.append("</tr>");}}/** -----------------------------数据列end----------------------------------- */tempHtml.append("</tbody></table></div>");return tempHtml;}/** 个性化数据-横着两列标题排列 reportFieldList:标题行 value:标题行对应的字段对应的值 */public static StringBuffer twoTitleHtml(List<ReportField> reportFieldList, List<Map<String,Object>> value){StringBuffer tempHtml= new StringBuffer();for(Map<String, Object> vMap : value){tempHtml.append("<div class=\"table_box_b\">");tempHtml.append("<table width=\"100%\" cellspacing=\"0\" style=\"border-right:1px solid #cdd3e0; border-top:1px solid #cdd3e0; \">");tempHtml.append("<tbody>");int flag = 1;for(ReportField reportField : reportFieldList){if(flag == 1 && flag == reportFieldList.size()){tempHtml.append("<tr>");tempHtml.append("<th height=\"30px\" width=\"25%\" nowrap=\"nowrap\" align=\"center\" style=\"background-color:#EAF2FA; color:#000000; font-size:10px;font-weight: 500;border-top:1px solid #F4F4F6;border-bottom: 1px solid #F4F4F6;border-left: 1px solid #F4F4F6;border-right:1px solid #F4F4F6; \">").append(reportField.getDesc()).append("</th>");tempHtml.append("<td height=\"30px\" width=\"25%\" nowrap=\"nowrap\" align=\"left\" style=\"font-size:10px;padding:5px 5px; line-height:12px;border-top:1px solid #F4F4F6;border-bottom: 1px solid #F4F4F6;border-right:1px solid #F4F4F6;\">").append(vMap.get(reportField.getName())).append("</td>");tempHtml.append("</tr>");}else if(flag == 1 || flag == 2){if(flag == 1){tempHtml.append("<tr>");tempHtml.append("<th height=\"30px\" width=\"25%\" nowrap=\"nowrap\" align=\"center\" style=\"background-color:#EAF2FA; color:#000000; font-size:10px;font-weight: 500;border-top:1px solid #F4F4F6;border-bottom: 1px solid #FFFFFF;border-left: 1px solid #F4F4F6;border-right:1px solid #F4F4F6;\">").append(reportField.getDesc()).append("</th>");tempHtml.append("<td height=\"30px\" width=\"25%\" nowrap=\"nowrap\" align=\"left\" style=\"font-size:10px;padding:5px 5px; line-height:12px;border-top:1px solid #F4F4F6;border-bottom: 1px solid #F4F4F6;border-right:1px solid #F4F4F6;\">").append(vMap.get(reportField.getName())).append("</td>");}else{tempHtml.append("<th height=\"30px\" width=\"25%\" nowrap=\"nowrap\" align=\"center\" style=\"background-color:#EAF2FA; color:#000000; font-size:10px;font-weight: 500;border-top:1px solid #F4F4F6;border-bottom: 1px solid #FFFFFF;border-right:1px solid #F4F4F6;\">").append(reportField.getDesc()).append("</th>");tempHtml.append("<td height=\"30px\" width=\"25%\" nowrap=\"nowrap\" align=\"left\" style=\"font-size:10px;padding:5px 5px; line-height:12px;border-top:1px solid #F4F4F6;border-bottom: 1px solid #F4F4F6;border-right:1px solid #F4F4F6;\">").append(vMap.get(reportField.getName())).append("</td>");tempHtml.append("</tr>");}} else{if(flag%2 == 1 && flag == reportFieldList.size()){tempHtml.append("<tr>");tempHtml.append("<th height=\"30px\" width=\"25%\" nowrap=\"nowrap\" align=\"center\" style=\"background-color:#EAF2FA; color:#000000; font-size:10px;font-weight: 500;border-bottom: 1px solid #F4F4F6;border-left: 1px solid #F4F4F6;border-right:1px solid #F4F4F6;\">").append(reportField.getDesc()).append("</th>");tempHtml.append("<td height=\"30px\" nowrap=\"nowrap\" align=\"left\" style=\"font-size:10px;;padding:5px 5px; line-height:12px;border-bottom:1px solid #F4F4F6;border-right:1px solid #F4F4F6;\" colspan=\"3\">").append(vMap.get(reportField.getName())).append("</td>");tempHtml.append("</tr>");}else if(flag%2 == 0){tempHtml.append("<th height=\"30px\" width=\"25%\" nowrap=\"nowrap\" align=\"center\" style=\"background-color:#EAF2FA; color:#000000; font-size:10px;font-weight: 500;border-bottom: 1px solid #FFFFFF;border-right:1px solid #F4F4F6;\">").append(reportField.getDesc()).append("</th>");tempHtml.append("<td height=\"30px\" nowrap=\"nowrap\" align=\"left\" style=\"font-size:10px;;padding:5px 5px; line-height:12px;border-bottom:1px solid #F4F4F6;border-right:1px solid #F4F4F6;\">").append(vMap.get(reportField.getName())).append("</td>");tempHtml.append("</tr>");}else{tempHtml.append("<tr>");tempHtml.append("<th height=\"30px\" width=\"25%\" nowrap=\"nowrap\" align=\"center\" style=\"background-color:#EAF2FA; color:#000000; font-size:10px;font-weight: 500;border-bottom: 1px solid #FFFFFF;border-left: 1px solid #F4F4F6;border-right:1px solid #F4F4F6;\">").append(reportField.getDesc()).append("</th>");tempHtml.append("<td height=\"30px\" width=\"30%\" nowrap=\"nowrap\" align=\"left\" style=\"font-size:10px;;padding:5px 5px; line-height:12px;border-bottom:1px solid #F4F4F6;border-right:1px solid #F4F4F6;\">").append(vMap.get(reportField.getName())).append("</td>");}}flag++;}tempHtml.append("</tbody></table></div>");}return tempHtml;}/** 个性化数据-横着单列标题排列 reportFieldList:标题行 value:标题行对应的字段对应的值 */public static StringBuffer oneTitleHtml(List<ReportField> reportFieldList, List<Map<String,Object>> value){StringBuffer tempHtml= new StringBuffer();for(Map<String, Object> vMap : value){tempHtml.append("<div class=\"table_box_b\">");tempHtml.append("<table width=\"100%\" border=\"0\" cellspacing=\"0\" style=\"border-right:1px solid #cdd3e0; border-top:1px solid #cdd3e0;\">");tempHtml.append("<tbody>");int flag = 1;for(ReportField reportField : reportFieldList){if(flag == 1){tempHtml.append("<tr>");tempHtml.append("<th height=\"30px\" width=\"25%\" nowrap=\"nowrap\" align=\"center\" style=\"").append(ReportLabelUtils.TH_FONT).append("border-top:1px solid #F4F4F6;border-bottom: 1px solid #F4F4F6;border-left: 1px solid #F4F4F6;border-right:1px solid #F4F4F6;\">").append(reportField.getDesc()).append("</th>");tempHtml.append("<td height=\"30px\" width=\"75%\" nowrap=\"nowrap\" align=\"left\" style=\"").append(ReportLabelUtils.TD_FONT).append("border-top:1px solid #F4F4F6;border-bottom:1px solid #F4F4F6;border-right:1px solid #F4F4F6;\">").append(vMap.get(reportField.getName())).append("</td>");tempHtml.append("</tr>");}else{tempHtml.append("<tr>");tempHtml.append("<th height=\"30px\" width=\"25%\" nowrap=\"nowrap\" align=\"center\" style=\"").append(ReportLabelUtils.TH_FONT).append("border-bottom: 1px solid #F4F4F6;border-left: 1px solid #F4F4F6;border-right:1px solid #F4F4F6;\">").append(reportField.getDesc()).append("</th>");tempHtml.append("<td height=\"30px\" width=\"75%\" nowrap=\"nowrap\" align=\"left\" style=\"").append(ReportLabelUtils.TD_FONT).append("border-bottom: 1px solid #F4F4F6;border-right:1px solid #F4F4F6;\">").append(vMap.get(reportField.getName())).append("</td>");tempHtml.append("</tr>");}flag++;}tempHtml.append("</tbody></table></div>");}return tempHtml;}/** 文本内容 content:文本内容;level:标题等级 */public static StringBuffer title(String content, int level){StringBuffer description = new StringBuffer();if(level == 1) {description.append("<br/><div align=\"left\"><img style=\"height:30px;width:520px;\" src=\"").append(forTitle1(content)).append("\"/></div>");}if(level == 2) {description.append("<br/><div align=\"left\"><img style=\"height:30px;width:520px;\" src=\"").append(forTitle2(content)).append("\"/></div>");}if(level == 3) {description.append("<br/><div>");description.append("<img style=\"height: 15px; width:10px\" src=\"").append(ConstantConfig.RISK_IMAGE + "三级标题.jpg").append("\"/>&thinsp; <span style=\"font-weight: 900;color:#005BAB;\">").append(content).append("</span>");description.append("</div>");}return description;}/** 文本内容 content:文本内容;fontSize:字体大小;color:字体颜色;backgroundColor:背景色 */public static StringBuffer description(String content, int fontSize, String color, String backgroundColor, int fontWeight){StringBuffer description = new StringBuffer();
//        description.append("<br/>");description.append("<div align=\"left\" style=\"");if(fontWeight > 0) description.append("font-weight:").append(fontWeight).append(";");if(fontSize > 0) description.append("font-size:").append(fontSize).append("px;");if(StringUtils.isNotBlank(color)) description.append("color:").append(color).append(";");if(StringUtils.isNotBlank(color)) description.append("padding-left:12px;padding-right:12px;padding-bottom:12px;background-color:").append(backgroundColor).append(";");description.append("line-height:").append(1.5).append(";");description.append("letter-spacing:").append(1.2).append("px;");description.append("\">");description.append("&nbsp;&nbsp;");description.append(content).append("</div>");
//        description.append("<br/>");return description;}/*** 加载图片(柱状图/折线图)*/public static StringBuffer getImgOneHtml(String title, List<String> xData, List<Double> yDataList, List<Color> colorList, String yUnit, int type,int width, int height, double SpaceFill, StringBuffer tempHtml) {if (xData == null || xData.size() == 0) return tempHtml;String path = new HistogramChar<>().getChart2(title, xData, yDataList, colorList, yUnit, type,width, height, SpaceFill);tempHtml.append("<div align=\"center\"><img style=\"height:262px;width:700px\" src=\"").append(path).append("\"/></div>");return tempHtml;}/*** 加载图片(扇形图)*/public static StringBuffer getImgPieHtml(List<String> keyList, List<Double> rDataList, Boolean isTitleVisible, String title,Boolean isLegendVisible, RectangleEdge rectangleEdge, int flag, StringBuffer tempHtml) {if (rDataList == null || rDataList.size() == 0) return tempHtml;tempHtml.append("<div align=\"center\">");String path = Pie2Chart.pieChart(keyList, rDataList, isTitleVisible, title, isLegendVisible, rectangleEdge, flag);tempHtml.append("<img style=\"width:700px\" src=\"").append(path).append("\"/>");tempHtml.append("</div>");return tempHtml;}/*** 加图片做pdf首页的底纹* @param srcPdfPath 源文件* @param tagetPdfPath  加工后文件* @param pageNum        页码* @param type           底纹类型:0:客户风险分析报告;1:客群风险分析报告*/public static void addImageWatermark(String srcPdfPath, String tagetPdfPath, int pageNum, int type) {try {// 加载PDF文件PdfReader reader = new PdfReader(srcPdfPath);PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(tagetPdfPath));int total = reader.getNumberOfPages();if(pageNum < 0) pageNum = total + pageNum;// 首页-头部-底纹com.itextpdf.text.Image watermark = com.itextpdf.text.Image.getInstance(ConstantConfig.RISK_IMAGE + "首页-头部-底纹.png");watermark.setAbsolutePosition(-2, 705); // 设置底纹位置watermark.scaleAbsolute(597, 135); // 设置底纹大小// 首页-中间-底纹com.itextpdf.text.Image watermark1 = com.itextpdf.text.Image.getInstance(ConstantConfig.RISK_IMAGE + "首页-中间-底纹.png");watermark1.setAbsolutePosition(-2, 155); // 设置底纹位置watermark1.scaleAbsolute(597, 700); // 设置底纹大小// 文字 - 客群风险分析报告/客户风险分析报告com.itextpdf.text.Image watermark_word1 = com.itextpdf.text.Image.getInstance(ConstantConfig.RISK_IMAGE + (type==0 ? "客户风险分析报告" : "客群风险分析报告") +".png");watermark_word1.setAbsolutePosition(200, 515); // 设置底纹位置watermark_word1.scaleAbsolute(360, 40); // 设置底纹大小// 首页-矩形-底纹com.itextpdf.text.Image watermark_line1 = com.itextpdf.text.Image.getInstance(ConstantConfig.RISK_IMAGE + "首页-矩形-底纹.png");watermark_line1.setAbsolutePosition(200, 485); // 设置底纹位置watermark_line1.scaleAbsolute(360, 2); // 设置底纹大小// 首页-分割线-底纹com.itextpdf.text.Image watermark_line2 = com.itextpdf.text.Image.getInstance(ConstantConfig.RISK_IMAGE + "首页-分割线-底纹.png");watermark_line2.setAbsolutePosition(-2, 153); // 设置底纹位置watermark_line2.scaleAbsolute(597, 1); // 设置底纹大小// 首页-底部-底纹com.itextpdf.text.Image watermark2 = com.itextpdf.text.Image.getInstance(ConstantConfig.RISK_IMAGE + "首页-底部-底纹.png");watermark2.setAbsolutePosition(-2, -2); // 设置底纹位置watermark2.scaleAbsolute(597, 215); // 设置底纹大小// 将底纹添加到首页页面PdfContentByte content = stamper.getOverContent(pageNum);content.addImage(watermark);content.addImage(watermark1);content.addImage(watermark_word1);content.addImage(watermark_line1);content.addImage(watermark_line2);content.addImage(watermark2);// 获取原始PDF内容并添加到新的PDF中PdfImportedPage page = stamper.getImportedPage(reader, pageNum);int rotation = reader.getPageRotation(1);if (rotation == 90 || rotation == 270) {content.addTemplate(page, 0, -1, 1, 0, 0, 610);} else {content.addTemplate(page, 1, 0, 0, 1, 0, 0);}// 关闭PDF文件stamper.close();reader.close();} catch (Exception e) {e.printStackTrace();}}/*** 加图片做pdf页眉* @param pdfFilePath 源文件* @param toFdfPath  加工后文件*/public static void pdf_yemei(String companyName, String pdfFilePath, String toFdfPath) {try {String NoFirstYeMeiImage = baseUrl + companyName + "合并后图片.png";getImageAndChar2(ConstantConfig.RISK_IMAGE + "非首页-页眉.jpg", NoFirstYeMeiImage, companyName);// 加载PDF文件PdfReader reader = new PdfReader(pdfFilePath);PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(toFdfPath));int total = reader.getNumberOfPages();int pageNum = 1;while(pageNum < total) {pageNum++;if (pageNum <= 1) continue;// 非首页-页眉com.itextpdf.text.Image yemei = com.itextpdf.text.Image.getInstance(NoFirstYeMeiImage);yemei.setAbsolutePosition(0, 807); // 设置底纹位置yemei.scaleAbsolute(597, 33); // 设置底纹大小// 将底纹添加到首页页面PdfContentByte content = stamper.getOverContent(pageNum);content.addImage(yemei);// 获取原始PDF内容并添加到新的PDF中PdfImportedPage page = stamper.getImportedPage(reader, pageNum);int rotation = reader.getPageRotation(1);if (rotation == 90 || rotation == 270) {content.addTemplate(page, 0, -1, 1, 0, 0, 610);} else {content.addTemplate(page, 1, 0, 0, 1, 0, 0);}}// 关闭PDF文件stamper.close();reader.close();} catch (Exception e) {e.printStackTrace();}}/** 页眉专用 -- 合并文字和图片,生成一个新的图片 */public static void getImageAndChar2(String srcImagePath, String toImagePath, String content){try {// 读取第一张图片BufferedImage image1 = ImageIO.read(new File(srcImagePath));/** 创建一个新的BufferedImage,其宽度为两张图片的宽度之和,高度为两张图片中较高的那张图片的高度 */BufferedImage mergedImage = new BufferedImage(image1.getWidth(), image1.getHeight(), image1.getType());/** 创建Graphics2D对象以在新的合并图像上绘图 */Graphics2D g2d = mergedImage.createGraphics();g2d.drawImage(image1, 0, 0, null);/** 设置字体和颜色 */Font font = new Font("黑体", Font.BOLD, 70); g2d.setFont(font); g2d.setColor(Color.white);// 在合并图像上绘制汉字String text = content; // 这里可以替换为您想要显示的汉字int x = 270; // 汉字的起始位置,可以根据需要调整int y = 90; // 汉字的垂直位置,可以根据需要调整for (int i = 0; i < text.length(); i++) {char c = text.charAt(i);g2d.drawString(String.valueOf(c), x + i * 70, y); // 在每个汉字之间添加一些间距,可以根据需要调整间距大小}// 释放此图形的上下文以及它使用的所有系统资源g2d.dispose();// 将合并后的图像写入文件ImageIO.write(mergedImage, "png", new File(toImagePath));} catch (Exception e) {e.printStackTrace();}}/** 1级标题专用 -- 合并文字和图片,生成一个新的图片 */public static String forTitle1(String content){String toImagePath = baseUrl + System.currentTimeMillis();try {// 读取第一张图片BufferedImage image1 = ImageIO.read(new File(ConstantConfig.RISK_IMAGE + "一级标题.jpg"));/** 创建一个新的BufferedImage,其宽度为两张图片的宽度之和,高度为两张图片中较高的那张图片的高度 */BufferedImage mergedImage = new BufferedImage(image1.getWidth(), image1.getHeight(), image1.getType());/** 创建Graphics2D对象以在新的合并图像上绘图 */Graphics2D g2d = mergedImage.createGraphics();g2d.drawImage(image1, 0, 0, null);/** 设置字体和颜色 */Font font = new Font("黑体", Font.BOLD, 50); g2d.setFont(font); g2d.setColor(new Color(0, 91, 171));// 在合并图像上绘制汉字String text = content; // 这里可以替换为您想要显示的汉字int x = 240; // 汉字的起始位置,可以根据需要调整int y = 60; // 汉字的垂直位置,可以根据需要调整for (int i = 0; i < text.length(); i++) {char c = text.charAt(i);g2d.drawString(String.valueOf(c), x + i * 55, y); // 在每个汉字之间添加一些间距,可以根据需要调整间距大小}// 释放此图形的上下文以及它使用的所有系统资源g2d.dispose();// 将合并后的图像写入文件ImageIO.write(mergedImage, "jpg", new File(toImagePath));} catch (Exception e) {e.printStackTrace();}return toImagePath;}/** 2级标题专用 -- 合并文字和图片,生成一个新的图片 */public static String forTitle2(String content){String toImagePath = baseUrl + System.currentTimeMillis();try {// 读取第一张图片BufferedImage image1 = ImageIO.read(new File(ConstantConfig.RISK_IMAGE + "二级标题.jpg"));/** 创建一个新的BufferedImage,其宽度为两张图片的宽度之和,高度为两张图片中较高的那张图片的高度 */BufferedImage mergedImage = new BufferedImage(image1.getWidth(), image1.getHeight(), image1.getType());/** 创建Graphics2D对象以在新的合并图像上绘图 */Graphics2D g2d = mergedImage.createGraphics();g2d.drawImage(image1, 0, 0, null);/** 设置字体和颜色 */Font font = new Font("黑体", Font.BOLD, 40); g2d.setFont(font); g2d.setColor(Color.white);// 在合并图像上绘制汉字String text = content; // 这里可以替换为您想要显示的汉字int x = 50; // 汉字的起始位置,可以根据需要调整int y = 50; // 汉字的垂直位置,可以根据需要调整for (int i = 0; i < text.length(); i++) {char c = text.charAt(i);g2d.drawString(String.valueOf(c), x + i * 45, y); // 在每个汉字之间添加一些间距,可以根据需要调整间距大小}// 释放此图形的上下文以及它使用的所有系统资源g2d.dispose();// 将合并后的图像写入文件ImageIO.write(mergedImage, "jpg", new File(toImagePath));} catch (Exception e) {e.printStackTrace();}return toImagePath;}}

实体类

package com.ruoyi.business.bankInterface.controller.risk.model1;import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.business.bankInterface.controller.risk.utils.ReportLabelUtils;
import com.ruoyi.business.bankInterface.controller.risk.utils.ReportUtils2;
import com.ruoyi.business.bankInterface.controller.tax.utils.annotation.ReportFieldAnnotation;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;import java.util.ArrayList;
import java.util.List;/*** 公司治理*/
@Data
public class K3 {@ReportFieldAnnotation(desc = "变更时间", sort = 1)private String bgsj;@ReportFieldAnnotation(desc = "变更事项", sort = 2)private String bgsx;@ReportFieldAnnotation(desc = "变更前", sort = 3)private String bgq;@ReportFieldAnnotation(desc = "变更后", sort = 4)private String bgh;@ReportFieldAnnotation(desc = "风险等级", sort = 5)private String fxdj;private String fxxl;public String getBgq() {if(StringUtils.isNotBlank(bgq)){int start = bgq.indexOf("(");int end = bgq.indexOf(")");if(start > 0 && end > 0){bgq = bgq.substring(0, start)+bgq.substring(end+1);}}return bgq;}public String getBgh() {if(StringUtils.isNotBlank(bgh)){int start = bgh.indexOf("(");int end = bgh.indexOf(")");if(start > 0 && end > 0){bgh = bgh.substring(0, start)+bgh.substring(end+1);}}return bgh;}public String getFxdj() {if(StringUtils.isNotBlank(fxdj)){if("红".equals(fxdj)) fxdj = "严重警告";if("橙".equals(fxdj)) fxdj = "警告";if("黄".equals(fxdj)) fxdj = "关注";if("提示".equals(fxdj)) fxdj = "提示";}return fxdj;}/** 公司治理 */public static StringBuffer getHtml1(List<K3> k3List){StringBuffer k3Html = new StringBuffer();if(k3List == null || k3List.size() == 0){k3Html.append(ReportUtils2.description("监测本公司近一年的注册资本、企业股东或高管、企业法定代表人变更情况,皆未发现有异常更改记录。",  12, ReportLabelUtils.NO_FX_COLOR, ReportLabelUtils.NO_FX_BACKGROUND_COLOR, 0));}else{k3Html.append(ReportUtils2.description("监测本公司近一年的注册资本、企业股东或高管、企业法定代表人变更情况,发现以下风险:",  12, ReportLabelUtils.FX_COLOR, ReportLabelUtils.FX_BACKGROUND_COLOR, 0));ReportUtils2.listHtml(K3.class, JSONArray.toJSONString(changeK3List(k3List)), k3Html, 1, 1);}return k3Html;}/** 经营资质 */public static StringBuffer getHtml2(List<K3> k3List){StringBuffer k3Html = new StringBuffer();k3Html.append(ReportUtils2.title("经营资质", 3));if(k3List == null || k3List.size() == 0){k3Html.append(ReportUtils2.description("本风险监测时段中,未监测到企业存在经营资质风险。",  12, ReportLabelUtils.NO_FX_COLOR, ReportLabelUtils.NO_FX_BACKGROUND_COLOR, 0));}else{StringBuffer text = new StringBuffer();text.append("在本风险监测时段中,监测到"+k3List.size()+"条经营资质风险,其中");if (k3List.stream().filter(item->"R004".equals(item.getFxxl())).count()>0)text.append("企业工商信息变更风险"+k3List.stream().filter(item->"R004".equals(item.getFxxl())).count()+"条。");if (k3List.stream().filter(item->"R005".equals(item.getFxxl())).count()>0)text.append("企业经营期限到期或状态为‘非存续’风险"+k3List.stream().filter(item->"R005".equals(item.getFxxl())).count()+"条。");k3Html.append(ReportUtils2.description(
//                    "本风险监测时段中,监测到企业存在"+k3List.size()+"条经营资质风险,其中企业工商信息变更风险"+
//                    k3List.stream().filter(item ->"R004".equals(item.getFxxl())).count()+"条,企业经营期限到期或状态为‘非存续’风险"+
//                    k3List.stream().filter(item ->"R005".equals(item.getFxxl())).count()+"条。"text.toString(),  12, ReportLabelUtils.FX_COLOR, ReportLabelUtils.FX_BACKGROUND_COLOR, 0));ReportUtils2.listHtml(K3.class, JSONArray.toJSONString(k3List), k3Html, 1, 1);}return k3Html;}/** 剔除数据 */private static List<K3> changeK3List(List<K3> k3List){if(k3List == null || k3List.size() == 0) return k3List;List<K3> k3NewList = new ArrayList<>();for(K3 k3 : k3List){/** 剔除掉企业注册资金非减少的数据 */try {if("企业注册资本抽离".equals(k3.getBgsx()) && Double.valueOf(k3.getBgq()) <= Double.valueOf(k3.getBgh())) continue;k3NewList.add(k3);}catch (Exception e){}}return k3NewList;}}

pdf工具类(根据html生成pdf文件)

package com.ruoyi.business.bankInterface.controller.risk;import com.itextpdf.text.*;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import com.ruoyi.business.bankInterface.controller.tax.utils.DateUtils;
import com.ruoyi.business.bankInterface.domain.utils.AsianFontProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.List;public class PdfUtils {
//    public static final String imgPrefixUrl="/home/cc/data/images/";public static final String imgPrefixUrl="D:\\test\\images\\";private static final Logger logger = LoggerFactory.getLogger(PdfUtils.class);//根据html生成pdf文件
//    public static void htmp2pdf(String creditName, String pdfName,String template){
//        Document document = null;
//        PdfWriter pdfWriter = null;
//        try {
//            document = new Documapi/v1/financial/queryFinancialDataent(PageSize.A4);
//            pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(pdfName));
//            document.open();
//            document.addAuthor("Soren");
//            document.addCreator("Soren");
//            document.addCreationDate();
//            document.addTitle("测试生成pdf");
//            XMLWorkerHelper helper = XMLWorkerHelper.getInstance();
//            //读取模板文件并且写入String中
//            InputStream in = new FileInputStream(template);
//            InputStreamReader isr = new InputStreamReader(in, "utf-8");
//            BufferedReader brd = new BufferedReader(isr);
//            StringBuffer sbf = new StringBuffer();
//            String line = "";
//            while((line=brd.readLine())!=null) {
//                sbf.append(line);
//            }
//            String htmlTemplete = sbf.toString();
//
//            //此处替换特定字符或者拼接html代码的处理
//            htmlTemplete = htmlTemplete.replaceAll("###TITLE###", "长三角征信链企业征信报告");
//            htmlTemplete = htmlTemplete.replaceAll("###INTRODUCE###", "该报告来源:"+creditName);
//            htmlTemplete = htmlTemplete.replaceAll("###date###", "日期:"+DateUtils.convertYMD(new Date()));
            htmlTemplete = htmlTemplete.replaceAll("###date###", "日期:"+UUID.randomUUID().toString());
//            //模板不支持svg格式图片
//            helper.parseXHtml(pdfWriter, document, new ByteArrayInputStream(htmlTemplete.getBytes("UTF-8")), Charset.forName("utf8"), new AsianFontProvider());
//            logger.info("根据html生成pdf文件完成!");
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (DocumentException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }finally {
//            if(document!=null)
//                //如果不关闭则会生成空的破损的pdf
//                document.close();
//        }
//        return;
//    }//根据html生成pdf文件
//    public static void htmppdf(String htmlStr, String pdfName, Boolean isTrue){
//        Document document = null;
//        PdfWriter pdfWriter = null;
//        try {
//            document = new Document(PageSize.A4);
//            pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(pdfName));
//            document.open();
//            document.addAuthor("Soren");
//            document.addCreator("Soren");
//            document.addCreationDate();
//            document.addTitle("测试生成pdf");
//            XMLWorkerHelper helper = XMLWorkerHelper.getInstance();
//            //模板不支持svg格式图片
//            helper.parseXHtml(pdfWriter, document, new ByteArrayInputStream(htmlStr.getBytes("UTF-8")), Charset.forName("utf8"), new AsianFontProvider());
//            logger.info("根据html生成pdf文件完成!");
//            if(isTrue){
//                //先把文件名字更换为临时名字
//                String temName = pdfName.substring(0, pdfName.lastIndexOf(".")) + "temp" + pdfName.substring(pdfName.lastIndexOf("."));//临时文件名
//                renameFile(pdfName, temName);
//                //文件加水印,输出原文件名
//                waterMark(temName,
//                        pdfName);
//            }
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (DocumentException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }finally {
//            if(document!=null)
//                //如果不关闭则会生成空的破损的pdf
//                document.close();
//        }
//        return;
//    }/** 合并pdf文件* @param files 要合并文件数组(绝对路径如{ "e:\\1.pdf", "e:\\2.pdf" ,* "e:\\3.pdf"}),合并的顺序按照数组中的先后顺序,如2.pdf合并在1.pdf后。* @param newfile 合并后新产生的文件绝对路径,如 e:\\temp\\tempNew.pdf,* @return boolean 合并成功返回true;否则,返回false**/public static boolean mergePdfFiles(List<String> files, String newfile) {boolean retValue = false;Document document = null;OutputStream os = null;PdfReader reader1 = null;PdfCopy copy = null;PdfReader reader = null;try {reader1 = new PdfReader(files.get(0));document = new Document(reader1.getPageSize(1));os = new FileOutputStream(newfile);copy = new PdfCopy(document, os);document.open();for (int i = 0; i < files.size(); i++) {reader = new PdfReader(files.get(i));int n = reader.getNumberOfPages();for (int j = 1; j <= n; j++) {document.newPage();PdfImportedPage page = copy.getImportedPage(reader, j);copy.addPage(page);}}retValue = true;} catch (Exception e) {e.printStackTrace();logger.info("合并文件.error:"+e.getMessage());} finally {try {if(reader != null){reader.close();}if(copy != null){copy.close();}if(os != null){os.close();}if(reader1 != null){reader1.close();}if(document != null){document.close();}}catch (Exception e){e.printStackTrace();logger.info(e.getMessage());}}logger.info("合并pdf文件完成!");return retValue;}/** pdf文件是否加密授权(加密文件不可更改。加一层水印,如果加成功,表示不是加密文件)*/public static boolean isEncryption(String oFile) {boolean retValue = false;PdfReader reader = null;PdfStamper stamper = null;try {String waterMarkName = "长三角征信链";reader = new PdfReader(oFile);stamper = new PdfStamper(reader, new FileOutputStream(oFile+"123"));BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",   BaseFont.EMBEDDED);Rectangle pageRect = null;PdfGState gs = new PdfGState();gs.setFillOpacity(0.2f);//控制水印深浅gs.setStrokeOpacity(0.4f);int total = reader.getNumberOfPages() + 1;JLabel label = new JLabel();FontMetrics metrics;int textH = 0;int textW = 0;label.setText(waterMarkName);metrics = label.getFontMetrics(label.getFont());textH = metrics.getHeight();textW = metrics.stringWidth(label.getText());PdfContentByte under;for (int i = 1; i < total; i++) {pageRect = reader.getPageSizeWithRotation(i);under = stamper.getOverContent(i);under.saveState();under.setGState(gs);under.beginText();under.setFontAndSize(base, 15);//控制水印字体大小// 水印文字成30度角倾斜//你可以随心所欲的改你自己想要的角度for (int height = interval + textH; height < pageRect.getHeight();height = height + textH*5) {for (int width = interval + textW; width < pageRect.getWidth() + textW;width = width + textW*2) {under.showTextAligned(Element.ALIGN_LEFT, waterMarkName, width - textW,height - textH, 30);}}// 添加水印文字under.endText();}} catch (Exception e) {retValue = true;}finally {try {//一定不要忘记关闭流if(stamper != null){stamper.close();}if(reader != null){reader.close();}}catch (Exception e){e.printStackTrace();logger.info(e.getMessage());}}return retValue;}/**文件转换为字节数组* @param file* @return*/public static byte[] fileToByteArray(File file) {byte[] imagebs = null;FileInputStream fis = null;ByteArrayOutputStream baos = null;try {//读取输入的文件====文件输入流fis = new FileInputStream(file);//字节数组输出流  在内存中创建一个字节数组缓冲区,所有输出流的数据都会保存在字符数组缓冲区中baos = new ByteArrayOutputStream();int len;byte[] buffer = new byte[1024];//将文件读入到字节数组中while ((len = fis.read(buffer)) != -1) {// 将指定字节数组中从偏移量 off 开始的 len 个字节写入此字节数组输出流。baos.write(buffer, 0, len);}imagebs = baos.toByteArray();//当前输出流的拷贝  拷贝到指定的字节数组中baos.close();fis.close();} catch (Exception e) {e.printStackTrace();}finally {try {//一定不要忘记关闭流if(baos != null){baos.close();}if(fis != null){fis.close();}}catch (Exception e){e.printStackTrace();logger.info(e.getMessage());}}return imagebs;}/**根据byte[] 数组生成文件  (在本地)* @param bfile  字节数组* @param filePath 文件路径*/public static void getFile(byte[] bfile, String filePath) {BufferedOutputStream bos = null;  //带缓冲得文件输出流FileOutputStream fos = null;      //文件输出流File file = null;try {String path = filePath.substring(0,filePath.lastIndexOf(File.separator));File dir = new File(path);if(!dir.exists()){//判断文件目录是否存在dir.mkdirs();}file = new File(filePath);  //文件路径+文件名fos = new FileOutputStream(file);bos = new BufferedOutputStream(fos);bos.write(bfile);} catch (Exception e) {e.printStackTrace();} finally {if (bos != null) {try {bos.close();} catch (Exception e1) {e1.printStackTrace();}}if (fos != null) {try {fos.close();} catch (Exception e1) {e1.printStackTrace();}}}}/*** 给文件重命名*/public static void renameFile(String oldname,String newname){logger.info("重命名文件开始oldname=" + oldname + "&newname=" + newname);if(!oldname.equals(newname)){//新的文件名和以前文件名不同时,才有必要进行重命名File oldfile=new File(oldname);File newfile=new File(newname);if(!oldfile.exists()){logger.info("重命名文件不存在!");return;//重命名文件不存在}if(newfile.exists())//若在该目录下已经有一个文件和新文件名相同,则不允许重命名logger.info(newname+"已经存在!");else{oldfile.renameTo(newfile);}}else{logger.info("新文件名和旧文件名相同...");}}private static int interval = -5;//给pdf加水印public static void waterMark(String inputFile,String outputFile,String userName,String time) {PdfReader reader = null;PdfStamper stamper = null;FileOutputStream outputStream = null;try {String waterMarkName = "长三角";reader = new PdfReader(inputFile);outputStream = new FileOutputStream(outputFile);stamper = new PdfStamper(reader, outputStream);BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",   BaseFont.EMBEDDED);Rectangle pageRect = null;PdfGState gs = new PdfGState();gs.setFillOpacity(0.15f);//控制水印深浅gs.setStrokeOpacity(0.4f);int total = reader.getNumberOfPages() + 1;JLabel label = new JLabel();FontMetrics metrics;int textH = 0;int textW = 0;label.setText(waterMarkName);metrics = label.getFontMetrics(label.getFont());textH = metrics.getHeight();textW = metrics.stringWidth(label.getText());PdfContentByte under;for (int i = 1; i < total; i++) {pageRect = reader.getPageSizeWithRotation(i);under = stamper.getOverContent(i);under.saveState();under.setGState(gs);under.beginText();under.setFontAndSize(base, 18);//控制水印字体大小// 水印文字成30度角倾斜// 获取每一页的高度、宽度float pageHeight = pageRect.getHeight();float pageWidth = pageRect.getWidth();// 根据纸张大小多次添加, 水印文字成30度角倾斜for (int height = interval + textH; height < pageHeight; height = height + textH * 8) {for (int width = interval + textW; width < pageWidth + textW; width = width + textW * 3) {// 将分段的字段进行输出编写under.setFontAndSize(base, 18);//控制水印字体大小under.showTextAligned(Element.ALIGN_LEFT, waterMarkName, width - textW, height -textH , 30);under.setFontAndSize(base, 12);//控制水印字体大小under.showTextAligned(Element.ALIGN_LEFT, userName, width - textW+12, height -textH*2 , 30);under.setFontAndSize(base, 12);//控制水印字体大小under.showTextAligned(Element.ALIGN_LEFT, time, width - textW+24, height -textH*3 , 30);}}under.endText();}} catch (Exception e) {e.printStackTrace();}finally {try {if(stamper != null){stamper.close();}if(outputStream != null){outputStream.close();}if(reader != null){reader.close();}}catch (Exception e){e.printStackTrace();logger.info(e.getMessage());}}}public static void main(String[] args) {
//        renameFile("D:\\test\\123.pdf","D:\\test\\模板.pdf");//根据html生成pdf
//        PdfUtils.htmp2pdf("苏信", "D:\\test\\1234.pdf","D:\\test\\template(3).html");
//        PdfUtils.htmp2pdf("<br/>苏州征信、江苏省南京分融服务信用平台、安徽征信", "D:\\test\\123.pdf","D:\\test\\first.html");
//        D:\test\template(1).html//合并pdf文件
//        List<String> files = new ArrayList<>();
//        files.add("D:\\test\\123.pdf");
//        files.add("D:\\test\\1234.pdf");
//        String savepath = "D:\\test\\xiaomingnew.pdf";
//        Boolean bool = mergePdfFiles(files, savepath);
//        //给pdf加水印
//        waterMark("D:\\test\\xiaomingnew.pdf", "D:\\test\\666.pdf", "xoads", "2021");
//        String pdfName = "/d:cdc.cdsc.csdcs";
//        String begin = pdfName.substring(0, pdfName.lastIndexOf("."));
//        String endStr = pdfName.substring(pdfName.lastIndexOf("."));
//        System.out.println("begin="+begin);System.out.println("endStr=" + "xxx.pdf".substring(0, "xxx.pdf".length() - 4));//        try {
//            //合并pdf文件
//            Boolean bool = PdfUtils.mergePdfFiles(Arrays.asList("D:\\test\\666.pdf","D:\\test\\666.pdf"), "D:\\test\\777.pdf");
//        }catch (Exception e){
//        }}//解压文件public static void zipToFile(String sourceFile, String toFolder) throws Exception {InputStream inputStream = null;OutputStream outputStream = null;ZipFile zfile = null;try {String toDisk = toFolder;//接收解压后的存放路径zfile = new ZipFile(sourceFile);//连接待解压文件logger.info("要解压的文件是:"+ zfile.getName());Enumeration zList = zfile.entries();//得到zip包里的所有元素ZipEntry ze = null;byte[] buf = new byte[1024];while (zList.hasMoreElements()) {ze = (ZipEntry) zList.nextElement();if (ze.isDirectory()) {logger.info("打开zip文件里的文件夹:"+ ze.getName() +"skipped...");continue;}logger.info("zip包里的文件:"+ ze.getName() +" "+"大小为:" + ze.getSize() +"KB");//以ZipEntry为参数得到一个InputStream,并写到OutputStream中outputStream = new BufferedOutputStream(new FileOutputStream(getRealFileName(toDisk, ze.getName())));inputStream = new BufferedInputStream(zfile.getInputStream(ze));int readLen = 0;while ((readLen = inputStream.read(buf, 0, 1024)) != -1) {outputStream.write(buf, 0, readLen);}inputStream.close();outputStream.close();logger.info("已经解压出:"+ ze.getName());}zfile.close();}catch (Exception e){e.printStackTrace();logger.info(e.getMessage());}finally {try {if(inputStream != null){inputStream.close();}if(outputStream != null){outputStream.close();}if(zfile != null){zfile.close();}}catch (Exception e){e.printStackTrace();logger.info(e.getMessage());}}}private static File getRealFileName(String zippath, String absFileName){String[] dirs = absFileName.split("/", absFileName.length());File ret = new File(zippath);// 创建文件对象if (dirs.length > 1) {for (int i = 0; i < dirs.length - 1; i++) {ret = new File(ret, dirs[i]);}}if (!ret.exists()) {// 检测文件是否存在ret.mkdirs();// 创建此抽象路径名指定的目录}ret = new File(ret, dirs[dirs.length - 1]);// 根据 ret 抽象路径名和 child 路径名字符串创建一个新 File 实例return ret;}//根据html生成pdf文件public static void htmp2pdf(String creditName, String htmlTemplate,String pdfName,boolean hasBackground,boolean isTrue){String path = pdfName.substring(0,pdfName.lastIndexOf(File.separator));File dir = new File(path);if(!dir.exists()){//判断文件目录是否存在dir.mkdirs();}Document document = null;PdfWriter pdfWriter = null;FileOutputStream fileOutputStream = null;ByteArrayInputStream byteArrayInputStream = null;try {document = new Document(PageSize.A4);fileOutputStream = new FileOutputStream(pdfName);pdfWriter = PdfWriter.getInstance(document, fileOutputStream);document.open();document.addAuthor("Soren");document.addCreator("Soren");document.addCreationDate();document.addTitle("测试生成pdf");if(hasBackground){Image image = Image.getInstance(imgPrefixUrl+"background.jpg");/* 设置图片的位置 */image.setAbsolutePosition(0, 80);/* 设置图片的大小 */image.scaleAbsolute(595, 842);document.add(image);}//此处替换特定字符或者拼接html代码的处理htmlTemplate = htmlTemplate.replaceAll("###TITLE###", "长三角报告");htmlTemplate = htmlTemplate.replaceAll("###INTRODUCE###", "该报告来源:"+creditName);htmlTemplate = htmlTemplate.replaceAll("###date###", "日期:"+ DateUtils.convertYMD(new Date()));XMLWorkerHelper helper = XMLWorkerHelper.getInstance();//模板不支持svg格式图片byteArrayInputStream = new ByteArrayInputStream(htmlTemplate.getBytes("UTF-8"));helper.parseXHtml(pdfWriter, document, byteArrayInputStream, Charset.forName("utf8"), new AsianFontProvider());logger.info("pdf文件转换完成!");
//            if(isTrue){
//                //先把文件名字更换为临时名字
//                String begin = pdfName.substring(0, pdfName.lastIndexOf("."));
//                String endStr = pdfName.substring(pdfName.lastIndexOf("."));
//                String temName = begin + "temp" + endStr;
//                renameFile(pdfName, temName);
//                //文件加水印,输出原文件名
//                waterMark(temName,
//                        pdfName);
//            }} catch (FileNotFoundException e) {e.printStackTrace();} catch (DocumentException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {try {if(document!=null){document.close();}if(fileOutputStream!=null){fileOutputStream.close();}if(byteArrayInputStream!=null){byteArrayInputStream.close();}if(pdfWriter!=null){pdfWriter.close();}}catch (Exception e){e.printStackTrace();logger.info(e.getMessage());}}}/*** 删除单个文件** @param fileName*            要删除的文件的文件名* @return 单个文件删除成功返回true,否则返回false*/public static boolean deleteFile(String fileName) {File file = new File(fileName);// 如果文件路径所对应的文件存在,并且是一个文件,则直接删除if (file.exists() && file.isFile()) {if (file.delete()) {logger.info("删除单个文件" + fileName + "成功!");return true;} else {logger.info("删除单个文件" + fileName + "失败!");return false;}} else {logger.info("删除单个文件失败:" + fileName + "不存在!");return false;}}
}

页脚工具类

package com.ruoyi.business.bankInterface.controller.tax.utils.pdf;import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.*;/***  @description 针对页码生成的PDF事件监听*  @author myw*  @date 2023-4-10 20:24:21**/
public class PdfNumPageEventTax extends PdfPageEventHelper {private int total;PdfNumPageEventTax(){}public PdfNumPageEventTax(int num){this.total = num;}@Overridepublic void onEndPage(PdfWriter writer, Document document) {try {if(writer.getPageNumber() ==1) return;// PDF文档内容PdfContentByte pdfContent = writer.getDirectContent();pdfContent.saveState();pdfContent.beginText();int footerFontSize = 10;// 解决页码中文无法显示 或者 显示为乱码的问题// 但是必须引入jar包 itext-asian-5.2.0.jarBaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);Font fontDetail = new Font(baseFont, footerFontSize, Font.NORMAL);pdfContent.setFontAndSize(baseFont, footerFontSize);// 页脚的页码 展示String footerNum = String.format("第%d页 共"+total+"页", writer.getPageNumber());Phrase phrase = new Phrase(footerNum, fontDetail);// 页码的 横轴 坐标 居中float x = ( document.left() + document.right() ) / 2 +10;// 页码的 纵轴 坐标float y = document.bottom(-20) ;// 添加文本内容,进行展示页码ColumnText.showTextAligned(pdfContent, Element.ALIGN_CENTER, phrase, x, y, 0);pdfContent.endText();pdfContent.restoreState();} catch (Exception e) {e.printStackTrace();}}@Overridepublic void onStartPage(PdfWriter writer, Document document) {
//        try {
//            // PDF文档内容
//            PdfContentByte pdfContent = writer.getDirectContent();
//            pdfContent.saveState();
//            pdfContent.beginText();
//            int footerFontSize = 10;
//            // 解决页码中文无法显示 或者 显示为乱码的问题
//            // 但是必须引入jar包 itext-asian-5.2.0.jar
//            BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);
//            Font fontDetail = new Font(baseFont, footerFontSize, Font.NORMAL);
//            pdfContent.setFontAndSize(baseFont, footerFontSize);
//            // 页脚的页码 展示
//            String footerNum = String.format("第%d页,共"+total+"页", writer.getPageNumber());
//            Phrase phrase = new Phrase(footerNum, fontDetail);
//            // 页码的 横轴 坐标 居中
//            float x = ( document.left() + document.right() ) / 2 +10;
//            // 页码的 纵轴 坐标
//            float y = document.bottom(-20) ;
//            // 添加文本内容,进行展示页码
//            ColumnText.showTextAligned(pdfContent, Element.ALIGN_CENTER, phrase, x, y, 0);
//            pdfContent.endText();
//            pdfContent.restoreState();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }super.onStartPage(writer, document);}
}

柱图工具类

package com.ruoyi.business.bankInterface.controller.risk.utils.chart;import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.business.bankInterface.ConstantConfig;
import com.ruoyi.business.bankInterface.controller.risk.utils.ColorEnum;
import org.apache.commons.lang3.StringUtils;
import org.knowm.xchart.BitmapEncoder;
import org.knowm.xchart.CategoryChart;
import org.knowm.xchart.CategoryChartBuilder;
import org.knowm.xchart.CategorySeries;
import org.knowm.xchart.internal.chartpart.Chart;
import org.knowm.xchart.style.Styler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;/*** 柱状图工具类*/
public class HistogramChar <C extends Chart<?, ?>> {private static final Logger logger = LoggerFactory.getLogger(HistogramChar.class);private static String baseUrl = ConstantConfig.IMAGE_TEMP_FOLDER;public static void main(String[] args) throws IOException {List<String> titleList = JSONArray.parseArray("[\"严重警告\", \"警告\", \"关注\", \"提示\"]", String.class);List<Double> yData1 = JSONArray.parseArray("[6.0, 3.0, 1.0, 5.0]", Double.class);//柱状图String zhuImg = new HistogramChar().getChart2("风险等级分布", titleList, yData1, ColorEnum.lColorList2, "%", 0,700, 263, 0.2);logger.info("柱状图" + zhuImg);}public String getChart(String title, List<String> titleList, List<String> xData, List<List<Double>> yDataList, List<Color> colorList, String yUnit, int type,int width, int height, double SpaceFill) {File file = new File(baseUrl);if (!file.exists()){file.mkdirs();}try {String path = baseUrl + titleList.get(0)+ System.currentTimeMillis()+".png";CategoryChart chart = new CategoryChartBuilder().width(width).height(height).theme(Styler.ChartTheme.Matlab).build();chart = chartTitle(chart, title);chart.getStyler().setPlotGridLinesVisible(true);//图表中垂直网格线的可见性chart.getStyler().setPlotGridLinesColor(new Color(184, 200,240));//设置图表中垂直网格线颜色chart.getStyler().setPlotBorderColor(new Color(236, 238,240));//设置图表中绘图区域的边框的颜色chart.getStyler().setLegendPosition(Styler.LegendPosition.OutsideS);//设置图例(Legend)的位置chart.getStyler().setLegendLayout(Styler.LegendLayout.Horizontal);//设置图例(Legend)的排列chart.getStyler().setXAxisLabelRotation(0);//设置X轴的字旋转角度if(xData != null && xData.size() > 6){chart.getStyler().setXAxisLabelRotation(40);//设置X轴的字旋转角度}chart.setYAxisGroupTitle(0, yUnit);//设置Y轴组的标题chart.getStyler().setYAxisGroupPosition(0, Styler.YAxisPosition.Left);//设置Y轴组的显示位置// 设置字体大小chart.getStyler().setLegendFont(new Font("宋体", Font.BOLD, 25));//设置图例的字体样式chart.getStyler().setAxisTitleFont(new Font("宋体", Font.BOLD, 25));//设置坐标轴标题的字体样式chart.getStyler().setLabelsFont(new Font("宋体", Font.BOLD, 25));//设置图表中标签的字体样式chart.getStyler().setLegendBorderColor(Color.white);chart.getStyler().setAvailableSpaceFill(SpaceFill);//设置图表可用空间的填充方式chart.getStyler().setYAxisDecimalPattern("#");//设置Y轴的小数点格式//避免科学计数法chart.getStyler().setChartPadding(10);//设置图表的内边距chart.getStyler().setPlotBorderVisible(true);//设置图表中绘图区域的边框是否可见
//      chart.getStyler().setYAxisMin(0.0);chart.getStyler().setAxisTickMarksColor(Color.white);//设置图表中刻度标记的颜色//柱状图if(type == 0 && titleList != null && titleList.size() > 0){chart.getStyler().setOverlapped(false);//设置图表中数据系列的重叠方式for(int i=0; i < titleList.size(); i++){CategorySeries series1 = chart.addSeries(titleList.get(i), xData, yDataList.get(i));series1.setChartCategorySeriesRenderStyle(CategorySeries.CategorySeriesRenderStyle.Bar);series1.setFillColor(colorList.get(i));series1.setYAxisGroup(0);}}/** 折线图 */if(type == 1 && titleList != null && titleList.size() > 0){chart.getStyler().setOverlapped(true);//设置图表中数据系列的重叠方式for(int i=0; i < titleList.size(); i++){CategorySeries series2 = chart.addSeries(titleList.get(i), xData, yDataList.get(i));series2.setChartCategorySeriesRenderStyle(CategorySeries.CategorySeriesRenderStyle.Line);series2.setYAxisGroup(0);series2.setLineColor(colorList.get(i));series2.setMarkerColor(colorList.get(i));}}BitmapEncoder.saveBitmap(chart, path, BitmapEncoder.BitmapFormat.PNG);return path;}catch (Exception e){e.printStackTrace();}return null;}/**** @param title* @param xDataList       X轴数据列表/线名字列表* @param yDataList   Y轴数据列表* @param colorList   颜色列表* @param yUnit       Y轴单位名字* @param type        图形类型,0:柱状图,1:折线图* @param width       图片宽度* @param height      图片高度* @param SpaceFill* @return*/public String getChart2(String title, List<String> xDataList, List<Double> yDataList, List<Color> colorList, String yUnit, int type,int width, int height, double SpaceFill) {File file = new File(baseUrl);if (!file.exists()){file.mkdirs();}try {String path = baseUrl + xDataList.get(0)+ System.currentTimeMillis()+".png";CategoryChart chart = new CategoryChartBuilder().width(width).height(height).theme(Styler.ChartTheme.Matlab).build();chart = chartTitle(chart, title);/** 标题配置 */chart = chartLegend(chart);/** 图例配置 */chart = chartGridOrBorder(chart);/** 网格或边框配置 */chart = chartXY(chart, xDataList.size() > 6 ? true : false, yUnit);/** 坐标轴相关配置 */chart.getStyler().setAvailableSpaceFill(SpaceFill);//设置图表可用空间的填充方式chart.getStyler().setOverlapped(true);//设置图表中数据系列的重叠方式//柱状图if(type == 0 && xDataList != null && xDataList.size() > 0){for(int i=0; i < xDataList.size(); i++){List<Double> yData = new ArrayList<>();for(int y=0; y < yDataList.size(); y++){if(y==i){yData.add(yDataList.get(i));}else{yData.add(0.0);}}CategorySeries series1 = chart.addSeries(xDataList.get(i), xDataList, yData);series1.setChartCategorySeriesRenderStyle(CategorySeries.CategorySeriesRenderStyle.Bar);series1.setFillColor(colorList.get(i));series1.setYAxisGroup(0);}}BitmapEncoder.saveBitmap(chart, path, BitmapEncoder.BitmapFormat.PNG);return path;}catch (Exception e){e.printStackTrace();}return null;}/** 标题配置 */private CategoryChart chartTitle(CategoryChart chart, String title){if(StringUtils.isNotBlank(title)){chart.setTitle(title);chart.getStyler().setChartTitleFont(new Font("宋体", Font.BOLD, 25));//设置标题字体chart.getStyler().setChartTitleBoxBackgroundColor(new Color(234,242,250));chart.getStyler().setChartTitleBoxVisible(true);chart.getStyler().setChartTitlePadding(15);}return chart;}/** 图例配置 */private CategoryChart chartLegend(CategoryChart chart){chart.getStyler().setLegendPosition(Styler.LegendPosition.OutsideS);//设置图例(Legend)的位置chart.getStyler().setLegendLayout(Styler.LegendLayout.Horizontal);//设置图例(Legend)的排列chart.getStyler().setLegendFont(new Font("宋体", Font.PLAIN, 20));//设置图例的字体样式chart.getStyler().setLegendBorderColor(Color.white);return chart;}/** 网格或边框配置 */private CategoryChart chartGridOrBorder(CategoryChart chart){chart.getStyler().setPlotGridLinesVisible(true);//图表中垂直网格线的可见性chart.getStyler().setPlotGridLinesColor(new Color(184, 200,240));//设置图表中垂直网格线颜色chart.getStyler().setPlotBorderColor(new Color(236, 238,240));//设置图表中绘图区域的边框的颜色chart.getStyler().setPlotBorderVisible(true);//设置图表中绘图区域的边框是否可见chart.getStyler().setChartPadding(10);//设置图表的内边距return chart;}/** X轴相关配置 */private CategoryChart chartXY(CategoryChart chart, Boolean isRotation, String yUnit){chart.getStyler().setAxisTickMarksColor(Color.white);//设置图表中刻度标记的颜色chart.getStyler().setAxisTitleFont(new Font("宋体", Font.PLAIN, 25));//设置坐标轴标题的字体样式chart.getStyler().setAxisTickLabelsFont(new Font("宋体", Font.PLAIN, 20));//设置坐标轴的字体样式/** X轴相关配置 */chart.getStyler().setXAxisLabelRotation(0);//设置X轴的字旋转角度if(isRotation){chart.getStyler().setXAxisLabelRotation(40);//设置X轴的字旋转角度}/** Y轴相关配置 */chart.setYAxisGroupTitle(0, yUnit);//设置Y轴组的标题chart.getStyler().setYAxisGroupPosition(0, Styler.YAxisPosition.Left);//设置Y轴组的显示位置chart.getStyler().setYAxisDecimalPattern("#");//设置Y轴的小数点格式//避免科学计数法
//    chart.getStyler().setYAxisMin(0.6);return chart;}
}

饼图工具类

package com.ruoyi.business.bankInterface.controller.risk.utils.chart;import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.business.bankInterface.ConstantConfig;
import com.ruoyi.business.bankInterface.controller.risk.utils.ColorEnum;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.PieSectionLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.ui.RectangleEdge;import java.awt.*;
import java.io.File;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;public class Pie2Chart {private static String baseUrl = ConstantConfig.IMAGE_TEMP_FOLDER;public static void main(String[] args) throws Exception{List<String> keyList = Arrays.asList("无任何司法异常", "涉及刑事诉讼且为被告人", "涉重大经济纠纷", "涉及经济纠纷", "涉及劳动纠纷","近一年立案数量大于3", "当前存在执行公开信息", "新增查封信息", "失信被执行");List<Double> rDataList = JSONArray.parseArray("[40, 15, 1, 8, 17, 5, 10, 2, 2]", Double.class);Boolean isTitleVisible = true;//标题是否显示String title = "司法风险分布";Boolean isLegendVisible = false;//图例是否显示RectangleEdge rectangleEdge = RectangleEdge.BOTTOM;//图例位置String path = pieChart(keyList, rDataList, isTitleVisible, title, isLegendVisible, rectangleEdge, 1);System.out.println(path);}public static String pieChart(List<String> keyList, List<Double> rDataList, Boolean isTitleVisible, String title,Boolean isLegendVisible, RectangleEdge rectangleEdge, int flag){String path = baseUrl + System.currentTimeMillis()+".png";try {// 创建数据集DefaultPieDataset dataset = new DefaultPieDataset();for(int i=0; i<keyList.size(); i++){dataset.setValue(keyList.get(i), rDataList.get(i));}// 创建饼图JFreeChart chart = ChartFactory.createPieChart(title,  // 图表标题dataset,           // 数据集true,             // 是否显示图例false,false);chart = chartLegend(chart, isLegendVisible, rectangleEdge);chart = chartTitle(chart, isTitleVisible);chart = chartPiePlot(keyList, chart, flag);ChartUtilities.saveChartAsPNG(new File(path), chart, 700, 350);}catch (Exception e){e.printStackTrace();}return path;}private static JFreeChart chartLegend(JFreeChart chart, Boolean isLegendVisible, RectangleEdge rectangleEdge){if(isLegendVisible){/** 图例显示 */Font font = new Font("宋体", Font.PLAIN, 12);chart.getLegend().setItemFont(font);// 设置图例类别字体chart.getLegend().setBorder(0, 0, 0, 0);//设置图例边框chart.getLegend().setMargin(10, 30, 10, 0);//设置图例边框内边框chart.getLegend().setPosition(rectangleEdge);//配置图例位置}else{/** 图例不显示 */chart.getLegend().setVisible(false);}return chart;}/** 标题配置 */private static JFreeChart chartTitle(JFreeChart chart, Boolean isTitleVisible){if(isTitleVisible){/** 标题显示 *///如果不使用Font,中文将显示不出来Font titleFont = new Font("宋体", Font.BOLD, 25);chart.getTitle().setFont(titleFont); // 设置标题字体chart.getTitle().setMargin(10, 30, 10, 0);chart.getTitle().setBackgroundPaint(Color.WHITE);}return chart;}/** PiePlot配置 */private static JFreeChart chartPiePlot(List<String> keyList, JFreeChart chart, int flag){PiePlot piePlot = (PiePlot)chart.getPlot();for(int i=0; i<keyList.size(); i++){piePlot.setSectionPaint(keyList.get(i), ColorEnum.zhColorList.get(i));}piePlot.setBackgroundPaint(Color.WHITE);// 例如,设置为白色背景piePlot.setShadowPaint(Color.WHITE);//饼图的阴影颜色piePlot.setOutlinePaint(Color.WHITE);//数据区的边界线条颜色if(flag == 0){/** 展示key+百分比 */PieSectionLabelGenerator labelGenerator = new StandardPieSectionLabelGenerator("{0}\n{2}", // 标签格式,其中 {0} 是扇区键,{1} 是扇区值,{2} 是百分比new DecimalFormat("0"), // 用于格式化数值的 DecimalFormat 对象new DecimalFormat("0%") // 用于格式化百分比的 DecimalFormat 对象);piePlot.setLabelGenerator(labelGenerator);}else if(flag == 1){/** 展示百分比 */PieSectionLabelGenerator labelGenerator = new StandardPieSectionLabelGenerator("{2}", // 标签格式,其中 {0} 是扇区键,{1} 是扇区值,{2} 是百分比new DecimalFormat("0"), // 用于格式化数值的 DecimalFormat 对象new DecimalFormat("0%") // 用于格式化百分比的 DecimalFormat 对象);piePlot.setLabelGenerator(labelGenerator);}else if(flag == 2){/** 展示百分比 */PieSectionLabelGenerator labelGenerator = new StandardPieSectionLabelGenerator("{0}\n企业占比{1}%", // 标签格式,其中 {0} 是扇区键,{1} 是扇区值,{2} 是百分比new DecimalFormat("0"), // 用于格式化数值的 DecimalFormat 对象new DecimalFormat("0%")// 用于格式化数值的 DecimalFormat 对象);piePlot.setLabelGenerator(labelGenerator);}else{/** 不展示内容 */piePlot.setLabelGenerator(null);}/** 配置标签 */Font font = new Font("宋体", Font.PLAIN, 12);piePlot.setLabelFont(font);/** 分类标签的字体 */piePlot.setIgnoreNullValues(true);//忽略空值piePlot.setLegendItemShape(new Rectangle(10, 10));piePlot.setLabelOutlinePaint(Color.WHITE);/** 分类标签的边框颜色 */piePlot.setLabelShadowPaint(Color.WHITE);/** 分类标签的阴影颜色 */piePlot.setLabelBackgroundPaint(Color.WHITE);/** 分类标签的背景颜色 */piePlot.setLabelLinkStroke(new BasicStroke(0.5F)); //piePlot.setLabelLinkPaint(Color.lightGray);piePlot.setLabelGap(0.01);// 调整标签与饼图边缘的间隙,以便更好地对齐return chart;}
}package com.ruoyi.business.bankInterface.controller.risk.utils.chart;import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.business.bankInterface.ConstantConfig;
import com.ruoyi.business.bankInterface.controller.risk.utils.ColorEnum;
import org.apache.commons.lang3.StringUtils;
import org.knowm.xchart.BitmapEncoder;
import org.knowm.xchart.PieChart;
import org.knowm.xchart.PieChartBuilder;
import org.knowm.xchart.PieSeries;
import org.knowm.xchart.internal.chartpart.Chart;
import org.knowm.xchart.style.PieStyler;
import org.knowm.xchart.style.Styler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;public class PieChar <C extends Chart<?, ?>> {private static final Logger logger = LoggerFactory.getLogger(PieChar.class);protected static String baseUrl = ConstantConfig.IMAGE_TEMP_FOLDER;public static void main(String[] args) throws IOException {List<String> titleList = Arrays.asList("无任何司法异常", "涉及刑事诉讼且为被告人", "涉重大经济纠纷", "涉及经济纠纷", "涉及劳动纠纷","近一年立案数量大于3", "当前存在执行公开信息", "新增查封信息", "失信被执行");List<Double> rDataList = JSONArray.parseArray("[40, 15, 1, 8, 17, 5, 10, 2, 2]", Double.class);String hImg = new PieChar().getChart("司法风险分布",titleList, rDataList, ColorEnum.zhColorList, false, Styler.LegendPosition.OutsideE, Styler.LegendLayout.Vertical);logger.info("环形图" + hImg);}/*** 饼状图/环图* @param title* @param titleList 模块名* @param rData 数据量* @param colorList 模块的颜色集合* @param legendVisible 图例是否可见,true:可见,false:不可见* @param legendPosition 设置图例(Legend)的位置  OutsideE:左侧 OutsideS:* @param legendLayout 设置图例(Legend)的排列 Vertical:竖着排列 Horizontal:横着排列* @return*/public String getChart(String title, List<String> titleList, List<Double> rData, List<Color> colorList,Boolean legendVisible, Styler.LegendPosition legendPosition, Styler.LegendLayout legendLayout){File file = new File(baseUrl);if (!file.exists()){file.mkdirs();}try {String path = baseUrl + titleList.get(0)+ System.currentTimeMillis()+".png";PieChart chart = new PieChartBuilder().width(1200).height(450).theme(Styler.ChartTheme.Matlab).build();chart = chartTitle(chart, title);/** 配置图例:是否可见、位置、排列*/chart = legendVisible(chart, legendVisible, legendPosition, legendLayout);chart = chartLabels(chart, true);chart.getStyler().setSliceBorderWidth(0.3);//设置饼图中每个扇区(Slice)的边框宽度chart.getStyler().setSliceBorderWidth(2);//环形图各个块的间距chart.getStyler().setDefaultSeriesRenderStyle(PieSeries.PieSeriesRenderStyle.Pie);//设置是环还是扇形chart.getStyler().setLabelType(PieStyler.LabelType.Percentage);//展示哪些内容chart.getStyler().setPlotContentSize(0.8);//设置饼图绘图内容的大小chart.getStyler().setSumVisible(false);chart.getStyler().setPlotBorderVisible(false);//设置饼图绘图边框的可见性for(int i=0; i < titleList.size(); i++){PieSeries series =chart.addSeries(titleList.get(i), rData.get(i));series.setFillColor(colorList.get(i));
//        series.setShowInLegend(true);}BitmapEncoder.saveBitmap(chart, path, BitmapEncoder.BitmapFormat.PNG);return path;}catch (Exception e){e.printStackTrace();}return null;}private PieChart legendVisible(PieChart chart, Boolean legendVisible, Styler.LegendPosition legendPosition, Styler.LegendLayout legendLayout){
//      chart.getStyler().setLegendPadding(100);//设置图例(Legend)的内边距
//      chart.getStyler().setLegendSeriesLineLength(200);//设置图例系列线条的长度chart.getStyler().setLegendBorderColor(Color.white);//设置图例边框颜色:白色chart.getStyler().setLegendVisible(false);//默认图例不可见if(legendVisible){chart.getStyler().setLegendVisible(true);//图例可见chart.getStyler().setLegendPosition(legendPosition);//设置图例(Legend)的位置chart.getStyler().setLegendLayout(legendLayout);//设置图例(Legend)的排列chart.getStyler().setLegendFont(new Font("宋体", Font.PLAIN, 12));//设置图例的字体样式}return chart;}private PieChart chartTitle(PieChart chart, String title){if(StringUtils.isNotBlank(title)){chart.setTitle(title);chart.getStyler().setChartTitleFont(new Font("宋体", Font.BOLD, 25));//设置标题字体chart.getStyler().setChartTitleBoxBackgroundColor(new Color(234,242,250));chart.getStyler().setChartTitleBoxVisible(true);chart.getStyler().setChartTitlePadding(15);}return chart;}private PieChart chartLabels(PieChart chart, Boolean labelsVisible){chart.getStyler().setLabelsDistance(0.8);chart.getStyler().setLabelsVisible(false);//设置饼图中每个扇区(Slice)的标签是否可见if(labelsVisible){chart.getStyler().setLabelsFont(new Font("宋体", Font.CENTER_BASELINE, 12));chart.getStyler().setLabelsFontColor(Color.black);chart.getStyler().setLabelsVisible(true);//设置饼图中每个扇区(Slice)的标签是否可见chart.getStyler().setLabelsFontColorAutomaticEnabled(true);//设置成false的时候,有个白色的图列字变成了指定的颜色chart.getStyler().setLabelsFontColorAutomaticLight(Color.black);chart.getStyler().setLabelsFontColorAutomaticDark(Color.orange);chart.getStyler().setForceAllLabelsVisible(true);//控制饼图中所有扇区的标签是否必须可见}return chart;}
}

http://www.mrgr.cn/p/53815626

相关文章

检测硬盘读写速度

1. 在左下角搜索框输入“cmd”,并点击“以管理员身份运行”。 2. 输入命令“winsat disk -drive 盘符”,并点击“回车”。从上到下,分别是:✬ 随机读取速度: 179.36MB/s✬ 顺序读取速度: 414.64MB/s✬ 顺序写入速度: 208.79MB/s原文出处 https://baijiahao.baidu.com/s?i…

Web3技术解析:区块链在去中心化应用中的角色

引言 在过去几年中&#xff0c;Web3技术已经成为了互联网领域的一个热门话题。作为区块链技术的延伸&#xff0c;Web3不仅仅是数字货币的代名词&#xff0c;更是一个能够为各种应用提供去中心化解决方案的强大工具。本文将深入探讨区块链在Web3去中心化应用中的关键角色&#…

实验七 智能手机互联网程序设计(微信程序方向)实验报告

请编写一个用户登录界面&#xff0c;提示输入账号和密码进行登录&#xff0c;要求在输入后登陆框显示为绿色&#xff1b; 二、实验步骤与结果&#xff08;给出对应的代码或运行结果截图&#xff09; index.wxml <view class"content"> <view class"a…

02. x86处理器运行方式

【CPU指令】 CPU控制器通过读取存储器中的指令确定要执行的功能,CPU运行需要不停的读取指令,计算机启动后CPU会从固定地址处开始读取指令,首先读取 NOR Flash 存储器中的固件,固件执行完毕后引导操作系统执行。 指令是一个二进制数据,主要由如下两部分组成:1.操作码,设置…

hadoop安装记录

目录 零、版本说明一、环境准备1.1.规划1.2.准备 二、安装配置hadoop 三、启动 零、版本说明 centos [rootnode1 ~]# cat /etc/redhat-release CentOS Linux release 7.9.2009 (Core)jdk [rootnode1 ~]# java -version java version "1.8.0_311" Java(TM) SE Run…

Hadoop集群模式的搭建之二:配置Hadoop平台基础环境

在进行Hadoop集群的安装部署之前,需要对虚拟机进行一些基础环境配置。例如,为了在集群中识别主机,需要配置主机和IP地址的映射关系;为了使用Web相关的服务和访问连接,需要在集群中关闭并禁用防火墙功能;为了减少节点之间访问时人工输入用户密码,需要配置SSH免密登录;为…

【嵌入式】Arduino IDE + ESP32开发环境配置

一 背景说明 最近想捣鼓一下ESP32的集成芯片&#xff0c;比较了一下&#xff0c;选择Arduino IDE并添加ESP32支持库的方式来开发&#xff0c;下面记录一下安装过程以及安装过程中遇到的坑。 二 下载准备 【1】Arduino IDE ESP32支持一键安装包&#xff08;非常推荐&#xff0…

iOS - 多线程-读写安全

文章目录 iOS - 多线程-读写安全1. 多读单写1.1 场景1.2 实现方案1.2.1 pthread_rwlock&#xff1a;读写锁1.2.1.1 示例 1.2.2 dispatch_barrier_async&#xff1a;异步栅栏调用1.2.2.1 示例 iOS - 多线程-读写安全 假设有一个文件&#xff0c;A线程进行读取操作&#xff0c;B…

ICESat-2 从ATL08中获取ATL03分类结果

ICESat-2 ATL03数据和ATL08数据的分段距离不一致,ATL08在ATL03的基础上重新分段,并对分段内的数据做处理得到一系列的结果,详情见数据字典: ATL08 Product Data Dictionary (nsidc.org) ATL08使用DRAGANN算法对ATL03数据做了去噪处理,并使用分类算法对每个光子进行分类标志…

【C++打怪之路Lv3】-- 类和对象(上)

&#x1f308; 个人主页&#xff1a;白子寰 &#x1f525; 分类专栏&#xff1a;C打怪之路&#xff0c;python从入门到精通&#xff0c;数据结构&#xff0c;C语言&#xff0c;C语言题集&#x1f448; 希望得到您的订阅和支持~ &#x1f4a1; 坚持创作博文(平均质量分82)&#…

Fastbin attackDouble free和Unsortbin leak的综合使用

Fastbin attack&&Double free和Unsortbin leak的综合使用✅ 今天做一个综合题目,包括利用Fastbin attack实现多指针指向一个地址,以及利用Unsortbin leak泄露libc基地址和修改__malloc_hook地址为one_gadget 题目是buuctf上面的一道题目,题目链接 https://buuoj.cn/…

C语言Linux vim shell命令

无论是在插入模式或者是其他模式下对于文件的修改都是对于内存缓冲区进行修改&#xff0c;只有当点击w进行保存以后才会将数据写入到一个新的文件中的&#xff0c;将源文件删除&#xff0c;并且新文件改为文件的名字 1. actionmotion dG删到文件尾 ggdG先到开头再删除到末尾…

Java中的接口

package day38; ​ public interface Test1 {void say();int add(int a,int b); ​ } ​ package day38; ​ public interface Test2 {void printhelllo();double add(double a,double b); } ​ package day38; ​ public class Implementall implements Test1,Test2 {Overri…

AJAX——案例

1.商品分类 需求&#xff1a;尽可能同时展示所有商品分类到页面上 步骤&#xff1a; 获取所有的一级分类数据遍历id&#xff0c;创建获取二级分类请求合并所有二级分类Promise对象等待同时成功后&#xff0c;渲染页面 index.html代码 <!DOCTYPE html> <html lang&qu…

Java web应用性能分析之【sysbench基准测试】

Java web应用性能分析之【CPU飙高分析之MySQL】-CSDN博客 Java web应用性能分析之【Linux服务器性能监控分析概叙】-CSDN博客 Java web应用性能分析概叙-CSDN博客 Java web应用性能分析之【基准测试】-CSDN博客 上面基本科普了一下基准测试&#xff0c;这里我们将从sysbench…

gpu机器没有开启ipv6

参考: https://blog.csdn.net/asdfaa/article/details/137884414检查系统是否支持 IPv6,查看被禁用了 在启用 IPv6 之前,首先要确保您的系统支持 IPv6。要检查内核是否启用了 IPv6,可以运行以下命令: cat /proc/sys/net/ipv6/conf/all/disable_ipv6 如果返回的结果为 0,…

TCP相关问题总结

文章目录 TCP连接建立过程1. TCP三次握手2. TCP四次挥手3. TCP为什么是三次握手4. TCP为什么是四次挥手 TCP流量控制TCP拥塞控制1. 为什么需要拥塞控制2. 控制手段 TCP连接建立过程中出现丢包 TCP连接建立过程 1. TCP三次握手 首先client端发出连接请求&#xff0c;并且请求同…

Git和Github绑定

天行健&#xff0c;君子以自强不息&#xff1b;地势坤&#xff0c;君子以厚德载物。 每个人都有惰性&#xff0c;但不断学习是好好生活的根本&#xff0c;共勉&#xff01; 文章均为学习整理笔记&#xff0c;分享记录为主&#xff0c;如有错误请指正&#xff0c;共同学习进步。…

11 c++版本的贪吃蛇

前言 呵呵 这大概是 大学里面的 c 贪吃蛇了吧 有一些 面向对象的理解, 但是不多 最近 因为想要 在单片机上面移植一下 贪吃蛇, 所以 重新拿出了一下 这份代码 然后 将它更新为 c 版本, 还是 用了一些时间 这里 具体的实现 就不赘述, 仅仅是 发一下代码 以及 具体的使用…