java将图片转为pdf

效果图

直接上代码

1.引入jar

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

2.测试类

package pers.wwz.study.img2pdf20240507;

import org.apache.pdfbox.io.IOUtils;
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 java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class Test {

    public static void main(String[] args) {
        downloadPdf("6.jpg","1.pdf");
    }

    /**
     * 下载
     *
     * @param imgFilePath img文件路径
     * @param filePath    文件路径
     * @throws IOException ioexception
     */
    private static void downloadPdf( String imgFilePath,String filePath) {
        PDDocument document = new PDDocument();
        try {
            // 1.将本地的图片转成流形式
            File imgFile = new File("6.jpg");
            byte[] imageBytes = readBytesFromFile(imgFile);

            File imgFile2 = new File("7.jpg");
            byte[] imageBytes2 = readBytesFromFile(imgFile2);

            File imgFile3 = new File("10.jpg");
            byte[] imageBytes3 = readBytesFromFile(imgFile3);


            // 2. 生成PDF

            genPdf(document, imageBytes);
            genPdf(document, imageBytes2);
            genPdf(document, imageBytes3);

            // 4. 保存PDF
            File outputFile = new File(filePath);
            File parentFolder = outputFile.getParentFile();
            if (parentFolder != null && !parentFolder.exists()) {
                parentFolder.mkdirs();
            }
            document.save(outputFile);
            document.close();
        }catch (Exception e){

        }

    }

    private static void genPdf(PDDocument document, byte[] imageBytes) throws IOException {
        //2. 生成一页 PDF document
        PDImageXObject image = PDImageXObject.createFromByteArray(document, imageBytes, "image");
        // 这里是你生成PDF自适应图片大小,不设置会默认为A4
        PDRectangle pageSize = new PDRectangle(image.getWidth(), image.getHeight());
        PDPage page = new PDPage(pageSize);

        document.addPage(page);
        // 3.将 图片 添加进PDF document
        PDPageContentStream contentStream = new PDPageContentStream(document, page);
        float pageWidth = pageSize.getWidth();
        float pageHeight = pageSize.getHeight();
        float imageWidth = image.getWidth();
        float imageHeight = image.getHeight();
        float scale = Math.min(pageWidth / imageWidth, pageHeight / imageHeight);
        float scaledWidth = imageWidth * scale;
        float scaledHeight = imageHeight * scale;
        float x = (pageWidth - scaledWidth) / 2;
        float y = (pageHeight - scaledHeight) / 2;
        // 这里是将你的图片填充入pdf页
        contentStream.drawImage(image, x, y, scaledWidth, scaledHeight);
        contentStream.close();
    }

    /**
     * 从文件读取字节
     *
     * @param file 文件
     * @return {@link byte[]}
     * @throws IOException ioexception
     */
    private static byte[] readBytesFromFile(File file) throws IOException {
        FileInputStream inputStream = new FileInputStream(file);
        byte[] bytes = IOUtils.toByteArray(inputStream);
        inputStream.close();
        return bytes;
    }

}

参考链接:java实现图片转PDF_java 图片转pdf-CSDN博客

相关推荐
C-SDN花园GGbond1 小时前
【探索数据结构与算法】插入排序:原理、实现与分析(图文详解)
c语言·开发语言·数据结构·排序算法
迷迭所归处2 小时前
C++ —— 关于vector
开发语言·c++·算法
架构文摘JGWZ3 小时前
Java 23 的12 个新特性!!
java·开发语言·学习
leon6253 小时前
优化算法(一)—遗传算法(Genetic Algorithm)附MATLAB程序
开发语言·算法·matlab
拾光师4 小时前
spring获取当前request
java·后端·spring
aPurpleBerry4 小时前
neo4j安装启动教程+对应的jdk配置
java·neo4j
锦亦之22334 小时前
QT+OSG+OSG-earth如何在窗口显示一个地球
开发语言·qt
我是苏苏4 小时前
Web开发:ABP框架2——入门级别的增删改查Demo
java·开发语言
姜太公钓鲸2334 小时前
c++ static(详解)
开发语言·c++
菜菜想进步4 小时前
内存管理(C++版)
c语言·开发语言·c++