7 Replies Latest reply on Apr 4, 2011 10:46 PM by c.pfaffenbichler

    Create a single tall image from a multi-page pdf file

    SloppySpackler

      I would like to take a multi-pake pdf file, and have all of the pages "appended" from top to bottom edge to create a single tall image. I am a teacher, and I currently do this by hand, I am then able to annotate the entire page and croll down an entire document inside of Photoshop.

       

      Here is an example: I have a pdf with 4 pages. Currently I open the pdf in PS, I end up with 4 images; I adjust the vertical canvas size of one of the images to be 4 times it's original size, then one-by-one I copy the contents of each image to that new tall image and move that layer vertically below the previous one. So the result is say a 1024x6400 pixel image.

       

      I would like to be able to just open the pdf with a script and get this all done in one step.

       

      Any help is appreciated.

        • 1. Re: Create a single tall image from a multi-page pdf file
          c.pfaffenbichler Level 9

          Could you give this a try?

           

          // opens all pages of pdfs cropped to trimbox with set settings;
          // 2011, use it at your own risk;
          #target photoshop
          var pdfOpenOpts = new PDFOpenOptions;
          pdfOpenOpts.antiAlias = true;
          pdfOpenOpts.bitsPerChannel = BitsPerChannelType.EIGHT;
          pdfOpenOpts.cropPage = CropToType.TRIMBOX;
          pdfOpenOpts.mode = OpenDocumentMode.CMYK;
          pdfOpenOpts.resolution = 150;
          pdfOpenOpts.suppressWarnings = true;
          pdfOpenOpts.usePageNumber  = true;
          // dialog for pdf-selection;
          var theFiles = app.openDialog();
          if (theFiles) {
          // change pref;
               var originalRulerUnits = app.preferences.rulerUnits;
          // change pref;
               var originalRulerUnits = app.preferences.rulerUnits;
               app.preferences.rulerUnits = Units.PIXELS;
               for (var m = 0; m < theFiles.length; m++) {
                    var theFile = theFiles[m];
                    if (theFile.name.slice(-4) == ".pdf") {
                         var thePdf = openMultipagePDF(theFile);
          // arrange pages;
                         var theHeight = thePdf.height;
                         thePdf.resizeCanvas(thePdf.width, theHeight * thePdf.layers.length, AnchorPosition.TOPCENTER);
                         for (var n = 0; n < thePdf.layers.length - 1; n++) {
                              thePdf.layers[n].translate(0, theHeight * (thePdf.layers.length - n - 1))
                              };
                         }
                    else {alert ("not a pdf-file")}
                    }
          // reset pref;
               app.preferences.rulerUnits = originalRulerUnits;
               };
          ////// function to open all pages of a pdf //////
          ////// influenced by PlaceMultipagePDF.jsx //////
          function openMultipagePDF(myPDFFile) {
          // suppress dialogs;
               var theDialogSettings = app.displayDialogs;
               app.displayDialogs = DialogModes.NO;
               var myCounter = 1;
               var myBreak = false;
               while(myBreak == false){
                    pdfOpenOpts.page = myCounter;
                    try {
                         var thePdf = app.open(myPDFFile, pdfOpenOpts);
                         thePdf.flatten();
                         thePdf.layers[0].isBackgroundLayer = false;
                         thePdf.layers[0].name = myPDFFile.name+"_"+myCounter;
                         if (myCounter == 1) {
                              var theFile = thePdf
                              }
                         else {
                              thePdf.layers[0].duplicate(theFile, ElementPlacement.PLACEATBEGINNING);
                              thePdf.close(SaveOptions.DONOTSAVECHANGES)
                              }
                         }
                    catch (e) {myBreak = true};
                    myCounter = myCounter + 1;
                    };
          // reset dialogmodes;
               app.displayDialogs = DialogModes.ERROR;
               return app.activeDocument
               };
          

          • 2. Re: Create a single tall image from a multi-page pdf file
            SloppySpackler Level 1

            Wow c.pfaffenbichler thanks for the quick reply! That script did what I was looking for; could I ask for a couple of modifications or assistance on which parameters would allow this?

             

            The final result of that script IS a tall image as requested; but the contents of the pdf have each been merged with a white background. I would like to place the contents of all of the pdf pages on a single layer "in front" of a white background (so I can erase text and contents of the pdf within my tall image and not erase the background as well). Only thing left is to perhaps automate saving: I would like the file to have the same name as the input pdf and in the same folder as well.

             

            In the meatime, THANK YOU SO MUCH, I scoured the internet and it seems that I am the first person to have publicly presented such a concept. Now I will try to study the script that you gave me and learn more about scripting.

            • 3. Re: Create a single tall image from a multi-page pdf file
              c.pfaffenbichler Level 9

              Well, the Script actually operates based on an unjustified assumption anyway, namely that all pages in the pdf are of the same dimensions.

               

              But editing pdfs in Photoshop is problematic in general, as vector and type content is rasterized.

               

              Try this:

               

              // opens all pages of pdfs cropped to trimbox with set settings and arranges them;
              // 2011, use it at your own risk;
              #target photoshop
              // dialog for pdf-selection;
              var theFiles = app.openDialog();
              if (theFiles) {
                   for (var m = 0; m < theFiles.length; m++) {
                        var theFile = theFiles[m];
                        if (theFile.name.slice(-4) == ".pdf") {
                             var thePdf = openMultipagePDF(theFile);
                             }
                        else {alert (theFile.name + " is not a pdf-file")}
                        }
                   };
              ////// function to open all pages of a pdf //////
              ////// influenced by PlaceMultipagePDF.jsx //////
              function openMultipagePDF(myPDFFile) {
              // pdf open options;
                   var pdfOpenOpts = new PDFOpenOptions;
                   pdfOpenOpts.antiAlias = true;
                   pdfOpenOpts.bitsPerChannel = BitsPerChannelType.EIGHT;
                   pdfOpenOpts.cropPage = CropToType.TRIMBOX;
                   pdfOpenOpts.mode = OpenDocumentMode.CMYK;
                   pdfOpenOpts.resolution = 150;
                   pdfOpenOpts.suppressWarnings = true;
                   pdfOpenOpts.usePageNumber  = true;
              // change pref;
                   var originalRulerUnits = app.preferences.rulerUnits;
                   app.preferences.rulerUnits = Units.PIXELS;
              // suppress dialogs;
                   var theDialogSettings = app.displayDialogs;
                   app.displayDialogs = DialogModes.NO;
              // iterate through pages until fail;
                   var myCounter = 1;
                   var myBreak = false;
                   var theWidth = 0;
                   var theHeight = 0;
                   while(myBreak == false){
                        pdfOpenOpts.page = myCounter;
                        try {
                             var thePdf = app.open(myPDFFile, pdfOpenOpts);
                             if (thePdf.width > theWidth) {theWidth = thePdf.width};
                             var offset = thePdf.height;
                             thePdf.layers[0].name = myPDFFile.name+"_"+myCounter;
                             if (myCounter == 1) {
                                  var theFile = thePdf
                                  }
                             else {
                                  thePdf.layers[0].duplicate(theFile, ElementPlacement.PLACEATBEGINNING);
                                  thePdf.close(SaveOptions.DONOTSAVECHANGES)
                                  };
                             myCounter = myCounter + 1;
                             var theLayer = app.activeDocument.activeLayer;
                             theLayer.translate(0, theHeight);
                             theHeight = theHeight + offset;
                             }
                        catch (e) {myBreak = true};
                        };
              // reset dialogmodes;
                   app.displayDialogs = DialogModes.ERROR;
                   app.preferences.rulerUnits = originalRulerUnits;
              // resize canvas;
                   theFile.resizeCanvas(theWidth, theHeight, AnchorPosition.TOPLEFT);
              // add white layer;
                   var white = new SolidColor;
                   white.hsb.hue = 0;
                   white.hsb.saturation = 0;
                   white.hsb.brightness = 100;
                   var bgLayer = theFile.artLayers.add();
                   bgLayer.move(theFile, ElementPlacement.PLACEATEND);
                   theFile.selection.selectAll();
                   theFile.selection.fill(white);
                   theFile.selection.deselect();
                   bgLayer.name = "background";
              // save psd;
                   psdOpts = new PhotoshopSaveOptions();
                   psdOpts.embedColorProfile = true;
                   psdOpts.alphaChannels = false;
                   psdOpts.layers = true;
                   psdOpts.spotColors = false;
                   var filePath = myPDFFile.path+"/"+myPDFFile.name.slice(0, -4)+".psd";
                   if (File(filePath).exists == true) {
                        var theConfirmation = confirm("overwrite existing file" + filePath + "?");
                        if (theConfirmation == true) {theFile.saveAs(File(filePath), psdOpts, false)}
                        else {};
                        }
                   else {theFile.saveAs(File(filePath), psdOpts, false)};
                   return theFile
                   };
              

              1 person found this helpful
              • 4. Re: Create a single tall image from a multi-page pdf file
                SloppySpackler Level 1

                Thanks again c.pfaffenbichler. I had most of it worked out in my own way before you got back to me, but I wasn't able to get the final naming part down.

                 

                Like I said, I am a math teacher, and I use photoshop as my whiteboard, and draw with a graphics tablet. So I can print to pdf from any program, now I can import any document into photoshop basically to write on it. So this is wonderful. I have never seen another teacher use PS as a whiteboard in class in this way, so I am most likely the only person in my county or state that would need such a script.

                 

                After I got everything worked out, I added some things to the script. Some of my colleagues will use my files as a whiteboard if I send it to them as a tall single page pdf that they can scroll down and write over; so they don't need photoshop. So I have scripted in the saving of that pdf as well. Here are my changes; most of what I changed is in the section commented MY WORK.

                // opens all pages of pdfs cropped to trimbox with set settings and arranges them;
                // 2011, use it at your own risk;
                #target photoshop
                // dialog for pdf-selection;
                var theFiles = app.openDialog();
                if (theFiles) {
                     for (var m = 0; m < theFiles.length; m++) {
                          var theFile = theFiles[m];
                          if (theFile.name.slice(-4) == ".pdf") {
                               var thePdf = openMultipagePDF(theFile);
                               }
                          else {alert (theFile.name + " is not a pdf-file")}
                          }
                     };
                ////// function to open all pages of a pdf //////
                ////// influenced by PlaceMultipagePDF.jsx //////
                function openMultipagePDF(myPDFFile) {
                // pdf open options;
                     var pdfOpenOpts = new PDFOpenOptions;
                     pdfOpenOpts.antiAlias = true;
                     pdfOpenOpts.bitsPerChannel = BitsPerChannelType.EIGHT;
                     pdfOpenOpts.cropPage = CropToType.MEDIABOX;
                     pdfOpenOpts.mode = OpenDocumentMode.RGB;
                     pdfOpenOpts.resolution = 247.8;
                     pdfOpenOpts.suppressWarnings = true;
                     pdfOpenOpts.usePageNumber  = true;
                // change pref;
                     var originalRulerUnits = app.preferences.rulerUnits;
                     app.preferences.rulerUnits = Units.PIXELS;
                // suppress dialogs;
                     var theDialogSettings = app.displayDialogs;
                     app.displayDialogs = DialogModes.NO;
                // iterate through pages until fail;
                          var myCounter = 1;
                          var myBreak = false;
                          var theWidth = 0;
                          var theHeight = 0;
                          while(myBreak == false){
                               pdfOpenOpts.page = myCounter;
                               try {
                                    var thePdf = app.open(myPDFFile, pdfOpenOpts);
                                    if (thePdf.width > theWidth) {theWidth = thePdf.width};
                                    var offset = thePdf.height;
                                    thePdf.layers[0].name = myPDFFile.name+"_"+myCounter;
                                    if (myCounter == 1) {
                                         var theFile = thePdf
                                         }
                                    else {
                                         thePdf.layers[0].duplicate(theFile, ElementPlacement.PLACEATBEGINNING);
                                         thePdf.close(SaveOptions.DONOTSAVECHANGES)
                                         };
                                    myCounter = myCounter + 1;
                                    var theLayer = app.activeDocument.activeLayer;
                                    theLayer.translate(0, theHeight);
                                    theHeight = theHeight + offset;
                                  }
                            catch (e) {myBreak = true};
                            };
                // reset dialogmodes;
                app.displayDialogs = DialogModes.ERROR;
                app.preferences.rulerUnits = originalRulerUnits;
                // resize canvas;
                    theFile.resizeCanvas(theWidth, theHeight, AnchorPosition.TOPLEFT);
                 
                //MY WORK
                // merge visible layers (even if there is only one layer)
                     try     {
                          theFile.mergeVisibleLayers();
                          }catch(e) {}
                // create layer, rename, movetobottom, fill white
                     theFile.artLayers.add();
                     theFile.layers[0].name = "Background";
                     theFile.layers[0].move(theFile.layers[1], ElementPlacement.PLACEAFTER);
                     var white = new SolidColor();
                     white.rgb["hexValue"] = "ffffff";
                     theFile.selection.selectAll();
                     theFile.selection.fill(white);
                //rename contents layer, add "work" layer for teacher to write on
                     theFile.layers[0].name = "Problems";
                     theFile.artLayers.add();
                     theFile.layers[0].name = "Work";
                 
                //force 2107 width
                //     if(theFile.width != 2107) theFile.resizeImage(UnitValue(2107, "px"), undefined, undefined, ResampleMethod.BICUBIC);
                // force 2048 width
                     if(theFile.width != 2048) theFile.resizeImage(UnitValue(2048, "px"), undefined, undefined, ResampleMethod.BICUBIC);
                 
                // save psd;
                    psdOpts = new PhotoshopSaveOptions();
                    psdOpts.embedColorProfile = true;
                    psdOpts.alphaChannels = false;
                    psdOpts.layers = true;
                    psdOpts.maximizeCompatibility = true;
                 
                    var filePathPsd = myPDFFile.path+"/"+myPDFFile.name.slice(0, -4)+".psd";
                    if (filePathPsd.exists) filePathPsd.remove();
                    theFile.saveAs(File(filePathPsd), psdOpts, false)
                 
                // save pdf whiteboard
                    var pdfOpts = new PDFSaveOptions;
                    pdfOpts.downSample = PDFResample.NONE;
                    pdfOpts.optimizeForWeb = true;
                    pdfOpts.PDFCompatibility = PDFCompatibility.PDF15;
                    pdfOpts.preserveEditing = false;
                    pdfOpts.encoding = PDFEncoding.PDFZIP;
                    
                    var filePathPdf = myPDFFile.path+"/"+myPDFFile.name.slice(0, -4)+" Whiteboard.pdf";
                    if (filePathPdf.exists) filePathPdf.remove();
                    theFile.saveAs(File(filePathPdf), pdfOpts, false)
                    theFile.close(SaveOptions.DONOTSAVECHANGES)
                 
                //MY WORK
                     return theFile
                     };
                

                • 5. Re: Create a single tall image from a multi-page pdf file
                  c.pfaffenbichler Level 9

                  One issue is the text will not be search-able in Acrobat if it is rasterized unless one does an OCR Text Recognition on it, I guess.

                   

                  Anyway, one thing about the pdf-pages-processing:

                  It is done with a try-clause, which is supposedly sloppy Scripting.

                  I had in the past gotten help from other contributors regarding some methods of determining a pdf’s page-count, but if I remember correctly they could all be tripped up with some pdf (e.g. a pdf created by combining different pdfs in Acrobat).

                  • 6. Re: Create a single tall image from a multi-page pdf file
                    SloppySpackler Level 1

                    I don't really use the "text" in pdf's. I work on the document as an image, and then since I am a math teacher, I will write on another layer above the pdf content as if I were working on an assignment myself. I do like to have all of the content with transparency preserved on one layer, because sometypes there can be small typo's in the assignments; in which case I will delete those typo's and replace them with text copied from other parts of the document, or the text tool if need be.

                     

                    Anywho, this got the job done for me, and got me SUPER interested in scripting. I just wish that I could write software in one language or another. Being such a technical person, or "geek," I make use of tons of software in my high-school classroom. I could implement alot of useful features in this software for teacher; I have the ideas. But I just need to be able to write my own software, or write my own plug-ins or extensions to the software.

                     

                    I don't want to have to go to a university. What is the best programming language and begginer course of study for programming? And where is a good place to get started at my own pace? How about some good forums that support tutorials? I appreciate any advice you can give.

                    • 7. Re: Create a single tall image from a multi-page pdf file
                      c.pfaffenbichler Level 9

                      I’m afraid I’m not the ideal person to answer those questions, plug-ins-creation is clear over my head.

                      For automating Adobe applications JavaScript seems preferable for its platform-indepedence.

                       

                      Apart from this Forum you could try asking at http://www.ps-scripts.com/