JAVA PDF文件转图片

工作中遇到需要把PDF文件转成图片使用,在此记录下,其中用到了三方JARJAR列表

package com.xxx.xxx.utils;

import org.icepdf.core.exceptions.PDFException;
import org.icepdf.core.exceptions.PDFSecurityException;
import org.icepdf.core.pobjects.Document;
import org.icepdf.core.pobjects.Page;
import org.icepdf.core.util.GraphicsRenderingHints;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


/**
 * @author qingyu
 * @version 1.0
 * @date 2022/2/17 17:33
 */
public class PdfConvertImg {

    public static void pdf2multiImage(String pdfFile, String outpath)
            throws PDFException, PDFSecurityException, IOException {
        InputStream is = new FileInputStream(pdfFile);
        List<BufferedImage> piclist = new ArrayList<BufferedImage>();
        Document document = new Document();
        document.setFile(pdfFile);
        float scale = 2.0f; // 缩放比例
        float rotation = 0f; // 旋转角度
        for (int i = 0; i < document.getNumberOfPages(); i++) {
            BufferedImage image = (BufferedImage) document.getPageImage(i, GraphicsRenderingHints.SCREEN,
                    Page.BOUNDARY_CROPBOX, rotation, scale);
            piclist.add(image);
        }
        document.dispose();
        yPic(piclist, outpath);
        is.close();
    }

    public static void yPic(List<BufferedImage> piclist, String outPath) throws IOException {// 纵向处理图片

        int height = 0, // 总高度
                _width = 0, // 临时宽度
                maxWidth = 0, // 最大宽度
                _height = 0, // 临时的高度 , 或保存偏移高度
                __height = 0, // 临时的高度,主要保存每个高度
                picNum = piclist.size();// 图片的数量
        int[] heightArray = new int[picNum]; // 保存每个文件的高度
        int[] widthArray = new int[picNum]; // 保存每个文件的宽度
        BufferedImage buffer = null; // 保存图片流
        List<int[]> imgRGB = new ArrayList<int[]>(); // 保存所有的图片的RGB
        int[] _imgRGB; // 保存一张图片中的RGB数据
        for (int i = 0; i < picNum; i++) {
                buffer = piclist.get(i);
            if (buffer.getWidth() > maxWidth) {
                    // 获取最大宽度
                    maxWidth = buffer.getWidth();
            }
        }
        for (int i = 0; i < picNum; i++) {
                buffer = piclist.get(i);
            heightArray[i] = _height = buffer.getHeight();// 图片高度
            widthArray[i] = _width = buffer.getWidth();// 图片宽度
            height += _height; // 获取总高度
            _imgRGB = new int[_width * _height];// 从图片中读取RGB
            _imgRGB = buffer.getRGB(0, 0, _width, _height, _imgRGB, 0, _width);
            imgRGB.add(_imgRGB);
        }
        _height = 0; // 设置偏移高度为0
        // 生成新图片
        BufferedImage imageResult = new BufferedImage(_width, height, BufferedImage.TYPE_INT_RGB);
        for (int i = 0; i < picNum; i++) {
                __height = heightArray[i];
            _width = widthArray[i];
            // if (i != 0) _height += heightArray[i-1]; // 计算偏移高度 ,若高度一致,则把此行打开
            if (i != 0)
                _height += heightArray[i - 1]; // 计算偏移高度 ,因高度不一致,所以i-1
            // imageResult.setRGB(0, _height, widthArray[i], __height, imgRGB.get(i), 0, widthArray[i]); // 写入流中
            // 居中,前两个参数为起始的宽度、高度
            imageResult.setRGB((maxWidth - _width) / 2, _height, _width, __height, imgRGB.get(i), 0, _width); // 写入流中
        }
        File outFile = new File(outPath);
        ImageIO.write(imageResult, "jpg", outFile);// 写图片
    }

    public static void main(String[] args) throws PDFSecurityException, PDFException, IOException {
        PdfConvertImg.pdf2multiImage("D:/home/user1/POSPPIC/AK1434726700369334272/creditInvestigation.pdf",
                "D:/home/user1/POSPPIC/AK1434726700369334272/creditInvestigation.jpg");
    }
}

这个工具类的yPic方法为多找PDF文件转成一张长图片,一般PDF会有多页这个方法输入的为一张图片。网站限制下载必须要积分,已设置成最低1分方便大家使用!
JAR下载地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中可以使用开源库Apache PDFBox来实现将PDF换为图片的功能。PDFBox是一个功能强大的Java库,可以用于处理PDF文件。 以下是使用PDFBox将PDF换为图片的基本步骤: 1. 首先,你需要在你的Java项目中引入PDFBox库。你可以通过在Maven项目的pom.xml文件中添加以下依赖来实现: ```xml <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> <version>2.0.26</version> </dependency> ``` 2. 创建一个Java类,并导入所需的类: ```java import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.rendering.PDFRenderer; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; public class PDFToImageConverter { public static void main(String[] args) { String pdfFilePath = "path/to/your/pdf/file.pdf"; String outputImagePath = "path/to/save/output/image.png"; try { PDDocument document = PDDocument.load(new File(pdfFilePath)); PDFRenderer pdfRenderer = new PDFRenderer(document); // 遍历每一页并将其换为图像 for (int pageIndex = 0; pageIndex < document.getNumberOfPages(); pageIndex++) { BufferedImage image = pdfRenderer.renderImageWithDPI(pageIndex, 300); // 设置图像分辨率为300dpi ImageIO.write(image, "PNG", new File(outputImagePath)); } document.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 在上面的代码中,你需要将`pdfFilePath`替换为你要换的PDF文件的路径,将`outputImagePath`替换为你要保存输出图像的路径。 3. 运行Java程序,它将读取PDF文件并将每一页换为图像。图像将保存在指定的输出路径中。 这就是使用JavaPDFBox库将PDF换为图片的基本过程。你可以根据自己的需求进行进一步的定制和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值