pdfbox-pdf转img

这篇博客介绍了如何利用PDFBox Java库将PDF文件转换为图像。首先,详细说明了如何添加PDFBox依赖到项目中,无论是通过Maven还是Gradle。接着,提供了转换的工具代码段,展示如何实现转换功能。最后,给出了测试代码,以验证转换过程的正确性。

pdfbox-pdf转img

1、依赖导入

maven导入

<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>2.0.22</version>
</dependency>

gradle导入

// https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox
implementation group: 'org.apache.pdfbox', name: 'pdfbox', version: '2.0.22'

2、工具代码

public static void pdfToImgCut(File file, String imgStore) throws Exception {
        File file1 = new File(imgStore);
        if (!file1.exists()) {
            boolean mkdirs = file1.mkdirs();
            if (!mkdirs) {
                log.error("创建转图片存贮文件夹失败 ,文件夹路径:{}", imgStore);
            }
        }
        String fileName = file.getName();
        PDDocument src = PDDocument.load(file);
        int totalPages = src.getNumberOfPages();
        for (int i = 0; i < totalPages; i++) {
            PDPage page = src.getPage(i);
            PDRectangle cropBox = page.getCropBox();
            float pageWidth = cropBox.getWidth();
            float pageHeight = cropBox.getHeight();
            float longSide = Math.max(pageHeight, pageWidth);
            float shortSide = Math.min(pageHeight, pageWidth);
            float scaleA = (1500 / longSide);
            float scaleB = 1000 / shortSide;
            float scale = (float) Math.floor((Math.min(scaleA, scaleB) * 10)) / 10;
            BufferedImage bufferedImage = pageConvertToImage(page, scale * 72, ImageType.RGB);
            byte[] jpgs = imgToBytes(bufferedImage, "jpg");
            String imgSavePath = imgStore + fileName.substring(0, fileName.lastIndexOf(".")) + "_" + i + ".jpg";
            cutImg(jpgs, imgSavePath);
        }
        src.close();
    }

    public static void cutImg(byte[] bs, String filePath) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try {
            file = new File(filePath);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bs);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static BufferedImage pageConvertToImage(PDPage page, float dpi, ImageType imageType) throws IOException {
        try (PDDocument document = new PDDocument()) {
            document.addPage(page);
            PDFRenderer renderer = new PDFRenderer(document);
            return renderer.renderImageWithDPI(0, dpi, imageType);
        }
    }

    public static byte[] imgToBytes(BufferedImage bufferedImage, String format) throws IOException {
        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            ImageIO.write(bufferedImage, format, outputStream);
            return outputStream.toByteArray();
        }
    }

3、测试代码

public class PdfToImgUtil {
    private static final Logger log = LogManager.getLogger(PdfToImgUtil.class);

    public static void main(String[] args) throws Exception {
        //pdf文件所在文件夹路径
        String path = "D:\\aaaaa\\file";
        //转换后的图片文件的文件夹路径
        String imgStore = "\"D:\\\\aaaaa\\\\img\"";
        File fileSrcStore = new File(path);
        File[] tmpFiles = fileSrcStore.listFiles();
        assert tmpFiles != null;
        for (File file : tmpFiles) {
            String fileName = file.getName();
            if (fileName.toLowerCase().endsWith(".pdf")) {
                pdfToImgCut(file, imgStore);
            } else {
                log.info("该文件不是pdf文件,文件名:{}", fileName);
            }
        }
    }
}

*转载请附加本文地址

AP PDF to IMAGE Batch Converter 是一个有用的 pdf工具,该换器能利用源 PDF 文档格式换全功能的带文字,图片,图表等图像。它不需要任何软件支持,如 Adobe Acrobat,Acrobat Reader 等。 AP PDF to IMAGE Batch Converter 支持自定义输出结果页面。你可以选择换几页文档,甚至自由换文档页面,如 "1,2,3","2-100" 等,要获得更多信息,请参考页面范围设置。它能处理多个文件,只要确认设置好每个文件的输出目录,这样所有处理完的文件都会放置到它们相应的目录中。 PDF to Image 功能 * 迅速PDF 文件到图像格式。 * 支持批量PDF 到图像。 * 支持加密的 PD F文件。 * 支持自定义输出结果页面。 * 支持矢量图形换到图像文件。 * 支持多个图像格式,如 'bmp','tiff','jpg','gif','png','pcx' 等。 * 支持生成多页面 tiff 文件。 * 支持生成任何分辨率的图像文件。 * 支持换为 1 位,4 位,8 位,4 位灰度,8 位灰度和 24 位格式图像。 * 支持 tiff 图像的多种压缩,如 LZW,JPEG,PACKBITS,CCITT Group3,CCITT Group4,RLE 等。 * 支持拖放文件换。 * 支持 Win98/ME/NT/2000/XP/2003/Vista 平台。 * 换后可以自动显示图像文件。 * 允许你保留源文档的布局。 * 容易使用,单独的应用程序。 * 不需要 Adobe Acrobat 或 Adobe Acrobat Reader 的支持。 * 自动安装和配置。 * 兼容 Adobe Acrobat 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 。 命令行功能 PDF to Image 换命令行程序也提供一个有正常PDF to Image 功能的控制台。 * 包括 PDF to Image 的所有功能。 * 它可以像一个 DOS 命令行应用程序一样简单使用。 * 支持文件夹。 * 批量换。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值