Discussion:
How do I export to single page PDFs with 3 digit page number?
(too old to reply)
G***@adobeforums.com
2006-11-21 16:22:05 UTC
Permalink
Hi - I hope somebody could help me out with this little problem: I'm using JavaScript successfully to export single page PDFs from multi-page InDesign files but would like a further tweak so that the page number (based on the actual page number as defined by Numbering and Section Options) in resulting PDF filenames is expressed as three digits.

Many thanks in advance for any suggestions

Gabriel
M***@adobeforums.com
2006-11-24 09:50:37 UTC
Permalink
you can get the page number by using the name-property of the page-object.

for example:

var pagenumber = app.activeDocument.pages[number].name;

where "number" is the number of the page you use for the pageRange property of you PDFExportPreference.

If your document start with pagenumber 1 then pagenumber=number, but if you specified a different pagenumber to start with, then these variables will hab different values.

Then you can build a string by simply checking the value of pagenumber. Something like that (not tested):

var filename = 'mypdf_';
if (pagenumber < 10) { filename = filename + '0';} if (pagenumber < 100) { filename = filename + '0';} filename = filename + pagenumber + '.pdf';

after all use 'filname' as name for your export file.
D***@adobeforums.com
2006-11-24 14:11:35 UTC
Permalink
Here's the function I use to export a PDF



function exportPDF(pageName, preset, myFolder) {
var altName = "000".slice(0,3-pageName.length) + pageName;
var myName = ( "/" + altName + myDoc.name).split(".indd").join(".pdf");
var myDestFile = File(myFolder.fullName + myName);
app.pdfExportPreferences.pageRange = pageName;
myDoc.exportFile("Adobe PDF", myDestFile, false, preset);
} // end exportPDF function




Where pageName is the name of the page being exported, preset is a reference to the preset to be used and myFolder is the destination folder for the PDFs.

Dave
D***@adobeforums.com
2006-11-25 00:07:25 UTC
Permalink
alert(("12345").slice(-3));
=> 345

Therefor:
var altName = ("00"+pageName).slice(-3);
D***@adobeforums.com
2006-11-25 01:20:36 UTC
Permalink
Oh yes. That's much simpler. Thanks.

Dave
D***@adobeforums.com
2006-11-25 13:54:11 UTC
Permalink
On the other hand, your version will truncate a four-digit page number to three digits while mine will leave all four digits there. I'm not saying this is good or bad, but it is a difference worth noting.

Dave
G***@adobeforums.com
2006-11-27 10:39:55 UTC
Permalink
Thank you guys so much - but I'm not having any luck transplanting this code into the one I'm using. The closest I've come is the first page with "ned" as the page name! I know it needs more than cut-n-paste to work but so far this morning none of my mods are doing it. Where should the variable be defined and how should it then be called into the name? Mmmm. My «BASIC» understanding just doesn't cut it! Any further help greatly appreciated.

The code I use was lifted from p139, "Setting PDF export options and exporting pages separately", of the InDesign_Scripting_Guide.pdf. Only mods are:

var myPDFExportPreset

