3 Replies Latest reply on Oct 29, 2008 12:38 PM by Peter Kahrel

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

    frameexpert Level 4
      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"));
      return 0;