当前位置: 首页 > news >正文

springboot生成PDF,并且添加水印

/***  导出调查问卷*/@ApiLog("导出调查问卷")@PostMapping("/print/{id}")@ApiOperationSupport(order = 23)@ApiOperation(value = "导出报告", notes = "导出报告")public void print(@PathVariable Long id, HttpServletResponse response) {BackendGradeEntity record = backendGradeService.getById(id);//如果record为空,直接返回错误信息if(record == null){throw new RuntimeException("未找到该记录");}Map<String, Object> values = new HashMap<>();Field[] fields = BackendGradeEntity.class.getDeclaredFields();//通过反射拿到对象的属性名并且赋值给mapfor (Field field : fields) {field.setAccessible(true);try {Object value = field.get(record);values.put(field.getName(), value);} catch (IllegalAccessException e) {// 处理访问异常e.printStackTrace();}}//通过用户id查询用户信息BackendUserinformationEntity user = backendUserinformationService.getById(record.getUserId());//获取用户性别Integer sex = null;if (user!= null){sex = user.getSex();}//添加用户性别values.put("sex", sex==1?"男":"女");String totalTime = values.get("totalTime").toString();double timeInSeconds = Double.parseDouble(totalTime) * 60; // 将分钟转换为秒int minutes = (int) timeInSeconds / 60;int seconds = (int) timeInSeconds % 60;String formattedTime = minutes + "分钟" + seconds + "秒";values.put("totalTime", formattedTime);//增加打印日期为当前日期values.put("printDate", DateUtil.format(LocalDate.now(), "yyyy年MM月dd日"));//修改map中的startTime 和 endTime 格式由2023-09-19T15:34:25  为 2023-09-19 15:34:25String startTime = values.get("startTime").toString().replace("T", " ");String endTime = values.get("endTime").toString().replace("T", " ");values.put("startTime", startTime);values.put("endTime", endTime);//将 正确率 错误率 未答题率 乘100再填回,因为数据存的是小数values.put("errorRate", Double.parseDouble(values.get("errorRate").toString())*100);values.put("accuracy", Double.parseDouble(values.get("accuracy").toString())*100);values.put("unansweredRate", Double.parseDouble(values.get("unansweredRate").toString())*100);String fileName = null;String tplName = null;if(true){fileName = "报告";tplName = "intuitionReport.ftl";}fileName = fileName + "-" + (values.get("userName")==null?UUID.randomUUID():values.get("userName"));String file = printer.print(values, tplName, fileName);//下载文件InputStream inStream = null;try {inStream = new FileInputStream(file);response.reset();response.setContentType("application/pdf");response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(new File(file).getName(), "UTF-8"));response.setCharacterEncoding("UTF-8");IoUtil.copy(inStream, response.getOutputStream());} catch (Exception e) {e.printStackTrace();} finally {if (inStream != null) {try { inStream.close(); } catch (IOException e) { e.printStackTrace(); }}}}/***  批量导出报告*/@ApiLog("批量导出报告")@PostMapping("/print/batch")@ApiOperationSupport(order = 23)@ApiOperation(value = "批量导出", notes = "批量导出报告")public void printBatch(@RequestParam String ids, HttpServletResponse response) {List<BackendGradeEntity> records = backendGradeService.listByIds(Func.toLongList(ids));String uuid = UUID.randomUUID().toString();int size = records.size();DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");String zipName = LocalDateTime.now().format(formatter)+"-"+size+"records.zip";Path zipPath = Paths.get(pathProperties.getPdf(), zipName);File zipFile = zipPath.toFile();if(zipFile.exists()){zipFile.delete();//如果文件存在则先删除旧的文件}List<File> files = null;try{if(!zipFile.getParentFile().exists()){Files.createDirectories(zipPath.getParent());}files = records.stream()
//				.filter(j -> j.getStatus() == 4).map(record -> {Map<String, Object> values = new HashMap<>();Field[] fields = BackendGradeEntity.class.getDeclaredFields();//通过反射拿到对象的属性名并且赋值给mapfor (Field field : fields) {field.setAccessible(true);try {Object value = field.get(record);values.put(field.getName(), value);} catch (IllegalAccessException e) {// 处理访问异常e.printStackTrace();}}//通过用户id查询用户信息BackendUserinformationEntity user = backendUserinformationService.getById(record.getUserId());//获取用户性别Integer sex = null;if (user!= null){sex = user.getSex();}//添加用户性别values.put("sex", sex==1?"男":"女");//处理总时长String totalTime = values.get("totalTime").toString();double timeInSeconds = Double.parseDouble(totalTime) * 60; // 将分钟转换为秒int minutes = (int) timeInSeconds / 60;int seconds = (int) timeInSeconds % 60;String formattedTime = minutes + "分钟" + seconds + "秒";values.put("totalTime", formattedTime);//增加打印日期为当前日期values.put("printDate", DateUtil.format(LocalDate.now(), "yyyy年MM月dd日"));//修改map中的startTime 和 endTime 格式由2023-09-19T15:34:25  为 2023-09-19 15:34:25String startTime = values.get("startTime").toString().replace("T", " ");String endTime = values.get("endTime").toString().replace("T", " ");values.put("startTime", startTime);values.put("endTime", endTime);//将 正确率 错误率 未答题率 乘100再填回,因为数据存的是小数values.put("errorRate", Double.parseDouble(values.get("errorRate").toString())*100);values.put("accuracy", Double.parseDouble(values.get("accuracy").toString())*100);values.put("unansweredRate", Double.parseDouble(values.get("unansweredRate").toString())*100);String fileName = null;String tplName = null;if(true){fileName = "报告";tplName = "intuitionReport.ftl";}fileName = fileName + "-" + (values.get("userName")==null?UUID.randomUUID():values.get("userName")+UUID.randomUUID().toString());String f = printer.print(values, tplName, uuid+"/"+fileName);return new File(f);}).collect(Collectors.toList());//			Path tempDir = Files.createTempDirectory("temp");
//			List<File> copiedFiles = new ArrayList<>();
//			for (File file : files) {
//				Path source = Paths.get(file.getPath());
//				Path destination = tempDir.resolve(file.getName());
//				Files.copy(source, destination);
//				copiedFiles.add(destination.toFile());
//			}
//			// 在这里调用添加水印的方法
//			PDFWatermarkExample.addWatermarkExample(copiedFiles);
//
//			files = copiedFiles;
//			try {
//				// 删除临时文件夹及其所有文件
//				FileUtils.deleteDirectory(tempDir.toFile());
//			} catch (IOException e) {
//				// 处理删除错误
//				e.printStackTrace();
//			}ZipTool.zipFile(files, zipPath.toFile().getAbsolutePath());}catch(Exception e){e.printStackTrace();}finally {if(files != null){for(File f : files){if(f.exists()) f.delete();}}Path dirPath = Paths.get(pathProperties.getPdf(), uuid);if(dirPath.toFile().exists()){dirPath.toFile().delete();}}//下载文件InputStream inStream = null;try {if(!zipFile.exists()){return ;}inStream = new FileInputStream(zipFile);response.reset();response.setContentType("application/zip");response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(zipFile.getName(), "UTF-8"));response.setCharacterEncoding("UTF-8");IoUtil.copy(inStream, response.getOutputStream());} catch (Exception e) {e.printStackTrace();} finally {if (inStream != null) {try { inStream.close(); } catch (IOException e) { e.printStackTrace(); }}}//这里删除临时文件夹内的压缩包,因为存着也没什么用浪费空间//通过zipPath获取绝对路径String absolutePath = zipPath.toFile().getAbsolutePath();//删除absolutepath文件夹,以及所有文件deleteDirectoryRecursively(new File(absolutePath));}

下面是加水印

package org.springblade.common.tool;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;import java.io.File;
import java.io.FileOutputStream;
import java.util.List;public class PDFWatermarkExample {public static void addWatermark(String inputFile, String outputFile, String watermarkText) {try {PdfReader reader = new PdfReader(inputFile);PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile));int numberOfPages = reader.getNumberOfPages();for (int i = 1; i <= numberOfPages; i++) {PdfContentByte content = stamper.getUnderContent(i);PdfGState gs = new PdfGState();gs.setFillOpacity(0.5f); // 设置水印透明度content.setGState(gs);ColumnText.showTextAligned(content,Element.ALIGN_CENTER,new Phrase(watermarkText, new Font(Font.FontFamily.HELVETICA, 40)),reader.getPageSizeWithRotation(i).getWidth() / 2,reader.getPageSizeWithRotation(i).getHeight() / 2,45);}stamper.close();reader.close();} catch (Exception e) {e.printStackTrace();}}public static void addWatermarkMulti(String inputFile, String outputFile, String watermarkText) {try {PdfReader reader = new PdfReader(inputFile);PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile));int numberOfPages = reader.getNumberOfPages();for (int i = 1; i <= numberOfPages; i++) {PdfContentByte content = stamper.getOverContent(i);PdfGState gs = new PdfGState();gs.setFillOpacity(0.05f); // 设置水印透明度content.setGState(gs);Rectangle pageSize = reader.getPageSizeWithRotation(i);float pageWidth = pageSize.getWidth();float pageHeight = pageSize.getHeight();// 设置水印间隔float xInterval = 200; // X轴间隔float yInterval = 50; // Y轴间隔// 计算水印个数int xCount = (int) Math.ceil(pageWidth / xInterval);int yCount = (int) Math.ceil(pageHeight / yInterval);// 平铺水印for (int x = 0; x < xCount; x++) {for (int y = 0; y < yCount; y++) {float xPosition = x * xInterval;float yPosition = y * yInterval;ColumnText.showTextAligned(content,Element.ALIGN_CENTER,new Phrase(watermarkText, new Font(Font.FontFamily.HELVETICA, 40)),xPosition,yPosition,0);}}}stamper.close();reader.close();} catch (Exception e) {e.printStackTrace();}}public static void addWatermarkExample(List<File> files) {// 在这里编写添加水印的代码逻辑,使用上面提到的添加水印的示例代码for (File file : files) {addWatermark(file.getPath(), file.getPath(), "Watermark Text");}}public static void main(String[] args) {String inputFile = "C:\\Users\\admin\\Downloads\\123.pdf";String outputFile = "C:\\Users\\admin\\Downloads\\789.pdf";String watermarkText = "zhijue.com";addWatermarkMulti(inputFile, outputFile, watermarkText);}
}

http://www.lryc.cn/news/236586.html

相关文章:

  • Tensorflow2.0:CNN、ResNet实现MNIST分类识别
  • 本地jar导入maven
  • 数据结构与算法【堆】的Java实现
  • 同创永益联合红帽打造一站式数字韧性解决方案
  • c++ call_once 使用详解
  • 【rosrun diagnostic_analysis】报错No module named rospkg | ubuntu 20.04
  • 高防CDN有什么作用?
  • 盛元广通开放实训室管理系统2.0
  • 3D建模基础教程:编辑多边形功能命令快捷方式
  • SaleSmartly新增AI意图识别触发器!让客户享受更精准的自动化服务
  • 计算机毕业设计选题推荐-个人博客微信小程序/安卓APP-项目实战
  • 一篇详解,Postman设置token依赖步骤
  • 音频录制实现 绘制频谱
  • nginx代理本地服务请求,避免跨域;前端图片压缩并上传
  • Vue3-readonly(深只读) 与 shallowReadonly(浅只读)
  • 中小企业怎么实现数字化转型?有什么实用的工单管理系统?
  • vue3.x中父组件添加自定义参数后,如何获取子组件$emit传递过来的参数
  • 【Machine Learning in R - Next Generation • mlr3】
  • CorelDraw2024(CDR)- 矢量图制作软件介绍
  • RT-DETR优化改进:轻量级Backbone改进 | VanillaNet极简神经网络模型 | 华为诺亚2023
  • 本地部署 EmotiVoice易魔声 多音色提示控制TTS
  • 5g路由器赋能园区无人配送车联网应用方案
  • ARTS 打卡第一周
  • 第八部分:JSP
  • Github小彩蛋显示自己的README,git 个人首页的 README,readme基本语法
  • dxva2+ffmpeg硬件解码(Windows)终结发布
  • C#密封类、偏类
  • C++菱形继承问题
  • 第20章 数据库编程
  • PS学习笔记——初识PS界面