added to line 20 (I don't actually use "Press Quality" but have changed it here for simplicity)
and the

myPDFExportPreset

parameter added to

myDocument.exportFile

on line 43.








//ExportEachPageAsPDF.jsx
//An InDesign CS2 JavaScript
//Exports each page of an InDesign CS document as a separate PDF to
//a selected folder using specific PDF export settings.
//Display a "choose folder" dialog box.
if(app.documents.length != 0){
var myFolder = Folder.selectDialog ("Choose a Folder");
if(myFolder != null){
myExportPages(myFolder);
}
}
else{
alert("Please open a document and try again.");
}
function myExportPages(myFolder){
var myPageName, myFilePath, myFile;
var myDocument = app.activeDocument;
var myDocumentName = myDocument.name;
//This next line sets the PDF export Setting
var myPDFExportPreset = app.pdfExportPresets.item("Press Quality")
var myDialog = app.dialogs.add();
with(myDialog.dialogColumns.add().dialogRows.add()){
staticTexts.add({staticLabel:"Base name:"});
var myBaseNameField = textEditboxes.add({editContents:myDocumentName, minWidth:160});
}
var myResult = myDialog.show({name:"ExportPages"});
if(myResult == true){
var myBaseName = myBaseNameField.editContents;
//Remove the dialog box from memory.
myDialog.destroy();
for(var myCounter = 0; myCounter < myDocument.pages.length; myCounter++){

myPageName = myDocument.pages.item(myCounter).name;

app.pdfExportPreferences.pageRange = myPageName;

//The name of the exported files will be the base name + the

//page name + ".pdf".

//If the page name contains a colon (as it will if the document

//contains sections),

//then remove the colon.

var myRegExp = new RegExp(":","gi");

myPageName = myPageName.replace(myRegExp, "_");

myFilePath = myFolder + "/" + myBaseName + " " + myPageName + ".pdf";

myFile = new File(myFilePath);

myDocument.exportFile(ExportFormat.pdfType, myFile, false, myPDFExportPreset);

}
}
else{
myDialog.destroy();
}
}
D***@adobeforums.com
2006-11-27 11:34:25 UTC
Permalink
If you want to retain the section prefixes in your page names, then you need to separate the two parts of the name into prefix and name, apply the three-digit stuff to only the name and then put the two back together:

myParts = myPageName.split(":);
myParts[1] = ("00" + myParts[1]).slice(-3);
myPageName = myParts.join("_");

But this will only work for page names that do have a section prefix. You might need to treat the two cases separately:

myParts = myPageName.split(":);
try {
myParts[1] = ("00" + myParts[1]).slice(-3);
myPageName = myParts.join("_");
} catch(e) {
myPageName = ("00" + myPageName).slice(-3);
}

is one way to do it.

As for posting here, change all < characters to &lt; or use Peter's script found here: Peter Kahrel, "JS: script poster" #5, 24 Oct 2006 8:54 am </cgi-bin/webx?14@@.3bc170eb/4>

Dave
G***@adobeforums.com
2006-11-27 13:37:32 UTC
Permalink
Hi Dave - thank you so much for your help - but I'm still no closer.

(Funnily enough sections do not output properly with this script - when a named section is reached PDFs are created for every page of the section - each containing all the pages within. That's not a propblem I have to worry about too much - I don't need sections - and is my best and only solution to the problem of exporting individual pages to properly numbered pages.)

Back to the script - I'm lost as to where to insert it [I've even tried it there]. Is this a var that needs to be defined and called at some point? If so where? Then there's an issue with the (".); bit that wasn't popular - even with a closing quote mark.

Questions questions!

Thanks again for any further light you may shed on this, oh hallowed master (and thanks to the forum gods for sorting out my unescaped code).
D***@adobeforums.com
2006-11-27 14:17:19 UTC
Permalink
Sorry about the missed close quote.

You need to provide much more specific information if anyone here is to help you.

Where did you insert the code (it should have been in place of the Regex stuff you had). What happens when you try it.

Dave
G***@adobeforums.com
2006-11-27 15:37:52 UTC
Permalink
Thanks for that Dave.

With the two Regex lines commented out and your suggestion in place I get single page PDFs named like this: myBaseName + "-" + "myPageName" + "_ned" + ".pdf", ie "BaseNameOfMagazine-26_ned.pdf".

No idea where "_ned" came from.

Sorry for not being specific enough with my first post - I'd not realised the complexity of the script - particularly after easily modding it to specify the joboptions with a bit of Google, the InDesign Scripting Guide and InDesign Scripting Reference.

The script currently is as follows. (Commented modifications to otherwise working script are between lines 39-50.) Please let me know if there's any other information I should be supplying.

Thanks again for your help thus far




//ExportEachPageAsPDF.jsx
//An InDesign CS2 JavaScript
//Exports each page of an InDesign CS document as a separate PDF to
//a selected folder using specific PDF export settings.
//Display a "choose folder" dialog box.
if(app.documents.length != 0){
var myFolder = Folder.selectDialog ("Choose a Folder");
if(myFolder != null){
myExportPages(myFolder);
}
}
else{
alert("Please open a document and try again.");
}
function myExportPages(myFolder){
var myPageName, myFilePath, myFile;
var myDocument = app.activeDocument;
var myDocumentName = myDocument.name;
//This next line sets the PDF export Setting
var myPDFExportPreset = app.pdfExportPresets.item("Press Quality")
var myDialog = app.dialogs.add();
with(myDialog.dialogColumns.add().dialogRows.add()){
staticTexts.add({staticLabel:"Base name:"});
var myBaseNameField = textEditboxes.add({editContents:myDocumentName, minWidth:160});
}
var myResult = myDialog.show({name:"ExportPages"});
if(myResult == true){
var myBaseName = myBaseNameField.editContents;
//Remove the dialog box from memory.
myDialog.destroy();
for(var myCounter = 0; myCounter < myDocument.pages.length; myCounter++){
myPageName = myDocument.pages.item(myCounter).name;
app.pdfExportPreferences.pageRange = myPageName;
//The name of the exported files will be the base name + the
//page name + ".pdf".
//If the page name contains a colon (as it will if the document
//contains sections),
//then remove the colon.
//[g] next two line commented out, trying Dave Saunders' suggestion
//var myRegExp = new RegExp(":","gi");
//myPageName = myPageName.replace(myRegExp, "_");
//[g] insert DS' suggestion:
myParts = myPageName.split(":");
try {
myParts[1] = ("00" + myParts[1]).slice(-3);
myPageName = myParts.join("_");
} catch(e) {
myPageName = ("00" + myPageName).slice(-3);
}
//[g] end mod
myFilePath = myFolder + "/" + myBaseName + "-" + myPageName + ".pdf";
myFile = new File(myFilePath);
myDocument.exportFile(ExportFormat.pdfType, myFile, false, myPDFExportPreset);
}
}
else{
myDialog.destroy();
}
}
D***@adobeforums.com
2006-11-27 15:50:31 UTC
Permalink
What does myPageName look like in the instance you quoted before I started pulling it apart and putting it back together?

Insert:

alert(myPageName);

immediately before the myParts = ... line

Dave
D***@adobeforums.com
2006-11-27 16:08:30 UTC
Permalink
Ah, thanks Peter. I keep expecting errors when they don't happen!

Dave
G***@adobeforums.com
2006-11-27 16:19:47 UTC
Permalink
Peter and Dave - you are the men!
Code way beyond my meagre understanding - but a perfect solution.
I'm almost as excited as my baby son with a new toy: without the wetness that is.

Thank you so much.
P***@adobeforums.com
2006-11-27 15:57:21 UTC
Permalink
The problem is that with page numbers without any colons, split(':') returns a one-element array, so myParts[1] is undefined. Take its last three characters and you get your 'ned'. The try/catch doesn't help you here. Instead you need something like this:



myParts = myPageName.split(":");
if( myParts.length > 1 )
{
myParts[1] = ("00" + myParts[1]).slice(-3);
myPageName = myParts.join("_");
}
else
{
myPageName = ("00" + myPageName).slice(-3);
}
//[g] end mod



Peter

Loading...