2 Replies Latest reply on Aug 23, 2012 7:18 AM by gkaiseril

    File Name Stamper

    Kirsty9102

      Is there anyway to amend the java script in the File Name stamper so that it will include the file path/directory in the stamp?

       

      The javascript is below

       

      //////////////////////////////////////////////
      ///////////////////////////////////////////////
      //
      //   File Name Stamper Action Script
      //
      //   by Thom Parker, WindJack Solutions, Inc.
      //      www.windjack.com, www.pdfscripting.com
      //   for Adobe Systems Inc. www.adobe.com
      //
      //       NOTE: Only for use in an Action Script
      //       Requires:  Acrobat 10 or later
      //
      //   Stamps the PDF file Name/Title/Date onto a PDF
      //   A popup dialog for entering stamping parameters
      //   is displayed for each PDF processed by the Action
      //   Parameters include Position, Text Size, Font, Text Color
      //  
      //   The stamp is created watermark
      //
      //////
      //
      //   Version 1.2 - 11/5/2020

      //Acrobat JavaScript Dialog
      //Created by DialogDesigner from WindJack Solutions
      //<CodeAbove>
      var aFontNames = [
      "Helvetica",
      "Times",
      "Courier",
      ];

      var aDateFmts = [
                  "mm/dd/yyyy",
                  "yyyy-mm-dd",
                  "mmmm d, yyyy",
                  "ddd mmm d, yyyy",
                  "dddd mmmm d, yyyy"
      ];

      function SetLabelText(dialog, dlg)
      {
           var strLab = "";
           var oRslt = dialog.store();

           strLab = oRslt["FlNm"]?dlg.strDocFileName:dlg.strDocTitle;
           if(oRslt["HDat"])
           {
              var path = new Array();
               strLab += " - " + ( (dlg.GetListSel(oRslt["DFmt"],path))?path.reverse():"").toString();
           }
           dialog.load({"DcSt":strLab});
      }
      //</CodeAbove>
      if(typeof(global.FileNameStamp) == "undefined")
      {

      global.FileNameStamp =
      {

          result:"cancel",
          DoDialog: function(){return app.execDialog(this);},
          strLabSource:"FlNm",
          strDocStamp:"",
          bUseDate:false,
          strDateFormat:"",
          strFontName:["Arial"],
          strFontSize:"8",
          strFontColor:"000000",
          strHorzPos:"PosR",
          nMarginX:"0.2",
          strVertPos:"PosT",
          nMarginY:"0.3",
          strPgRangeSel:"rAll",
          strStrtPg:"",
          strEndPg:"",
          GetRadioSel:function(oRslts,aCtrls){
            for(var strRtn=aCtrls[0];aCtrls.length>0;strRtn=aCtrls.pop()){
              if(oRslts[strRtn] == true)
                return strRtn;
            }
            return "";
          },
          SetListSel:function(list,path){if(path.length == 0) return;
          eval("list[\""+ ((typeof path.join != "function")?path:path.join("\"][\"")) + "\"] = 1")},
          GetListSel:function(oLstRslts,path){
             for(var item in oLstRslts){
                if( ((typeof oLstRslts[item]=="number")&&(oLstRslts[item]>0))
                   || this.GetListSel(oLstRslts[item],path) )
                 {path.push(item);return true;}
             }
             return false;
          },
          bHidden:true,
          nNumPages:1,
          nCurPage:0,
          strDocTitle:"My Title",
          strDocFileName:"mytestdoc.pdf",
          SetTheLabel:SetLabelText,
          initialize: function(dialog)
          {
              var listDFmt = new Object();
              this.SetListSel(listDFmt, this.strDateFormat);

              var listFont =
              {
                  "Helvetica": -1,
              };
              this.SetListSel(listFont, this.strFontName);

              var dlgInit =
              {
                  "Font": listFont,
                  "FtSz": this.strFontSize,
                  "FtCl": this.strFontColor,
                  "MrgH": this.nMarginX,
                  "MrgV": this.nMarginY,
                      "DcSt": this.strDocStamp,
                      "HDat": this.bUseDate,
                      "tFPg": this.strStrtPg,
                      "tTPg": this.strEndPg,
              };
              dlgInit[this.strLabSource] = true;
              dlgInit[this.strHorzPos] = true;
              dlgInit[this.strVertPos] = true;
              dlgInit[this.strPgRangeSel] = true;
              dialog.load(dlgInit);
              dialog.enable(
                  {
                      "tTPg": false,
                      "tFPg": false,
                      "DFmt": false,
                  }
          );
              if( (this.strStrtPg == "")|| isNaN(this.strStrtPg) || (Number(this.strStrtPg) > this.nNumPages) )
              {
                 if(this.bHidden)
                     this.strStrtPg = "1";
                 else
                     this.strStrtPg = (this.nCurPage+1).toString();
              }
             
              if((this.strEndPg == "") || isNaN(this.strEndPg) || (Number(this.strEndPg) > this.nNumPages) )
                 this.strEndPg = this.nNumPages.toString();
             
              var flist = {};
              /*
              for(var i=0;i<aFontNames.length;i++)
              */
              for(var nm in font)
                 flist[font[nm]] = -1;
              flist[this.strFontName] = 1;
             
              var dlist = {};
              var oDt = new Date();
              for(var i=0;i<aDateFmts.length;i++)
                 dlist[util.printd(aDateFmts[i],oDt)] = (i==0)?1:-1;
             
              var exInit ={"tFPg": this.strStrtPg,"tTPg":this.strEndPg, "sOfN":"of (" + this.nNumPages+")",
                                   "DcSt":(this.strLabSource == "FlNm")?this.strDocFileName:this.strDocTitle,
                                   "TopL":"Working on File: " + this.strDocFileName, "Font":flist, "DFmt":dlist };
             
             
              if(this.bHidden && this.strPgRangeSel == "rCur")
              {  
                    this.strPgRangeSel = "rAll";
                    exInit["rCur"] = false;
                    exInit[this.strPgRangeSel] = true;
              }
              
              dialog.load(exInit);
             
              var exInit = {"ExPg":this.strPgRangeSel =="rFro", "MrgH":this.strHorzPos!="PosC", "MrgV":this.strVertPos!="PosM",
                                      "tFPg":this.strPgRangeSel =="rFro", "tTPg":this.strPgRangeSel == "rFro", "rCur":!this.bHidden,
                                      "DcSt":(this.strLabSource == "Titl"), "DFmt":this.bUseDate};
             
              dialog.enable(exInit);
              this.SetTheLabel(dialog,this);
          },
          validate: function(dialog)
          {
              var oRslt = dialog.store();
             
              if(isNaN(oRslt["FtSz"]) || (Number(oRslt["FtSz"]) < 0))
              {
                   app.alert("Font Size must be a positive number");
                   return false;
              }
             
              var rg = /([\dabcdef]{2})([\dabcdef]{2})([\dabcdef]{2})/i;
              if(!rg.test(oRslt["FtCl"]))
              {
                   app.alert("The Font Color must be a series of 3 pairs of Hexadecimal numbers,"
                                 + " where each pair represents one 8 bit color component, Red Green Blue\n"
                                 + "For Example:\n   Black = 000000,  Red = FF0000, Green = 00FF00, Blue = 0000FF");
                   return false;
              }
             
              if(isNaN(oRslt["MrgH"]) || (Number(oRslt["MrgH"]) < 0))
              {
                   app.alert("The Horizontal Margin must be a positive number");
                   return false;
              }
             
              if(isNaN(oRslt["MrgV"]) || (Number(oRslt["MrgV"]) < 0))
              {
                   app.alert("The Vertical Margin must be a positive number");
                   return false;
              }
             
              return true;
          },
          commit: function(dialog)
          {
              var oRslt = dialog.store();
              this.strLabSource = this.GetRadioSel(oRslt,["FlNm","Titl"]);
              this.strDocStamp = oRslt["DcSt"];
              this.bUseDate = oRslt["HDat"];
              var path = new Array();
              this.strDateFormat = (this.GetListSel(oRslt["DFmt"],path))?path.reverse():"";
              var path = new Array();
              this.strFontName = (this.GetListSel(oRslt["Font"],path))?path.reverse():"";
              this.strFontSize = oRslt["FtSz"];
              this.strFontColor = oRslt["FtCl"];
              this.strHorzPos = this.GetRadioSel(oRslt,["PosL","PosC","PosR"]);
              this.nMarginX = oRslt["MrgH"];
              this.strVertPos = this.GetRadioSel(oRslt,["PosT","PosM","PosB"]);
              this.nMarginY = oRslt["MrgV"];
              this.strPgRangeSel = this.GetRadioSel(oRslt,["rAll","rCur","rFro"]);
              this.strStrtPg = oRslt["tFPg"];
              this.strEndPg = oRslt["tTPg"];
          },
          "But1": function(dialog)
          {
              dialog.end("Abrt");
          },
          "tTPg": function(dialog)
          {
              var x;
             
             
             
             
          },
          "rFro": function(dialog)
          {
              dialog.enable({tFPg:true, tTPg:true, "ExPg":true});
             
             
          },
          "rCur": function(dialog)
          {
              dialog.enable({tFPg:false, tTPg:false,"ExPg":false});
             
             
          },
          "rAll": function(dialog)
          {
              dialog.enable({tFPg:false, tTPg:false, "ExPg":false});
             
             
          },
          "PosB": function(dialog)
          {
              dialog.enable({"MrgV":true});
          },
          "PosM": function(dialog)
          {
              dialog.enable({"MrgV":false});
          },
          "PosT": function(dialog)
          {
              dialog.enable({"MrgV":true});
          },
          "PosR": function(dialog)
          {
              dialog.enable({"MrgH":true});
          },
          "PosC": function(dialog)
          {
              dialog.enable({"MrgH":false});
          },
          "PosL": function(dialog)
          {
              dialog.enable({"MrgH":true});
          },
          "DFmt": function(dialog)
          {
              this.SetTheLabel(dialog,this);
             
          },
          "HDat": function(dialog)
          {
              this.SetTheLabel(dialog,this);
              dialog.enable({"DFmt":dialog.store()["HDat"]});
          },
          "DcSt": function(dialog)
          {
              /*
              var oRslt = dialog.store();
              if(oRslt["Titl"])
                this.strDocTitle = oRslt["DcSt"];
              */
          },
          "Titl": function(dialog)
          {
              this.SetTheLabel(dialog,this);
              dialog.enable({"DcSt":true});
          },
          "FlNm": function(dialog)
          {
              this.SetTheLabel(dialog,this);
              dialog.enable({"DcSt":false});
          },
          description:
          {
              name: "File Name Stamper",
              elements:
              [
                  {
                      type: "view",
                      elements:
                      [
                          {
                              type: "view",
                              char_height: 10,
                              elements:
                              [
                                  {
                                      type: "static_text",
                                      item_id: "TopL",
                                      name: "Put Dialog Controls Here",
                                      char_width: 15,
                                      alignment: "align_fill",
                                      font: "palette",
                                      bold: true,
                                  },
                                  {
                                      type: "cluster",
                                      item_id: "cls1",
                                      name: "Label Options",
                                      elements:
                                      [
                                          {
                                              type: "view",
                                              align_children: "align_row",
                                              alignment: "align_fill",
                                              elements:
                                              [
                                                  {
                                                      type: "radio",
                                                      item_id: "FlNm",
                                                      group_id: "FUse",
                                                      name: "Use File Name",
                                                      variable_Name: "strLabSource",
                                                  },
                                                  {
                                                      type: "radio",
                                                      item_id: "Titl",
                                                      group_id: "FUse",
                                                      name: "Use Document Title (or custom)",
                                                  },
                                                  {
                                                      type: "edit_text",
                                                      item_id: "DcSt",
                                                      variable_Name: "strDocStamp",
                                                      width: 200,
                                                      height: 23,
                                                      alignment: "align_fill",
                                                  },
                                              ]
                                          },
                                          {
                                              type: "view",
                                              align_children: "align_row",
                                              elements:
                                              [
                                                  {
                                                      type: "check_box",
                                                      item_id: "HDat",
                                                      name: "Include Date",
                                                      variable_Name: "bUseDate",
                                                  },
                                                  {
                                                      type: "static_text",
                                                      item_id: "sta2",
                                                      name: "Format",
                                                  },
                                                  {
                                                      type: "popup",
                                                      item_id: "DFmt",
                                                      variable_Name: "strDateFormat",
                                                      width: 180,
                                                      height: 23,
                                                      char_width: 8,
                                                  },
                                              ]
                                          },
                                          {
                                              type: "view",
                                              align_children: "align_row",
                                              alignment: "align_fill",
                                              elements:
                                              [
                                                  {
                                                      type: "static_text",
                                                      item_id: "sta1",
                                                      name: "Font:",
                                                  },
                                                  {
                                                      type: "popup",
                                                      item_id: "Font",
                                                      variable_Name: "strFontName",
                                                      width: 111,
                                                      height: 23,
                                                      char_width: 8,
                                                  },
                                                  {
                                                      type: "static_text",
                                                      item_id: "sta0",
                                                      name: "Font Size:",
                                                      alignment: "align_right",
                                                      font: "dialog",
                                                  },
                                                  {
                                                      type: "edit_text",
                                                      item_id: "FtSz",
                                                      variable_Name: "strFontSize",
                                                      width: 29,
                                                      height: 23,
                                                  },
                                                  {
                                                      type: "static_text",
                                                      item_id: "sta3",
                                                      name: "Color(8bit Hex RGB):",
                                                      alignment: "align_right",
                                                      font: "dialog",
                                                  },
                                                  {
                                                      type: "edit_text",
                                                      item_id: "FtCl",
                                                      variable_Name: "strFontColor",
                                                      width: 80,
                                                      height: 23,
                                                      char_width: 8,
                                                  },
                                              ]
                                          },
                                      ]
                                  },
                                  {
                                      type: "cluster",
                                      item_id: "cls1",
                                      name: "Position",
                                      width: 188,
                                      height: 80,
                                      char_width: 8,
                                      char_height: 8,
                                      elements:
                                      [
                                          {
                                              type: "view",
                                              align_children: "align_top",
                                              elements:
                                              [
                                                  {
                                                      type: "radio",
                                                      item_id: "PosL",
                                                      group_id: "PosH",
                                                      name: "Left",
                                                      variable_Name: "strHorzPos",
                                                  },
                                                  {
                                                      type: "radio",
                                                      item_id: "PosC",
                                                      group_id: "PosH",
                                                      name: "Center",
                                                  },
                                                  {
                                                      type: "radio",
                                                      item_id: "PosR",
                                                      group_id: "PosH",
                                                      name: "Right ",
                                                  },
                                                  {
                                                      type: "static_text",
                                                      item_id: "sta2",
                                                      name: " Margin (inches):",
                                                  },
                                                  {
                                                      type: "edit_text",
                                                      item_id: "MrgH",
                                                      variable_Name: "nMarginX",
                                                      char_width: 8,
                                                  },
                                              ]
                                          },
                                          {
                                              type: "view",
                                              align_children: "align_top",
                                              elements:
                                              [
                                                  {
                                                      type: "radio",
                                                      item_id: "PosT",
                                                      group_id: "PosV",
                                                      name: "Top ",
                                                      variable_Name: "strVertPos",
                                                  },
                                                  {
                                                      type: "radio",
                                                      item_id: "PosM",
                                                      group_id: "PosV",
                                                      name: "Middle",
                                                  },
                                                  {
                                                      type: "radio",
                                                      item_id: "PosB",
                                                      group_id: "PosV",
                                                      name: "Bottom",
                                                  },
                                                  {
                                                      type: "static_text",
                                                      item_id: "sta2",
                                                      name: "Margin (inches):",
                                                  },
                                                  {
                                                      type: "edit_text",
                                                      item_id: "MrgV",
                                                      variable_Name: "nMarginY",
                                                      char_width: 8,
                                                  },
                                              ]
                                          },
                                      ]
                                  },
                                  {
                                      type: "cluster",
                                      item_id: "cls3",
                                      name: "Page range",
                                      align_children: "align_row",
                                      elements:
                                      [
                                          {
                                              type: "radio",
                                              item_id: "rAll",
                                              group_id: "GRP1",
                                              name: "All",
                                              variable_Name: "strPgRangeSel",
                                              height: 20,
                                          },
                                          {
                                              type: "radio",
                                              item_id: "rCur",
                                              group_id: "GRP1",
                                              name: "Current (Applies only to Open Document)",
                                              height: 20,
                                          },
                                          {
                                              type: "radio",
                                              item_id: "rFro",
                                              group_id: "GRP1",
                                              name: "From:",
                                              width: 12,
                                              height: 24,
                                          },
                                          {
                                              type: "edit_text",
                                              item_id: "tFPg",
                                              variable_Name: "strStrtPg",
                                              height: 24,
                                              char_width: 6,
                                          },
                                          {
                                              type: "static_text",
                                              item_id: "sta1",
                                              name: "To:",
                                              height: 24,
                                          },
                                          {
                                              type: "edit_text",
                                              item_id: "tTPg",
                                              variable_Name: "strEndPg",
                                              height: 24,
                                              char_width: 6,
                                          },
                                          {
                                              type: "static_text",
                                              item_id: "sOfN",
                                              name: "of (N)          ",
                                              height: 24,
                                          },
                                      ]
                                  },
                              ]
                          },
                          {
                              type: "view",
                              align_children: "align_row",
                              alignment: "align_fill",
                              elements:
                              [
                                  {
                                      type: "ok_cancel",
                                      ok_name: "Apply",
                                      cancel_name: "Skip",
                                  },
                                  {
                                      type: "button",
                                      item_id: "But1",
                                      name: "Abort Process",
                                  },
                                  {
                                      type: "gap",
                                      item_id: "gap1",
                                      width: 210,
                                      height: 10,
                                  },
                                  {
                                      type: "static_text",
                                      item_id: "sta1",
                                      name: "version 1.2  11/5/2010",
                                      alignment: "align_right",
                                  },
                              ]
                          },
                      ]
                  },
              ]
          }
      };
      }


      var oDoc = event.target;

      if(typeof(oDoc.xfa) == "undefined")
      {

          global.FileNameStamp.bHidden = oDoc.hidden;
          global.FileNameStamp.nNumPages = oDoc.numPages;
          if(!oDoc.hidden)
             global.FileNameStamp.nCurPage = oDoc.pageNum;
          global.FileNameStamp.strDocTitle = oDoc.info.title;
          global.FileNameStamp.strDocFileName = oDoc.documentFileName.replace(/\.pdf$/i,"");
         

          var cRtn = global.FileNameStamp.DoDialog();
          if("ok" == cRtn)
          {
              // Setup starting parameters
              var nPgStart, nPgEnd;
              var nTextSize = Number(global.FileNameStamp.strFontSize);
              var strLabel = global.FileNameStamp.strDocStamp.replace(/\n/g,"\r");

              // Get Font Color
              rgCol = /([\dabcdef]{2})([\dabcdef]{2})([\dabcdef]{2})/i;
              rgCol.test(global.FileNameStamp.strFontColor);
              var aFontCol = ["RGB", parseInt(RegExp.$1,16)/255,
                              parseInt(RegExp.$2,16)/255, parseInt(RegExp.$3,16)/255];

          
              switch(global.FileNameStamp.strPgRangeSel)
              {
                case "rAll":
                  nPgStart = 0;
                  nPgEnd = oDoc.numPages -1;
                  break;
                case "rCur":
                  nPgEnd = nPgStart = oDoc.hidden?0:oDoc.pageNum;
                  break;
                case "rFro":
                  nPgStart = Number(global.FileNameStamp.strStrtPg)-1;
                  if(nPgStart > (oDoc.numPages -1))
                    nPgStart = oDoc.numPages -1;
          
                  nPgEnd = Number(global.FileNameStamp.strEndPg)-1;
                  if(nPgEnd > (oDoc.numPages -1))
                    nPgEnd = oDoc.numPages -1;
                  break;
              }

                 var nTextAlign,nHAlign,nVAlign;
                 var nHMarg, nVMarg;
                 var nMargX = Number(global.FileNameStamp.nMarginX) * 72;
                 switch(global.FileNameStamp.strHorzPos)
                 {
                    case"PosL":
                      nTextAlign = app.constants.align.left; // Left Aligned Text
                      nHAlign = app.constants.align.left;
                      nHMarg = nMargX;
                      break;
                    case"PosC":
                      nTextAlign = app.constants.align.center;
                      nHAlign = app.constants.align.center;
                      nHMarg = 0;
                      break;
                    case"PosR":
                      nTextAlign = app.constants.align.right;
                      nHAlign = app.constants.align.right;
                      nHMarg = -nMargX;
                      break;
                 }
           
                 var nMargY = Number(global.FileNameStamp.nMarginY) * 72;
           
                 switch(global.FileNameStamp.strVertPos)
                 {
                    case"PosT":
                      nVAlign = app.constants.align.top;
                      nVMarg = -nMargY;
                      break;
                    case"PosM":
                      nVAlign = app.constants.align.center;
                      nVMarg = 0;
                      break;
                    case"PosB":
                      nVAlign = app.constants.align.bottom;
                      nVMarg = nMargY;
                      break;
                 }
           
               // Find and rename watermark;
               var aGs = oDoc.getOCGs();
               for(var i=0;aGs && (i<aGs.length);i++)
               {
                  if(aGs[i].name == "Watermark");
                     aGs[i].name = "Old_Watermark";
               }

               try{
                //  Create watermark
                oDoc.addWatermarkFromText({cText:strLabel, nTextAlign:nTextAlign, cFont:global.FileNameStamp.strFontName,
                      nFontSize:nTextSize, aColor:aFontCol, nStart:nPgStart, nEnd:nPgEnd,
                      nHorizAlign:nHAlign, nHorizValue:nHMarg,
                      nVertAlign:nVAlign, nVertValue:nVMarg});
               }catch(e){
                  app.alert("Error applying Label:\n" + e);
               }

               // Find and rename watermark;
               var aGs = oDoc.getOCGs();
               for(var i=0;aGs && (i<aGs.length);i++)
               {
                  if(aGs[i].name == "Watermark");
                  {
                     aGs[i].name = "DocumentLabel";
                     break;
                  }
               }
          }
          else if(cRtn == "Abrt")
            event.rc = false;
      }   
      else
      {
         if(3 == app.alert(oDoc.documentFileName + ": is a LiveCycle Form, which cannot be labeled\n\n Do you want to continue processing files? (Pressing No will Abort the file processing)",1,2))
           event.rc = false;
      }