19 Replies Latest reply on Jul 23, 2012 7:05 AM by mangalarajiv

    Working with Pdf files

    mangalarajiv

      Hi,

       

      I have ten pdf files in a folder. Total number of pages are variying from one to another.

       

      How do i open a pdf file, how to open all the pages one by one for image editing and save it as "jpg"

       

      Could you please help me

       

      thanks,

      rajiv.s

        • 1. Re: Working with Pdf files
          Paul Riggott Level 6

          This might help...

           

          http://www.scriptsrus.talktalk.net/Pdfprocessor.htm

           

          EDIT (Pierre Courtejoie): Paul's scripts are now hosted here: https://github.com/Paul-Riggott/PS-Scripts

          • 2. Re: Working with Pdf files
            mangalarajiv Level 1

            Hi Paul,

             

            I had tried this one. We are expecting to find a script which should be able to improve the jagged text in the pdf and save it as 'jpg'.

            then it should arrange the jpg files by order (Page1 - Page 100) and again save as "pdf".

             

            thanks,

            Rajiv.s

            • 3. Re: Working with Pdf files
              c.pfaffenbichler Level 9

              Are the original pdfs proper vector-pdfs, are the texts actual searchable text?

              Because then I would doubt the wisdom of converting them to pdfs than only contain pixel images.

              • 4. Re: Working with Pdf files
                mangalarajiv Level 1

                Not searchable text.

                 

                 

                var pdf = File.openDialog( "select file to open","PDF:*.pdf");

                if(pdf != null){

                for(var i =1; i<500; i++){   

                    var mode = OpenDocumentMode.RGB;

                     rasterizePDF( i, 300, mode, pdf); //Page Number, Resolution, File

                  if(app.documents.length == 0) break;    //loop passed the number of pages

                      // code to process page      

                     

                app.activeDocument.flatten();

                app.activeDocument.activeLayer.duplicate();

                app.activeDocument.activeLayer = app.activeDocument.artLayers.getByName('Background copy');

                var idDfs = charIDToTypeID( "Dfs " );

                var desc44 = new ActionDescriptor();

                var idMd = charIDToTypeID( "Md  " );

                var idDfsM = charIDToTypeID( "DfsM" );

                var idanisotropic = stringIDToTypeID( "anisotropic" );

                desc44.putEnumerated( idMd, idDfsM, idanisotropic );

                var idFlRs = charIDToTypeID( "FlRs" );

                desc44.putInteger( idFlRs, 4837760 );

                executeAction( idDfs, desc44, DialogModes.NO );

                app.activeDocument.activeLayer.applyUnSharpMask(50, 2.0, 0);

                app.activeDocument.flatten();

                 

                if(app.documents.length > 0){

                   var saveFile = new File(decodeURI(pdf.fsName.slice(0,-4) +"-Page" + i + ".jpg"));

                   SaveJPEG(saveFile,12);

                   app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

                                }

                            }

                        }

                 

                 

                function rasterizePDF( pageNumber,res, mode, pdfFile){//pageNumber as integer, pdfFile as File object

                    var desc = new ActionDescriptor();

                        var optionsDesc = new ActionDescriptor();

                        optionsDesc.putString( charIDToTypeID( "Nm  " ), "rasterized page" );

                        optionsDesc.putEnumerated( charIDToTypeID( "Crop" ), stringIDToTypeID( "cropTo" ), stringIDToTypeID( "boundingBox" ) );

                        optionsDesc.putUnitDouble( charIDToTypeID( "Rslt" ), charIDToTypeID( "#Rsl" ), res);

                        optionsDesc.putEnumerated( charIDToTypeID( "Md  " ), charIDToTypeID( "ClrS" ), charIDToTypeID( "ERGB" ) );

                        optionsDesc.putInteger( charIDToTypeID( "Dpth" ), 8 );

                        optionsDesc.putBoolean( charIDToTypeID( "AntA" ), true );

                        optionsDesc.putBoolean( stringIDToTypeID( "suppressWarnings" ), false );

                        optionsDesc.putEnumerated( charIDToTypeID( "fsel" ), stringIDToTypeID( "pdfSelection" ), stringIDToTypeID( "page"  ));

                        optionsDesc.putInteger( charIDToTypeID( "PgNm" ), pageNumber );

                    desc.putObject( charIDToTypeID( "As  " ), charIDToTypeID( "PDFG" ), optionsDesc );

                    desc.putPath( charIDToTypeID( "null" ), pdfFile );

                executeAction( charIDToTypeID( "Opn " ), desc, DialogModes.NO );

                };

                 

                function SaveJPEG(saveFile, jpegQuality){

                jpgSaveOptions = new JPEGSaveOptions()

                jpgSaveOptions.embedColorProfile = true

                jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE

                jpgSaveOptions.matte = MatteType.NONE

                jpgSaveOptions.quality = jpegQuality //1-12

                activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE)

                }

                 

                The above mentioned script is executing 50% of the task.  While to loading this script, we need to select every files manually.

                 

                I want to make this script to load a pdf file in Photoshop, the pdf file contains some 98 pages (Note: I have a different total number of pdf). So It should load the pages one by one.

                The script will enhance the pages and save it as 'jpg'. I want to save the jpg files in a separate folder for each pdf file. Once completed with the enhancement. We need to stack all the "jpg"s from a folder and save it as "pdf".

                 

                Could you please help me on this

                Thank you so much for your help!

                 

                rajiv.s

                • 5. Re: Working with Pdf files
                  c.pfaffenbichler Level 9

                  Do you have Photoshop CS6?

                  Because PDF Presentation was reintroduced and therefore one could create multipage pdfs from Photoshop again.

                  • 6. Re: Working with Pdf files
                    c.pfaffenbichler Level 9

                    Something like the following should work; I amended the code you posted and you should probably include a check for the existence of a jpgs-folder to avoid overwriting existing files.

                     

                    // 2012, use it at your own risk;

                    #target photoshop

                    var pdf = File.openDialog( "select file to open","PDF:*.pdf");

                     

                     

                    if(pdf != null){

                    var theFiles = new Array;

                    var folderPath = pdf.fsName.slice(0, pdf.fsName.lastIndexOf("/"))+"/jpgs";alert (folderPath);

                    alert (folderPath);

                    var theFolder = new Folder(folderPath);

                    theFolder.create();

                    for(var i =1; i<500; i++){  

                     

                     

                        var mode = OpenDocumentMode.RGB;

                     

                     

                         rasterizePDF( i, 300, mode, pdf); //Page Number, Resolution, File

                     

                     

                      if(app.documents.length == 0) break;    //loop passed the number of pages

                     

                     

                          // code to process page     

                     

                     

                     

                     

                     

                    app.activeDocument.flatten();

                     

                     

                    app.activeDocument.activeLayer.duplicate();

                     

                     

                    app.activeDocument.activeLayer = app.activeDocument.artLayers.getByName('Background copy');

                     

                     

                    var idDfs = charIDToTypeID( "Dfs " );

                     

                     

                    var desc44 = new ActionDescriptor();

                     

                     

                    var idMd = charIDToTypeID( "Md  " );

                     

                     

                    var idDfsM = charIDToTypeID( "DfsM" );

                     

                     

                    var idanisotropic = stringIDToTypeID( "anisotropic" );

                     

                     

                    desc44.putEnumerated( idMd, idDfsM, idanisotropic );

                     

                     

                    var idFlRs = charIDToTypeID( "FlRs" );

                     

                     

                    desc44.putInteger( idFlRs, 4837760 );

                     

                     

                    executeAction( idDfs, desc44, DialogModes.NO );

                     

                     

                    app.activeDocument.activeLayer.applyUnSharpMask(50, 2.0, 0);

                     

                     

                    app.activeDocument.flatten();

                     

                     

                    if(app.documents.length > 0){

                              var saveFile = new File(folderPath + "/" + pdf.name +"-Page" + i + ".jpg");

                    //          var saveFile = new File(decodeURI(pdf.fsName.slice(0,-4) +"-Page" + i + ".jpg"));

                              SaveJPEG(saveFile,12);

                              app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

                              theFiles.push(saveFile);

                              };

                    };

                    //alert (theFiles.join("\n"))

                    pdfPresentation (theFiles, String(pdf.fsName).replace(".pdf", "_jpg.pdf"))

                    };

                     

                     

                     

                     

                     

                     

                     

                     

                    function rasterizePDF( pageNumber,res, mode, pdfFile){//pageNumber as integer, pdfFile as File object

                     

                     

                        var desc = new ActionDescriptor();

                     

                     

                            var optionsDesc = new ActionDescriptor();

                     

                     

                            optionsDesc.putString( charIDToTypeID( "Nm  " ), "rasterized page" );

                     

                     

                            optionsDesc.putEnumerated( charIDToTypeID( "Crop" ), stringIDToTypeID( "cropTo" ), stringIDToTypeID( "boundingBox" ) );

                     

                     

                            optionsDesc.putUnitDouble( charIDToTypeID( "Rslt" ), charIDToTypeID( "#Rsl" ), res);

                     

                     

                            optionsDesc.putEnumerated( charIDToTypeID( "Md  " ), charIDToTypeID( "ClrS" ), charIDToTypeID( "ERGB" ) );

                     

                     

                            optionsDesc.putInteger( charIDToTypeID( "Dpth" ), 8 );

                     

                     

                            optionsDesc.putBoolean( charIDToTypeID( "AntA" ), true );

                     

                     

                            optionsDesc.putBoolean( stringIDToTypeID( "suppressWarnings" ), false );

                     

                     

                            optionsDesc.putEnumerated( charIDToTypeID( "fsel" ), stringIDToTypeID( "pdfSelection" ), stringIDToTypeID( "page"  ));

                     

                     

                            optionsDesc.putInteger( charIDToTypeID( "PgNm" ), pageNumber );

                     

                     

                        desc.putObject( charIDToTypeID( "As  " ), charIDToTypeID( "PDFG" ), optionsDesc );

                     

                     

                        desc.putPath( charIDToTypeID( "null" ), pdfFile );

                     

                     

                    executeAction( charIDToTypeID( "Opn " ), desc, DialogModes.NO );

                     

                     

                    };

                     

                     

                     

                     

                     

                    function SaveJPEG(saveFile, jpegQuality){

                     

                     

                    jpgSaveOptions = new JPEGSaveOptions()

                     

                     

                    jpgSaveOptions.embedColorProfile = true

                     

                     

                    jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE

                     

                     

                    jpgSaveOptions.matte = MatteType.NONE

                     

                     

                    jpgSaveOptions.quality = jpegQuality //1-12

                     

                     

                    activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE)

                     

                     

                    };

                     

                    ////// function pdf presentation //////

                    function pdfPresentation (theFiles, thePath) {

                    // =======================================================

                    var idPDFExport = stringIDToTypeID( "PDFExport" );

                        var desc1 = new ActionDescriptor();

                        var idflst = charIDToTypeID( "flst" );

                            var list1 = new ActionList();

                    for (var m = 0; m < theFiles.length; m++) {

                            list1.putPath( new File( theFiles[m] ) );

                    //        list1.putPath( new File( "/Volumes/Backup HD/photoshop/test/illustratorMove.jpg" ) );

                    //        list1.putPath( new File( "/Volumes/Backup HD/photoshop/test/scripteventmanager.jpg" ) );

                                        };

                        desc1.putList( idflst, list1 );

                        var idT = charIDToTypeID( "T   " );

                        desc1.putPath( idT, new File( thePath ) );

                        var idincludeAnnotations = stringIDToTypeID( "includeAnnotations" );

                        desc1.putBoolean( idincludeAnnotations, true );

                        var idBckC = charIDToTypeID( "BckC" );

                        var idBckC = charIDToTypeID( "BckC" );

                        var idWht = charIDToTypeID( "Wht " );

                        desc1.putEnumerated( idBckC, idBckC, idWht );

                        var idAs = charIDToTypeID( "As  " );

                            var desc2 = new ActionDescriptor();

                            var idpdfPresetFilename = stringIDToTypeID( "pdfPresetFilename" );

                            desc2.putString( idpdfPresetFilename, """High Quality Print""" );

                            var idpdfPreserveEditing = stringIDToTypeID( "pdfPreserveEditing" );

                            desc2.putBoolean( idpdfPreserveEditing, false );

                            var idpdfDownSample = stringIDToTypeID( "pdfDownSample" );

                            var idpdfDownSample = stringIDToTypeID( "pdfDownSample" );

                            var idNone = charIDToTypeID( "None" );

                            desc2.putEnumerated( idpdfDownSample, idpdfDownSample, idNone );

                            var idpdfCompressionType = stringIDToTypeID( "pdfCompressionType" );

                            desc2.putInteger( idpdfCompressionType, 7 );

                        var idPhtP = charIDToTypeID( "PhtP" );

                        desc1.putObject( idAs, idPhtP, desc2 );

                    executeAction( idPDFExport, desc1, DialogModes.NO );

                    };

                    • 7. Re: Working with Pdf files
                      mangalarajiv Level 1

                      Instead of select pdf files manually, Why can't we select an entire folder to process?

                       

                      It would be very helpful to me....

                       

                      thanks.,

                      rajiv.s

                      • 8. Re: Working with Pdf files
                        c.pfaffenbichler Level 9

                        If the Script works OK for single files, you could wrap it in a function to be used in a for-function to pricess the pdfs selected with a Folder-selection.

                         

                        var theFolder = Folder.selectDialog ("select folder");

                        if (theFolder) {

                        var theFiles = theFolder.getFiles(/\.(pdf)$/i);

                        };

                        • 9. Re: Working with Pdf files
                          mangalarajiv Level 1

                          Could you please combine the above lates script with the old script?

                           

                          I am getting the following error; Could you please help me to get rid off that?

                           

                           

                          Error.jpg

                           

                          thanks,

                          rajiv.s

                          • 10. Re: Working with Pdf files
                            c.pfaffenbichler Level 9

                            How have you set up the for-clause to iterate through the pdf-files from the selected Folder?

                            • 11. Re: Working with Pdf files
                              c.pfaffenbichler Level 9

                              By the way: You mentioned the pdfs have no searchable text, so I wonder if the individual odf-pages are actually images?

                              In that case converting the pages would seem less advisable than opening the images.

                              • 12. Re: Working with Pdf files
                                mangalarajiv Level 1

                                I have to enhance the quality of the jagged text in the pages, so i need to open it.

                                • 13. Re: Working with Pdf files
                                  c.pfaffenbichler Level 9

                                  So do the pages originally contain vector/font elements or not?

                                  • 14. Re: Working with Pdf files
                                    mangalarajiv Level 1

                                    We need to diffuse all the pages.

                                    • 15. Re: Working with Pdf files
                                      c.pfaffenbichler Level 9

                                      That does not answer my question.

                                      Does the original pdf consist of pages that contain vector data or is it actually a pdf that consists of only one image per page?

                                       

                                      // 2012, use it at your own risk;

                                      #target photoshop

                                      // get folder;

                                      var theFolder = Folder.selectDialog ("select folder");

                                      if (theFolder) {

                                      var theFiles = theFolder.getFiles(/\.(pdf)$/i);

                                      if (theFiles.length > 0) {

                                      // work through files;

                                      for (var x = 0; x < theFiles.length; x++) {

                                      var pdf = theFiles[x];

                                      // create jpgs;

                                      if(pdf != null){

                                      var theseFiles = new Array;

                                      var folderPath = pdf.fsName.slice(0, pdf.fsName.lastIndexOf("/"))+"/jpgs";

                                      var theFolder = new Folder(folderPath);

                                      if (theFolder.exists == false) {theFolder.create()};

                                      for(var i =1; i<500; i++){

                                          var mode = OpenDocumentMode.RGB;

                                                rasterizePDF( i, 300, mode, pdf); //Page Number, Resolution, File

                                                if(app.documents.length == 0) break;    //loop passed the number of pages

                                      // code to process page     

                                      app.activeDocument.flatten();

                                      app.activeDocument.activeLayer.duplicate();

                                      app.activeDocument.activeLayer = app.activeDocument.artLayers.getByName('Background copy');

                                      var idDfs = charIDToTypeID( "Dfs " );

                                      var desc44 = new ActionDescriptor();

                                      var idMd = charIDToTypeID( "Md  " );

                                      var idDfsM = charIDToTypeID( "DfsM" );

                                      var idanisotropic = stringIDToTypeID( "anisotropic" );

                                      desc44.putEnumerated( idMd, idDfsM, idanisotropic );

                                      var idFlRs = charIDToTypeID( "FlRs" );

                                      desc44.putInteger( idFlRs, 4837760 );

                                      executeAction( idDfs, desc44, DialogModes.NO );

                                      app.activeDocument.activeLayer.applyUnSharpMask(50, 2.0, 0);

                                      app.activeDocument.flatten();

                                      if(app.documents.length > 0){

                                                var saveFile = new File(folderPath + "/" + pdf.name +"-Page" + i + ".jpg");

                                                SaveJPEG(saveFile,12);

                                                app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

                                                theseFiles.push(saveFile);

                                                };

                                      };

                                      // create the collection pdf;

                                      pdfPresentation (theseFiles, String(pdf.fsName).replace(".pdf", "_jpg.pdf"))

                                      }

                                      }

                                      }

                                      };

                                      ////////////////////////////////////

                                      function rasterizePDF( pageNumber,res, mode, pdfFile){//pageNumber as integer, pdfFile as File object

                                          var desc = new ActionDescriptor();

                                              var optionsDesc = new ActionDescriptor();

                                              optionsDesc.putString( charIDToTypeID( "Nm  " ), "rasterized page" );

                                              optionsDesc.putEnumerated( charIDToTypeID( "Crop" ), stringIDToTypeID( "cropTo" ), stringIDToTypeID( "trimBox" ) );

                                              optionsDesc.putUnitDouble( charIDToTypeID( "Rslt" ), charIDToTypeID( "#Rsl" ), res);

                                              optionsDesc.putEnumerated( charIDToTypeID( "Md  " ), charIDToTypeID( "ClrS" ), charIDToTypeID( "ERGB" ) );

                                              optionsDesc.putInteger( charIDToTypeID( "Dpth" ), 8 );

                                              optionsDesc.putBoolean( charIDToTypeID( "AntA" ), true );

                                              optionsDesc.putBoolean( stringIDToTypeID( "suppressWarnings" ), false );

                                              optionsDesc.putEnumerated( charIDToTypeID( "fsel" ), stringIDToTypeID( "pdfSelection" ), stringIDToTypeID( "page"  ));

                                              optionsDesc.putInteger( charIDToTypeID( "PgNm" ), pageNumber );

                                          desc.putObject( charIDToTypeID( "As  " ), charIDToTypeID( "PDFG" ), optionsDesc );

                                          desc.putPath( charIDToTypeID( "null" ), pdfFile );

                                      executeAction( charIDToTypeID( "Opn " ), desc, DialogModes.NO );

                                      };

                                      function SaveJPEG(saveFile, jpegQuality){

                                      jpgSaveOptions = new JPEGSaveOptions()

                                      jpgSaveOptions.embedColorProfile = true

                                      jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE

                                      jpgSaveOptions.matte = MatteType.NONE

                                      jpgSaveOptions.quality = jpegQuality //1-12

                                      activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE)

                                      };

                                      ////// function pdf presentation //////

                                      function pdfPresentation (theFiles, thePath) {

                                      // =======================================================

                                      var idPDFExport = stringIDToTypeID( "PDFExport" );

                                          var desc1 = new ActionDescriptor();

                                          var idflst = charIDToTypeID( "flst" );

                                              var list1 = new ActionList();

                                      for (var m = 0; m < theFiles.length; m++) {

                                              list1.putPath( new File( theFiles[m] ) );

                                      //        list1.putPath( new File( "/Volumes/Backup HD/photoshop/test/illustratorMove.jpg" ) );

                                      //        list1.putPath( new File( "/Volumes/Backup HD/photoshop/test/scripteventmanager.jpg" ) );

                                                          };

                                          desc1.putList( idflst, list1 );

                                          var idT = charIDToTypeID( "T   " );

                                          desc1.putPath( idT, new File( thePath ) );

                                          var idincludeAnnotations = stringIDToTypeID( "includeAnnotations" );

                                          desc1.putBoolean( idincludeAnnotations, true );

                                          var idBckC = charIDToTypeID( "BckC" );

                                          var idBckC = charIDToTypeID( "BckC" );

                                          var idWht = charIDToTypeID( "Wht " );

                                          desc1.putEnumerated( idBckC, idBckC, idWht );

                                          var idAs = charIDToTypeID( "As  " );

                                              var desc2 = new ActionDescriptor();

                                              var idpdfPresetFilename = stringIDToTypeID( "pdfPresetFilename" );

                                              desc2.putString( idpdfPresetFilename, """High Quality Print""" );

                                              var idpdfPreserveEditing = stringIDToTypeID( "pdfPreserveEditing" );

                                              desc2.putBoolean( idpdfPreserveEditing, false );

                                              var idpdfDownSample = stringIDToTypeID( "pdfDownSample" );

                                              var idpdfDownSample = stringIDToTypeID( "pdfDownSample" );

                                              var idNone = charIDToTypeID( "None" );

                                              desc2.putEnumerated( idpdfDownSample, idpdfDownSample, idNone );

                                              var idpdfCompressionType = stringIDToTypeID( "pdfCompressionType" );

                                              desc2.putInteger( idpdfCompressionType, 7 );

                                          var idPhtP = charIDToTypeID( "PhtP" );

                                          desc1.putObject( idAs, idPhtP, desc2 );

                                      executeAction( idPDFExport, desc1, DialogModes.NO );

                                      };

                                      • 16. Re: Working with Pdf files
                                        c.pfaffenbichler Level 9

                                        It seems the code you originally posted would include open documents in the pdf (provided the pdf was below 500 pages) and close them.

                                        Easiest to avoid that might be to change

                                        if (theFolder) {

                                        to

                                        if (theFolder && app.documents.length == 0) {

                                        • 17. Re: Working with Pdf files
                                          mangalarajiv Level 1

                                          Hi,

                                           

                                          When i use this script in Photoshop Cs5 extended version, it shows, " PDF exporting does not support in the version of this software".

                                           

                                          Could you please check and let me know

                                          • 18. Re: Working with Pdf files
                                            c.pfaffenbichler Level 9

                                            Like I said, PDF Presentation was reintroduced in CS6.

                                            1 person found this helpful
                                            • 19. Re: Working with Pdf files
                                              mangalarajiv Level 1

                                              If Photoshop quits while it's processing mid pages (for example Page number-500 out of 1000); It's hard to start the process from the beginning. If it's possible to start processing from 501 page or if the script completed processing all the pages, except export as pdf, How to do that?

                                               

                                              Could you please help me on this

                                               

                                              thanks,

                                              rajiv,s