9 Replies Latest reply: Apr 19, 2012 12:41 PM by FUBARinSFO RSS

    Dynamic date stamp along edge of page

    HardDriver2

      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=AddingdynamicstampstoPDFswhenprin ting

      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

       

       

        • 1. Re: Dynamic date stamp along edge of page
          try67 CommunityMVP

          It sounds like the pages are rotated.

          There's an example in the reference files how to rotate fields so that they

          match the page rotation. I think it's one of the addLink examples.

          • 2. Re: Dynamic date stamp along edge of page
            HardDriver2 Community Member

            The pages are not rotated.  This is happening on every document and some are Word documents that were converted to PDF and have no landscape or rotation.  Which reference files are you referring to?

             

            Thanks

            • 3. Re: Dynamic date stamp along edge of page
              Bernd Alheit CommunityMVP

              This is a bug of Adobe Acrobat. Add the following code after your for loop:

              for (var p = 0; p < this.numPages; p++) {

                  var f = this.getField("PrintField." + p);

                  f.rotation = 90;
              }
              • 4. Re: Dynamic date stamp along edge of page
                HardDriver2 Community Member

                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);

                 

                • 5. Re: Dynamic date stamp along edge of page
                  FUBARinSFO

                  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

                  • 6. Re: Dynamic date stamp along edge of page
                    try67 CommunityMVP

                    I don't see a reason why not to include it in the first loop.

                    • 7. Re: Dynamic date stamp along edge of page
                      FUBARinSFO Community Member

                      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

                      • 8. Re: Dynamic date stamp along edge of page
                        try67 CommunityMVP

                        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.

                        • 9. Re: Dynamic date stamp along edge of page
                          FUBARinSFO Community Member

                          I checked the API reference, and the code snippet does indeed show the unique field naming for each page.  But no mention anywhere of why this should be so. 

                           

                          Thanks for the confirmation. 

                           

                          -- Roy Zider