6 Replies Latest reply on Oct 17, 2014 5:48 AM by camilo umana

    [JS][CS4] Export PDF pages by section?

    booyajamo

      I have to automate an export process for a multipage INDD file with 2 sections.  First, I need to export a PDF with all pages, then a PDF only with pages from the first section.  I had a script working great where I labeled each section with a Section Prefix. Section 1 as A and section 2 as B, then I simply set my pageRange = "A"; and I could easily export my 2 different PDFs.

       

      with(app.pdfExportPreferences){

      pageRange = PageRange.allPages;

      app.activeDocument.exportFile(ExportFormat.pdfType, File("myDocument.pdf"),

      false);

      }

       

      with(app.pdfExportPreferences){

      pageRange = "A";

      app.activeDocument.exportFile(ExportFormat.pdfType, File("myDocument_sectionA.pdf"),

      false);

      }

       

      The problem now is that due to processes that happen further in the workflow to the PDF, the section prefix A isn't an option.  I need to be able to have the pages be numerical (1, 2, 3, etc.) instead of alpha then numeric (A1, A2, A3, etc.).

       

      Is this possible?  Is using Section Prefixes the best approach for trying to accomplish this?

       

      Thanks,

      Lindsay

        • 1. Re: [JS][CS4] Export PDF pages by section?
          John Hawkinson Level 5

          Why not just use pageRange = section.pageStart+"-"+(section.pageNumberStart+section.length); ?

          • 2. Re: [JS][CS4] Export PDF pages by section?
            booyajamo Level 1

            Hi John,

             

            Thanks for the help!  I'm sorry if my questions are extremely simple.  I'm just beginning to understand how to write javascript for INDD.  I tried to use the code you suggest below but got errors.  I figured it was because I didn't define the variable "section"?

             

            What you suggest - is this basically stripping out the alpha prefix so that in the PDF in the pages you see "1, 2, 3", etc. instead of "A1, A2, A3"?  If not, can you describe what your suggested code is doing?  It might be a better approach than what I was trying to do.

             

            Thanks!

            • 3. Re: [JS][CS4] Export PDF pages by section?
              John Hawkinson Level 5

              Lindsay:

               

              Thanks for the help!  I'm sorry if my questions are extremely simple.  I'm just beginning to understand how to write javascript for INDD.  I tried to use the code you suggest below but got errors.  I figured it was because I didn't define the variable "section"?

              Yes. I was asssuming you would set the variable section to the relevant section of the document you wanted to print.

              For the first section, you could use: var section = app.activeDocument.sections[0]; and so-on for the next section, scaling arbitraily.

              What you suggest - is this basically stripping out the alpha prefix so that in the PDF in the pages you see "1, 2, 3", etc. instead of "A1, A2, A3"?  If not, can you describe what your suggested code is doing?  It might be a better approach than what I was trying to do.

              My example assumed you had no prefix set, it did not do any "stripping". It merely asks the section what pages are inside it, and then constructs the page range appropriately.

               

              That said, there were a few errors in my suggestion. pageStart returns a Page not a number, so you would have wanted either pageStart.name for the name of the page, which has duplicates, or pageStart.documentOffset for the absolute page number (zero-indexed). And to give a page range with absolute page numbers, you must prefix them with a plus sign. So, after, all that, probably you want:

               

              var section;
              section = app.activeDocument.sections[0];
              app.pdfExportPreferences.pageRange =
                "+"+(section.pageStart.documentOffset+1)+
                "-+"+
                (section.pageStart.documentOffset+1+section.length);
              app.activeDocument.exportFile(
                ExportFormat.PDFTYPE,
                new File("myDocument.pdf"));
              

               

              Still untested but maybe a little bit better thought out.

              (p.s.: with is evil).

              • 4. Re: [JS][CS4] Export PDF pages by section?
                booyajamo Level 1

                Hi John,

                 

                Thank you for your help!  It took me a while to really understand what you were suggesting but it all makes sense now and is much better than using the section prefix solution I had originally tried. I did modify your code slightly as follows:

                 

                (section.pageStart.documentOffset+0+section.length);
                

                 

                with "1" in there it was also exporting the first page of the second section. Do you see any potential problems with that change?  (i.e. does that # affect something else I am unfamiliar with?)

                 

                Thanks again!

                • 5. Re: [JS][CS4] Export PDF pages by section?
                  John Hawkinson Level 5

                  No, you're absolutely right. I told you it was untested! :-)

                  Somehow I was thinking that section.length was zero for a 1-page section and one for a 2-page section, etc., so you would have to add one to it. But of course that is not the case, so we should not be adding one. You don't need to add zero either, you can just use (section.pageStart.documentOffset+section.length); but of course it is the same either way.

                   

                  Sorry if I was a little cryptic along the way.

                  • 6. Re: [JS][CS4] Export PDF pages by section?
                    camilo umana Level 1

                    Good morning JH,

                     

                    This script you wrote is exactly something that could be of big help and could not find another reference in the Web to avoid posting here.

                    I tried to use it but is giving an error 55...

                     

                    Hope I am not violating the rules of begging for free script working if I ask you to give a little help?

                     

                    Thanks,

                     

                    Screen Shot 2014-10-17 at 07.40.39.jpg