Java如何将图片适配居中放置PDF中间

java 复制代码
package com.junfun.pms.controller.excelImport;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;
import com.junfun.seafar.common.entity.ServiceResponse;
import io.swagger.annotations.Api;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

@RestController
@RequestMapping("test")
@Api(value = "ImageUploadController", tags = "截图导入接口")
public class ImageUploadController {

    @PostMapping("/upload-image")
    public ServiceResponse<String> handleFileUpload(@RequestParam("image") MultipartFile file) {
        if (file.isEmpty()) {
            throw new RuntimeException("文件为空");
        }

        ServiceResponse<String> response = new ServiceResponse<>();

        // 将图片保存到 D:\tu\ 目录下
        String filePath = "D:\\tu\\" + file.getOriginalFilename();

        // 生成 PDF 文件名
        String fileName = StringUtils.cleanPath(file.getOriginalFilename());
        String pdfFileName = fileName.substring(0, fileName.lastIndexOf('.')) + ".pdf";
        String pdfFilePath = "D:\\tu\\" + pdfFileName;

        try {
//            File file1 = new File(filePath);
//            //拷贝文件
//            file.transferTo(file1);

            // 将图片保存到临时文件
            File tempImageFile = new File("D:\\tu\\" + fileName);
            file.transferTo(tempImageFile);
            // 创建 PDF 文件
            createPdfFromImage(tempImageFile, pdfFilePath);
            // 删除临时图片文件
            tempImageFile.delete();

            response.setSucess("上传成功");
        } catch (Exception e) {
            response.setError("上传失败");
        }

        return response;
    }

    private void createPdfFromImage(File imageFile, String pdfFilePath) throws DocumentException, IOException {
        // 创建 PDF 文档
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(pdfFilePath));

        // 打开文档
        document.open();

        // 加载图片
        Image img = Image.getInstance(imageFile.getAbsolutePath());

        // 获取 PDF 页面的宽度和高度
        float pageWidth = document.getPageSize().getWidth();
        float pageHeight = document.getPageSize().getHeight();

        // 计算图片的原始宽度和高度
        float imgWidth = img.getScaledWidth();
        float imgHeight = img.getScaledHeight();

        // 计算缩放因子,确保图片不超出页面宽度
        float scaleFactor = pageWidth / imgWidth;

        // 如果图片的高度超过页面高度,重新计算缩放因子
        if ((imgHeight * scaleFactor) > pageHeight) {
            scaleFactor = pageHeight / imgHeight;
        }

        // 根据缩放因子调整图片大小
        img.scaleAbsolute((float)(imgWidth * scaleFactor), (float)(imgHeight * scaleFactor));

        // 计算图片在页面上的水平居中位置
        float posX = (pageWidth - img.getScaledWidth()) / 2;
        float posY = (pageHeight - img.getScaledHeight()) / 2;

        // 将图片添加到 PDF 文档
        img.setAbsolutePosition(posX, posY);
        document.add(img);

        // 关闭文档
        document.close();
    }
}

引包的话引入这个

XML 复制代码
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext7-core</artifactId>
            <version>7.1.15</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.12</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf.tool</groupId>
            <artifactId>xmlworker</artifactId>
            <version>5.5.9</version>
        </dependency>   用的是这个
相关推荐
梦想科研社几秒前
【无人机设计与控制】四旋翼无人机俯仰姿态保持模糊PID控制(带说明报告)
开发语言·算法·数学建模·matlab·无人机
风等雨归期几秒前
【python】【绘制小程序】动态爱心绘制
开发语言·python·小程序
千穹凌帝4 分钟前
SpinalHDL之结构(二)
开发语言·前端·fpga开发
AlexMercer10126 分钟前
【C++】二、数据类型 (同C)
c语言·开发语言·数据结构·c++·笔记·算法
Jarlen7 分钟前
将本地离线Jar包上传到Maven远程私库上,供项目编译使用
java·maven·jar
friklogff7 分钟前
【无标题】云端之C#:全面解析6种云服务提供商的SDK
开发语言·flask·c#
蓑 羽13 分钟前
力扣438 找到字符串中所有字母异位词 Java版本
java·算法·leetcode
Reese_Cool20 分钟前
【C语言二级考试】循环结构设计
android·java·c语言·开发语言
海里真的有鱼21 分钟前
Spring Boot 项目中整合 RabbitMQ,使用死信队列(Dead Letter Exchange, DLX)实现延迟队列功能
开发语言·后端·rabbitmq