Skip to main content
frameexpert
Community Expert
Community Expert
October 29, 2008
Question

Getting the number of pages in a PDF [JavaScript in CS3]

  • October 29, 2008
  • 3 replies
  • 695 views
Hello All,

I am an InDesign JavaScript newbie that will be posting questions in the near future. Before asking a lot of questions, I would like to contribute something to the group. Here is a function that will query a PDF and return the number of pages that it contains. This is useful for placing multi-page PDFs.

Rick Quatro
rick at frameexpert dot com

function GetPdfPageCount(oFile)
{
// Convert filename to a VBS-friendly format.
var sFile = oFile.fsName.replace(/\\/g, "\\\\");
var aFile = [sFile]; // An array of arguments.
if(File.fs == "Windows"){
var sGetPdfPageCount = "Dim AcroApp, AVDoc, PDDoc, iPages\r";
sGetPdfPageCount += "Set AcroApp = CreateObject(\"AcroExch.App\")\r";
sGetPdfPageCount += "Set PDDoc = CreateObject(\"AcroExch.PDDoc\")\r";
sGetPdfPageCount += "If PDDoc.Open(arguments(0)) Then\r";
sGetPdfPageCount += " iNumPages = PDDoc.GetNumPages()\r";
sGetPdfPageCount += " Set oInDesign = CreateObject(\"InDesign.Application\")\r";
sGetPdfPageCount += " oInDesign.ScriptArgs.SetValue \"ScriptArgumentA\", CStr(iNumPages)\r";
sGetPdfPageCount += " PDDoc.Close()\r";
sGetPdfPageCount += "End If\r";
sGetPdfPageCount += "Set AcroApp = Nothing\r";
sGetPdfPageCount += "Set PDDoc = Nothing\r";
app.doScript(sGetPdfPageCount, ScriptLanguage.visualBasic,aFile);
return Number(app.scriptArgs.getValue("ScriptArgumentA"));
}
else
{
return 0;
}
}
This topic has been closed for replies.

3 replies

Peter Kahrel
Community Expert
Community Expert
October 29, 2008
Ah -- good to know.

Peter
frameexpert
Community Expert
Community Expert
October 29, 2008
Hi Peter,

The solutions that open the PDF files with f.open and look for particular strings don't work for all of the PDF files I have. This one seems to work well for me so far. Thanks for the link.

Rick
www.frameexpert.com
Peter Kahrel
Community Expert
Community Expert
October 29, 2008
Rick,

A number of solutions for this one were suggested here: http://www.adobeforums.com/webx/.59b51fa7/5

Peter