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

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

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;
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5612697.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-15
下一篇2022-12-15

发表评论

登录后才能评论

评论列表(0条)

    保存