动态 PDF 图章对话框的 JavaScript 不起作用

问题描述 投票:0回答:2

我一直在尝试创建一个动态 PDF-XChange 印章,在印章上有 4 个动态文本字段(Text1、Text2、Text3 和 Text4),用户可以在放置印章之前在对话框中进行编辑。我想要的是用户选择图章并打开带有 4 个用户输入字段的对话框:

  1. RMA 状态 - 用户添加状态(名为 RMAS)
  2. 供用户输入任何内容的自由文本字段(名为 FREE)
  3. 用户的姓名和业务团队,从用户的身份信息中提取(名为 TEAM)
  4. 今天的日期,可编辑,以防他们在事后盖章(名为日期)

我让它工作,它弹出一个对话框 4 次,连续询问用户每个问题,但所要求的是一个带有 4 个文本字段的对话框。我似乎无法弄清楚下面的代码需要如何更改才能工作,但我对此很陌生。

目前它只打开最后一个问题(日期),在对话框激活之前未填充我的 JavaScript 日期代码,没有其他问题。如果我删除日期元素,它会询问前面的问题(名称和业务单位),但不会显示一个对话框,其中包含使用代码末尾处理的数据预先填充的所有 4 个问题,这已经过测试单独并正在工作。

我的代码注释如下。如果有人可以帮助我打开对话框并预先填充 4 个文本字段,我很想看看我哪里出错了。如果您能帮助我将数据添加到邮票的 Text1 到 Text4 框中,我会欣喜若狂!

// Dialog Definition 
var oDlg = {
    
    RMAStatus: "",
    FreeText: "",
    NameAndUnit: "",
    TodaysDate: "",

    description:
    {
        name: "Stamp details",
        elements:
        [
            {
                type: "view",
                elements:
                [
                    {
                        type: "view",
                        elements:
                        [
                            {
                                item_id: "lbl1",
                                type: "static_text",
                                name: "&RMA Stage",
                            },
                            {
                                width: 200,
                                height: 22,
                                type: "edit_text",
                                item_id: "rmas",
                            }
                        ],
                        type: "view",
                        elements:
                        [
                            {
                                item_id: "lbl1",
                                type: "static_text",
                                name: "&Free text field",
                            },
                            {
                                width: 200,
                                height: 88,
                                type: "edit_text",
                                item_id: "free",
                            }
                        ],
                        type: "view",
                        elements:
                        [
                            {
                                item_id: "lbl1",
                                type: "static_text",
                                name: "&Your name and team",
                            },
                            {
                                width: 200,
                                height: 22,
                                type: "edit_text",
                                item_id: "team",
                            }
                        ],
                        type: "view",
                        elements:
                        [
                            {
                                item_id: "lbl1",
                                type: "static_text",
                                name: "&Stamp date",
                            },
                            {
                                width: 200,
                                height: 22,
                                type: "edit_text",
                                item_id: "date",
                            }
                        ]
                    },
                    {
                        type: "ok_cancel",
                    }
                ]
            }
        ]
    },
    initialize: function(dialog) { // where the data in dialog elements is set when the dialog is first activated
        dialog.load({
            "rmas":this.RMAStage,  
            "free":this.FreeText,
            "team":this.CorrectName,
            "date":this.todaysDate,
        });
    },
    commit: function(dialog) { // where element data is acquired when the user presses the OK button
        var data = dialog.store();
        this.RMAStage = data["rmas"];
        this.FreeText = data["free"];
        this.CorrectName = data["team"];
        this.todaysDate = data["date"];
    }
};

/*
 * Pre-fill the dialog text fields with the data below
 */
/* RMA STAGE (for RMAS to be populated) */
oDlg.RMAStage = "RMA Stage";

/* FREE TEXT FIELD (for FREE to be populated)  */
oDlg.FreeText = "";

/* NAME AND UNIT (for TEAM to be populated)  */
var IdentityName, IdentityNameSplit, Unit;

/* Set Organisation Unit */
Unit = "Parks & Landscapes Team";  // Unlikely to change

/* Find correctly formatted username from Identity info */
if((identity.name != null) && !/^\s*$/.test(identity.name))
    IdentityName = identity.name;
else
    IdentityName = identity.loginName.replace(/\./g," ").replace(/\./g," ").replace(/\b(\w)/g,function(Word,cFst){return cFst.toUpperCase()});

if (IdentityName.indexOf(', ') > -1) { // If the result is "Surname, Firstname" swap the names to make "Firstname Surname"
    IdentityNameSplit = IdentityName.split(', ');
    oDlg.NameAndUnit = IdentityNameSplit[1] + " " + IdentityNameSplit[0] + ", " + Unit;
}
else
    oDlg.NameAndUnit = IdentityName + ", " + Unit;

