码晓林 2022-01-05 17:36 采纳率: 0%
浏览 75
已结题

java按页数分割pdf文件

java在pdf文件按页分割时,如何将目标页转成流,最后加密返回字符串?

img

  • 写回答

1条回答 默认 最新

  • 神仙别闹 2022-01-05 17:53
    关注

    参考下这个

    package com.ces.component.pictrueCut.entity;
    import java.awt.Image;  
    import java.awt.Rectangle;  
    import java.awt.image.BufferedImage;  
    import java.io.File;  
    import java.io.FileNotFoundException;  
    import java.io.FileOutputStream;  
    import java.io.IOException;  
    import java.io.RandomAccessFile;  
    import java.lang.reflect.Method;  
    import java.nio.MappedByteBuffer;  
    import java.nio.channels.FileChannel;  
    import java.security.AccessController;  
    import java.security.PrivilegedAction;  
      
    import com.sun.image.codec.jpeg.JPEGCodec;  
    import com.sun.image.codec.jpeg.JPEGEncodeParam;  
    import com.sun.image.codec.jpeg.JPEGImageEncoder;  
    import com.sun.pdfview.PDFFile;  
    import com.sun.pdfview.PDFPage; 
    public class pdfToImage {
    
        public static int changePdfToImg(String instructiopath,String picturepath) {  
            int countpage =0;  
            try {  
                //instructiopath ="D:/instructio/2015-05-16/Android 4编程入门经典.pdf"  
                //picturepath = "D:/instructio/picture/2015-05-16/";  
                  
                File file = new File(instructiopath);  
                RandomAccessFile raf = new RandomAccessFile(file, "r");  
                FileChannel channel = raf.getChannel();  
                MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY,  
                        0, channel.size());  
                PDFFile pdffile = new PDFFile(buf);  
                //创建图片文件夹  
                File dirfile = new File(picturepath);  
                   if(!dirfile.exists()){  
                         dirfile.mkdirs();  
                    }  
                //获得图片页数  
                countpage = pdffile.getNumPages();  
                for (int i = 1; i <= pdffile.getNumPages(); i++) {  
                    PDFPage page = pdffile.getPage(i);  
                    Rectangle rect = new Rectangle(0, 0, ((int) page.getBBox()  
                            .getWidth()), ((int) page.getBBox().getHeight()));  
                    int n = 2;  
                    /** 图片清晰度(n>0且n<7)【pdf放大参数】 */  
                    Image img = page.getImage(rect.width * n, rect.height * n,  
                            rect, /** 放大pdf到n倍,创建图片。 */  
                            null, /** null for the ImageObserver */  
                            true, /** fill background with white */  
                            true /** block until drawing is done */  
                    );  
                    BufferedImage tag = new BufferedImage(rect.width * n,  
                            rect.height * n, BufferedImage.TYPE_INT_RGB);  
                    tag.getGraphics().drawImage(img, 0, 0, rect.width * n,  
                            rect.height * n, null);  
                    /** 
                     * File imgfile = new File("D:\\work\\mybook\\FilesNew\\img\\" + 
                     * i + ".jpg"); if(imgfile.exists()){ 
                     * if(imgfile.createNewFile()) { System.out.println("创建图片:"+ 
                     * "D:\\work\\mybook\\FilesNew\\img\\" + i + ".jpg"); } else { 
                     * System.out.println("创建图片失败!"); } } 
                     */  
                    FileOutputStream out = new FileOutputStream(picturepath+"/" + i  
                            + ".png");  
                    /** 输出到文件流 */  
                    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);  
                    JPEGEncodeParam param2 = encoder.getDefaultJPEGEncodeParam(tag);  
                    param2.setQuality(1f, true);  
                    /** 1f~0.01f是提高生成的图片质量 */  
                    encoder.setJPEGEncodeParam(param2);  
                    encoder.encode(tag);  
                    /** JPEG编码 */  
                    out.close();  
                }  
                channel.close();  
                raf.close();  
                unmap(buf);  
                /** 如果要在转图片之后删除pdf,就必须要这个关闭流和清空缓冲的方法 */  
            } catch (FileNotFoundException e) {  
                e.printStackTrace();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
            return countpage;  
              
        }  
      
        @SuppressWarnings("unchecked")  
        public static void unmap(final Object buffer) {  
            AccessController.doPrivileged(new PrivilegedAction() {  
                public Object run() {  
                    try {  
                        Method getCleanerMethod = buffer.getClass().getMethod(  
                                "cleaner", new Class[0]);  
                        getCleanerMethod.setAccessible(true);  
                        sun.misc.Cleaner cleaner = (sun.misc.Cleaner) getCleanerMethod  
                                .invoke(buffer, new Object[0]);  
                        cleaner.clean();  
                    } catch (Exception e) {  
                        e.printStackTrace();  
                    }  
                    return null;  
                }  
            });  
        }  
    }
    
    
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 1月5日
  • 创建了问题 1月5日

悬赏问题

  • ¥15 非科班怎么跑代码?如何导数据和调参
  • ¥15 福州市的全人群死因监测点死亡原因报表
  • ¥15 打开powerpont询问是否安装officeplus不小心点了不安装以后再也不提示是否安装了
  • ¥15 Altair EDEM中生成一个颗粒,并且各个方向没有初始速度
  • ¥15 系统2008r2 装机配置推荐一下
  • ¥500 服务器搭建cisco AnyConnect vpn
  • ¥15 悬赏Python-playwright部署在centos7上
  • ¥15 psoc creator软件有没有人能远程安装啊
  • ¥15 快速扫描算法求解Eikonal方程咨询
  • ¥20 我的是道格手机,重置后屏幕右上角出现红色字的未写入tee key 和未写入google key请问怎么去掉啊