PDF转换成图片

下面代码引用icepdf-core-4.1.1.jar,可到此链接下载:
https://download.csdn.net/download/bacoder/10457873
计算机小白想要这个工具的话可以到此链接下载:
https://download.csdn.net/download/bacoder/10457851
积分可以通过做任务获取,任务链接:http://task.csdn.net/

/**
 * @Description:PDF转换成图片
 * @author  bacoder
 */
package com.belong.pdf;

import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileSystemView;

import org.icepdf.core.exceptions.PDFException;
import org.icepdf.core.exceptions.PDFSecurityException;
import org.icepdf.core.pobjects.Document;
import org.icepdf.core.util.GraphicsRenderingHints;

/**
 * @Description:TODO
 * @author:bacoder
 * @time:2017年5月13日 下午3:24:51
 */
public class PDFToPhoto {

    /* 转换的log信息 */
    private StringBuffer message;

    /**
     * 文件选择
     * @return void
     */
    public void chooseFile(){
        // 文件选择器
        JFileChooser jfChooser = new JFileChooser();
        // 设置文件选择类型
        jfChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        // 设置是否可以多选
        jfChooser.setMultiSelectionEnabled(true);
        // 去除默认的所有文件选择器
//      jfChooser.removeChoosableFileFilter(jfChooser.getAcceptAllFileFilter());
        // 添加PDF文件选择器
        jfChooser.setFileFilter(new FileFilter(){
            public boolean accept(File f)
            {
                // 必须有  要不然选择器什么也不显示
                if (f.isDirectory())
                {
                    return true;
                }
                int len = f.getName().length();
                //后缀名
                String extension = f.getName().substring(len - 3,len).toUpperCase();
                if (extension != null)
                {
                    return extension.toUpperCase().equals("PDF")? true:false;
                }
                return false;
            }

            public String getDescription()
            {
                return "PDF文件(*.pdf,*.PDF)";
            }
        });
        // 设置选择器的title
        jfChooser.setDialogTitle("请选择PDF文件");
        // 将桌面设置为默认打开路径
        FileSystemView fsv = FileSystemView.getFileSystemView(); 
        jfChooser.setCurrentDirectory(fsv.getHomeDirectory());

        // 显示文件选择器
        int state = jfChooser.showDialog(new JLabel(), "选择");

        // 点击取消按钮
        if (state == jfChooser.CANCEL_OPTION){
            return;
        }

        // 获取选中的文件
        File[] files = jfChooser.getSelectedFiles();

        // 遍历文件进行转换
        for (File file:files){
            // 文件转换
            this.turning(file);
        }
        JOptionPane.showMessageDialog(null,"转换完成!","提示消息",JOptionPane.WARNING_MESSAGE);
    }

    /**
     * PDF转换成图片
     * @param file
     * @return true:转换成功,false:转换失败
     */
    public boolean turning(File file){

        String filePath = file.getPath();
        if (null == filePath || "".equals(filePath)){
            return false;
        }

        System.out.println(filePath);

        File pdfFile = new File(filePath);

        if (!pdfFile.exists()){
            message = message.append("文件<" + pdfFile.getPath() + ">" + "不存在!请确认。。。" + "\n");
            return false;
        }

        Document document = new Document();
        try {

            document.setFile(filePath);

            float scale = 2.5f;// 缩放比例
            float rotation = 0f;// 旋转角度

            for (int i = 0; i < document.getNumberOfPages(); i++) {
                BufferedImage image = (BufferedImage) document.getPageImage(i, GraphicsRenderingHints.SCREEN,
                        org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation, scale);
                RenderedImage rendImage = image;

                File imgFile = new File(pdfFile.getParent() + "/" + pdfFile.getName() 
                + "__" + i + ".jpg");
                // 转换
                ImageIO.write(rendImage, "png", imgFile);
                image.flush();
            }

            return true;
        } catch (PDFException | PDFSecurityException | IOException e1) {
            JOptionPane.showMessageDialog(null,e1.getMessage(),"提示消息",JOptionPane.WARNING_MESSAGE);

            return false;

        }finally{
            document.dispose();
        }
    }

    /**
     * @Description:PDF转换成图片
     * @param args
     * void
     */
    public static void main(String[] args){

        PDFToPhoto pdfToPhoto = new PDFToPhoto();

        try {
            // 设置成系统默认的文件选择器样式
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
                | UnsupportedLookAndFeelException e) {
        }
        pdfToPhoto.chooseFile();
    }
}

猜你喜欢

转载自blog.csdn.net/bacoder/article/details/76038658