/* FORMATTED DATE FIELD (for DATE to be populated)  */
var stampDate;
stampDate = new Date();
oDlg.TodaysDate = util.printd("dd mmmm, yyyy", stampDate);

// Start dialog function
app.execDialog (oDlg);
javascript pdf dialog pdfstamper stamp
2个回答
0
投票

成功了!如果其他人想自己复制/粘贴此解决方案:

var pagDlg =
{

    result: "cancel",
    DoDialog: function () { return app.execDialog(this); },
    //definitions
    strDefFmt: "dd-mm-yyyy",
    strFmt: "dddd d mmmm, yyyy",
    vDate: new Date,
    strDate: "",
    iOChecked: false,
    abwChecked: false,
    //
    SetListSel: function (list, path) {
        if (path.length == 0) return;
        eval("list[\"" + ((path.join != "function") ? path : path.join("\"][\"")) + "\"] = 1");
    },
    GetListSel: function (oLstResult) {
        for (var item in oLstResult) {
            if ((typeof oLstResult[item] == "number") && (oLstResult[item] > 0))
                return item;
        }
        return "";
    },
    formatDate: function (d, f) {
        return util.printd(f, d);
    },
    //Initialize values that will be used in the pop-up dialogue
    initialize: function (dialog) {
        var unit = "Technical Services & Design";
        this.strDate = this.formatDate(this.vDate, this.strDefFmt);
        //this.strDate = this.formatDate(this.vDate, "dd mmmm, yyyy");
        //
        var dlgInit =
        {
            "date": this.strDate,
            "free": "Enter your description text here.", //enable this checkbox option as default
            "abw_2": IdentityName + ", " + unit,
            "rma": "RMA Stage",
        };
        dialog.load(dlgInit);
        iOChecked = dlgInit["free"]; // initialise and keep it the same as the value in dlgInit
        abwChecked = dlgInit["abw_2"];
        rmaChecked = dlgInit["rma"];
    },
    commit: function (dialog) { // Called when OK button is pressed
        var oRslt = dialog.store();
        var path = new Array();
        var d = oRslt["date"];  //OK Go date
        //
        if (typeof d == "string")
            d = util.scand(this.strDefFmt, d);
        if (d != null)
        {
            var cur = new Date;
            d = new Date(d.getFullYear(), d.getMonth(), d.getDate(), cur.getHours(), cur.getMinutes(), cur.getSeconds());
        }
        this.vDate = d;
        //

        this.rmaChecked = oRslt["rma"];
        this.iOChecked = oRslt["free"];
        this.abwChecked = oRslt["abw_2"];
        this.strDate = oRslt["date"];
    },
    //Description of the layout of the Dialoge Window
    description:
    {
        name: "Stamp details",
        width: 450,
        elements:
        [
            {
                type: "view",
                width: 450,
                elements:
                [
                    {
                        type: "view",
                        align_children: "align_row",
                        elements:
                        [
                            {
                                item_id: "lbl1",
                                name: "RMA Stage",
                                type: "static_text",
                                width: 100,
                            },
                            {
                                item_id: "rma",
                                type: "edit_text",
                                width: 300,
                            },
                        ]
                    },
                    {
                        type: "view",
                        align_children: "align_row",
                        elements:
                        [
                            {
                                item_id: "lbl1",
                                name: "Free text",
                                type: "static_text",
                                width: 100,
                                alignment: "align_top",
                                align_children: "align_top",
                            },
                            {
                                item_id: "free",
                                type: "edit_text",
                                multiline: true,
                                width: 300,
                                height: 88,
                            },
                        ]
                    },
                    {
                        type: "view",
                        align_children: "align_row",
                        elements:
                        [
                            {
                                item_id: "lbl1",
                                name: "Name and team",
                                type: "static_text",
                                width: 100,
                            },
                            {
                                item_id: "abw_2",
                                type: "edit_text",
                                width: 300,
                            },
                        ]
                    },
                    {
                        type: "view",
                        align_children: "align_row",
                        elements:
                        [
                            {
                                item_id: "lbl1",
                                name: "Date",
                                type: "static_text",
                                width: 100,
                            },
                            {
                                item_id: "date",
                                type: "edit_text",
                                DateEdit: true,
                                width: 100,
                            }
                        ]

                    },
                    {
                        type: "ok_cancel",
                    },
                ]
            }
        ]
    }
};
/* NAME */
var IdentityName, IdentityNameSplit;

/* Find correctly formatted username from Identity info */
if((identity.name != null) && !/^\s*$/.test(identity.name))
    IdentityName = identity.name;
else
    IdentityName = identity.loginName.replace(/\./g," ").replace(/\./g," ").replace(/\b(\w)/g,function(Word,cFst){return cFst.toUpperCase()});

