How can I get a real page count in the InDesign Java document

445 Views Asked by At

I am using Adobe InDesign CS5 Server Java. For setting the desired preferences, I am using the following code:

Document myDocument = myApp.addDocument(OptArg.noDocumentPreset());
DocumentPreference docPrefs = myDocument.getDocumentPreferences();
docPrefs.setPageHeight(UnitUtils.createString("800pt"));
docPrefs.setPageWidth(UnitUtils.createString("600pt"));
docPrefs.setPageOrientation(kPageOrientationLandscape.value);
docPrefs.setPagesPerDocument(16);

I would like to know if it is somehow possible to find out the real document page count in java, without setting setPagesPerDocument? Thank you in advance for any help.

2

There are 2 best solutions below

8
mdomino On

You can simply find out the number of pages like this:

var pageCount = myDocument.pages.length

$.writeln("The document has " + pageCount + " pages.");

Btw. the InDesign scripting is done in JavaScript (or more precisely in ExtendScript which is a JavaScript dialect) which is a very different language than Java.

Edit: Ok, answering your comment, I have no idea what InDesignServerAPI.jar is, but looking at your code it looks like the InDesign ExtendScript language is just sort of wrapped into Java code. So my guess would be, that you can get the page count like this:

int pageCount = myDocument.pages.length;
0
Yuri Khristich On

Just in case. Sorry, I don't know how it works in Java. But in Python on Windows it can be done this way:

from win32com.client import Dispatch

app = Dispatch('InDesign.Application.CS6')
doc = app.Open(r"d:\sample.indd")
pages = doc.pages;
pages_length = len(pages)
doc.Close()

print(pages_length)