360SDN.COM

首页/Java/列表

用PDFRenderer将pdf转成png

来源:  2017-08-25 15:10:06    评论:0点击:

    package com.vi.doc;  
      
    import java.awt.Image;  
    import java.awt.Rectangle;  
    import java.awt.image.BufferedImage;  
    import java.io.File;  
    import java.io.IOException;  
    import java.io.RandomAccessFile;  
    import java.nio.ByteBuffer;  
    import java.nio.channels.FileChannel;  
      
    import javax.imageio.ImageIO;  
    import javax.imageio.stream.FileImageOutputStream;  
      
    import com.sun.pdfview.PDFFile;  
    import com.sun.pdfview.PDFPage;  
      
    public class Pdf2Png {  
        public static String[] setup(String pdfPathName,String pathout) throws IOException {  
            PDFFile pdffile = getPDFFile(pdfPathName);  
              
            System.out.println("--->Pages="+pdffile.getNumPages());  
              
            FileImageOutputStream fios = null;  
            int len = pdffile.getNumPages();  
            String[] pathPngs = new String[len];  
            for(int i=1;i<=len;i++){  
                // draw the first page to an image  
                PDFPage page = pdffile.getPage(i);  
                // get the width and height for the doc at the default zoom  
                Rectangle rect = new Rectangle(0, 0,(int) page.getBBox().getWidth(),  
                        (int) page.getBBox().getHeight() );  
      
                // generate the image  
                Image img = page.getImage(rect.width, rect.height, // width & height  
                        rect, // clip rect  
                        null, // null for the ImageObserver  
                        true, // fill background with white  
                        true // block until drawing is done  
                        );  
                String pathPng = pathout+"/"+i+".png";    
                mkdir(pathout);  
                pathPngs[i-1] = pathPng;  
                fios = new FileImageOutputStream(new File(pathPng));  
                BufferedImage bImg = (BufferedImage)img;  
                ImageIO.write(bImg, "png", fios);  
                fios.flush();  
            }  
              
            fios.close();  
              
            return pathPngs;  
        }  
          
        private static PDFFile getPDFFile(String pathname) throws IOException{  
            File file = new File(pathname);  
            RandomAccessFile raf = new RandomAccessFile(file, "r");  
            FileChannel channel = raf.getChannel();  
            ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());  
            PDFFile pdffile = new PDFFile(buf);  
              
            return pdffile;  
        }  
        private static void mkdir(String path){  
            File f = new File(path);  
            if(!f.exists()||!f.isDirectory()){  
                f.mkdirs();  
            }  
        }  
    }
为您推荐

友情链接 |九搜汽车网 |手机ok生活信息网|ok生活信息网|ok微生活
 Powered by www.360SDN.COM   京ICP备11022651号-4 © 2012-2016 版权