Copy link to clipboard
Copied
I have a pdf with more than 400 pages, and want to open them in a single Photoshop file as separate layers. Is there a way to do it? Currently, I only know of the option to open the pdf as separate tabs, which is unwieldy and very taxing on the computer.
Copy link to clipboard
Copied
It is taxing and will take time but can be automated into a single layer document from all the document opened,
/* ==========================================================
// 2017 John J. McAssey (JJMack)
// Stack the open Document Visible layer composite into a new 300 DPI document
// This script is supplied as is. It is provided as freeware.
// 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();
if (documents.length >= 2) {
app.togglePalettes(); // toggle off palettes so Photoshop will not need to update them
stackpages(); // Photosho document can be different when oneo via UI
}
else {
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.resolution = 300;
pdfOpenOpts.suppressWarnings = true;
pdfOpenOpts.usePageNumber = true;
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 };
};
if (documents.length >= 2) { stackpages(); }
else {
app.togglePalettes(); // toggle on palettes
alert("multiple Document are not open in Photoshop");
}
}
///////////////////////////////////////////////////////////////////////////////
// stack pages function //
///////////////////////////////////////////////////////////////////////////////
function stackpages() {
var orig_ruler_units = app.preferences.rulerUnits;
var orig_display_dialogs = app.displayDialogs;
app.preferences.rulerUnits = Units.PIXELS; // set the ruler units to PIXELS
app.displayDialogs = DialogModes.NO; // Set Dialogs off
//newFile = theFile.name.substr(0,theFile.name.indexOf(".pdf")); //New File name
newFile = app.activeDocument.name.substr(0,app.activeDocument.name.lastIndexOf("-")); //New File name
var doc = app.documents.add(app.activeDocument.width.value, app.activeDocument.height.value, app.activeDocument.resolution, newFile, NewDocumentMode.RGB, DocumentFill.TRANSPARENT ); // create a new document
//for (var i=0;i<documents.length-1;i++) { // stack a layer for the open document into the new document page 1 on botton
for (var i=documents.length-2;i>-1;i--) { // stack a layer for the open document into the new document page 1 on top
app.activeDocument = documents; // switch active document
var layerName = app.activeDocument.name; // get document name
app.activeDocument.flatten(); // Flatten remove transparency
var idDplc = charIDToTypeID( "Dplc" );
var desc259 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref24 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref24.putEnumerated( idLyr, idOrdn, idTrgt );
desc259.putReference( idnull, ref24 );
var idT = charIDToTypeID( "T " );
var ref25 = new ActionReference();
var idDcmn = charIDToTypeID( "Dcmn" );
ref25.putName( idDcmn, documents[documents.length-1].name );
desc259.putReference( idT, ref25 );
var idNm = charIDToTypeID( "Nm " );
desc259.putString( idNm, layerName );
var idVrsn = charIDToTypeID( "Vrsn" );
desc259.putInteger( idVrsn, 5 );
executeAction( idDplc, desc259, DialogModes.NO ); // Copy Layer to document
app.activeDocument = documents[documents.length-1]; // switch to newly created document
layerName = layerName.substr(0,layerName.indexOf(".pdf"));
app.activeDocument.activeLayer.name=layerName; // label layer with Document name
}
app.activeDocument = documents[documents.length-1]; // switch to newly created document
var layers = activeDocument.layers; // get layers
activeDocument.activeLayer = layers[layers.length-1] // Target Bottom Layer
activeDocument.activeLayer.remove(); // Remove original layer
while (documents.length>1) { // close all opened document except the newly created document
app.activeDocument = documents[0];
activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
app.togglePalettes(); // toggle user's palettes back on
app.runMenuItem(charIDToTypeID(("FtOn"))); // fit the document to the screen
app.displayDialogs = orig_display_dialogs; // Reset display dialogs
app.preferences.rulerUnits = orig_ruler_units; // reset units to original settings
}
///////////////////////////////////////////////////////////////////////////////
// stack open pages function //
///////////////////////////////////////////////////////////////////////////////
function stackopenpages() {
var orig_ruler_units = app.preferences.rulerUnits;
var orig_display_dialogs = app.displayDialogs;
app.preferences.rulerUnits = Units.PIXELS; // set the ruler units to PIXELS
app.displayDialogs = DialogModes.NO; // Set Dialogs off
//newFile = theFile.name.substr(0,theFile.name.indexOf(".pdf")); //New File name
newFile = app.activeDocument.name.substr(0,app.activeDocument.name.lastIndexOf("-")); //New File name
var doc = app.documents.add(app.activeDocument.width.value, app.activeDocument.height.value, app.activeDocument.resolution, newFile, NewDocumentMode.RGB, DocumentFill.TRANSPARENT ); // create a new document
for (var i=0;i<documents.length-1;i++) { // stack a layer for the open document into the new document page 1 on botton
//for (var i=documents.length-2;i>-1;i--) { // stack a layer for the open document into the new document page 1 on top
app.activeDocument = documents; // switch active document
var layerName = app.activeDocument.name; // get document name
app.activeDocument.flatten(); // Flatten remove transparency
var idDplc = charIDToTypeID( "Dplc" );
var desc259 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref24 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref24.putEnumerated( idLyr, idOrdn, idTrgt );
desc259.putReference( idnull, ref24 );
var idT = charIDToTypeID( "T " );
var ref25 = new ActionReference();
var idDcmn = charIDToTypeID( "Dcmn" );
ref25.putName( idDcmn, documents[documents.length-1].name );
desc259.putReference( idT, ref25 );
var idNm = charIDToTypeID( "Nm " );
desc259.putString( idNm, layerName );
var idVrsn = charIDToTypeID( "Vrsn" );
desc259.putInteger( idVrsn, 5 );
executeAction( idDplc, desc259, DialogModes.NO ); // Copy Layer to document
app.activeDocument = documents[documents.length-1]; // switch to newly created document
layerName = layerName.substr(0,layerName.indexOf(".pdf"));
app.activeDocument.activeLayer.name=layerName; // label layer with Document name
}
app.activeDocument = documents[documents.length-1]; // switch to newly created document
var layers = activeDocument.layers; // get layers
activeDocument.activeLayer = layers[layers.length-1] // Target Bottom Layer
activeDocument.activeLayer.remove(); // Remove original layer
while (documents.length>1) { // close all opened document except the newly created document
app.activeDocument = documents[0];
activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
app.togglePalettes(); // toggle user's palettes back on
app.runMenuItem(charIDToTypeID(("FtOn"))); // fit the document to the screen
app.displayDialogs = orig_display_dialogs; // Reset display dialogs
app.preferences.rulerUnits = orig_ruler_units; // reset units to original settings
}
Copy link to clipboard
Copied
Umm is this code that I put somewhere? I'm not sure what that means.
Thanks.
Copy link to clipboard
Copied
Its a Photoshop Script. Photoshop scripts are normally installed into each Photoshop version presets\scripts\ folder. I install my photoshop scripts into my own Photoshop scripts tree and link all install photoshop versions presets\scripts\ folder to my scripts tree. All installed versions of Photoshop on my machine use the same copy of my scripts. It make installing all my scripts into a new photoshop versions easy. All I have to do is add a single link into its preset\scripts\ folder.
Photoshop scripts can be anywhere on you machine. After the last script name in the scripts list there is a Browse, you can use to navigate to the script you want to use menu File>Scripts>Browse... I have too many scripts using Adobe's menu File>Scripts>Script Name is too slow for Adobe only scroll the list one name at a time. Scrolling should met a scree full scroll. I launch scripts from my scripts tree using Windows file explorer. I drag and drop script onto photoshop for execution. New version of Photoshop need an entry added into PSUserConfig.txt to suppress the warning dialog Adobe added to new Photoshop versions if you use that method.
Copy link to clipboard
Copied
More here on working with various script files found online for Adobe apps:
https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html
Copy link to clipboard
Copied
I would suggest that you use a script such as PDF Processor II where it will open and save separate files one by one, which should be less taxing.
Then use the default File > Scripts > Load files into stack script or a similar script as a second pass to combine the separate files.
Copy link to clipboard
Copied
The script is fantastic, it saved me a whole lot of time thanks
Copy link to clipboard
Copied
Stephen the above script cut out the saving of opened pages into files and need to then use adobe script load files into a stack and then the need to delete all saved pages files. The script just opens the selected PDF document pages in photoshop as page document. Then stacks all the open page documents into a document and then closes the open page documents. It just that it is a script and if the document has many pages it will take a long time for the script to open all the PDF pages. I do not know how to code Action Manager code to find out how many pages the selected PDF document has so I could select all the pages to have Photoshop open the all from the import PDF dialog using Action Manager code. Or Know if that is even. The script opens one page at a time till the pages open fails then the script stack the pages. If the PDF is large it may perform better if you Opens all the PDF Pages using Photoshop UI import PDF or just the pages you want in a stack. Then run my script. If the pages are open when you run my script it will stack the open page document into a document and then close the page documents. No files are created not clean up is needed. My script should perform better the using PDF processor II, Load Files into Stack and deleting the saved Paged files.
Copy link to clipboard
Copied
Hi @JJMack – you are correct:
Using a 21 page test file, your script created the x21 RGB/300ppi files in 13 seconds.
The PDF processor took around 20 seconds. The stacker script then took around 10 seconds.
(This is not including the time to use the GUI in each script to setup both separate batches).
For me, your script stopped without any error/warning – without actually combining the 21 open files. I couldn't use the Adobe stacker script as the files are unsaved (Mac + Ps 22.4.3).
EDIT: If it helps, your script unexpecedly stops after the step where the new document is created to contain all files as layers from the PDF (the new file #22 in my 21 page PDF file).
I was able to run some code from one of my scripts to easily combine the open docs to layers, which took approx. 3 seconds:
// Stack all open docs to the base doc
while (app.documents.length > 1) {
app.activeDocument = documents[1];
app.activeDocument.activeLayer.duplicate(documents[0]);
app.documents[1].close(SaveOptions.DONOTSAVECHANGES);
}
Copy link to clipboard
Copied
I use Windows I try to make my script compatible on both Platform. However, I have not had access to a Mac since mid 1990 where I gave up on Mac OS9. There are some files system difference between platforms one need sto address to have their scripts to work on both platforms. You should consider my script more suitable for PC than Mac. I have reports where my Scripts work on Mac machines and other reports that users have problems on Mac. There seems to be compatibility between version of MacOS. Its hard to create a scripts that will be compatible with all Photoshop versions on Both Platforms and all OS versions. There are compatibility issues between Photoshop versions and all versions of Photoshop have issues. Its a no win situation.
Copy link to clipboard
Copied
I removed your two layer stacking functions and replaced them with my own and everything works as expected now at around 16 seconds (13 seconds for your code as before + 3 seconds for my stacking code).
/* ==========================================================
// 2017 John J. McAssey (JJMack)
// Stack the open Document Visible layer composite into a new 300 DPI document
// This script is supplied as is. It is provided as freeware.
// 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();
if (documents.length >= 2) {
app.togglePalettes(); // toggle off palettes so Photoshop will not need to update them
stackpages(); // Photosho document can be different when oneo via UI
} else {
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.resolution = 300;
pdfOpenOpts.suppressWarnings = true;
pdfOpenOpts.usePageNumber = true;
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
};
};
if (documents.length >= 2) {
//////////////////////////////////////
// Stack all open docs to the base doc
// Stephen Marsh - 2021
while (app.documents.length > 1) {
app.activeDocument = documents[1];
app.activeDocument.activeLayer.duplicate(documents[0]);
app.documents[1].close(SaveOptions.DONOTSAVECHANGES);
}
//////////////////////////////////////
app.togglePalettes(); // toggle user's palettes back on
app.runMenuItem(charIDToTypeID(("FtOn"))); // fit the document to the screen
app.displayDialogs = orig_display_dialogs; // Reset display dialogs
app.preferences.rulerUnits = orig_ruler_units; // reset units to original settings
} else {
app.togglePalettes(); // toggle on palettes
alert("multiple Document are not open in Photoshop");
}
}
Copy link to clipboard
Copied
You need to fix the first "if" statement then it used the function stackpages().... That function also toggled the paletts back on and fitted the document to the display
app.togglePalettes(); // toggle user's palettes back on
app.runMenuItem(charIDToTypeID(("FtOn"))); // fit the document to the screen
Copy link to clipboard
Copied
Ah yes, I was not looking any deeper than the intial problem that I stumbled over! Code updated.
Copy link to clipboard
Copied
You did not fix the first if. I change your stacking code to fix the first if by replacing my stacking function with your code which I added the white paper like my stacking code to remove the transparency and numbered the pages more like my script. The get the Page number from the doc name ins case the user just opened some of the PDF pages before running the script.
/* ==========================================================
// 2017 John J. McAssey (JJMack)
// Stack the open Document Visible layer composite into a new 300 DPI document
// This script is supplied as is. It is provided as freeware.
// 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();
if (documents.length >= 2) {
app.togglePalettes(); // toggle off palettes so Photoshop will not need to update them
stackpages(); // Photosho document can be different when oneo via UI
} else {
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.resolution = 300;
pdfOpenOpts.suppressWarnings = true;
pdfOpenOpts.usePageNumber = true;
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
};
};
if (documents.length >= 2) stackpages();
else {
app.togglePalettes(); // toggle on palettes
alert("multiple Document are not open in Photoshop");
}
}
function stackpages() {
//////////////////////////////////////
// Stack all open docs to the base doc
// Stephen Marsh - 2021
app.activeDocument = documents[0];
var page = app.activeDocument.name.replace(/\.[^\.]+$/, ''); // strip the extension off
app.activeDocument.flatten(); // Flatten remove transparency
app.activeDocument.activeLayer.isBackgroundLayer=0; // Make it a normal Layer
app.activeDocument.activeLayer.name = page
while (app.documents.length > 1) {
app.activeDocument = documents[1];
var page = app.activeDocument.name.replace(/\.[^\.]+$/, ''); // strip the extension off
app.activeDocument.flatten(); // Flatten remove transparency
app.activeDocument.activeLayer.isBackgroundLayer=0; // Make it a normal Layer
app.activeDocument.activeLayer.name = page
app.activeDocument.activeLayer.duplicate(documents[0]);
app.documents[1].close(SaveOptions.DONOTSAVECHANGES);
}
//////////////////////////////////////
app.togglePalettes(); // toggle user's palettes back on
app.runMenuItem(charIDToTypeID(("FtOn"))); // fit the document to the screen
}
Copy link to clipboard
Copied
I just tested in Windows and the original script code has the same problem, failing at the stacking step, so this is not a Mac issue, but perhaps a version issue in later versions.
EDIT: I just tested your new revised code and it now works as expected on both Mac and Win.
Copy link to clipboard
Copied
Perhaps the original script is not downloading correctly or was not uploaded correctly. The script I pasted into the site reply function works fine on my Windows machine. There have been issues with the site code where post with javascript code is not done correctly. Space characters have been dropped or add which effect Photoshop events id. In fact when I download your code there was a syntax error for there was a save added after the # character in #target Photoshop. If you look at your append you will see there is a space there..