pdf转图片

pdf转图片png 

 public void uploadFile(HttpServletRequest request, HttpServletResponse response, MultipartFile file) {

      String fileName = file.getOriginalFilename();
         
            if(!CheckUtil.isNull(fileName) && (fileName.indexOf("PDF")>-1 ||fileName.indexOf("pdf")>-1)){
                CommonsMultipartFile cf= (CommonsMultipartFile)file;
                DiskFileItem fi = (DiskFileItem)cf.getFileItem();
                File fileTemp = fi.getStoreLocation();
                if(fileTemp!=null){
                    file=PDFReader.pdf2Pic(fileTemp.getAbsolutePath());
                }

                fileName=file.getOriginalFilename();
            }
}



PDFReader.java


package com.datacenter.jeefp.common.utils.pdf;

import com.datacenter.jeefp.common.file.FileUcloudUpdate;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.text.PDFTextStripper;
import org.icepdf.core.exceptions.PDFException;
import org.icepdf.core.exceptions.PDFSecurityException;
import org.icepdf.core.util.GraphicsRenderingHints;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.w3c.dom.Document;


import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.RenderedImage;
import java.awt.image.WritableRaster;
import java.io.*;


public class PDFReader {

    public static void main(String[] args)  {
        MultipartFile file=null;
        pdfToImage(file);
    }

    /**
     * 解析pdf文件转图片 清晰度不好
     *
     * @param
     */
    public static File  pdfToImage(MultipartFile file) {
//        File file = new File("E:/177SVANZN46741A二维码(1).PDF");
//        String fileYgtPath = "";
        File dfile=null;
        try {
            CommonsMultipartFile cf= (CommonsMultipartFile)file;
            DiskFileItem fi = (DiskFileItem)cf.getFileItem();
            File fileTemp = fi.getStoreLocation();
            PDDocument doc = PDDocument.load(fileTemp);
            PDFRenderer renderer = new PDFRenderer(doc);
            int pageCount = doc.getNumberOfPages();
            for (int i = 0; i < pageCount; i++) {
                BufferedImage image = renderer.renderImageWithDPI(i, 296);
//                BufferedImage image = renderer.renderImage(i, 2.5f);
//              fileYgtPath = FileUcloudUpdate.compressStreamToTemp(file,file.getOriginalFilename());
                ImageIO.write(image, "PNG", new File("D:\\pdfbox.png"));
                dfile = new File("D:\\pdfbox.png");
                return dfile;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return dfile;
    }
    /**
     * 解析pdf文件转图片 清晰度不好
     *
     * @param
     */
    public static MultipartFile pdf2Pic(String pdfPath){
        org.icepdf.core.pobjects.Document document = new org.icepdf.core.pobjects.Document();
        MultipartFile mfile=null;
        try {
            document.setFile(pdfPath);
            //缩放比例
            float scale = 2.5f;
            //旋转角度
            float rotation = 0f;
            BufferedImage image = (BufferedImage)
                    document.getPageImage(0, GraphicsRenderingHints.SCREEN, org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation, scale);
//            RenderedImage rendImage = image;
            try {
                File file = new File("D:\\pdfbox.png");
                ImageIO.write(image, "png", file);
            } catch (IOException e) {
                e.printStackTrace();
            }
            image.flush();
            document.dispose();
            File dfile = new File("D:\\pdfbox.png");
            FileInputStream fileInputStream = new FileInputStream(dfile);
            mfile = new MockMultipartFile(dfile.getName(), dfile.getName(),
                    "image/png", fileInputStream);
            return mfile;
        }catch(Exception e){
            e.printStackTrace();
        }
        return mfile;
    }
}

 

 

发布了13 篇原创文章 · 获赞 5 · 访问量 1万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览