11 Replies Latest reply: Sep 4, 2013 4:50 AM by D@veB RSS

    Display Filename in PDF File

    mjdowers Community Member

      Hi there, hoping for some help on what I think is quite a simple issue.

       

      I want to create a blank single page PDF file which has a text field automatically and dynamically completed based on the file name of the pdf document. So for example if the file is named "My Sample File.pdf", the pdf file will display "My Sample File.pdf" and that is all. If I then rename the pdf file in Windows Explorer to "My Renamed File.pdf", when I next open the file it will display "My Renamed File.pdf".

       

      I would then insert this one page pdf into an array of existing pdf files as their new page no1 to effectively be a title page for each created automatically based on their current filenames.

       

      My research suggests this is possible using Javascript in a text field, or a form field (although I would prefer text field), however, as I have no experience of Javascript or its implementation within Acrobat, is proving impossible for me to do. The nearest I have got is a script which puts the filename as a watermark; unfortunately that script keeps overlaying the new filename over the old filename watermark, rather than updating it. For information that script was:-

       

      this.addWatermarkFromText({

      cText: this.documentFileName,

      nTextAlign: app.constants.align.center,

      nHorizAlign: app.constants.align.center,

      nVertAlign: app.constants.align.bottom,

      nFontSize: 12,

      nVertValue: 12});

       

      Ideally I would like the script to strip the file extension and if possible split the output onto two lines based on the filename portion which preceeds/follows a comma in the filename (which it would also strip). For example, if a had a file which was named "My Sample File, created on Monday.pdf", it would display as:-

       

      My Sample File

      created on Monday

       

      If anyone has any ideas on this, it would be greatly appreciated.

       

      Regards and thanks in advance,

      Malcolm Dowers

        • 1. Re: Display Filename in PDF File
          George_Johnson CommunityMVP

          Add the following to a document-level JavaScript:

           

           

          // Document-level JavaScript code
          (function () {
          
              // Get the file name of this document
              fn = documentFileName;
          
              // Get rid of the PDF extension
              fn = fn.replace(/\.pdf$/i, "");
          
              // Replace any comma/space combinations with a carriage return
              fn = fn.replace(/\, /g, "\r");
          
              // Set the field value
              getField("fn").value = fn;
          
          })();
          

           

          Replace "fn" in the getField statement with the name of your multiline text field. This code will run when the form is opened. If you want it to update when the file is saved, you can also place it in the Document Did Save event. Note that some OSes and other things might have a problem with a comma in the file name. An underscore would be a safe alternative.

          • 2. Re: Display Filename in PDF File
            mjdowers Community Member

            George,

             

            Many thanks for this, it works a dream.

             

            Regards

            Malcolm Dowers

            • 3. Re: Display Filename in PDF File
              DerekLee1 Community Member

              This was very helpful!  Worked great for me.  However, if I rename the document, the filename in the stamp does not change, even upon reopening.  Can this not be done?

              • 4. Re: Display Filename in PDF File
                George_Johnson CommunityMVP

                When you say stamp, do you mean form field or something else? Where exactly did you place the code?

                • 5. Re: Display Filename in PDF File
                  DerekLee1 Community Member

                  I placed the following code in a user field on a custom stamp.

                   

                   

                  event.value = event.source.source.documentFileName.replace(/\.pdf$/i, "")

                   

                  It works perfectly for placing the filename into the stamp, but seems to become static once it's stamped.  If you close the file, rename, and reopen, it is still stamped with the original filename.  You have to delete that stamp and restamp it to pick up the new filename.  Not a terribly big deal, but could be a lot of work if there is a large batch of files that this needs to be done on.

                  • 6. Re: Display Filename in PDF File
                    GKaiseril CommunityMVP

                    That is exactly how stamps work.

                     

                    If you want a dynamic object that updates as the file is moved, downloaded, renamed etc then look at adding a form field and either use a document level script to update the field each time the PDF is opened or use the custom JavaScript calculation for the form field.

                     

                    You can get the file name from any one of the following methods:

                     

                    this.URL; // need to extract file name;

                    this.path; // need to extract file name;

                    this.documentFileName;

                     

                    If you want to disable the save warning when closing the PDF use "this.dirty = false;" to clear the "dirty" flag that is used to test if the PDF has been updated.

                    • 7. Re: Display Filename in PDF File
                      DerekLee1 Community Member

                      I'm not sure I understand what "document level" means.  Additionally, any help you could provide on how to auto-place the stamp would be wonderful as well (as in bottom center, top right, etc.).  I'm really new to Java scripting, so I'm very appreciative of your comments and direction.

                      • 8. Re: Display Filename in PDF File
                        GKaiseril CommunityMVP

                        I am suggesting using a form field because a form field will remain interactive and not become static. The field can be made Read Only so the user cannot directly change the contents.

                         

                        Dynamic stamps have fields but they only remain dynamic until the stamp has been placed and then the field values become static content within the stamp and is not dynamically updated as the file is renamed.

                         

                        Entering Document Level Scripts by Thom Parker

                        Automating Placement of Annotations by Thom Parker

                        • 9. Re: Display Filename in PDF File
                          DerekLee1 Community Member

                          I see.  A form field, however, would not include the image that I've created as with the stamp.  I suppose a script could be written that would place a stamp with a form field over it, though, right?

                          • 10. Re: Display Filename in PDF File
                            GKaiseril CommunityMVP

                            Yes, but you will still need to program the field so it is dynamically updated.

                            • 11. Re: Display Filename in PDF File
                              D@veB Community Member

                              The easiest way to fix this problem is to edit the current javascript.

                               

                              find the line:

                              var strLabel = global.FileNameStamp.strDocStamp.replace(/\n/g,"\r");

                               

                              Place this line underneath it:

                              var strLabel = global.FileNameStamp.strDocStamp.replace(/\.pdf$/i, "");

                               

                              So you get:

                              var strLabel = global.FileNameStamp.strDocStamp.replace(/\n/g,"\r");
                              var strLabel = global.FileNameStamp.strDocStamp.replace(/\.pdf$/i, "");

                               

                              That's it!