These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Numbering PDF Files with zero leading numbers needed

Gebhard
Registered: Jul 18 2011
Posts: 25
Answered

Hello Forum,
 
I am looking here in the forum to get the syntax for zero leading numbers, but of the topics are in Formulars. I need the following:
 
A part of my script is this:
//Save the current Document in the same Path with suffix 001,002//
for ( var i = 0; i < this.numPages; i++ )
this.extractPages ({
nStart: i,
cPath: ""+this.documentFileName+"_" +(i+1)+".pdf"
 
Acrobat output with this Folderscript is:
e.g. Page_1.pdf, Page_2.pdf...Page_99.pdf
 
I need this numbering:
e.g. Page_001.pdf, Page_002.pdf...Page_099.pdf
Why? Because sorting by Numbers with e.g.Page_1.pdf is not good when if have more than 10 Pages to save.
I tried all for some hours with e.g. "_00" or i+1 andso on.
Please help me
kind regards
Gebhard

My Product Information:
Acrobat Pro 8.2.6, Windows
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
Use a text field and to concatenate the number with leading zeros, use the 'util.printf()' method. To keep leading zeros you may also need to use the 'valueAsString' property.




George Kaiser

Gebhard
Registered: Jul 18 2011
Posts: 25
Dear Mr. Kaiser,
thank you for responce, but I´m a newbie in Javascript and Javascript for Acrobat.
After 6 hours now if give it up.It´s not possible for me to do what I want.
I tried all with textfield and so on. In the Javascript for Acrobat theres no good information about valueAsString, but i understand the difference between value on Page 448.(20 and 020)
Please can you help me with the code or better give it to the forum?

Thank you very much
kind regards
Gebhard
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
See valueAsString. Of course this assumes one entered '020' into the text field and not "20'.
.
.
The printf.
.
.
// open the JS console;
console.show();
console.clear();
// test value;
var nTest = 20;
// without leading zeros
console.println("no leading zeros: "+ nTest);
// format with leading zeros;
var cTest = util.printf("%,203.0f", nTest);
console.println("with leading zeros: " + cTest);


George Kaiser

Gebhard
Registered: Jul 18 2011
Posts: 25
Hello Mr.Kaiser,
thank you for your response.
But how to use it?
I get this output from Console:
no leading zeros: 20
with leading zeros: 020
I insert this(your) code in my folder script below:
/*Extract Pages to Folder*/
// regular expression acquire the base name of file
ProcessDocument4 = app.trustedFunction(function()
{
for ( var i = 0; i < this.numPages; i++ )
this.extractPages ({
nStart: i,
cPath: ""+this.documentFileName+"_" + (i +1)+".pdf"
});
// open the JS console;
console.show();
console.clear();
// test value;
var nTest = 20;
// without leading zeros
console.println("no leading zeros: "+ nTest);
// format with leading zeros;
var cTest = util.printf("%,203.0f", nTest);
console.println("with leading zeros: " + cTest);
});
// add the menu item
app.addMenuItem({
cName: "splitpages1JS", // this is the internal name used for this menu item
cUser: "MehrseitigesPDF in Einzelseiten AufTrennen 001.pdf,002.pdf usw", // this is the label that is used to display the menu item
cParent: "EinzelSeitenTools",// this is the parent menu. The file menu would use "File"
cExec: "ProcessDocument4()",// this is the JavaScript code to execute when this menu item is selected
cEnable: "event.rc = (event.target != null);", // when should this menu item be active?
nPos: 3
});

But the files don´t have the suffix *001.pdf, *002.pdf...
Is there some code missing?
Your Script should rename the Pdfs
Thanks and waiting for you next answer
kind Regards
Gebhard
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
I provided an example to show how to convert a number to fixed length character string with leading zeros.

The code that converts the number, nTest, to the character string, cTest, is:

var cTest = util.printf("%,203.0f", nTest);


George Kaiser

Gebhard
Registered: Jul 18 2011
Posts: 25
Hello Mr. Kaiser,
I see that your var cTest = util.printf("%,203.0f", nTest); is for leading zeros.
But after 8 hours now i give it up, because I tried verything with trusted functions, app.beginPriv ...
I´m not able to get it run. Heres my code:
/*Extract Pages to Folder*/
// regular expression acquire the base name of file
var ProcessDocument4 = app.trustedFunction(function(cPath)
{
for ( var i = 0; i < this.numPages; i++ )
this.extractPages ({
nStart: i,
cPath: ""+this.documentFileName+("_")+(i +1)+".PDF" + util.printf("%,203.0f", i),
}
)
});
// add the menu item
app.addMenuItem({
cName: "splitpages1JS", // this is the internal name used for this menu item
cUser: "MehrseitigesPDF in Einzelseiten AufTrennen 001.pdf,002.pdf usw", // this is the label that is used to display the menu item
cParent: "EinzelSeitenTools", // this is the parent menu. The file menu would use "File"
cExec: "ProcessDocument4()",// this is the JavaScript code to execute when this menu item is selected
cEnable: "event.rc = (event.target != null);", // when should this menu item be active?
nPos: 1
});
I always get the following errorcode:
NotAllowedError: Security settings prevent access to this property or method.
Doc.extractPages:8:Menu splitpages1JS:Exec

Can you please help me the erroe always occured with ("%,203.0f", i), I know there must be a Problem with trustedFunction, but I don´t know how to configure my script
Thank you very much
kind regards
Gebhard
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
Your error is due to writing to the root directory and this location is not considered as a safe path since the root directory on certain OSs has a number of special system sensitive files.

From the Acrobat JS API Reference:

"Safe path

"Acrobat 6.0 introduced the concept of a safe path for JavaScript methods that write data to the local hard drive based on a path passed to it by one of its parameters.

" path cannot point to a system critical folder, for example a root, windows or system directory. A path is also subject to other unspecified tests.

"or many methods, the file name must have an extension appropriate to the type of data that is to be saved. Some methods may have a no-overwrite restriction. These additional restrictions are noted in the documentation.
Generally, when a path is judged to be not safe, a NotAllowedError exception is thrown (see Error object) and the method fails."

George Kaiser

Gebhard
Registered: Jul 18 2011
Posts: 25
Dear Mr Kaiser,
what I want again:
This script should be for my lazy customers who don´t want to invest in the professional tools like Pitstop, Callas Quite and so on. I use this Softwarepackages in my professional environment every day and bought them.
By the time I made as a bloody new user in Javascript many scripts that are running perfectly.
Thom Parker gave me a part of my problem script, so I want any user can open a pdf file from every location of his harddrive saving the result into the same directory.
e.g. in this script i open a pdf from a location on my harddrive, than its duplicated by Thom Parkers values: for ( var i = 0; i < this.numPages; i++ )....
"Safe path:
So its a saveas path and not a save path (I think so, because save directly is not allowed)
"Acrobat 6.0 introduced .... I think here are my parameters from Thom Parkers script also ok, because I get a ok result
"or many methods, the file name must have an extension....
I tried to give another extension e.g. *.tif this won´t run
In my script I have *.pdf and this runs ok

So whats the Problem?
I think a company like Adobe should be able to set the value *.001.pdf, *.002.pdf as a registry value or default, because numbering 1.pdf,2.pdf...10.pdf is stupid
I tried a batchscript and there its possible to set this values.(Suffix and/orPrefix
We are using a very expensive Agfa PDF Workflow based on Adobe APPE2.5 (130.000,00Euro) for running our Webpresses. But the customers all whant utilities with no cost delivering their files to our Portal in the way I want to have them: e.g Page_German001.pdf, PageEnglish002.pdf and so on
By searching on google i see that many other users are also having this problem.
Don´t be angry Mr Kaiser, but this is a big problem for me
Is there any solution to fix my problem?
Thank you very much
kind regards
Gebhard
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Accepted Answer
Looks like you started off simple and got a bit to complex ;^)

Part of the trick this kind of script is to build the path in two steps.
1. Before the loop, build the root
2. Inside the loop, add the specific postfix

Like this.
var ProcessDocument4 = app.trustedFunction(function(cPath)
{
   app.beginPriv();
   // Build path root from current document.
   // Split off the ".pdf" at the end (this is just one way it can be done)
   var aPth = this.path.split(".");
   aPth.pop();
   var cPathRoot = aPth.join(".");

   // Now run the extraction loop
   for ( var i = 0; i < this.numPages; i++ )
      this.extractPages({nStart: i, cPath: util.printf("%s_%03d.pdf",cPathRoot,i)}) ;

   app.endPriv();
});
Notice the begin and end Priv functions. These mark the privileged area and are the reason for your security error.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

Gebhard
Registered: Jul 18 2011
Posts: 25
Hello Thom,
thank you very, very much.
I combined this syntax in all my scripts i have made and it runs perfectly:-)
I made a little modification to your syntax because the numbering begins at *.000.pdf
cPath: util.printf("%s_%03d.pdf",cPathRoot,i+1)}) ;//+1 numbers at the beginning now with *.001.pdf

Have a nice day until my next question(s) ;-)
kind regards
Gebhard