PDF文档生成缩略图

public class SmallImage {
    
    /**
     * PDF缩略图
    * @Method PDFToImage
    * @Description TODO
    * @param pdfpath PDF文档路径
    * @param outImagePath 输出图片的文件夹路径,如果没有会自动创建
    * @param imgWidth 缩略图宽度,0按照文档宽度
    * @param imgHeight 缩略图高度,0按照文档高度
    * @param pageNum 需要打印的页数,0打印所有,其他打印对应页数
    * @Return void
     */
    public static void PDFToImage(String pdfPath, String outImgPath, int imgWidth, int imgHeight, int outImgNum){
        try {
            File file = new File(pdfPath);
            if(!file.exists()){
                System.out.println("PDF文档不存在!");
            }
            RandomAccessFile raf = new RandomAccessFile(file, "r");//r表示只读
            FileChannel channel = raf.getChannel();
            ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());  
            PDFFile pdffile = new PDFFile(buf);
            
            System.out.println("PDF页数: " + pdffile.getNumPages());  
            
            int outImageNum = pdffile.getNumPages();
            if(outImgNum!=0){
                outImageNum = outImgNum;
            }
            for (int i=1; i<=outImageNum; i++){
                PDFPage page = pdffile.getPage(i);
                Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox().getHeight()); 
                int width = rect.width;
                int height = rect.height;
                if(imgWidth!=0){
                    width = imgWidth;
                }
                if(imgHeight!=0){
                    height = imgHeight;
                }
                Image img = page.getImage(width, height,rect,null,true,true);
                BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
                tag.getGraphics().drawImage(img, 0, 0, width, height, null);
                File imgPath = new File(outImgPath);
                //如果图输出文件夹不存在,创建文件夹
                if(!imgPath.exists()){
                    imgPath.mkdir();
                }
                FileOutputStream out = new FileOutputStream(imgPath.toString()+"//"+file.getName()+"_"+i+".jpg"); // 输出到文件流  
                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);  
                encoder.encode(tag); // JPEG编码  
                out.close();  
            }  
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        SmallImage.PDFToImage("c://2.pdf", "c://picture", 240, 338, 1);
    }
}

 用到的额外jar包:pdf-renderer-1.0.5.jar,下载地址:http://pan.baidu.com/s/1pJC26Wb

备注:jdk需要1.6,否则JPEGImageEncoder找不到

转载于:https://www.cnblogs.com/shangrongyiweng/p/4210101.html

WIN7 X64 PDF缩略图 补丁软件 Introduction: This page contains simple fixes for Adobe's PDF preview handler and thumbnails on 64-bit versions of Windows. Before I looked into this, people had been complaining about it for over two years with no official response. Only the thumbnails fix is still needed, except if you upgraded from Vista to Windows 7, where both fixes may still be needed. Half a year after I published the information, Adobe finally incorporated the preview handler fix (but no thumbnails fix, and they leave the preview handler broken if you upgraded from Vista to Windows 7) into the installer for Adobe Reader 9.3.2 (April 2010). Maybe in 2011 Adobe will manage to fix the thumbnails as well; until then you can get my fix for them below. :-) Preview Handler Preview handlers are lightweight components which let you view various file types within programs like Windows Explorer (in Windows Vista and Windows 7), Outlook 2007/2010 and Directory Opus (via my bundled plugin). Adobe Reader comes with Adobe's PDF preview handler but the installer had a mistake which meant the preview handler did not work on 64-bit systems. It turned out the problem could be fixed via a simple registry change. The change is described below and a small program which performs the fix is also provided for your convenience. Unlike the thumbnail fix, described below, the preview handler fix should no longer be needed by most people, since Adobe have finally fixed their installer, but it's still provided in case it helps repair things. If you had Adobe Reader installed under Vista and later upgraded to Windows 7 then you will probably still need to run the preview handler fix once to clean up a registry value which Adobe set inconsistently between the two OS versions. If in doubt, run the fix and it will tell you if anything needs to be done. If everything is good already then the preview handler fix won't change anything.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值