对于基于服务器的 j2ee 应用程序,我需要从 word 文档中检索页数。有什么想法有效吗?
问问题
2702 次
5 回答
3
如果文档是现代 Word 2007 格式,您可以通过OOXML直接使用基于 XML 的操作。这是迄今为止更好的长期解决方案,尽管我意识到整个组织在一夜之间改变可能是不现实的。
如果它们是较旧的 Word 格式,您可能会被服务器端的 Word/Excel/Powerpoint/Outlook 可编程对象模型困住,尽管您不应该在服务器上这样做..
于 2008-11-12T14:02:14.047 回答
3
关于 Office Open XML 支持,Java-POI的最新测试版应该支持它。
于 2008-11-12T14:05:25.427 回答
1
以前没用过,但你可以试试Apache POI。看起来它有一个WordCount功能。
于 2008-11-12T14:07:24.723 回答
0
//打开Word文档
Document doc = new Document("C:\\Temp\\file.doc");
//获取页数
int pageCount = doc.getPageCount();
于 2016-01-01T07:41:16.867 回答
0
要阅读 MS Office 文件的页数,您可以使用 aspose 库(aspose-words、aspose-cells、aspose-slides)。
例子:
Excel:工作簿可打印版本的页数:
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;
}
字: 页数:
import com.aspose.words.Document;
public int getPageCount(String filePath) throws Exception {
Document document = new Document(filePath);
return document.getPageCount();
}
PowerPoint:幻灯片数量:
import com.aspose.slides.*;
public int getPageCount(String filePath) throws Exception {
Presentation presentation = new Presentation(filePath);
return presentation.getSlides().toArray().length;
}
于 2019-09-27T14:55:52.903 回答