• pdf 转png (多页pdf转一张png) aspose pdf jar


    /**
    * 将pdf转图片
    */
    public static void pdfToImage(InputStream inputStream, File imgFile) {
    try {
    long old = System.currentTimeMillis();
    System.out.println("begin..............");
    Document pdfDocument = new Document(inputStream);
    Resolution resolution = new Resolution(130);
    JpegDevice jpegDevice = new JpegDevice(resolution);
    List<BufferedImage> imageList = new ArrayList<BufferedImage>();
    List<File> fileList = new ArrayList<>();
    for (int index = 1; index <= pdfDocument.getPages().size(); index++) {
    File file = File.createTempFile("tempFile", "png");
    FileOutputStream fileOS = new FileOutputStream(file);
    jpegDevice.process(pdfDocument.getPages().get_Item(index), fileOS);
    fileOS.close();
    imageList.add(ImageIO.read(file));
    fileList.add(file);
    }
    //临时文件删除
    BufferedImage mergeImage = mergeImage(false, imageList);
    ImageIO.write(mergeImage, "png", imgFile);
    long now = System.currentTimeMillis();
    System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");
    //删除临时文件
    for (File f : fileList) {
    f.delete();
    }
    } catch (Exception e) {
    e.printStackTrace();
    }

    }





    /**
    * 合并任数量的图片成一张图片
    *
    * @param isHorizontal true代表水平合并,fasle代表垂直合并
    * @param imgs 待合并的图片数组
    * @return
    * @throws IOException
    */
    public static BufferedImage mergeImage(boolean isHorizontal, List<BufferedImage> imgs) throws IOException {
    // 生成新图片
    BufferedImage destImage = null;
    // 计算新图片的长和高
    int allw = 0, allh = 0, allwMax = 0, allhMax = 0;
    // 获取总长、总宽、最长、最宽
    for (int i = 0; i < imgs.size(); i++) {
    BufferedImage img = imgs.get(i);
    allw += img.getWidth();

    if (imgs.size() != i + 1) {
    allh += img.getHeight() + 5;
    } else {
    allh += img.getHeight();
    }


    if (img.getWidth() > allwMax) {
    allwMax = img.getWidth();
    }
    if (img.getHeight() > allhMax) {
    allhMax = img.getHeight();
    }
    }
    // 创建新图片
    if (isHorizontal) {
    destImage = new BufferedImage(allw, allhMax, BufferedImage.TYPE_INT_RGB);
    } else {
    destImage = new BufferedImage(allwMax, allh, BufferedImage.TYPE_INT_RGB);
    }
    Graphics2D g2 = (Graphics2D) destImage.getGraphics();
    g2.setBackground(Color.LIGHT_GRAY);
    g2.clearRect(0, 0, allw, allh);
    g2.setPaint(Color.RED);

    // 合并所有子图片到新图片
    int wx = 0, wy = 0;
    for (int i = 0; i < imgs.size(); i++) {
    BufferedImage img = imgs.get(i);
    int w1 = img.getWidth();
    int h1 = img.getHeight();
    // 从图片中读取RGB
    int[] ImageArrayOne = new int[w1 * h1];
    // 逐行扫描图像中各个像素的RGB到数组中
    ImageArrayOne = img.getRGB(0, 0, w1, h1, ImageArrayOne, 0, w1);
    if (isHorizontal) {
    // 水平方向合并
    // 设置上半部分或左半部分的RGB
    destImage.setRGB(wx, 0, w1, h1, ImageArrayOne, 0, w1);
    } else {
    // 垂直方向合并
    // 设置上半部分或左半部分的RGB
    destImage.setRGB(0, wy, w1, h1, ImageArrayOne, 0, w1);
    }
    wx += w1;
    wy += h1 + 5;
    }


    return destImage;
    }
  • 相关阅读:
    Facebook第三方登录切换账号的问题
    内部类中调用外部变量
    前端还原UI设计图CSS样式工具(ps+蓝湖+PxCook+imgcook)
    git 报错 eslint fix found some errors. Please fix them and try committing again.
    在vue中使用vuebaidumap实现地址定位标点及关键字搜索定位(获取地址数据并获取经纬度)
    elementui的时间范围组件,限制选了第一个时间后,第二个只能在第一个的前后一周
    JavaScript API v2.0+ v3.0
    BFC块级格式化上下文
    解决小程序api无法返回promise对象
    移动端适配 视口(viewport)和vw、vh、rem
  • 原文地址:https://www.cnblogs.com/woshuaile/p/11685556.html
Copyright © 2020-2023  润新知