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

java将指定的两张图片合成pdf并在指定坐标位置写入内容

引入jar包

 <dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.27</version></dependency>

 

 

package org.example.pdf;import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;/*** @author 50649*/
public class ImageToPdfWithName {public static void main(String[] args) {// 配置参数List<String> imagePaths = new ArrayList<>();// 第一张图片路径imagePaths.add("C:\\Users\\50649\\Desktop\\pdf图片\\画板 2.png");// 第二张图片路径imagePaths.add("C:\\Users\\50649\\Desktop\\pdf图片\\画板 3.png");List<String> names = new ArrayList<>();// 第一张图片中要填写的姓名names.add("张三");// 第二张图片中要填写的姓名names.add("李四");// 输出PDF路径String outputPdfPath = "C:\\Users\\50649\\Desktop\\测试pdf\\output2.pdf";// 表格中姓名区域的位置(需要根据实际图片调整)List<Point> namePositions = new ArrayList<>();// 第一张图片中姓名位置namePositions.add(new Point(751, 415));// 第二张图片中姓名位置//Point2D.Float point2 = new Point2D.Float(27.72f, 15.29f);namePositions.add(new Point(150, 200));try {// 处理图片并生成PDF
            createPdfFromImages(imagePaths, names, namePositions, outputPdfPath);System.out.println("PDF创建成功: " + outputPdfPath);} catch (IOException e) {System.err.println("处理过程中发生错误: " + e.getMessage());e.printStackTrace();}}public static void createPdfFromImages(List<String> imagePaths, List<String> names,List<Point> namePositions, String outputPath) throws IOException {if (imagePaths.size() != names.size() || imagePaths.size() != namePositions.size()) {throw new IllegalArgumentException("图片、姓名和位置列表的大小必须相同");}try (PDDocument document = new PDDocument()) {for (int i = 0; i < imagePaths.size(); i++) {// 加载原始图片BufferedImage originalImage = ImageIO.read(new File(imagePaths.get(i)));System.out.println("names.get(i)=="+names.get(i));System.out.println(" namePositions.get(i)=="+namePositions.get(i));// 在图片上添加姓名BufferedImage imageWithName = addNameToImage(originalImage, names.get(i), namePositions.get(i));// 创建PDF页面(使用图片尺寸)PDPage page = new PDPage(new PDRectangle(imageWithName.getWidth(), imageWithName.getHeight()));document.addPage(page);// 将处理后的图片添加到PDF页面try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) {// 转换BufferedImage为PDImageXObjectPDImageXObject pdImage = PDImageXObject.createFromByteArray(document,bufferedImageToByteArray(imageWithName, "jpg"),imagePaths.get(i));// 绘制图片到PDF页面contentStream.drawImage(pdImage, 0, 0, imageWithName.getWidth(), imageWithName.getHeight());}}// 保存PDF文档
            document.save(outputPath);}}private static BufferedImage addNameToImage(BufferedImage originalImage, String name, Point position) {// 创建图片副本以便编辑BufferedImage imageWithText = new BufferedImage(originalImage.getWidth(),originalImage.getHeight(),BufferedImage.TYPE_INT_RGB);// 绘制原始图片Graphics2D graphics = imageWithText.createGraphics();graphics.drawImage(originalImage, 0, 0, null);// 设置文本属性
        graphics.setColor(Color.BLACK);// 使用宋体,粗体,16号字graphics.setFont(new Font("SimSun", Font.BOLD, 16));// 在指定位置绘制姓名
        graphics.drawString(name, position.x, position.y);// 释放图形资源
        graphics.dispose();return imageWithText;}private static byte[] bufferedImageToByteArray(BufferedImage image, String format) throws IOException {java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();ImageIO.write(image, format, baos);return baos.toByteArray();}
}

获取坐标  https://www.cnblogs.com/xianz666/p/19083294

http://www.wxhsa.cn/company.asp?id=57

相关文章:

  • v-model的简单实现
  • 旧版本Flutter使用xcode16打包上架时报错
  • 国产化Excel处理组件Spire.XLS教程:C# 读取 CSV 文件,从基础读取到 DataTable 转换
  • 常用redis客户端对比
  • 【URP】Shader绘制棋盘格对比内置管线
  • Codeforces Round 1049 (Div. 2)(C-E)
  • 虚拟列表
  • 条码控件Aspose.BarCode教程:使用 C# 构建 Code11 条形码生成器
  • 智能引擎驱动软件工业化革命:Gitee PPM如何重塑企业级开发范式
  • 深入解析Web Components:Shadow DOM实战指南
  • HCIP回顾— BGP基础
  • 你的测试又慢又不可靠-因为你测错了东西
  • 你的错误处理一团糟-是时候修复它了-️
  • 物理焦距、像素焦距、像元与相机内参(fx, fy)的意义与作用
  • 实时通信的头痛-问题不在WebSocket而是你的框架
  • 文件不只是数据-一份稳健的文件处理指南
  • vue+websocket+Stomp组件实现前端长连接
  • java课前问题列表
  • 多字段排序工具类,支持树形
  • 鸿蒙 HAP 包处理全攻略:从解包到签名,So 库加固一步到位
  • 关于vue在PC端,rem对不同屏幕进行适配
  • GreatSQL分页查询优化案例实战
  • 技术面:Java并发(线程同步、死锁、多线程编排)
  • vue3中两对容易搞混的概念
  • LoadRunner 对 WebTours 实现订票的性能分析
  • mac一键关闭chrome自动更新
  • Python游戏开发:使用Pygame库的全面教程
  • 同城黑卡小程序系统介绍
  • 限行提醒小程序介绍
  • 365 快乐农场小程序介绍