poi 读取word ppt pdf 页数

pom.xml依赖

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>1.8.11</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.0.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.5-beta6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.5-beta6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.5-beta6</version>
        </dependency>

test类

import java.io.File;
import java.io.FileInputStream;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xslf.XSLFSlideShow;
import org.apache.poi.xslf.extractor.XSLFPowerPointExtractor;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

public class TestPage {
    public static void main(String[] args) throws Exception {
        // TODO code application logic here
        //parse2007();
        //parse97();
        pdf();
        //ppt();
        //pptx();
    }

    public static void parse2007() throws Exception {
        int pages = 0;
        int wordCount = 0;
        try {
            XWPFDocument docx = new XWPFDocument(POIXMLDocument.openPackage("E:\\11.docx"));
            pages = docx.getProperties().getExtendedProperties().getUnderlyingProperties().getPages();// 总页数
            wordCount = docx.getProperties().getExtendedProperties().getUnderlyingProperties().getCharacters();// 忽略空格的总字符数
                                                                                                                // 另外还有getCharactersWithSpaces()方法获取带空格的总字数。
        } catch (Exception e) {
            // TODO: handle exception
        }
        System.out.println("docxpages=" + pages + " wordCount=" + wordCount);
    }

    public static void parse97() throws Exception {
        int pages = 0;
        int wordCount = 0;
        try {
            WordExtractor doc = new WordExtractor(new FileInputStream("E:\\1.doc"));
            pages = doc.getSummaryInformation().getPageCount();// 总页数
            wordCount = doc.getSummaryInformation().getWordCount();// 总字符数
        } catch (Exception e) {

        }
        System.out.println("docpages=" + pages + " wordCount=" + wordCount);
    }

    public static void pdf() throws Exception {
        // 创建PdfDocument实例
        int pages = 0;
        try {
            File file = new File("E:\\2.pdf");
            PDDocument pdf = PDDocument.load(file);
            pages = pdf.getNumberOfPages();
            System.out.println("pdf全部页数" + pdf.getNumberOfPages());
        } catch (Exception e) {

        }
    }

    public static void ppt() throws Exception {

        int pages = 0;
        String filePath = "E:\\56.ppt";
        FileInputStream fis = new FileInputStream(filePath);
        SlideShow pptfile = new SlideShow(new HSLFSlideShow(fis));
        pages = pptfile.getSlides().length;
        System.out.println("ppt" + pages);

    }

    public static void pptx() throws Exception {
        int pages = 0;
        String text = "";
        String filePath = "E:\\23234.pptx";
        XSLFSlideShow fis = new XSLFSlideShow(filePath);
        XMLSlideShow pptxfile = new XMLSlideShow(fis);
        pages = pptxfile.getSlides().length;
        System.out.println("pptx" + pages);
    }
}
 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值