zoukankan      html  css  js  c++  java
  • 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找不到

  • 相关阅读:
    android Intent的常用flags
    android 判断当前界面是否是桌面
    "****" is not translated in zh, zh_CN.的解决方法
    sqlite实现oracle的rownum功能
    android 滚动的缓冲图片
    Java 泛型通配符详解
    SpringBoot(审计) 统计接口调用次数及成功率
    flume自定义反序列化器deserializer
    flume自定义拦截器实现添加IP
    SpringMVC的简单应用
  • 原文地址:https://www.cnblogs.com/shangrongyiweng/p/4210101.html
Copyright © 2011-2022 走看看