How open pdf in photoshop with option 2.

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

Raziel
Posts: 2
Joined: Tue Jun 25, 2024 6:09 am

How open pdf in photoshop with option 2.

Post by Raziel »

Hi,
I use a jxs script than can import all pages from one pdf, into one photoshop document. But I can't find how to import all pictures without text (Work with option 2 if I do it manually). It's possible ? How ?

Thank for your help.


Image

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

User avatar
Stephen_A_Marsh
Posts: 37
Joined: Sun Aug 04, 2019 12:37 pm

Re: How open pdf in photoshop with option 2.

Post by Stephen_A_Marsh »

The ScriptingListener plug-in records basic info:

Code: Select all

var idopen = stringIDToTypeID( "open" );
    var desc227 = new ActionDescriptor();
    var idas = stringIDToTypeID( "as" );
        var desc228 = new ActionDescriptor();
        var idsuppressWarnings = stringIDToTypeID( "suppressWarnings" );
        desc228.putBoolean( idsuppressWarnings, true );
        var idreverse = stringIDToTypeID( "reverse" );
        desc228.putBoolean( idreverse, true );
        var idselection = stringIDToTypeID( "selection" );
        var idpdfSelection = stringIDToTypeID( "pdfSelection" );
        var idimage = stringIDToTypeID( "image" );
        desc228.putEnumerated( idselection, idpdfSelection, idimage );
        var idpageNumber = stringIDToTypeID( "pageNumber" );
        desc228.putInteger( idpageNumber, 1 ); // The image to extract
    var idPDFGenericFormat = stringIDToTypeID( "PDFGenericFormat" );
    desc227.putObject( idas, idPDFGenericFormat, desc228 );
    var idnull = stringIDToTypeID( "null" );
    desc227.putPath( idnull, new File( "~/Desktop/my-pdf-file.pdf" ) ); // The path to the file
executeAction( idopen, desc227, DialogModes.NO );
However, it isn't clear to me how to select multiple images in one go, apart from incrementing the (idpageNumber). It also isn't clear to me how to check how many images one would need to increment over to extract ALL the images.
Raziel
Posts: 2
Joined: Tue Jun 25, 2024 6:09 am

Re: How open pdf in photoshop with option 2.

Post by Raziel »

Thank. It's perfect.

