pdf转换成图片

我的是maven项目,需求是把上传的pdf转换成一张一张的图片

一、下面是加入的jar包

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

        </dependency>

二、写一个工具类

  package com.sojson.common.utils;

import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

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;

public class CovertPDFFileToImageUtil {

     /**
     *  
     * @param pdfPath pdf文件的路径
     * @param savePath 图片保存的地址
     * @param imgType 图片保存方式
     */  
    public  static  Map<String,Object> pdf2img(String pdfPath,String savePath,String imgType){  
        
         Map<String,Object> map=new HashMap<String, Object>();
         boolean converReturnResult=false;//是否全部转成功  
        List<String> imgNamesList=new ArrayList<String>();//PDF转成图片后所有名称集合  
        
        String fileName = pdfPath.substring(pdfPath.lastIndexOf("\\")+1, pdfPath.length());  
        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();  
            @SuppressWarnings("unchecked")
            List<PDPage> pages = pdDocument.getDocumentCatalog().getAllPages();  
            
            //判断要存取的ppt图片文件是存在,不存在则创建一个
            File dir = new File(savePath);
            if(!dir.exists()){
                dir.mkdirs();
            }
            
            for (int i = 0; i < pages.size(); i++) {  
              //  String saveFileName = savePath+"\\"+fileName+i+"."+imgType;  
                
                String imgName=(i+1)+"."+imgType;
                imgNamesList.add(imgName);//将图片名称添加的集合中  
                
                String saveFileName = dir+"\\"+imgName;  
                
                PDPage page =  pages.get(i);  
                pdfPage2Img(page,saveFileName,imgType);  
            }  
            
            converReturnResult=true;  
            map.put("converReturnResult", converReturnResult);  
            map.put("imgNames", imgNamesList);  
            
        } catch (Exception e) {  
            e.printStackTrace();  
            map.put("converReturnResult", converReturnResult);  
            return map;  
        }finally{  
            if(pdDocument != null){  
                try {  
                    pdDocument.close();  
                } catch (IOException e) {  
                    e.printStackTrace();
                    map.put("converReturnResult", converReturnResult);  
                    return map;  
                }  
            }
            
            
        }  
          
        return map;
          
    }  
      
    /**
     * pdf页转换成图片
     * @param page       
     * @param saveFileName
     * @throws IOException
     */  
    public static void pdfPage2Img(PDPage page,String saveFileName,String imgType) throws IOException{  
        BufferedImage img_temp  = page.convertToImage();  
        Iterator<ImageWriter> 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();  
    }  
    

}


三、直接在需要的地方调用就可以了




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值