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     }

 

posted @ 2020-11-20 14:11  龙谷情Sinoam  阅读(1263)  评论(0编辑  收藏  举报
Smiley face