栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java通过Apach POI获取文档页数(Word、PPT、PDF)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java通过Apach POI获取文档页数(Word、PPT、PDF)

        前言:最近要做一个打印机的项目,用户可以上传文件,然后选择打印的页数,所以后端需要对上传的文件进行解析获取页数。

        Maven项目直接先上pom

    
        
            com.itextpdf
            itextpdf
            5.0.6
        
        
        
            org.apache.poi
            poi
            4.1.2
        
        
        
            org.apache.poi
            poi-ooxml
            4.1.2
        
        
        
            org.apache.poi
            poi-scratchpad
            4.1.2
        
    

        首先,现在的后端都是SpringBoot项目,都是用MultipartFile对象接收文件,默认上传大小好像是1MB,如果文件大的话可以设置上传大小,不然会报错org.apache.tomcat.util.http.fileupload.impl.SizeLimitExceededException: the request was rejected because its size (4543309) exceeds the configured maximum (1048576)

 设置方法为在yml配置文件添加配置:

spring:  
    servlet:
        multipart:
          max-file-size: 100MB
          max-request-size: 100MB
          enabled: true

第二步、因为word、pdf、ppt获取页数的方法都不一样,所以先通过后缀获取文件类型

String fileName = file.getOriginalFilename();  //获取文件名
String type = fileName.substring(fileName.lastIndexOf("."));  获取后缀

第三步,将MultipartFile转成InputStream流的形式进行解析

MultipartFile转成InputStream的方法:

MultipartFile file;

byte[] byteArr = file.getBytes();
InputStream inputStream = new ByteArrayInputStream(byteArr);

第四步:因为经常要用到获取页数的方法,所以我直接写了一个工具类,大家可以直接复制;

public class FilePagesUtils {
    
    public static int filesPage(InputStream fileInputStream, String fileType) throws IOException {
        int count = 0;
        if (".doc".equals(fileType)) {
            count = countWord2003Page(fileInputStream);
        }
        if (".docx".equals(fileType)) {
            count = countWord2007Page(fileInputStream);
        }
        if (".pdf".equals(fileType)) {
            count = countPdfPage(fileInputStream);
        }
        if (".pptx".equals(fileType)) {
            count = countPPTXPage(fileInputStream);
        }
        if (".ppt".equals(fileType)) {
            count = countPPTPage(fileInputStream);
        }
        return count;
    }

    
    public static int countPdfPage(InputStream fileInputStream) {
        int pageCount = 0;
        PdfReader reader = null;
        try {
            reader = new PdfReader(fileInputStream);
            pageCount = reader.getNumberOfPages();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            reader.close();
        }
        return pageCount;
    }

    
    public static int countPPTPage(InputStream fileInputStream) throws IOException {
        int pageCount = 0;
        ZipSecureFile.setMinInflateRatio(-1.0d);

        HSLFSlideShow hslfSlideShow = new HSLFSlideShow(fileInputStream);
        try {
            pageCount = hslfSlideShow.getSlides().size();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            fileInputStream.close();
        }
        return pageCount;

    }

    
    public static int countPPTXPage(InputStream fileInputStream) throws IOException {
        int pageCount = 0;
        ZipSecureFile.setMinInflateRatio(-1.0d);
        try {
            XMLSlideShow pptxFile = new XMLSlideShow(fileInputStream);
            pageCount = pptxFile.getSlides().size();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            fileInputStream.close();
        }
        return pageCount;
    }

    
    public static int countWord2007Page(InputStream fileInputStream) throws IOException {
        int pageCount = 0;
        ZipSecureFile.setMinInflateRatio(-1.0d);
        XWPFdocument docx = null;
        try {
            docx = new XWPFdocument(fileInputStream);
            pageCount = docx.getProperties().getExtendedProperties().getUnderlyingProperties().getPages();//总页数
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            docx.close();
        }
        return pageCount;
    }

    
    public static int countWord2003Page(InputStream fileInputStream) throws IOException {
        int pageCount = 0;
        WordExtractor doc = null;
        ZipSecureFile.setMinInflateRatio(-1.0d);
        try {
            doc = new WordExtractor(fileInputStream);//.doc格式Word文件提取器
            pageCount = doc.getSummaryInformation().getPageCount();//总页数
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            doc.close();
        }
        return pageCount;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/606559.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号