目录

1.引包

2.工具类

3.数据构建

4.pdf模板​编辑

5.输出​编辑


1.引包

项目要使用iText,必须引入jar包。才能使用,maven依赖如下:

<dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.11</version>
</dependency>

二维码生成

<dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.3.0</version>
</dependency>

2.工具类

package com.zz.common.utils;import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import lombok.extern.slf4j.Slf4j;import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.Map;/*** pdf工具类** @author zhooulianjie* @date 2021/2/3*/@Slf4j
public class PdfUtils<main> {/*** pdf添加水印** @param outputFile 添加完水印的文件输出地址* @param inputFile  原PDF文件地址* @param word       水印内容* @param model      水印添加位置1中间,2两边*/public static void setPdfWatermark(String outputFile, String inputFile, String word, int model) {PdfReader reader;try {BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(outputFile)));FileInputStream input = new FileInputStream(inputFile);reader = new PdfReader(input);PdfStamper stamper = new PdfStamper(reader, bos);PdfContentByte content;// 创建字体BaseFont base = BaseFont.createFont("C:/WINDOWS/Fonts/simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);PdfGState gs = new PdfGState();// 获取PDF页数int total = reader.getNumberOfPages();// 遍历每一页for (int i = 0; i < total; i++) {// 页宽度float width = reader.getPageSize(i + 1).getWidth();// 页高度float height = reader.getPageSize(i + 1).getHeight();// 内容content = stamper.getOverContent(i + 1);//开始写入文本content.beginText();//水印透明度gs.setFillOpacity(0.9f);content.setGState(gs);content.setColorFill(BaseColor.LIGHT_GRAY);//设置字体的输出位置content.setTextMatrix(70, 200);if (model == 1) {// 平行居中的3条水印// 字体大小content.setFontAndSize(base, 30);//showTextAligned 方法的参数分别是(文字对齐方式,位置内容,输出// .水印X轴位置,Y轴位置,旋转角度)content.showTextAligned(Element.ALIGN_CENTER, word, width / 2, 800, 30);content.showTextAligned(Element.ALIGN_CENTER, word, width / 2, 600, 30);content.showTextAligned(Element.ALIGN_CENTER, word, width / 2, 400, 30);content.showTextAligned(Element.ALIGN_CENTER, word, width / 2, 200, 30);content.showTextAligned(Element.ALIGN_CENTER, word, width / 2, 0, 30);} else {// 左右两边个从上到下4条水印// 水印旋转度数float rotation = 30;content.setFontAndSize(base, 20);content.showTextAligned(Element.ALIGN_LEFT, word, 20, height - 50, rotation);content.showTextAligned(Element.ALIGN_LEFT, word, 20, height / 4 * 3 - 50, rotation);content.showTextAligned(Element.ALIGN_LEFT, word, 20, height / 2 - 50, rotation);content.showTextAligned(Element.ALIGN_LEFT, word, 20, height / 4 - 50, rotation);content.setFontAndSize(base, 22);content.showTextAligned(Element.ALIGN_RIGHT, word, width - 20, height - 50, rotation);content.showTextAligned(Element.ALIGN_RIGHT, word, width - 20, height / 4 * 3 - 50, rotation);content.showTextAligned(Element.ALIGN_RIGHT, word, width - 20, height / 2 - 50, rotation);content.showTextAligned(Element.ALIGN_RIGHT, word, width - 20, height / 4 - 50, rotation);}//结束写入文本content.endText();//要打图片水印的话//Image image = Image.getInstance("c:/1.jpg");//content.addImage(image);}stamper.close();} catch (IOException | DocumentException e) {log.error("pdf添加水印失败,原因:" + e.getMessage());}}// 利用模板生成pdfpublic static void pdfout(String templatePath, String fileName, Map<String, List<Map<String, Object>>> o, HttpServletResponse response) {// 生成的新文件路径String property = System.getProperty("user.dir");String newPDFPath = property + "/files/" + fileName + ".pdf";FileOutputStream out;try {out = new FileOutputStream(newPDFPath);// 输出流Document doc = new Document();PdfCopy copy = new PdfCopy(doc, out);doc.open();List<Map<String, Object>> mapList = o.get("datemap");List<Map<String, Object>> imgList = o.get("imgmap");for (int i = 0; i < mapList.size(); i++) {PdfReader reader = new PdfReader(templatePath);// 读取pdf模板ByteArrayOutputStream bos;PdfStamper stamper;bos = new ByteArrayOutputStream();stamper = new PdfStamper(reader, bos);AcroFields form = stamper.getAcroFields();//文字类的内容处理for (String key : mapList.get(i).keySet()) {String value = String.valueOf(mapList.get(i).get(key));form.setField(key, value);}//图片类的内容处理for(String key : imgList.get(i).keySet()) {String value = String.valueOf(imgList.get(i).get(key));String imgpath = value;int pageNo = form.getFieldPositions(key).get(0).page;Rectangle signRect = form.getFieldPositions(key).get(0).position;float x = signRect.getLeft();float y = signRect.getBottom();//根据路径读取图片File file = new File(imgpath);if (file.exists()) {Image image = Image.getInstance(imgpath);//获取图片页面PdfContentByte under = stamper.getOverContent(pageNo);//图片大小自适应image.scaleToFit(signRect.getWidth(), signRect.getHeight());//添加图片image.setAbsolutePosition(x, y);under.addImage(image);}}stamper.setFormFlattening(true);// 如果为false,生成的PDF文件可以编辑,如果为true,生成的PDF文件不可以编辑stamper.close();PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);copy.addPage(importPage);}doc.close();FileUtils.downLoad(newPDFPath, fileName + ".pdf", response);FileUtils.deleteFile(newPDFPath);} catch (IOException e) {System.out.println(e);} catch (DocumentException e) {System.out.println(e);}}}

3.数据构建

@Overridepublic void printpdf(HttpServletResponse response) {QueryWrapper<T> query = new QueryWrapper<>();List<Map<String, Object>> ryxq = this.list(query);List<Map<String, Object>> zpMap = new ArrayList<Map<String, Object>>();Map<String, List<Map<String, Object>>> map = new HashMap<String, List<Map<String, Object>>>();ryxq.forEach(m -> {Map<String, Object> retMap = new HashMap<>();if (Utils.notEmpty(m.get("lczp"))) {retMap.put("lczpPath", "照片地址");}String destPath = "二维码存储地址";String ewmcs = "二维码信息";try {EwmUtil.encode(ewmcs, destPath);retMap.put("ewmPath", destPath);} catch (Exception e) {e.printStackTrace();}zpMap.add(retMap);});map.put("datemap", ryxq);map.put("imgmap", zpMap);PdfUtils.pdfout(System.getProperty("user.dir") + bmzsmb_url, "zhengshu", map, response);}

4.pdf模板

5.输出

pdf模板带图片二维码导出(多个)相关推荐

