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 分钟前
阿里云kafka集成boot在docker启动找不到kafka.client.truststore.jks文件问题
java·spring boot·kafka
ALex_zry2 分钟前
让 Python 脚本在后台持续运行:架构级解决方案与工业级实践指南
开发语言·python·架构
就是蠢啊5 分钟前
Maven 基础知识
java·maven
珹洺1 小时前
Java-servlet(完结篇)过滤器乱码解决与监听器
java·运维·服务器·hive·servlet·jsp
Blood_J2 小时前
python网络爬虫
开发语言·爬虫·python
小开不是小可爱2 小时前
leetcode_383. 赎金信_java
java·数据结构·算法·leetcode
xiaowu0803 小时前
C# task任务异步编程提高UI的响应性
开发语言·c#
kill bert5 小时前
Java八股文背诵 第四天JVM
java·开发语言·jvm
低头专研7 小时前
Markdown标题序号处理工具——用 C 语言实现
c语言·开发语言·typora·markdown文件标题编号·md文件标题序号
刚入门的大一新生8 小时前
C++初阶-C++入门基础
开发语言·c++