3 Replies Latest reply on Oct 22, 2016 1:36 AM by dev9togo

    Number of pages in imported PDF file?


      Hi all.

      How to know the number of pages in imported PDF file?

        • 1. Re: Number of pages in imported PDF file?
          Loic.Aigon Adobe Community Professional

          This question was already discussed in the past and as far as I can remember Peter Karhel offered a snippet.

          Do some search.



          • 2. Re: Number of pages in imported PDF file?
            Laubender Adobe Community Professional & MVP

            You cannot read out the number of pages directly by any DOM properties.
            Instead one can use two strategies:


            1. Inspect the PDF file and try your best.
            See especially Scott Zanelli's famous PDF Importer script. He implemented a function for reading the PDF and extracts information out of the PDF regarding pages. Note: That approach will not work with every PDF. PDFs saved as "Optimized" from Acrobat Pro could fail.


            2. Use a Divide-and-Conquer algorithm for placing a page number.


            Pick a high number as start and see if that fails ( * ).

            If yes, cut that number in half and try again.

            Go back and forth until you found the highest number that will fail.


            See Marc Autret:

            Indiscripts :: On Dichotomic Divide-and-Conquer Algorithms


            ( * ) What does failing exactly mean?

            I'll answer that with a code snippet that uses a rather naive approach that simply tries to place a PDF over and over again.

            Details in the comments. So read them carefully before running the snippet.


            // GetNumberOfPages-PlacedPDF_SELECTION.jsx
            // Uwe Laubender
            // Slow and naive approach!
            * @@@BUILDINFO@@@ GetNumberOfPages-PlacedPDF_SELECTION.jsx !Version! Sun Oct 16 2016 10:07:36 GMT+0200
            // Select the graphic frame, that holds the placed PDF and run the snippet:
            var graphicFrame = app.selection[0];
            var pdfFile = File(app.selection[0].graphics[0].itemLink.filePath);
            var startPageNumberTest = graphicFrame.graphics[0].pdfAttributes.pageNumber;
            var doc = app.documents[0];
            // Change that number to your best guess.
            // The already used page number plus that range will be tested.
            var testRange = 10;
            // We begin testing with the next page number of the placed PDF:
            // startPageNumberTest + 1
            // The number of iterations in the for loop is the testRange
            for(var n=startPageNumberTest+1;n<=startPageNumberTest+testRange;n++)
                // We test placing the PDF in a temporarily added rectangle:
                var tempFrame = doc.rectangles.add({geometricBounds : [0,0,100,100]});
                // Here we feed the page number to the pdfPlacePreferences:
                app.pdfPlacePreferences.pageNumber = n;
                // Now we place the PDF where the place options will not be invoked (second argument of the method place()):
                // Let's see what happened after placing.
                // What is the page number of the placed PDF?
                var currentPageNumber = tempFrame.getElements()[0].graphics[0].pdfAttributes.pageNumber;
                // InDesign will not give an error, if the value of pageNumber with pdfPlacePreferences exceeds
                // the number of pages in the PDF. Instead page 1 will be placed.
                // Note: We never place page 1 of the PDF.
                // So if the value of the current pageNumber is 1, we reached the end of the test.
                if(currentPageNumber == 1)
                    $.writeln( "Number of pages in PDF:"+"\t"+(n-1) );
                // If that will never happen, we have to expand our test range.
                // Or revert to a different approach with a Divide-and-Conquer algorithm (not shown here)
                    $.writeln( "End of test range reached. Last tested page number:"+"\t"+(n) );




            1 person found this helpful