当前位置:   article > 正文

Spring Boot pdf文件转图片_springboot pdf转长图

springboot pdf转长图

PDF转图片工具类
亲测可以使用,无需下载其他jar包

pom.xml

        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.20</version>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

PDFToIMG.java


import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Iterator;
import java.util.List;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;

import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.springframework.stereotype.Component;

/**
 * @description: PDF转图片工具类
 * @author: LCHYUE
 * @time: 2021/6/15 13:35
 */
@Component
public class PDFToIMG {

    public final static String IMG_TYPE_JPG = "jpg";
    public final static String IMG_TYPE_PNG = "png";

//    public static void main(String[] args) throws IOException {
//        App app = new App();
//        app.pdf2img("D:\\测试文件.pdf", "D:", IMG_TYPE_PNG);
    		PDDocument pdDocument = app.pdfInfo("D:\\api.pdf");
//    }


    /**
     * @param pdfPath  pdf文件的路径
     * @param savePath 图片保存的地址
     */
    public void pdf2img(String pdfPath, String savePath) {
        //imgType  图片保存方式
        String imgType = IMG_TYPE_PNG;
//        String fileName = pdfPath.substring(pdfPath.lastIndexOf("\\") + 1, pdfPath.length());/*---window---*/
        String fileName = pdfPath.substring(pdfPath.lastIndexOf(File.separator) + 1, pdfPath.length());/*---linux---*/
        fileName = fileName.substring(0, fileName.lastIndexOf("."));
        InputStream is = null;
        PDDocument pdDocument = null;
        try {
            is = new BufferedInputStream(new FileInputStream(pdfPath));
            PDFParser parser = new PDFParser(is);
            parser.parse();
            pdDocument = parser.getPDDocument();
            List pages = pdDocument.getDocumentCatalog().getAllPages();
            for (int i = 0; i < pages.size(); i++) {
//                String saveFileName = savePath + "\\" + fileName + i + "." + imgType; /*---window---*/
                String saveFileName = savePath + File.separator + fileName + i + "." + imgType;/*---linux---*/
                PDPage page = (PDPage)pages.get(i);
                pdfPage2Img(page, saveFileName, imgType);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (pdDocument != null) {
                try {
                    pdDocument.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
    /**
     * pdf页转换成图片
     *
     * @param page
     * @param saveFileName
     * @throws IOException
     */
    public void pdfPage2Img(PDPage page, String saveFileName, String imgType) throws IOException {
        BufferedImage img_temp = page.convertToImage();
        Iterator it = ImageIO.getImageWritersBySuffix(imgType);
        ImageWriter writer = (ImageWriter) it.next();
        ImageOutputStream imageout = ImageIO.createImageOutputStream(new FileOutputStream(saveFileName));
        writer.setOutput(imageout);
        writer.write(new IIOImage(img_temp, null, null));
    }


    public PDDocument pdfInfo(String filePath) throws IOException {
        InputStream is = new BufferedInputStream(new FileInputStream(filePath));
        PDFParser parser = new PDFParser(is);
        parser.parse();
        PDDocument pdDocument = parser.getPDDocument();
        System.out.println("pageNum:" + pdDocument.getNumberOfPages());
        return pdDocument;
    }


    public void createPdf() throws COSVisitorException, IOException {
        PDDocument document = new PDDocument();
        PDPage blankPage = new PDPage();
        document.addPage(blankPage);
        document.save("D:\\test.pdf");
        document.close();
    }
}


  • 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
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/1007894
推荐阅读
相关标签
  

闽ICP备14008679号