I modify this to open and to save every images as tiff. And a control before open every images (some "pages" aren't images and code stop without this.) The code could perhaps be done better. But it's good enough for me. I'll share it if anyone ever needs an approach like this. I couldn't get the number of images via a formula.

P.s. I don't speak English very well. Sorry for my mistakes.

Code: Select all

#target photoshop

// Function to open a page from PDF
function openPDFPage(pdfFilePath, pageNumber) {
    var idopen = stringIDToTypeID("open");
    var desc227 = new ActionDescriptor();
    var idas = stringIDToTypeID("as");
    var desc228 = new ActionDescriptor();
    var idsuppressWarnings = stringIDToTypeID("suppressWarnings");
    desc228.putBoolean(idsuppressWarnings, true);
    var idreverse = stringIDToTypeID("reverse");
    desc228.putBoolean(idreverse, true);
    var idselection = stringIDToTypeID("selection");
    var idpdfSelection = stringIDToTypeID("pdfSelection");
    var idimage = stringIDToTypeID("image");
    desc228.putEnumerated(idselection, idpdfSelection, idimage);
    var idpageNumber = stringIDToTypeID("pageNumber");
    desc228.putInteger(idpageNumber, pageNumber);
    var idPDFGenericFormat = stringIDToTypeID("PDFGenericFormat");
    desc227.putObject(idas, idPDFGenericFormat, desc228);

    desc227.putPath(stringIDToTypeID("null"), new File(pdfFilePath));

    try {
        executeAction(idopen, desc227, DialogModes.NO);
    } catch (e) {
        //Do nothing if there is an error when opening the page
        // You can add a log message here if necessary
        return false; // show that the opening failed
    }
    return true; // show that the opening success
}

//Function to save image as TIFF
function saveAsTIFF(saveFolderPath, pageNumber) {
    var tiffFileName = "page-" + pageNumber + ".tif";
    var saveFolder = new Folder(saveFolderPath);
    if (!saveFolder.exists) {
        saveFolder.create();
    }
    var tiffFile = new File(saveFolderPath + "/" + tiffFileName);

    var tiffSaveOptions = new TiffSaveOptions();
    tiffSaveOptions.imageCompression = TIFFEncoding.NONE; // chose the compressor type

    try {
        activeDocument.saveAs(tiffFile, tiffSaveOptions, true, Extension.LOWERCASE);
    } catch (e) {
        // Do nothing if there is an error when save the page
        // You can add a log message here if necessary
        return false; // show that the save failed
    }

    // Close the document without saving changes
    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
    return true; // show that the save success
}

// Specify the path of the PDF file
var pdfFilePath = "YourFile.pdf";

// Specify the backup folder
var saveFolderPath = "YourFolder/";

// Number of pages in PDF (you should adjust this based on the number of pages(images option 2) in your PDF)
var totalPages = 230;

// Loop to open and save each page
for (var page = 1; page <= totalPages; page++) {
    if (openPDFPage(pdfFilePath, page)) {
        saveAsTIFF(saveFolderPath, page);
    }
}
User avatar
Stephen_A_Marsh
Posts: 37
Joined: Sun Aug 04, 2019 12:37 pm

Re: How open pdf in photoshop with option 2.

Post by Stephen_A_Marsh »

You're welcome, I extended your code to suit my work.

Code: Select all

/*
Save raster images from the selected PDF as TIFF files.jsx
Based on:
https://www.ps-scripts.com/viewtopic.php?p=172190#p172190
*/

#target photoshop

    (function () {

        // Specify the path of the PDF file
        var pdfFilePath = File.openDialog("Select the PDF file:");
        if (pdfFilePath === null) {
            //alert('Script cancelled!');
            return;
        }

        // Specify the save folder
        var saveFolderPath = Folder.selectDialog("Select the save folder:");
        if (saveFolderPath === null) {
            //alert('Script cancelled!');
            return;
        }

        // Loop the input prompt until a number is entered
        var theInput;
        while (isNaN(theInput = prompt("Enter the number of images to extract:", "3")));
        if (theInput === null) {
            //alert('Script cancelled!');
            return;
        }
        var totalPages = parseInt(theInput);

        // Set the file saving counter
        var counter = 0;

        // Loop to open and save each page
        for (var page = 1; page <= totalPages; page++) {
            if (openPDFPage(pdfFilePath, page)) {
                saveAsTIFF(saveFolderPath, page);
            }
            // Increment the counter
            counter++;
        }

        alert('Script completed!' + '\r' + counter + ' TIFF files saved to:' + '\r' + saveFolderPath.fsName);


        // Functions

        function openPDFPage(pdfFilePath, pageNumber) {
            var idopen = stringIDToTypeID("open");
            var desc227 = new ActionDescriptor();
            var idas = stringIDToTypeID("as");
            var desc228 = new ActionDescriptor();
            var idsuppressWarnings = stringIDToTypeID("suppressWarnings");
            desc228.putBoolean(idsuppressWarnings, true);
            var idreverse = stringIDToTypeID("reverse");
            desc228.putBoolean(idreverse, true);
            var idselection = stringIDToTypeID("selection");
            var idpdfSelection = stringIDToTypeID("pdfSelection");
            var idimage = stringIDToTypeID("image");
            desc228.putEnumerated(idselection, idpdfSelection, idimage);
            var idpageNumber = stringIDToTypeID("pageNumber");
            desc228.putInteger(idpageNumber, pageNumber);
            var idPDFGenericFormat = stringIDToTypeID("PDFGenericFormat");
            desc227.putObject(idas, idPDFGenericFormat, desc228);

            desc227.putPath(stringIDToTypeID("null"), new File(pdfFilePath));

            try {
                executeAction(idopen, desc227, DialogModes.NO);
            } catch (e) {
                //Do nothing if there is an error when opening the page
                // You can add a log message here if necessary
                return false; // show that the opening failed
            }
            return true; // show that the opening success
        }

        function saveAsTIFF(saveFolderPath, pageNumber) {
            var tiffFileName = pdfFilePath.name.replace(/\.[^\.]+$/, '') + "_image-" + pageNumber + ".tif";
            var saveFolder = new Folder(saveFolderPath);
            if (!saveFolder.exists) {
                saveFolder.create();
            }
            var tiffFile = new File(saveFolderPath + "/" + tiffFileName);

            var tiffSaveOptions = new TiffSaveOptions();
            tiffSaveOptions.imageCompression = TIFFEncoding.NONE; // TIFFLZW | TIFFZIP | JPEG
            /*
            tiffSaveOptions.embedColorProfile = true;
            tiffSaveOptions.byteOrder = ByteOrder.IBM;
            tiffSaveOptions.transparency = true;
            tiffSaveOptions.layers = true;
            tiffSaveOptions.layerCompression = LayerCompression.ZIP;
            tiffSaveOptions.interleaveChannels = true;
            tiffSaveOptions.alphaChannels = true;
            tiffSaveOptions.annotations = true;
            tiffSaveOptions.spotColors = true;
            tiffSaveOptions.saveImagePyramid = false;
            */

            try {
                activeDocument.saveAs(tiffFile, tiffSaveOptions, true, Extension.LOWERCASE);
            } catch (e) {
                // Do nothing if there is an error when save the page
                // You can add a log message here if necessary
                return false; // show that the save failed
            }

            // Close the document without saving changes
            app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
            return true; // show that the save success
        }


    }());