使用pdfbox将pdf转换成图片

导入pom文件

 		<dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>fontbox</artifactId>
            <version>2.0.22</version>
        </dependency>

        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.31</version>
            <!--3.0及以上没有load方法,PDDocument.load(file);-->
        </dependency>

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>net.sf.cssbox</groupId>
            <artifactId>pdf2dom</artifactId>
            <version>2.0.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox-tools</artifactId>
            <version>2.0.22</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.10.1</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba.fastjson2</groupId>
            <artifactId>fastjson2</artifactId>
            <version>2.0.49</version>
        </dependency>

整个pdf转换成图片

package pdfToPng;

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;
import java.io.IOException;

public class Pdf2Png {

    /**
     * 使用pdfbox将整个pdf转换成图片
     * 多页pdf会生成多页的图片,后缀会生成图片的位置序号
     *
     * @param fileAddress 文件地址 如:C:\\Users\\user\\Desktop\\test
     * @param filename    PDF文件名不带后缀名
     * @param type        图片类型 png和jpg
     */
    public static void pdf2png(String fileAddress, String filename, String type) {
        long startTime = System.currentTimeMillis();
        // 将文件地址和文件名拼接成路径 注意:线上环境不能使用\\拼接
        File file = new File(fileAddress + "\\" + filename + ".pdf");
        try {
            // 写入文件
            PDDocument doc = PDDocument.load(file);
            PDFRenderer renderer = new PDFRenderer(doc);
            int pageCount = doc.getNumberOfPages();
            for (int i = 0; i < pageCount; i++) {
                // dpi为144,越高越清晰,转换越慢  Windows native DPI
                BufferedImage image = renderer.renderImageWithDPI(i, 144);
                // 将图片写出到该路径下
                ImageIO.write(image, type, new File(fileAddress + "\\" + filename + "_" + (i + 1) + "." + type));
            }
            long endTime = System.currentTimeMillis();
            System.out.println("共耗时:" + ((endTime - startTime) / 1000.0) + "秒");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        pdf2png("C:\\Users\\MES\\Desktop\\test1", "1", "png");
    }
}


*确定起始页和终止页

package pdfToPng;

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;
import java.io.IOException;

public class Pdf3Png {

    /**
     * *确定起始页和终止页
     *
     * @param fileAddress  文件地址 如:C:\\Users\\user\\Desktop\\test
     * @param filename     PDF文件名不带后缀名
     * @param indexOfStart 开始页  开始转换的页码,从0开始
     * @param indexOfEnd   结束页  停止转换的页码,-1为全部
     * @param type         图片类型 png 和jpg
     */
    public static void pdf3png(String fileAddress, String filename, int indexOfStart, int indexOfEnd, String type) {
        long startTime = System.currentTimeMillis();
        // 将文件地址和文件名拼接成路径 注意:线上环境不能使用\\拼接
        File file = new File(fileAddress + "\\" + filename + ".pdf");
        try {
            PDDocument doc = PDDocument.load(file);
            PDFRenderer renderer = new PDFRenderer(doc);
            int pageCount = doc.getNumberOfPages();
            for (int i = indexOfStart; i < indexOfEnd; i++) {
                // dpi为144,越高越清晰,转换越慢
                BufferedImage image = renderer.renderImageWithDPI(i, 144);
                // 将图片写出到该路径下
                ImageIO.write(image, type, new File(fileAddress + "\\" + filename + "_" + (i + 1) + "." + type));
            }
            long endTime = System.currentTimeMillis();
            System.out.println("共耗时:" + ((endTime - startTime) / 1000.0) + "秒");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        pdf3png("C:\\Users\\MES\\Desktop\\", "1", 1, 1, "png");
    }

}


使用文件流整个pdf转换成图片

package pdfToPng;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;

public class Pdf4Png {

    /**
     * 使用文件流整个pdf转换成图片
     *
     * @param fileAddress 文件地址 如:C:\\Users\\user\\Desktop\\test
     * @param filename    PDF文件名不带后缀名
     * @param type        图片类型 png 、jpg
     */
    public static void pdfToImage(String fileAddress, String filename, String type) {
        long startTime = System.currentTimeMillis();

        List<Map<String, String>> list = new ArrayList<>();
        Map<String, String> resultMap = null;
        PDDocument pdDocument = null;

        String fileName = null;
        String imgPath = null;

        try {
            // 将文件地址和文件名拼接成路径 注意:线上环境不能使用\\拼接
            File FilePath = new File(fileAddress + "\\" + filename + ".pdf");
            // 文件流
            FileInputStream inputStream = new FileInputStream(FilePath);

            int dpi = 296;
            pdDocument = PDDocument.load(inputStream);
            PDFRenderer renderer = new PDFRenderer(pdDocument);
            int pageCount = pdDocument.getNumberOfPages();
            /* dpi越大转换后越清晰,相对转换速度越慢 */
            for (int i = 0; i < pageCount; i++) {
                resultMap = new HashMap<>();
                fileName = filename + "_" + (i + 1) + "." + type;
                imgPath = fileAddress + "\\" + fileName;
                BufferedImage image = renderer.renderImageWithDPI(i, dpi);
                ImageIO.write(image, type, new File(imgPath));
                resultMap.put("fileName", fileName);
                resultMap.put("filePath", imgPath); // 图片路径

                list.add(resultMap);
            }
            long endTime = System.currentTimeMillis();
            System.out.println("共耗时:" + ((endTime - startTime) / 1000.0) + "秒");  //转化用时
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // 这里需要关闭PDDocument,不然如果想要删除pdf文件时会提示文件正在使用,无法删除的情况
                pdDocument.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public static void main(String[] args) throws FileNotFoundException {
        pdfToImage("C:\\Users\\MES\\Desktop\\", "1", "png");
    }
}

上一篇:Maven学习记录


下一篇:自动化测试 —— Pytest fixture及conftest详解