java中将PDF转图片格式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011301815/article/details/59181908
package test;
import java.awt.image.BufferedImage;  
import java.awt.image.RenderedImage;  
import java.io.File;  
import java.io.IOException;  
import javax.imageio.ImageIO;  
  
import org.icepdf.core.pobjects.Document;  
import org.icepdf.core.util.GraphicsRenderingHints;  
/*  
 * yuashishi
 * 2017-02-27 13:34
 * pdf 转 图片  
 */  
public class Icepdf_PDF_TO_IMG {  
 String  PDF_To_imgName = "";//生成的图片名字
 String  img_postfix = "";//生成图片的后缀名如.jpg\.png
 String  pdfPath = "";//PDF路径
 String  path = "";//图片保存路径
 String  result = "false";//是否转换成功 false\success
 String  PDF_To_imgName_var = "";//临时变量 图片名称
 
/**
* yuanshishi
* 2017-02-27 12:05 
* PDF转换成图片格式 .jpg\.png
* @param args
     * 图片名称前部分    如果有多张就会:图片名称前部分+0、1、2、3.....jpg
     * PDF_To_imgName 生成的图片名字
     * img_postfix    生成图片的后缀名如.jpg\.png
     * pdfPath        PDF路径
     * path           图片保存路径
     * PDF_To_imgName_var 临时变量 可能会
     * 
     */
    public  String  pdf_toConvert_img(String PDF_To_imgName,String img_postfix,String pdfPath, String path){  
    Document document = new Document();  
    try {
    document.setFile(pdfPath);  
} catch (Exception e) {
System.out.println("*********java.io.FileNotFoundException,系统找不到指定的文件。*********");
}
   
        float scale = 2.5f;//缩放比例  
        float rotation = 0f;//旋转角度  
        int totalPage = document.getNumberOfPages();//获取PDF总页数
        System.out.println("文件总页数totalPage为:========"+totalPage+"页");
        for (int i = 0; i < totalPage; i++) {  
            BufferedImage image = (BufferedImage)  
            document.getPageImage(i, GraphicsRenderingHints.SCREEN, org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation, scale);  
            RenderedImage rendImage = image;  
            try {  
            if(PDF_To_imgName==null || "".equals(PDF_To_imgName)){
            PDF_To_imgName_var = i +img_postfix;  //0.jpg 等等
            }else{
            if(totalPage>1){//大于1页存在有多页数据时  将通过传输的名字+加上每页的页数
            PDF_To_imgName_var = PDF_To_imgName+"_"+(i+1)+img_postfix;  
            }else{
            //只有一页数据时 将通过传送的名字当做图片名称
            PDF_To_imgName_var = PDF_To_imgName+img_postfix;  
            }
           
            }
               
                System.out.println("PDF_To_imgName========="+PDF_To_imgName_var);  
                File file = new File(path + PDF_To_imgName_var);  
                ImageIO.write(rendImage, "jpg", file); 
                result="success";//返回转换成功的结果
            } catch (IOException e) {  
                e.printStackTrace();  
                System.out.println("==================PDF格式转换"+img_postfix+"格式失败!=====result=="+result);  
                return result;  //返回转换失败
            }  
            image.flush();  
        }  
        document.dispose();
        System.out.println("=======转换的结果======result==="+result);
        return result; 
    }  
    
    public static void main(String[] args) {  
        String imgName = "201702271210";  //生成的图片名字
           String img_postfix = ".jpg";  //生成图片的后缀名如.jpg\.png
           //String PDFfilePath = "F:\\PDFtoJPG\\ATM密码键盘解析层接口文档.pdf"; //PDF路径
           String PDFfilePath = "F:\\PDFtoJPG\\0109091732.pdf"; //PDF路径
           
           String path = "F:\\PDFtoJPG\\";  // 图片保存路径
           Icepdf_PDF_TO_IMG obj = new Icepdf_PDF_TO_IMG();
          String ret= obj .pdf_toConvert_img(imgName, img_postfix,PDFfilePath,path);
          System.out.println("========ret====="+ret);
          
    }  
}  

猜你喜欢

转载自blog.csdn.net/u011301815/article/details/59181908