Java实现pdf转图片案例

作者:简若宁 时间:2022-08-11 21:45:41 

工程加入依赖:


<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.15</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox-tools</artifactId>
<version>2.0.15</version>
</dependency>

pdf文件转图片:


   public static List<String> pdf2Img(File pdfFile) {
       if (pdfFile == null || !pdfFile.exists()) {
           throw new RuntimeException("pdf文件不能为空");
       }
       String name = pdfFile.getName().substring(0, pdfFile.getName().lastIndexOf("."));
       String targetPath = pdfFile.getParent() + File.separator + name;
       List<String> imgList = new ArrayList<>();
       try {
           PDDocument doc = PDDocument.load(pdfFile);
           // 页数
           int pageCount = doc.getNumberOfPages();
           PDFRenderer pdfRenderer = new PDFRenderer(doc);
           for (int i = 0; i < pageCount; i++) {
               File targetFile = new File(targetPath + File.separator + name + "-" + (i + 1) + ".jpg");
               if (!targetFile.getParentFile().exists()) {
                   FileUtil.mkdir(targetFile.getParentFile());
               }
               pdfRenderer.renderImage(i);
               BufferedImage image = pdfRenderer.renderImageWithDPI(i, 105, ImageType.RGB);
               ImageIOUtil.writeImage(image, targetFile.getPath(), 105);
               imgList.add(targetFile.getPath());
           }
       } catch (IOException e) {
           log.error("文件转换异常", e);
           throw new RuntimeException("文件转换异常,err=" + e.getMessage());
       }

pdf转成一张图片:


   /**
    * pdf转成一张图片
    *
    * @param pdfFile pdf图片文件
    * @return 图片地址
    */
   public static String pdf2OneImg(File pdfFile) {

List<String> imgs = pdf2Img(pdfFile);
       int len = imgs.size();
       File[] src = new File[len];
       BufferedImage[] images = new BufferedImage[len];
       int[][] ImageArrays = new int[len][];
       for (int i = 0; i < len; i++) {
           try {
               src[i] = new File(imgs.get(i));
               if (!src[i].exists()) {
                   throw new RuntimeException("文件【" + imgs.get(i) + "】不存在");
               }
               images[i] = ImageIO.read(src[i]);
           } catch (Exception e) {
               log.error("", e);
               throw new RuntimeException(e);
           }
           int width = images[i].getWidth();
           int height = images[i].getHeight();
           // 从图片中读取RGB 像素
           ImageArrays[i] = new int[width * height];
           ImageArrays[i] = images[i].getRGB(0, 0, width, height, ImageArrays[i], 0, width);
       }

int dst_height = 0;
       int dst_width = images[0].getWidth();
       // 合成图片像素
       for (int i = 0; i < images.length; i++) {
           dst_width = dst_width > images[i].getWidth() ? dst_width : images[i].getWidth();
           dst_height += images[i].getHeight();
       }
       if (dst_height < 1) {
           throw new RuntimeException("文件合成失败,合成后的图片文件高度=" + dst_height);
       }
       String name = pdfFile.getName().substring(0, pdfFile.getName().lastIndexOf("."));
       String targetPath = pdfFile.getParent() + File.separator + name;
       // 输出路径
       File outFile = new File(targetPath + File.separator + name + "-bigone.jpg");
       // 生成新图片
       try {
           dst_width = images[0].getWidth();
           BufferedImage ImageNew = new BufferedImage(dst_width, dst_height, BufferedImage.TYPE_INT_RGB);
           int height_i = 0;
           for (int i = 0; i < images.length; i++) {
               ImageNew.setRGB(0, height_i, dst_width, images[i].getHeight(), ImageArrays[i], 0, dst_width);
               height_i += images[i].getHeight();
           }
           ImageIO.write(ImageNew, "jpg", outFile);
       } catch (Exception e) {
           log.error("图片合并异常=", e);
           throw new RuntimeException(e);
       }
       return outFile.getPath();
   }

来源:https://blog.csdn.net/tanzhming/article/details/117930788

标签:Java,pdf,图片
0
投稿

猜你喜欢

  • Java超详细讲解设计模式中的命令模式

    2023-07-26 15:23:11
  • IDEA全量替换一次性解决旧项目并将所有文件换行符改为LF问题

    2022-09-17 18:44:32
  • Netty分布式ByteBuf使用的回收逻辑剖析

    2023-07-18 23:26:26
  • Android实现图片设置圆角形式

    2023-07-29 16:37:32
  • 深入解析C#编程中struct所定义的结构

    2022-03-01 12:43:08
  • Springboot集成JSR303参数校验的方法实现

    2023-02-06 06:34:44
  • Android入门之实现自定义Adapter

    2021-09-30 17:34:10
  • C#画笔Pen绘制光滑模式曲线的方法

    2021-11-26 05:13:33
  • 解析Android游戏中获取电话状态进行游戏暂停或继续的解决方法

    2023-09-09 00:31:09
  • Spring中自动注入的两种方式总结

    2022-10-24 08:54:14
  • SpringBoot注册Servlet的三种方法详解

    2022-06-15 15:56:29
  • SrpingDruid数据源加密数据库密码的示例代码

    2021-06-21 03:26:26
  • java安全编码指南之:表达式规则说明

    2021-06-01 17:48:44
  • C#实现洗牌游戏实例

    2021-07-07 00:45:29
  • 基于spring data jpa @query返回map的踩坑记录

    2023-11-27 22:51:00
  • 学习Java HashMap,看这篇就够了

    2023-11-11 11:18:56
  • 详解Android控件状态依赖框架

    2023-07-01 10:01:06
  • Android语音识别技术详解及实例代码

    2022-09-23 03:46:33
  • java poi sax方式处理大数据量excel文件

    2021-09-19 19:28:48
  • SpringBoot如何实现word文档转pdf

    2023-04-19 09:33:55
  • asp之家 软件编程 m.aspxhome.com