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
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
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 );
};
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 );
};
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
North America
Europe, Middle East and Africa
Asia Pacific