java - 使用 java 无法在 Word 文档中获取页数

标签 java apache-poi

我需要使用java获取word文件的页数。我使用 poi jar 3.11 和以下代码。当我使用下面的代码时,我能够获取 2003 格式的 Word 文档,但是在读取页数时,它会抛出错误,如下所示 “”

公共(public)类CreateDocumentFromScratch {

public static int pagesNo;

public static void main(String[] args) {
    XWPFDocument document = new XWPFDocument();

    XWPFParagraph Paragraph = document.createParagraph();
    Paragraph.setBorderBottom(Borders.SINGLE);
    Paragraph.setBorderTop(Borders.SINGLE);
    Paragraph.setBorderRight(Borders.SINGLE);
    Paragraph.setBorderLeft(Borders.SINGLE);
    Paragraph.setBorderBetween(Borders.SINGLE);

    XWPFRun Titleparagraph = Paragraph.createRun();
    Titleparagraph.setBold(true);
    Titleparagraph.setItalic(true);
    Titleparagraph.setFontFamily("Latha");
    Titleparagraph.setFontSize(10);
    Titleparagraph.setText("ஓட்டு எண்ணிக்கையில் புதிய முறை: முதல் முடிவு வெளியாக தாமதம் ஆகும்");

    XWPFRun Headlineparagraph = Paragraph.createRun();
    Headlineparagraph.setBold(true);
    Headlineparagraph.setItalic(true);
    Headlineparagraph.setFontFamily("Latha");
    Headlineparagraph.setFontSize(10);
    Headlineparagraph.setText("தமிழகத்தில் ஓட்டு எண்ணிக்கையின் போது புதிய முறை கடை பிடிக்கப்படுவதால், முதல் முடிவு வெளிவர தாமதம் ஆகும் என்று தலைமை தேர்தல் அதிகாரி பிரவீன்குமார் கூறினார்.");
    Headlineparagraph.addBreak();

    FileOutputStream outStream = null;
    try {
        outStream = new FileOutputStream("Sample.doc");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    try {
        document.write(outStream);
        outStream.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }


    WordExtractor extractor = null;
    FileInputStream fis = null;
    try {
        fis = new FileInputStream("Sample.doc");
    } catch (FileNotFoundException ex) {
        Logger.getLogger(CreateDocumentFromScratch.class.getName()).log(Level.SEVERE, null, ex);
    }
    HWPFDocument documentextract = null;
    try {
        documentextract = new HWPFDocument(fis);
    } catch (IOException ex) {
        Logger.getLogger(CreateDocumentFromScratch.class.getName()).log(Level.SEVERE, null, ex);
    }
    extractor = new WordExtractor(documentextract);
    extractor = new WordExtractor(documentextract);
    String[] paragraphs = extractor.getParagraphText();

    int pageCount = 1;
    for (int i = 0; i < paragraphs.length; ++i) {
        if (paragraphs[i].indexOf("\f") >= 0) {
            ++pageCount;
        }
    }
    JOptionPane.showMessageDialog(null, "pageCount --> "+pagesNo);
}

}

Error: 
Exception in thread "main" org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
    at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:133)
    at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:106)
    at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:128)
    at org.apache.poi.hwpf.HWPFDocumentCore.verifyAndBuildPOIFS(HWPFDocumentCore.java:106)
    at org.apache.poi.hwpf.HWPFDocument.<init>(HWPFDocument.java:174)
    at testapp.CreateDocumentFromScratch.main

最佳答案

WordExtractor 替换为 XWPFWordExtractor,将 HWPFDocument 替换为 XWPFDocument。您可以使用 documentextract.getParagraphs() 提取 XWPFParagraph 列表。

像这样:

List<XWPFParagraph> paragraphs = documentextract.getParagraphs();
int pageCount = 1;
for (XWPFParagraph par :paragraphs) {
    if (par.getText().indexOf("\f") >= 0) {
        ++pageCount;
    }
}

关于java - 使用 java 无法在 Word 文档中获取页数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28670669/

相关文章:

java - 在 Coldfusion/Java 中拆分多语言字符串

java - 使用 Java Apache POI 从只读 xlsm 文件中读取数据

java - 使用 apache poi 通过流/分页策略将 xlsx 文件解析为 block

java - 使用 POI 库进行条件格式化

java - 如何模拟应用程序上下文

java - 使用 ProcessBuilder/RunTime 从 java 调用 easy_install 应用程序

java - 如何根据 Word 文档大小 Apache-POI 自动调整表格并将表格居中对齐?

java - 使用 Apache POI 读取 .docx 文件

java - Android - 如何从 api.烂番茄 解析 JSONObject 和 JSONArray

java - StringTokenizer 和 String split - 按特殊字符拆分