当前位置:   article > 正文

java实现将PDF文件拆分成图片_java pdf 拆分图片

java pdf 拆分图片

利用java实现将PDF文件拆分成图片

实现代码如下:

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Description pdf工具类
 * @ClassName PdfUtils
 * @Author yuhuofei
 * @Date 2022/3/16 20:21
 * @Version 1.0
 */
public class PdfUtils {

    /**
     * 将PDF文档拆分成多张图片,并返回所有图片的路径
     *
     * @param pdfPath
     * @param pictureFolderPath
     * @return
     * @throws Exception
     */
    public static List<String> pdfSwitchToPicture(String pdfPath, String pictureFolderPath) throws Exception {
        List<String> picUrlList = new ArrayList<>();
        File file = new File(pictureFolderPath);
        if (!file.exists()) {
            file.mkdirs();
        }
        String pictureRootName = file.getName() + "_";
        List<byte[]> imageList = handlePdf(pdfPath);
        AtomicInteger pictureNameNumber = new AtomicInteger(1);
        for (byte[] image : imageList) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byteArrayOutputStream.write(image);
            String pictureUrl = file.getAbsolutePath() + File.separator + pictureRootName + pictureNameNumber.getAndIncrement() + ".jpg";
            byteArrayOutputStream.writeTo(new FileOutputStream(pictureUrl));
            picUrlList.add(pictureUrl);
            byteArrayOutputStream.close();
        }
        return picUrlList;
    }

    /**
     * 处理PDF文档
     *
     * @param pdfPath
     * @return
     * @throws Exception
     */
    public static List<byte[]> handlePdf(String pdfPath) throws Exception {
        File pdfFile = new File(pdfPath);
        //加载PDF文档
        PDDocument pdDocument = PDDocument.load(pdfFile);
        //创建PDF渲染器
        PDFRenderer pdfRenderer = new PDFRenderer(pdDocument);
        int pageNum = pdDocument.getNumberOfPages();
        List<byte[]> list = new ArrayList<>();
        for (int i = 0; i < pageNum; i++) {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            //将PDF的每一页渲染成一张图片,并放大1.5倍
            BufferedImage image = pdfRenderer.renderImage(i, 1.5F);
            ImageIO.write(image, "jpg", outputStream);
            list.add(outputStream.toByteArray());
            outputStream.close();
        }
        pdDocument.close();
        return list;
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78

需要引入的jar包:

<dependency>
     <groupId>org.apache.pdfbox</groupId>
     <artifactId>pdfbox</artifactId>
     <version>2.0.24</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/530666
推荐阅读
相关标签
  

闽ICP备14008679号