  1. HuTool工具生成带图片二维码

    首先是依赖文件: <!--糊涂工具类--><dependency><groupId>cn.hutool</groupId><artifactId& ...

  2. 微信小程序点击图片实现长按预览、保存、识别带参数二维码、转发等功能

    微信小程序开发交流qq群   581478349    承接微信小程序开发.扫码加微信. 正文: 先上效果图,再附上完整源码: 1.多张图片循环渲染后预览.保存.识别带参数二维码 <view w ...

  3. 扫描二维码、扫描条形码、相册获取图片后识别、生成带 Logo 二维码、支持微博微信 QQ 二维码扫描样式

    GitHub项目的链接地址 目录 功能介绍 常见问题 效果图与示例 apk Gradle 依赖 布局文件 自定义属性说明 接口说明 关于我 功能介绍 ZXing 生成可自定义颜色.带 logo 的二维 ...

  4. java关于Zxing 生成带Logo 二维码图片失真问题

    java关于Zxing 生成带Logo 二维码图片失真问题 问题点 logo本身是高清图片,但是Zxing生成的二维码中,logo像素失真,感觉被严重压缩一样. 排查问题 是Graphics2D 绘制 ...

  5. QRCode 扫描二维码、扫描条形码、相册获取图片后识别、生成带 Logo 二维码、支持微博微信 QQ 二维码扫描样式...