if (IdentityName.indexOf(', ') > -1) { // If the result is "Surname, Firstname" swap the names to make "Firstname Surname"
    IdentityNameSplit = IdentityName.split(', ');
    IdentityName = IdentityNameSplit[1] + " " + IdentityNameSplit[0];
}

if (event.source.forReal && (event.source.stampName == "#T2CKw07Lo6sr42d6F3ao#0"))
{
    if (pagDlg.DoDialog() == "ok")
    {
        var d = pagDlg.vDate; 

        this.getField("Text1").value = pagDlg.rmaChecked; // RMA Stage
        this.getField("Text2").value = pagDlg.iOChecked; // Free text
        this.getField("Text3").value = pagDlg.abwChecked; // Name and team
        this.getField("Text4").value = util.printd(pagDlg.strFmt, d); // Date
    }
    else
    {
        app.alert("Stamp cancelled", 3);
    }
}
else
{
    var d = pagDlg.vDate;
    event.value = util.printd(pagDlg.strFmt, d);
}

希望有人会发现这很有用。


0
投票

感谢您回来并留下您的代码 Skry,这对我在 PDFXChange 编辑器中创建自己的动态图章非常有用。这个是基于你的,但计算的是管道速度+压力损失。请参阅下文,以防对其他人有帮助

var pagDlg = {
    result: "cancel",
    DoDialog: function () { return app.execDialog(this); },

    vDate: new Date,
    flowChecked: false,
    size_widthChecked: false,
    size_heightChecked: false,
    size_diaChecked: false,

    initialize: function (dialog) {
        var dlgInit = {
            "flow": "flow (l/s)",
            "size_width": "width (mm)",
            "size_height": "height (mm)",
            "size_dia": "or dia (mm)",
        };
        dialog.load(dlgInit);

        this.flowChecked = dlgInit["flow"];
        this.size_widthChecked = dlgInit["size_width"];
        this.size_heightChecked = dlgInit["size_height"];
        this.size_diaChecked = dlgInit["size_dia"];
    },

    commit: function (dialog) {
        var oRslt = dialog.store();

        this.flowChecked = oRslt["flow"];
        this.size_widthChecked = oRslt["size_width"];
        this.size_heightChecked = oRslt["size_height"];
        this.size_diaChecked = oRslt["size_dia"];
    },

    description: {
        name: "Stamp details",
        elements: [
            {
                type: "view",
                elements: [
                    {
                        type: "view",
                        elements: [
                            {item_id: "flow", type: "edit_text", name: "Flow"},
                            {item_id: "size_width", type: "edit_text", name: "Size (width)"},
                            {item_id: "size_height", type: "edit_text", name: "Size (height)"},
                            {item_id: "size_dia", type: "edit_text", name: "Size (dia)"},
                        ]
                    },
                    {type: "ok_cancel"},
                ]
            }
        ]
    }
};

if (pagDlg.DoDialog() == "ok") {
    var flowInCubicMeters = parseFloat(pagDlg.flowChecked) / 1000;
    var velocity;
    var hydraulicDiameter;
    var areaInSquareMeters;

    if (pagDlg.size_diaChecked !== "or dia (mm)") {
        // Set "Text1" to "N/A" and format "Text2"
        this.getField("Text1").value = "N/A";
        this.getField("Text2").value = "Ø" + pagDlg.size_diaChecked + " mm";

        var diameterInMeters = parseFloat(pagDlg.size_diaChecked) / 1000;
        areaInSquareMeters = Math.PI * (diameterInMeters / 2) ** 2;
        hydraulicDiameter = diameterInMeters; 
        velocity = flowInCubicMeters / areaInSquareMeters;
    } else {
        // Format "Text1" and set "Text2" to "N/A"
        this.getField("Text1").value = pagDlg.size_widthChecked + "x" + pagDlg.size_heightChecked + " mm";
        this.getField("Text2").value = "N/A";

        var widthInMeters = parseFloat(pagDlg.size_widthChecked) / 1000;
        var heightInMeters = parseFloat(pagDlg.size_heightChecked) / 1000;
        areaInSquareMeters = widthInMeters * heightInMeters;
        hydraulicDiameter = 2 * (widthInMeters * heightInMeters) / (widthInMeters + heightInMeters);
        velocity = flowInCubicMeters / areaInSquareMeters;
    }

    var f = 0.02; // Assumed friction factor
    var rho = 1.2; // Density of air in kg/m³
    var deltaP = f * (1/hydraulicDiameter) * 0.5 * rho * velocity ** 2; // Pressure loss per meter

    this.getField("Text3").value = velocity.toFixed(2) + " m/s";
    this.getField("Text4").value = deltaP.toFixed(2) + " Pa/m";
    this.getField("Text5").value = pagDlg.flowChecked + " l/s";
} else {
    
}
© www.soinside.com 2019 - 2024. All rights reserved.