Skip navigation
mangalarajiv
Currently Being Moderated

Working with Pdf files

Jun 21, 2012 5:13 AM

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

 
Replies
  • Currently Being Moderated
    Jun 21, 2012 12:21 PM   in reply to mangalarajiv
     
    |
    Mark as:
  • Currently Being Moderated
    Jun 21, 2012 11:48 PM   in reply to mangalarajiv

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 22, 2012 5:46 AM   in reply to mangalarajiv

    Do you have Photoshop CS6?

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

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 22, 2012 6:52 AM   in reply to mangalarajiv

    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 );

    };

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 23, 2012 3:11 AM   in reply to mangalarajiv

    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);

    };

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 23, 2012 4:06 AM   in reply to mangalarajiv

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

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 23, 2012 4:10 AM   in reply to mangalarajiv

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 23, 2012 4:18 AM   in reply to mangalarajiv

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

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 23, 2012 4:48 AM   in reply to mangalarajiv

    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 );

    };

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 23, 2012 4:54 AM   in reply to mangalarajiv

    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) {

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 18, 2012 12:51 AM   in reply to mangalarajiv

    Like I said, PDF Presentation was reintroduced in CS6.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points