7 Replies Latest reply on Jun 12, 2018 10:36 AM by Leilang

    Changing the resolution for different pages in a multi-page PDF file

    Leilang

      Hello, I modified the script that I found on the page.

      I would like to change the resolution based on the height and width of a currently opened page.

      For example, if page 1 has 100cmx200cm resolution = 100dpi. If page 2 has 1000cmx2000cm resolution = 20dpi.

       

      The problem is that I do not know how to get the width and height of a specific page in a multipage pdf file.

       

      I will be grateful for any help

       

       

      This is what the code looks like:

       

      /* ==========================================================

      // 2017  John J. McAssey (JJMack) 

      // save the open Document as tif

      // The author accepts no liability for any problems arising from its use.

      // ======================================================= */ 

      // enable double-clicking from Mac Finder or Windows Explorer 

      #target photoshop // this command only works in Photoshop CS2 and higher 

      // bring application forward for double-click events 

      app.bringToFront(); 

       

       

       

       

       

       

        var theFile = File.openDialog("Select your PDF file", "Select:*.pdf");   

        app.displayDialogs = DialogModes.NO;   

        var check = true;   

        var page = 1;   

        // define pdfopenoptions;   

        var pdfOpenOpts = new PDFOpenOptions;   

        pdfOpenOpts.antiAlias = true;   

        pdfOpenOpts.bitsPerChannel = BitsPerChannelType.EIGHT;   

        pdfOpenOpts.cropPage = CropToType.MEDIABOX;   

        pdfOpenOpts.mode = OpenDocumentMode.RGB;   

        pdfOpenOpts.suppressWarnings = true;   

        pdfOpenOpts.usePageNumber  = true;   

        pdfOpenOpts.resolution = 30;       //   how to change the resolution based on the height and width of the currently opened page in the pdf file ??

       

       

          

       

        app.togglePalettes(); // toggle off palettes so Photoshop will not need to update them 

        while (check == true) {   

           try {   

                pdfOpenOpts.page = page; // open a page at it slower the using Photoshop UI ans selection all the pages you want   

                var thePdf = app.open(theFile, pdfOpenOpts);   

               page++;   

              

           }  

           catch (e) { check =  false };   

        };  

        • 1. Re: Changing the resolution for different pages in a multi-page PDF file
          sidpalas Level 2

          While not ideal (it is slow because it requires each page to be opened twice), this could work:

           

          Steps:

          1) Open the page without specifying a resolution (this will use the native resolution of the PDF)

          2) Once open, store the width/height

          3) Close the page (to avoid cluttering your workspace)

          4) Calculate the desired resolution based on the width/height (from step 2)

          5) Reopen the page with the desired resolution (from step 4)

           

          See below for an example of what I mean. I removed some of the additional options from pdfOpenOpts to keep the example simpler.

           

          You will want to update line 26 with whatever your formula for the desired resolution is.

           

          Hopefully this helps! Let me know if you have additional questions.

           

          #target photoshop
          
          var theFile = File.openDialog("Select your PDF file", "Select:*.pdf");
          var pagesRemaining = true;
          var page = 1;
          app.preferences.rulerUnits = Units.CM; //because you specified your input resolutions in CM
          
          while (pagesRemaining){
              try { 
                  // define pdfopenoptions; 
                  var pdfOpenOpts = new PDFOpenOptions; 
                  pdfOpenOpts.usePageNumber  = true; 
                  pdfOpenOpts.page = page; // open a page at it slower the using Photoshop UI ans selection all the pages you want 
                 
                  //open once without a resolution specified
                  var thePage = app.open(theFile, pdfOpenOpts); 
                 
                  //get the size of the page
                  var thePageWidth = thePage.width;
                  var thePageHeight = thePage.height;
                 
                  //close once you have the necessary info
                  thePage.close(SaveOptions.DONOTSAVECHANGES);
                 
                  //determine the desired resolution
                  var desiredResolution = 50 * (200 / thePageWidth) //replace with your desired formula
                  pdfOpenOpts.resolution = desiredResolution; 
                 
                  //reopen with the correct resolution
                  var thePage = app.open(theFile, pdfOpenOpts);     
                  page++;
                  } 
          
                catch (e) {
                    $.writeln("Breaking loop on page" + page)
                    pagesRemaining =  false //no pages remain
                    }; 
            };
          
          1 person found this helpful
          • 2. Re: Changing the resolution for different pages in a multi-page PDF file
            Stephen_A_Marsh Adobe Community Professional

            Not sure if this helps or not…  PDF files use PostScript points 1/72 inch as the native unit of measure. So an A4 210mm wide x 297mm tall document would likely be reported as 595 x 842 pt in size.

            • 3. Re: Changing the resolution for different pages in a multi-page PDF file
              Leilang Level 1

              cool ! works perfectly;) I added a function that changes the dpi for printing.

              I also changed the resolution at the first opening to 1 dpi = faster

              I throw the code, maybe someone will need it. Thank you for your help !

               

              var theFile = File.openDialog("Select your PDF file", "Select:*.pdf");

              var pagesRemaining = true;

              var page = 1;

              app.preferences.rulerUnits = Units.CM; //because you specified your input resolutions in CM

               

              var pdfOpenOpts = new PDFOpenOptions;  

              pdfOpenOpts.antiAlias = true;  

                pdfOpenOpts.bitsPerChannel = BitsPerChannelType.EIGHT;  

                pdfOpenOpts.cropPage = CropToType.CROPBOX;  

                pdfOpenOpts.mode = OpenDocumentMode.CMYK;  

                pdfOpenOpts.resolution = 1;  

                pdfOpenOpts.suppressWarnings = true;  

                pdfOpenOpts.usePageNumber  = true;    

               

               

               

              while (pagesRemaining){

                  try { 

                      // define pdfopenoptions; 

                      var pdfOpenOpts = new PDFOpenOptions; 

                       pdfOpenOpts.cropPage = CropToType.CROPBOX;

                       pdfOpenOpts.mode = OpenDocumentMode.CMYK; 

                       pdfOpenOpts.bitsPerChannel = BitsPerChannelType.EIGHT;

                      pdfOpenOpts.usePageNumber  = true; 

                      pdfOpenOpts.page = page; // open a page at it slower the using Photoshop UI ans selection all the pages you want 

                     

                      //open once without a resolution specified

                      var thePage = app.open(theFile, pdfOpenOpts); 

                     

                      //get the size of the page

                      var thePageWidth = thePage.width;

                      var thePageHeight = thePage.height;

                     

                      //close once you have the necessary info

                      thePage.close(SaveOptions.DONOTSAVECHANGES);

                     

                      //determine the desired resolution

                      var testres = thePageWidth * thePageHeight;

                    

                      var res = "";

              if (testres <= 20000) {

                    var res = 150;

                  }  else if (testres > 20000 && testres <= 50000) {

                      var res = 100;

                    

                      }  else if (testres > 50000 && testres <= 150000) {

                      var res = 72;

                    

                      }  else if (testres > 150000 && testres <= 300000) {

                      var res = 60;

                    

                      }  else if (testres > 300000 && testres <= 450000) {

                      var res = 50;

                    

                      }  else if (testres > 450000 && testres <= 600000) {

                      var res = 40;

                    

                      }  else if (testres > 600000 ) {

                      var res = 30;

                    

                   

                      }

                    

                    

                    

                    

                      var desiredResolution = res; //replace with your desired formula

                      pdfOpenOpts.resolution = desiredResolution; 

                     

                      //reopen with the correct resolution

                      var thePage = app.open(theFile, pdfOpenOpts);     

                      page++;

                      } 

               

                    catch (e) {

                     

                        pagesRemaining =  false //no pages remain

                        }; 

                };

               

               

              But I have one more question What to add to the script so that I can open several files at once in one folder?

              For example, the tiff, jpg and png files simply open without changes, but pdf files with the above-written script?

              • 4. Re: Changing the resolution for different pages in a multi-page PDF file
                sidpalas Level 2

                If you use app.openDialog() instead of File.openDialog(), the user can select multiple files.

                 

                The method will return an array of those File objects which you can then loop through and check the filetype extension to decide how to handle.

                • 5. Re: Changing the resolution for different pages in a multi-page PDF file
                  Leilang Level 1

                  Thank you for the hint;)

                   

                  I tried it according to your advice but I'm stuck.

                  The script opens all pdf, tiff and jpg files but does not change the resolution in pdf files and opens only the first page.

                  Even if I open only one pdf file by Folder.selectDialog it looks the same.

                   

                  var theFolder = Folder.selectDialog("select folder");
                  
                  
                  if (theFolder) {
                  
                  
                      var theFiles = theFolder.getFiles(/\.(jpg|tif|pdf)$/i);
                  
                  
                      for (var m = 0; m < theFiles.length; m++) {
                  
                  
                          app.open(File(theFiles[m]))
                  
                  
                            }
                      }
                  
                  
                  var pagesRemaining = true;  
                  var page = 1;  
                  app.preferences.rulerUnits = Units.CM; //because you specified your input resolutions in CM  
                    
                  var pdfOpenOpts = new PDFOpenOptions;    
                  pdfOpenOpts.antiAlias = true;    
                    pdfOpenOpts.bitsPerChannel = BitsPerChannelType.EIGHT;    
                    pdfOpenOpts.cropPage = CropToType.CROPBOX;    
                    pdfOpenOpts.mode = OpenDocumentMode.CMYK;    
                    pdfOpenOpts.resolution = 1;    
                    pdfOpenOpts.suppressWarnings = true;    
                    pdfOpenOpts.usePageNumber  = true;      
                    
                    
                    
                  while (pagesRemaining){  
                      try {   
                          // define pdfopenoptions;   
                          var pdfOpenOpts = new PDFOpenOptions;   
                           pdfOpenOpts.cropPage = CropToType.CROPBOX;
                           pdfOpenOpts.mode = OpenDocumentMode.CMYK;   
                           pdfOpenOpts.bitsPerChannel = BitsPerChannelType.EIGHT; 
                          pdfOpenOpts.usePageNumber  = true;   
                          pdfOpenOpts.page = page; // open a page at it slower the using Photoshop UI ans selection all the pages you want   
                           
                          //open once without a resolution specified  
                          var thePage = app.open(theFile, pdfOpenOpts);   
                           
                          //get the size of the page  
                          var thePageWidth = thePage.width;  
                          var thePageHeight = thePage.height;  
                           
                          //close once you have the necessary info  
                          thePage.close(SaveOptions.DONOTSAVECHANGES);  
                           
                          //determine the desired resolution  
                          var testres = thePageWidth * thePageHeight; 
                          
                          var res = ""; 
                  if (testres <= 20000) {
                        var res = 150;
                      }  else if (testres > 20000 && testres <= 50000) {
                          var res = 100;
                          
                          }  else if (testres > 50000 && testres <= 150000) {
                          var res = 72;
                          
                          }  else if (testres > 150000 && testres <= 300000) {
                          var res = 60;
                          
                          }  else if (testres > 300000 && testres <= 450000) {
                          var res = 50;
                          
                          }  else if (testres > 450000 && testres <= 600000) {
                          var res = 40;
                          
                          }  else if (testres > 600000 ) {
                          var res = 30;
                          
                         
                          }
                          
                          var desiredResolution = res; //replace with your desired formula  
                          pdfOpenOpts.resolution = desiredResolution;   
                           
                          //reopen with the correct resolution  
                          var thePage = app.open(theFile, pdfOpenOpts);       
                          page++;  
                          }   
                    
                        catch (e) {  
                           
                            pagesRemaining =  false //no pages remain  
                            };   
                    };  
                  
                  
                  
                  
                  • 6. Re: Changing the resolution for different pages in a multi-page PDF file
                    sidpalas Level 2

                    You are close!

                     

                    The while loop must be run for each pdf file, so it needs to go into a for loop containing the pdf files. The other filetypes can be handled separetely without the special resolution check.

                     

                    I think the below code accomplishes what you want:

                     

                    #target photoshop
                    
                    var theFolder = Folder.selectDialog("select folder");
                    
                    if (theFolder) {
                        //get all of the jpg and tiff files and handle them separately
                        var theNonPDFFiles = theFolder.getFiles((/\.(jpg|tif)$/i));
                        for (var fileIdx = 0; fileIdx < theNonPDFFiles.length; fileIdx++) {
                            app.open(File(theNonPDFFiles[fileIdx]))
                            }
                    
                        //get the pdf files to handle with the resolution check
                        var thePDFFiles = theFolder.getFiles("*.pdf");
                        app.preferences.rulerUnits = Units.CM; //because you specified your input resolutions in CM
                        app.displayDialogs = DialogModes.NO;
                        for (var fileIdx = 0; fileIdx < thePDFFiles.length; fileIdx++) {
                            var theFile = thePDFFiles[fileIdx];
                            var pagesRemaining = true;
                            var page = 1;
                            //we need to perform our loop over each pdf file
                            while (pagesRemaining){
                                try {
                                    // define pdfopenoptions;
                                    var pdfOpenOpts = new PDFOpenOptions;
                                    pdfOpenOpts.antiAlias = true;
                                    pdfOpenOpts.bitsPerChannel = BitsPerChannelType.EIGHT;
                                    pdfOpenOpts.cropPage = CropToType.CROPBOX;
                                    pdfOpenOpts.mode = OpenDocumentMode.CMYK;
                                    pdfOpenOpts.resolution = 1;
                                    pdfOpenOpts.bitsPerChannel = BitsPerChannelType.EIGHT;
                                    pdfOpenOpts.usePageNumber  = true;
                                    pdfOpenOpts.page = page;
                                    //open once without a resolution specified
                                    var thePage = app.open(theFile, pdfOpenOpts);
                    
                    
                                    //get the size of the page
                                    var thePageWidth = thePage.width;
                                    var thePageHeight = thePage.height;
                    
                                    //close once you have the necessary info
                                    thePage.close(SaveOptions.DONOTSAVECHANGES);
                    
                                    //determine the desired resolution
                                    var testres = thePageWidth * thePageHeight;
                    
                                    var res = "";
                                    if (testres <= 20000) {
                                          var res = 150;
                                        }
                                    else if (testres > 20000 && testres <= 50000) {
                                            var res = 100;
                                            }
                                    else if (testres > 50000 && testres <= 150000) {
                                            var res = 72;
                                            }
                                    else if (testres > 150000 && testres <= 300000) {
                                            var res = 60;
                                            }
                                    else if (testres > 300000 && testres <= 450000) {
                                            var res = 50;
                                            }
                                    else if (testres > 450000 && testres <= 600000) {
                                            var res = 40;
                                            }
                                    else { //(testres > 600000 )
                                            var res = 30;
                                            }
                    
                                    pdfOpenOpts.resolution = res;
                    
                                    //reopen with the correct resolution
                                    var thePage = app.open(theFile, pdfOpenOpts);
                    
                                    page++;
                                    }
                    
                                  catch (e) {
                                      pagesRemaining =  false //no pages remain
                                      };
                              };
                            }
                        }
                    
                    1 person found this helpful
                    • 7. Re: Changing the resolution for different pages in a multi-page PDF file
                      Leilang Level 1

                      Ideally, that's what I needed.

                      Thank you again for help, without you I would not be able to make it;)