I have a javascript to put a dynamic date stamp vertically along the left edge of a PDF. This script works fine for the first page. However, on a multi-page document the date stamp is not rotated 90 degrees, so from the 2nd page on, there are just a few letters horizontally. Here is the script.
var inch = 72;
for (var p = 0; p < this.numPages; p++) {
// create rectangle quads for field
var aRect = this.getPageBox( {nPage: p} );
aRect[0] = 0.0;
aRect[1] = 0.0;
aRect[2] = 26.0;
aRect[3] = 792;
// now construct text field to fill with date information
var f = this.addField("PrintField", "text", p, aRect )
f.delay = true;
f.rotation = 90;
f.textSize = 16;
f.textFont = font.HelvB;
f.textColor = color.black;
f.alignment = "center";
f.readonly = true;
f.print = true;
f.hidden = true;
f.delay = false;
}
// Adds a doc level script to populate text field when doc prints from
// Acrobat or the free Adobe Reader. Note: the '\r' character represents a
// carriage return.
var myWillPrintScript = 'var f = this.getField("PrintField"); \r' + 'f.hidden = false; \r' + 'var d = new Date(); \r' + 'var year = d.getYear()+1900; \r' + 'var mins = d.getMinutes(); \r' + 'if (mins < 10) { \r' + ' mins = "0" + mins}; \r' + 'if (d.getHours() >= 12) { \r' + 'mins = mins + " PM"; \r' + '} else { \r' + 'mins = mins + " AM"; } \r' + 'f.value = "THIS DOCUMENT ONLY EFFECTIVE ON " + (d.getMonth()+1) + "/" + d.getDate() + "/" + year; \r'
var myDidPrintScript = 'var f = this.getField("PrintField"); \r'
+ 'f.value = ""; \r'
+ 'f.hidden = true; \r'
// Now set the scripts to execute on the Will/Did Print events.
this.setAction("WillPrint", myWillPrintScript);
this.setAction("DidPrint", myDidPrintScript);
FYI: This script came from the following other threads and links
http://www.planetpdf.com/developer/article.asp?ContentID=Addingdynamic stampstoPDFswhenprinting
http://forums.adobe.com/thread/286289
http://forums.adobe.com/message/1097488#1097488
Can anyone figure out why this script is not working on all pages?
Thanks
Thank you!
For others to reference, the final script that works is:
// Description: This script adds a text field to the left side of all
// pages. It also sets a doc level JavaScript that executes when the
// document is printed and fills the field with date.
// Add field to each page of form
var inch = 72;
for (var p = 0; p < this.numPages; p++) {
// create rectangle quads for field
var aRect = this.getPageBox( {nPage: p} );
aRect[0] = 0.0;
aRect[1] = 0.0;
aRect[2] = 26.0;
aRect[3] = 792;
// now construct text field to fill with date information
var f = this.addField("PrintField", "text", p, aRect )
f.delay = true;
f.textSize = 16;
f.textFont = font.HelvB;
f.textColor = color.black;
f.alignment = "center";
f.readonly = true;
f.print = true;
f.hidden = true;
f.delay = false;
}
for (var p = 0; p < this.numPages; p++) {
// rotate the text
var f = this.getField("PrintField." + p)
f.rotation = 90;
}
// Adds a doc level script to populate text field when doc prints from
// Acrobat or the free Adobe Reader. Note: the '\r' character represents a
// carriage return.
var myWillPrintScript = 'var f = this.getField("PrintField"); \r' + 'f.hidden = false; \r' + 'var d = new Date(); \r' + 'var year = d.getYear()+1900; \r' + 'f.value = "THIS DOCUMENT ONLY EFFECTIVE ON " + (d.getMonth()+1) + "/" + d.getDate() + "/" + year; \r'
var myDidPrintScript = 'var f = this.getField("PrintField"); \r'
+ 'f.value = ""; \r'
+ 'f.hidden = true; \r'
// Now set the scripts to execute on the Will/Did Print events.
this.setAction("WillPrint", myWillPrintScript);
this.setAction("DidPrint", myDidPrintScript);
Does anybody know why the text rotation is applied on a separate pass, rather than when the field is established intitally? I've been testing 'rotation' and it appears to rotate the text in a field on only the first page to which it is applied. The remaining pages all have horizontal text (0 degree rotation) in a vertical text box, which is not what is wanted.
-- Roy Zider
I didn't either. But in a similar piece of my test code, the .rotation property (or method?) did not work after the first page. I had to name each text box on each page with a unique name, and then it worked within a single loop.
If you note, the text field 'PrintField' is not renamed in the first loop. In the second, it is 'PrintField.'+p with getfield. so maybe the first loop names the text field on each page uniquely automatically, not explicitly.
Wasn't able to find anything about this anomaly anywhere.
-- Roy Zider
The rotation property is unique for each widget, that's why it requires a loop that accesses each widget individually (using the period and number after the field's name). I would suggest changing the first loop so that each field has a unique name, something like "PrintField_"+p .
Otherwise, just leave it the way that it is since it seems to be working.
Edit: it shouldn't actually work that way, but as was mentioned above, it seems to be a bug.
North America
Europe, Middle East and Africa
Asia Pacific