gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 06:11:30 25 4
gpt4 key购买 nike

我需要使用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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com