File Name Stamper


is there anyway amend java script in file name stamper include file path/directory in stamp?

 

the javascript below

 

//////////////////////////////////////////////
///////////////////////////////////////////////
//
//   file name stamper action script
//
//   thom parker, windjack solutions, inc.
//      www.windjack.com, www.pdfscripting.com
//   adobe systems inc. www.adobe.com
//
//       note: use in action script
//       requires:  acrobat 10 or later
//
//   stamps pdf file name/title/date onto pdf
//   popup dialog entering stamping parameters
//   displayed each pdf processed action
//   parameters include position, text size, font, text color
//  
//   stamp created watermark
//
//////
//
//   version 1.2 - 11/5/2020

//acrobat javascript dialog
//created dialogdesigner 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 positive number");
             return false;
        }
       
        var rg = /([\dabcdef]{2})([\dabcdef]{2})([\dabcdef]{2})/i;
        if(!rg.test(orslt["ftcl"]))
        {
             app.alert("the font color must series of 3 pairs of hexadecimal numbers,"
                           + " each pair represents 1 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 positive number");
             return false;
        }
       
        if(isnan(orslt["mrgv"]) || (number(orslt["mrgv"]) < 0))
        {
             app.alert("the vertical margin must 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 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");

        // 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 , 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 , 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 + ": livecycle form, cannot labeled\n\n want continue processing files? (pressing no abort file processing)",1,2))
     event.rc = false;
}  

you can use doc.documentfilename acccess file name of current document (see javascript api reference , related methods).



More discussions in JavaScript


adobe

Comments

Popular posts from this blog

How to change text Component easybook reloaded *newbee* - Joomla! Forum - community, help and support

After Effect warning: A problem occurred when processing OpenGL commands

Preconditions Failed. - Joomla! Forum - community, help and support