2

I got a word file and I want to count how many pages are in it.

The file has been created with Docx4Java.

Anyone did this before?

Thanks!

gert789
  • 424
  • 1
  • 6
  • 13
  • possible duplicate of : http://stackoverflow.com/questions/13268541/how-to-get-page-sheet-count-of-word-excel-documents – Marc Aug 27 '13 at 13:12

1 Answers1

2

docx4j doesn't have a page layout model, so it can't tell you a page count.

You can get an approximate page count by using FOP's page layout model. docx4j's PDF output now supports a "2 pass" generation:

  • first pass calculates the page count(s)
  • second pass generates the pdf

See https://github.com/plutext/docx4j/blob/master/src/main/java/org/docx4j/convert/out/fo/AbstractPlaceholderLookup.java and https://github.com/plutext/docx4j/blob/master/src/main/java/org/docx4j/convert/out/fo/ApacheFORenderer.java

So doing the first pass would give you (approximately) what you want. This uses org.apache.fop.apps.FormattingResults which records the number of pages in a page sequence, or in the document as a whole.

An alternative approach might be to use LibreOffice/OpenOffice (or Microsoft Word, for that matter).

JasonPlutext
  • 15,352
  • 4
  • 44
  • 84