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为PDImageXObject
                    PDImageXObject 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

posted @ 2025-09-10 11:03  红尘沙漏  阅读(16)  评论(0)    收藏  举报