How can I get a total page count of a PDF before placing every page in the PDF?
Copy link to clipboard
Copied
I'd like to create a script that places a multiPage PDF in any number of different impositions. (saddle stitch, 4up signatures, 16up signatures etc.)
I've easily created a script that iterates through a PDF and places it into a new document as (4,1|2,3), (8,5|6,7) etc, which works for printing in duplex, folding each page in half, and gluing the resulting spines to make a simple thick book (for PDFs with more than, say, 64 pages).
However, the next step is to re-write the script to create a saddle stitch document (16,1|2,15), (14,3|4,13) ... (10,7|8,9). For this I need to know how many pages there are in the PDF before I start placing the PDF pages, and then making the new document [int((PDFpages+3)/4)] pages long.
Is there a simple way to get the count of PDFpages without going through a loop and placing the next page from the PDF until the placed page number equals the first page number?
This way seems wasteful:
var totPDFPages = 1;
app.pdfPlacePreferences.pageNumber = totPDFPages;
myPDFPage = sheetFront.place(File(srcPDF), [0,0])[0];
var pdfFirstPage = myPDFPage.pdfAttributes.pageNumber;
while (doneCounting == false) {
totPDFPages += 1;app.pdfPlacePreferences.pageNumber = totPDFPages;
myPDFPage = sheetFront.place(File(srcPDF), [0,0])[0];
if (myPDFPage.pdfAttributes.pageNumber == pdfFirstPage) {
totPDFPages -=1;
doneCounting = true;
alert("PDF has " + totPDFPages + " pages!");exit();
};
myPDFPage.remove();
};
NB. Javascript above *hasn't* been run, but should look similar once debugged.
The only thing I've though of to relieve the sheer duplication of placing the PDF twice (once for the count, and once for the imposition), is to create an array of impoPages[counter]=myPDFPage, and then shuffle the pages referenced by the array to the correct sheet and position.
It'd be much easier to be able to assign pageCount = File(srcPDF).pageCount !!!
Thanks for any help/tips or even a simple "What are you smoking, man!?"
Cheers,
Jezz
Copy link to clipboard
Copied
Trevor×… wrote
… But I get the point.
Hi Trevor,
tested a bit on with my approach to make it a bit faster.
FWIW:
I went back to place the PDF to a graphic frame instead to the place gun.
Why?
Because I could minimize the time for creating a preview image by changing:
app.displayPerformancePreferences,
app.displaySettings[0] and
tempDoc.layoutWindows[0].viewDisplaySetting
Using the placeGun plus changing the settings above did not minimize the time for loading, a full preview image was generated every time. Using a frame placing the image did the trick. I did this with a temp document showing a layout window.
Regards,
Uwe
Copy link to clipboard
Copied
BarlaeDC​ Thanks, that seems to very much go against what acrobat interapplication communication is about.
Laubender​
Sounds better, about how much % difference does it make?
I tried a little snippet on the mac
tell application "Adobe Acrobat"
open POSIX file "/Users/Trevor/Downloads/Creative Cloud Ecosystem- Developer Engagement and Support.pdf" with invisible
set doc to document 1
set c to number of pages of doc
close doc
end tell
c
I was disappointed at the speed (about 5 seconds) and other potential problems with it. Compared with Windows acrobat interapplication communication method which tool a fraction of a second.
I will probably post a question next week on the Acrobat SDK forum to see if there's a Mac equivalent.
The 5 line VBS snippet does a good job.
Creates an invisible Acrobat process, opens the doc, counts the pages, closes the doc and the Acrobat process and returns the count all in about 150ms. There's no problems with being concerned with whats happening on an open Acrobat instance, dialogs etc.
As I type I can think of a solution to that problem, I'll try it out and if it works post it.
Copy link to clipboard
Copied
The more people vote for this feature request:
Please expose a PDF's page count to scripting – Adobe InDesign Feedback
the bigger the chance that we get it it and can bin all those awful hacks.
P.
Copy link to clipboard
Copied
https://forums.adobe.com/people/Peter+Kahrel wrote
The more people vote for this feature request:
Please expose a PDF's page count to scripting – Adobe InDesign Feedback
the bigger the chance that we get it it and can bin all those awful hacks.
Hi Peter,
already voted. Thank you for posting the link here!
Best,
Uwe
Copy link to clipboard
Copied
Hi,
IAC is there to allow you to control the Acrobat program on a users desktop, Acrobat has not been built or tested in a server environment.
And if you are looking for speed then it may be better to investigate the PDF Library, as you could build a simple application ( or even build it into your application) to open, get page number, close. all without requiring Adobe Acrobat to be installed at all.
Regards
Malcolm
Copy link to clipboard
Copied
Very useful Malcom.
I was thinking of calling a bash like
open -na "Adobe Acrobat" "/Users/Trevor/Downloads/Creative Cloud Ecosystem- Developer Engagement and Support.pdf" & echo $!
and then counting the pages and close as per PID
Copy link to clipboard
Copied
Voted too. But it does somewhat go against the crossword value of scripting 🙂
Copy link to clipboard
Copied
If one wants an Adobe API for PDFs on a server the Adobe PDF Library is available for negotiation. It does not have an IAC interface, C++ is used.
Copy link to clipboard
Copied
HI,
The PDF Library is just a collection of header files and such, but can be used to make just about any kind of application, so you could make your own application with IAC capabilities that you call when you want to count the Page numbers.
Or you could build and InDesign Server plugin that exposes a scripting interface that uses the PDF library to count the number of pages of a PDF file, you could even go as far as writing your own PDF import plug-in that could be used and it could have functionality to count the pages before import.
Just to complete the options
Regards
Malcolm
Copy link to clipboard
Copied
Hi All,
I am using this code snippet with a temporary frame (curTextFrame) to find the number of pages in the pdf. Haven't tested on CC yet.
Happy about any comment.
/*
Importiert alle Seiten des PDF Dokumentes
Besonderheit bei InDesign:
Wenn app.pdfPlacePreferences.pageNumber größer ist als die Anzahl der Seiten im
PDF Dokument, dann platziert InDesign trotzdem eine Seite
pdfAttributes.pageNumber fängt dann wieder bei "1" an.
*/
PDFPageCounter.PAGE_NAME_END = 99999;
PDFPageCounter.doSampleImport = function (curFile, curTextFrame) {
var curPageNumber = 0;
for (var i=1; i<PDFPageCounter.PAGE_NAME_END; i++) {
var curCounter = curPageNumber = i+1;
app.pdfPlacePreferences.pageNumber = curCounter;
if (curFile.exists) {
try {
curTextFrame.place(curFile);
} catch (e) {
$.writeln("cannot place file in InDesign. Corrupt file ?");
}
}
var curPDF = curTextFrame.allGraphics[0];
var placedPDFPageNumber = curPDF.pdfAttributes.pageNumber;
if (placedPDFPageNumber == 1) {
curPageNumber--;
break;
}
}
return curPageNumber;
}
Copy link to clipboard
Copied
I am writeing a script in VB that take a pdf file and place the
pages acording to the right position and so on.
Thank you all for your scripts and ideas.
I have a question.
What if the pdf contains pages that do not have the same width or height. It hapened to me with some pdf files.
So eaven it is slow I'm placeing all the pdf pages (and counting them) and at the same time checking the width and the height to
be dhe same in every one of them.
I'm scripting for InDesign CS2
Nice Coding!
Copy link to clipboard
Copied
Wel that seams to slow, if you have to many pages It doesn't pay.
While trying to find something for VB, insted of geting and paying for a PDF page counter DLL, searching the internet I come up with this code.
'===========================================
OpenFileDialog1.ShowDialog()
Dim FileName As String
FileName = OpenFileDialog1.FileName
Dim result As Integer
Dim fileReader As System.IO.StreamReader
fileReader = My.Computer.FileSystem.OpenTextFileReader(FileName)
Dim pdfText As String
pdfText = fileReader.ReadToEnd
Dim regx As New Regex("/Type\s*/Page[^s]")
Dim matches As System.Text.RegularExpressions.MatchCollection
matches = regx.Matches(pdfText)
result = matches.Count
MsgBox(result.ToString)
'============================================
For someone that use VB might be usefull.
Nice coding!
Copy link to clipboard
Copied
Take a look at the PlaceMultipagePDF.vbs script that comes with InDesign CS3--it solves this problem without attempting to read the PDF, and may already do most of what you want.
The trouble with opening the PDF and using RegExp to try to get the page count is that you are not guaranteed a.) that you can open the PDF, and b.) that there will be only one instance of the page count in the PDF. The internal structure of PDFs can be quite complicated--especially if multiple PDFs have been merged.
Thanks,
Ole
Copy link to clipboard
Copied
You are right about a) and b).
What we all want, as far as I'v understud is to get the
PDF total number of pages WITHOUT having to place each of them,
in order to count.
Nice coding!
-
- 1
- 2