如何在java中获取microsoft word文档的页数?

发布于 2024-07-08 14:23:49 字数 50 浏览 8 评论 0原文

对于基于服务器的j2ee应用程序,我需要从word文档中检索页数..有什么想法有效吗?

for a server based j2ee application, I need to retrieve the number of pages from word documents.. any ideas what works?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

慕巷 2024-07-15 14:23:49

如果文档是现代 Word 2007 格式,您可以通过

如果它们是较旧的 Word 格式,您可能会陷入服务器端 Word/Excel/Powerpoint/Outlook 可编程对象模型,尽管 你不应该在服务器上这样做..

If the documents are modern Word 2007 format you can use direct XML-based manipulation, through OOXML. This is by far the better long term solution, though I realize it may not be realistic for an entire organization to change overnight.

If they are older Word formats, you're probably stuck with server-side Word/Excel/Powerpoint/Outlook programmable object models, although you're not supposed to do that on the server..

吻泪 2024-07-15 14:23:49

关于 Office Open XML 支持,Java-POI 的最新测试版应该支持它。

Regarding Office Open XML support, the latest beta of Java-POI is supposed to support it.

迟月 2024-07-15 14:23:49

以前没有使用过它,但您可以尝试 Apache POI。 看起来它有一个 WordCount功能。

Haven't used it before but you could try Apache POI. Looks like it has a WordCount function.

煮茶煮酒煮时光 2024-07-15 14:23:49

//打开Word文档

Document doc = new Document("C:\\Temp\\file.doc"); 

//获取页数

int pageCount = doc.getPageCount();

//Open the Word Document

Document doc = new Document("C:\\Temp\\file.doc"); 

//Get page count

int pageCount = doc.getPageCount();
断桥再见 2024-07-15 14:23:49

要读取 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;
    }

Word: 页数:

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;
 }

To read the page count of MS Office files you can use aspose libraries (aspose-words, aspose-cells, aspose-slides).

Examples:

Excel:
number of pages of the printable version of the Woorkbook:

   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;
    }

Word: number of pages:

import com.aspose.words.Document;
public int getPageCount(String filePath) throws Exception {
     Document document = new Document(filePath);
     return document.getPageCount();
 }

PowerPoint: number of slides:

import com.aspose.slides.*;
public int getPageCount(String filePath) throws Exception {
     Presentation presentation = new Presentation(filePath);
     return presentation.getSlides().toArray().length;
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文