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博客

相关推荐
又是忙碌的一天1 分钟前
Maven基本概念
java·maven
@淡 定8 分钟前
JVM内存区域划分详解
java·jvm·算法
❀͜͡傀儡师13 分钟前
运维问题排查笔记:磁盘、Java进程与SQL执行流程
java·运维·笔记
篱笆院的狗13 分钟前
Java 中如何创建多线程?
java·开发语言
默 语14 分钟前
RAG实战:用Java+向量数据库打造智能问答系统
java·开发语言·数据库
晨晖215 分钟前
二叉树遍历,先中后序遍历,c++版
开发语言·c++
醒过来摸鱼16 分钟前
Java Compiler API使用
java·开发语言·python
wangchen_020 分钟前
C/C++时间操作(ctime、chrono)
开发语言·c++
dazhong201222 分钟前
Mybatis 敏感数据加解密插件完整实现方案
java·数据库·mybatis
Dev7z31 分钟前
基于MATLAB HSI颜色空间的图像美颜系统设计与实现
开发语言·matlab