zoukankan      html  css  js  c++  java
  • pdf工具类之获取pdf的总页数以及每页的宽度和高度

    没啥可说的,毫无技术的水贴

     1     /**
     2      * 获取pdf的总页数以及每页的宽度和高度
     3      *
     4      * @param filePath
     5      * @return Map<String, List<Map<String, String>>>
     6      * @author 龙谷情
     7      * @date 2020/6/25 19:47
     8      * @exception/throws [异常类型] [异常说明]
     9      * @since [v1.0]
    10      */
    11     public static Map<String, List<Map<String, String>>> getPageNumByPath(String filePath) {
    12         Map resultMap = new HashMap(16);
    13         List<Map<String, String>> pageList = new ArrayList<>();
    14         int totalPages = 0;
    15         FileInputStream inputStream = null;
    16         PdfReader pdfReader = null;
    17         try {
    18             File file = new File(filePath);
    19             inputStream = new FileInputStream(file);
    20             pdfReader = new PdfReader(inputStream);
    21             totalPages = pdfReader.getNumberOfPages();
    22             for (int pageNum = 1; pageNum <= totalPages; pageNum++) {
    23                 Map<String, String> pageMap = new HashMap<>(16);
    24                 float width = pdfReader.getPageSize(pageNum).getWidth();
    25                 float height = pdfReader.getPageSize(pageNum).getHeight();
    26                 pageMap.put("pageNum", String.valueOf(pageNum));
    27                 pageMap.put("width", String.valueOf(width));
    28                 pageMap.put("height", String.valueOf(height));
    29                 pageList.add(pageMap);
    30             }
    31             resultMap.put("totalPages", totalPages);
    32             resultMap.put("pageList", pageList);
    33         } catch (FileNotFoundException e) {
    34             e.printStackTrace();
    35         } catch (IOException e) {
    36             e.printStackTrace();
    37         } finally {
    38             if (null != inputStream) {
    39                 try {
    40                     inputStream.close();
    41                 } catch (IOException e) {
    42                     e.printStackTrace();
    43                 }
    44             }
    45             if (null != pdfReader) {
    46                 pdfReader.close();
    47             }
    48         }
    49         return resultMap;
    50     }
    昔日我曾苍老,如今风华正茂(ง •̀_•́)ง
  • 相关阅读:
    tensorflow实践学习一
    计算CPU的MIPS
    计算机原理一
    SecureCRT连接虚拟机失败及虚拟机ping不通外网
    03.os
    02.random
    01.time
    01.面试过程中其他问题
    06.秒杀系统架构
    05.项目并发分析
  • 原文地址:https://www.cnblogs.com/lgqrlchinese/p/14010599.html
Copyright © 2011-2022 走看看