0

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.

nean0502
  • 17
  • 5
  • There are the methods `myDoc.getAllChildFonts()`, `myTable.getAllChildCells()`, `myApp.getAllChildDocuments()`. I wonder if there is a method `myDoc.getAllChildPages()`? – Yuri Khristich Apr 28 '21 at 18:09

2 Answers2

0

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;
mdomino
  • 1,195
  • 1
  • 8
  • 22
  • Thank you very much for your help and attention. I have used InDesignServerAPI.jar as an external jar in my Java application. The code in my question is in Java. I would like to know how to get number of pages in one existing document. Thank you in advance. – nean0502 May 27 '20 at 11:40
  • Should still be kind of the same command. See my edited answer. – mdomino May 27 '20 at 11:48
  • Thank you for the idea. There is a possibility to do something like that: int numberOfPages = myDocument.countPages();But I am getting errors when I try to open an existing document.. – nean0502 May 27 '20 at 12:05
  • 1
    Your feedback is not exactly helpful. What sort of error do you get? And where are these methods (`countPages`) documented? And what happens if you try the suggestion I made above `myDocument.pages.length`? – mdomino May 27 '20 at 12:08
  • The only document that I have is http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.173.9015&rep=rep1&type=pdf. The method countPages is not documented enough. I am getting an error in the line Document myDocument = myApp.addDocument(OptArg.noDocumentPreset());. The error is com.adobe.ids.IdsException: IDL:com/adobe/ids/IdsException:1.0 at com.adobe.ids.IdsExceptionHelper.read(Unknown Source) at com.adobe.ids.basics._ApplicationStub.addDocument(Unknown Source) and there is no collection "pages" for the object myDocument – nean0502 May 27 '20 at 12:20
  • Ok, sorry, in this case please totally disregard my answer. Was completely unaware that there is in fact a complete Java automation implementation for InDesign *server*. Looking into this document, I would also guess that the method `countPages()` should take care of what you want to do, but if that does not work, I don't know what is causing the error. I will go ahead and delete my answer, as it does not answer the question in any way. – mdomino May 27 '20 at 19:38
  • I would like to ask you do not delete the answer, because I got an idea how to use the method countPages() from your answer. And, regarding my issue, Java is not supported language for the newest version of InDesign server. The newest InDesign supports only script languages (I found it). My document is very old one.. so I need to leave my idea about external InDesign jar... – nean0502 May 28 '20 at 02:18
  • Yes, usually InDesign (as well as InDesign server I believe) are automated via ExtendScript, which is a JavaScript dialect and is comparatively well documented. So if I were you I would look into using that, if possible. – mdomino May 28 '20 at 09:21
0

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)
Yuri Khristich
  • 13,448
  • 2
  • 8
  • 23