How to get the page count of a microsoft word document in java?

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

Asked by: Sienna119 | Posted: 21-01-2022

Answer 1

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..

Answered by: Melissa465 | Posted: 22-02-2022

Answer 2

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

Answered by: Kristian931 | Posted: 22-02-2022

Answer 3

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

Answered by: Alberta332 | Posted: 22-02-2022

Answer 4

//Open the Word Document

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

//Get page count

int pageCount = doc.getPageCount();

Answered by: Dainton986 | Posted: 22-02-2022

Answer 5

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


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.

        int pageCount = 0;
        for (int i = 0; i < book.getWorksheets().getCount(); i++) {
            Worksheet sheet = book.getWorksheets().get(i);

            PageSetup pageSetup = sheet.getPageSetup();




            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;

Answered by: Ada400 | Posted: 22-02-2022

Similar questions

How to print a microsoft word document from Java web server?

Are there any open source or commercial API available to print word document from Java application?

java - Is it possible to import data from Microsoft word document?

Usually CSV and excel file format will be used to import data as it is easy to extract data programatically. My users doesn't like excel file format for data entry, they like word document. But I am not sure how to extract data from Microsoft word document. Has anyone tried? do you have any suggestions? Found this link...

sql - Sending data to Microsoft Word document in Java

I would like to program a Java program, which sends data to a Microsoft Word file, which contains form textfields. Here is the Word-Document So MS Word file with form textfields gets data from a Java program. I have searched and found an API:

java - How to convert HTML to a Microsoft Word document ?

How can I convert HTML from a CKEditor into a Microsoft Word document?

java - How to display Microsoft word document on html page using jsp?

I need to display Microsoft Doc on my web page and then parse the doc for further process.

How to create preview image from Microsoft document using java

Currently, I am working on Microsoft document : Word (doc, docx), Powerpoint (ppt, pptx), and Excel (xls, xlsx) I would like to create the a preview image from it's first page. Only PowerPoint document can be done by Apache-poi library. But I cannot find the solution for other types. I have got an idea to convert the document to pdf (1) and the convert to image (2) . For step 2 (conv...

java - how to check if certain Microsoft word document is closed

If user is writing in a word file(eg: abc.docx) and he finishes working and closes this file, I want to get its status that abc.docx is been close is there any way to find out using java i am just passing the path of the docx file File file = new File("abc.docx");"file {} exits {}",file,file.exists()); i want to get the status eithe...

c# - Can Microsoft Sync framework for work with Java?

I am planning to use the following architecture for one of the applications: Client: Java based application, which will use a MySQL database Server: Will be C# based and the database will be SQL Server Now is it possible to use Microsoft Sync Framework in Java? (Probably by implementing some interfaces?). I want the data from Java client to be synced to the server and vice versa. IS this possible? If not, t...

tfs - Microsoft Team System and Java

We're starting a project written in Java in a company which works purely on MS technologies. Microsoft Team System is used as source control tool. A question is whether we should try to integrate Eclipse with MTS (which makes sense from the top level as there would be still a single repository for the company) or we should try to setup another source control tool - most likely Subversion (which makes sense from developers ...

java - How to fetch data from a Microsoft Access database through JDBC and insert the table as a test field in an Applet

I have made a small letter game as a Java Applet. I have made a Microsoft Access Database through JBDC for the high scores. I have managed to insert values (scores) into the database, but I am having trouble fetching them and displaying the table in the textArea of an ajFrame. I am not even sure if the connection is established. I have created the SQL statement for it.

java - Overhead with Microsoft JDBC driver when executing a stored procedure

I am using Microsoft JDBC Driver 2.0 with SQL Server 2005. To explain my question better, let me start with a sample code to call a stored procedure. public static void executeSproc(Connection con) { CallableStatement cstmt = con.prepareCall("{call dbo.getEmployeeManagers(?)}"); cstmt.setInt(1, 50); ResultSet rs = cstmt.executeQuery(); while ( { // print results in the result set ...

java - Call Microsoft EWS from JSF app running on JBoss

I'm running JBoss 4.2.3, Java 1.5, and Ubuntu. Before you tell me to post on the JBossWS forum, I already have and there is not a lot of activity over there. I am trying to call a Microsoft Exchange web service end point from a JSF web application. I have written other web service end points and have successfully built clients into my web application. I also use the same code for this client in a stand alone Java applicati...

windows - How to convert Microsoft Locale ID (LCID) into language code or Locale object in Java

I need to translate a Microsoft locale ID, such as 1033 (for US English), into either an ISO 639 language code or directly into a Java Locale instance. (Edit: or even simply into th...

Java awt applet with Microsoft JVM white screen

I have a java chat applet that's compatible with JDK1.1 so it works well even with Microsoft JVM. the thing is, sometimes the chat area within the applet becomes white and messed up. It can only be fixed back by closing the whole Internet Explorer (all windows and tabs) and restarting it. this doesn't exist when running the applet on sun's JVM, but we have to use MS's. it seems like it's related to the ins...

java - Getting Number of Rows in a Group By Query with Microsoft Access

I have basically the same problem outlined in this question, however I am using Microsoft Access as a database instead of MySQL. The result of which is that SQL_CALC_FOUND_ROWS doesn't seem to be available to me. Believe me, I want to switch, but for the moment it is out of the question. I have a query that...

Using Microsoft Access with Java without the JdbcOdbc default bridge

we have to use in our project some legacy Microsoft Access 97/2000 databases with Java. The problem is: we face many problems with the default JdbcOdbc driver which is available on the JDK (memory leaks are the most common issue). So here is my question: is there any other JDBC Driver we could use (open source whould be great) to achieve this task?

java - Feedback about JDBC Type 4 Driver for Microsoft Access

we need some data which is stored on several Microsoft Access databases on our servers, and our application will be executed on a Linux machine. So, our only alternative consist in using a JDBC Type 4 driver. In our research, we found two alternatives: HXTT - StelsMDB -

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)