如何在java中获取microsoft word文档的页数?
对于基于服务器的j2ee应用程序,我需要从word文档中检索页数。。有什么好主意吗?如果文档是现代Word 2007格式,您可以通过使用直接基于XML的操作。这是迄今为止更好的长期解决方案,尽管我意识到整个组织一夜之间改变可能不现实如何在java中获取microsoft word文档的页数?,java,jakarta-ee,ms-word,Java,Jakarta Ee,Ms Word,对于基于服务器的j2ee应用程序,我需要从word文档中检索页数。。有什么好主意吗?如果文档是现代Word 2007格式,您可以通过使用直接基于XML的操作。这是迄今为止更好的长期解决方案,尽管我意识到整个组织一夜之间改变可能不现实 Document doc = new Document("C:\\Temp\\file.doc"); int pageCount = doc.getPageCount(); 如果它们是较旧的Word格式,您可能会使用服务器端Word/Excel/Powerpo
Document doc = new Document("C:\\Temp\\file.doc");
int pageCount = doc.getPageCount();
如果它们是较旧的Word格式,您可能会使用服务器端Word/Excel/Powerpoint/Outlook可编程对象模型,尽管..关于Office Open XML支持,最新的beta版应该支持它。以前没有使用过,但您可以尝试。看起来它有一个功能。//打开Word文档
Document doc = new Document("C:\\Temp\\file.doc");
int pageCount = doc.getPageCount();
//获取页面计数
Document doc = new Document("C:\\Temp\\file.doc");
int pageCount = doc.getPageCount();
要读取MS Office文件的页数,可以使用aspose库(aspose单词、aspose单元格、aspose幻灯片)
Document doc = new Document("C:\\Temp\\file.doc");
int pageCount = doc.getPageCount();
示例:
Document doc = new Document("C:\\Temp\\file.doc");
int pageCount = doc.getPageCount();
卓越:
Woorkbook可打印版本的页数:
Document doc = new Document("C:\\Temp\\file.doc");
int pageCount = doc.getPageCount();
import com.aspose.cells.*;
public int getPageCount(String filePath) throws Exception {
Workbook book = new Workbook(filePath);
ImageOrPrintOptions imageOrPrintOptions = new ImageOrPrintOptions();
// Default 0 Prints all pages.
// IgnoreBlank 1 Don't print the pages which the cells are blank.
// IgnoreStyle 2 Don't print the pages which cells only contain styles.
imageOrPrintOptions.setPrintingPage(PrintingPageType.IGNORE_STYLE);
int pageCount = 0;
for (int i = 0; i < book.getWorksheets().getCount(); i++) {
Worksheet sheet = book.getWorksheets().get(i);
PageSetup pageSetup = sheet.getPageSetup();
pageSetup.setOrientation(PageOrientationType.PORTRAIT);
pageSetup.setPaperSize(PaperSizeType.PAPER_LETTER);
pageSetup.setTopMarginInch(1);
pageSetup.setBottomMarginInch(1);
pageSetup.setRightMarginInch(1);
pageSetup.setLeftMarginInch(1);
SheetRender sheetRender = new SheetRender(sheet, imageOrPrintOptions);
int sheetPageCount = sheetRender.getPageCount();
pageCount += sheetPageCount;
}
return pageCount;
}
PowerPoint:幻灯片数量:
Document doc = new Document("C:\\Temp\\file.doc");
int pageCount = doc.getPageCount();
import com.aspose.slides.*;
public int getPageCount(String filePath) throws Exception {
Presentation presentation = new Presentation(filePath);
return presentation.getSlides().toArray().length;
}