    QRCode 扫描二维码.扫描条形码.相册获取图片后识别.生成带 Logo 二维码.支持微博微信 QQ 二维码扫描样式 参考链接:https://github.com/bingoogolapple/B ...

  6. python——生成带logo的二维码图片并且保存、控制打印机打印图片二维码、整合打印(获取输入框的值)、打包成exe文件

    1.生成带logo的二维码图片并且保存 前提条件:在D盘里有logo.png的图片,生成的二维码图片在D盘里的111.png import qrcode from PIL import Image# ...

  7. 【C#】最全单据打印(打印模板、条形码二维码、字体样式、项目源码)

    系列文章 [C#]编号生成器(定义单号规则.固定字符.流水号.业务单号) 本文链接:https://blog.csdn.net/youcheng_ge/article/details/12912978 ...

  8. 带参数二维码如何跟踪用户来自哪个推广人员?

    运营微信公众号难免对公众号进行推广,比如每个推广人员都去推广这个公众号,然后需要统计每个推广人员到底带来了多少粉丝.公众号后台的二维码没有这个功能,开发平台接口提供带参数的二维码生成,但是需要程序员. ...

  9. 微信公众平台----带参数二维码生成和扫描事件

    原文:微信公众平台----带参数二维码生成和扫描事件 摘要: 账号管理----生成带参数的二维码 消息管理----接收消息----接收事件推送 为了满足用户渠道推广分析和用户帐号绑定等场景的需要,公众 ...

最新文章

  1. hbase系列之:独立模式部署hbase
  2. VUE2.X组件之间通信的2种方式(针对子组件值变化去改变相应父组件的值)
  3. 查找两个已经排好序的数组的第k大的元素
  4. Servlet 是线程安全的吗?
  5. mysql 5.6压缩安装_MySQL 5.6 for Windows 解压缩版配置安装
  6. kafka可视化工具_Kafka值得一用的监控系统
  7. 使用SQLDMO中“接口SQLDMO.Namelist 的 QueryInterface 失败”异常的解决方法
  8. 用代码建立与数据库的连接 c#连sqlserver
  9. 通达信资金净流入公式_通达信主力净流入指标公式
  10. Unity 实现部分模型流光效果
  11. 安卓手机SSH远程链接服务器教程
  12. CTU CU CB PU TU
  13. 计算机时代汉字书写有了新的方式,网络时代的汉字书写
  14. 全网最通俗易懂的爬虫教程
  15. 哈工大pyltp库安装的踩坑经历—windows10+python3.8
  16. 手把手教你将矩阵画成张量网络图
  17. 解决chrome自动填充白色背景(input:-internal-autofill-previewed)问题
  18. 2021年第四届“传智杯“大学B组
  19. 【PMSM】二. 经典电流环、速度环设计(下)
  20. mysql 输出名称_MySQL常用的SQL语句//输出所有信息showfullfieldsfrom'表名称';//改表

热门文章

  1. 开漏输出,推挽输出,开集输出
  2. Invalid Host header 服务器域名访问出现的问题
  3. rabbitMQ学习心得
  4. php微信域名检测工具,微信域名检测 微信域名检测官方接口的调用代码分享
  5. 视频直播悖论:影响网络效应的6大陷阱
  6. itext导出pdf中新加一页空白页面(itext 遇到的问题持续更新)
  7. 股票解禁是好事还是坏事?
  8. UNIX/LINUX 平台可执行文件格式分析
  9. 技巧 | 如何快速生成文件清单
  10. Javascript面向对象编程思考与总结