java poi 批量提取word,ppt,pdf文档总页数

该代码示例展示了如何使用Java处理不同类型的文档,包括Word 2003 (.doc), Word 2007 (.docx)和PDF,计算它们的页数。通过引入Hutool, Apache POI和iTextPDF等库,实现了对D盘下所有题库文件的遍历和页数统计。
相关jar包:
<!--工具类-->
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-core</artifactId>
    <version>5.0.4</version>
</dependency>

<!-- 针对2007以上版本的库 -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.0.1</version>
</dependency>

<!-- 针对2003版本的库 -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-scratchpad</artifactId>
    <version>4.0.1</version>
    <exclusions>
        <exclusion>
            <artifactId>poi</artifactId>
            <groupId>org.apache.poi</groupId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>fr.opensagres.poi.xwpf.converter.xhtml</artifactId>
    <version>2.0.2</version>
</dependency>
package com.xxx;


import cn.hutool.core.io.FileUtil;
import com.itextpdf.text.pdf.PdfReader;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.ooxml.POIXMLDocument;
import org.apache.poi.openxml4j.util.ZipSecureFile;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;


public class MainTest {

    public static void main(String[] args) throws IOException {
        run();
    }


    public static void run() throws IOException {
        List<File> files = FileUtil.loopFiles("D:\\题库");
        System.out.println("文件总数:" + files.size());
        int num = 1;
        for (File file : files) {
            int count = 0;
            //System.out.println(file);
            String[] fileTypeArray = file.getName().split("\\.");
            String fileType = fileTypeArray[fileTypeArray.length - 1];
            if ("doc".equals(fileType)) {
                count = countWord2003Page(file.getPath());
            }
            if ("docx".equals(fileType)) {
                count = countWord2007Page(file.getPath());
            }
            if ("pdf".equals(fileType)) {
                count = countPdfPage(file.getPath());
            }
            System.out.println(file + "@" + fileType + "@" + count);
            num++;
        }
    }

    /**
     * 计算PDF格式文档的页数
     */
    public static int countPdfPage(String filepath) {
        int pageCount = 0;
        PdfReader reader = null;
        try {
            reader = new PdfReader(filepath);
            pageCount = reader.getNumberOfPages();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            reader.close();
        }
        return pageCount;
    }


    /**
     * 计算PPT格式文档的页数
     */
    public static int countPPTPage(String filePath) throws IOException {
        int pageCount = 0;
        ZipSecureFile.setMinInflateRatio(-1.0d);
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(filePath);
            XMLSlideShow pptxFile = new XMLSlideShow(fis);
            pageCount = pptxFile.getSlides().size();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            fis.close();
        }
        return pageCount;
    }

    /**
     * 计算WORD2007(*.docx)格式文档的页数
     */
    public static int countWord2007Page(String filePath) {
        int pageCount = 0;
        ZipSecureFile.setMinInflateRatio(-1.0d);
        XWPFDocument docx = null;
        try {
            docx = new XWPFDocument(POIXMLDocument.openPackage(filePath));
            pageCount = docx.getProperties().getExtendedProperties().getUnderlyingProperties().getPages();//总页数
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                docx.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return pageCount;
    }

    /**
     * 计算WORD2003(*.doc)格式文档的页数
     */
    public static int countWord2003Page(String filePath) {
        int pageCount = 0;
        WordExtractor doc = null;
        ZipSecureFile.setMinInflateRatio(-1.0d);
        try {
            doc = new WordExtractor(new FileInputStream(filePath));//.doc格式Word文件提取器
            pageCount = doc.getSummaryInformation().getPageCount();//总页数
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                doc.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return pageCount;
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值