javascript – PDF hostContainer回调

按照此SO解决方here通知客户PDF文档中的单击事件,如何在客户端使用this.myPDF.submitForm(“localhost / Handler.ashx?r = 2”)提交PDF时通知客户端)功能

PDF文件用户控件内创建,然后呈现为HTML对象:

string container = ("<object data='/myfile.pdf' type='application/pdf'></object>");

附加到PDF的JS文件是这样完成的:

var webClient = new WebClient();
 string htmlContent = webClient.DownloadString(fileurl + "pdf_script.js");
 PdfAction action = PdfAction.JavaScript(htmlContnent,pdfstamper.Writer);
 pdfstamper.Writer.SetopenAction(action);

以及js文件内容

this.disclosed = true;
if (this.external && this.hostContainer) {

function onMessageFunc(stringArray) {
     try {
          this.myPDF.submitForm("http://localhost/Handler.ashx?EmpNo=12345" + "#FDF",false);

        }
        catch (e) {

        }
    }
    function onErrorFunc(e) {
        console.show();
        console.println(e.toString());
    }
    try {
        if (!this.hostContainer.messageHandler);
        this.hostContainer.messageHandler = new Object();
        this.hostContainer.messageHandler.myPDF = this;
        this.hostContainer.messageHandler.onMessage = onMessageFunc;
        this.hostContainer.messageHandler.onError = onErrorFunc;
        this.hostContainer.messageHandler.ondisclose = function () { return true; };
    }
    catch (e) {
        onErrorFunc(e);
    }
}

调用submitForm时,PDF内容(表单字段)将成功保存,并通过执行以下操作在PDF中显示警报:

message = "%FDF-1.2
                   1 0 obj
                   <<
                   /FDF
                   <<
                      /Status("Success!")
                   >>
                   >>
                   endobj
                   trailer
                   <</Root 1 0 R>>
           %%EOF");
return message;

我想要做的是让PDF在客户端发送表单提交调用后回调客户端,这是一种向客户确认表单已提交的方式,而不是以警报的形式,而是一种触发主机功能方法(容器,iframe,对象……等).

解决方法

您使用的FDF回复对我来说不了解,所以我从您的问题中学到了一些新东西.我已经在PDF参考中研究过AcroJS参考和FDF规范,现在我对你的代码的作用有了更好的理解.谢谢你.

我假设您已经知道如何使用PDF中的JavaScript调用在HTML文件中触发JavaScript消息.请参阅JavaScript Communication between HTML and PDF文章中的createMessageHandler().

我将您的问题解释为:“如何在成功提交数据后调用方法?”

如果有这个问题的解决方案,它将涉及JavaScript.我看到可以在FDF文件添加JavaScript,但我不确定JavaScript是否可以“与HTML”对话.我不确定你是否可以从FDF响应调用初始PDF中的JavaScript函数.如果可能,您应该向PDF添加类似于/ Status条目的JavaScript条目.

此条目的值是字典,类似于:

<<
/Before (app.alert\("before!"\))
/After (app.alert\("after"\))
/Doc [/MyDocScript1,(myFunc1\(\)),/MyDocScript2,(myFunc2\(\))
>>

在您的情况下,我会删除/ Before和/ Doc键.我认为你不需要它们,我会将字典缩减为:

<<
/After (talkToHtml\(\))
>>

其中talkToHtml()是PDF中已存在的方法

function talkToHtml() {
    var names = new Array();
    names[0] = "Success!";
    try{
        this.hostContainer.postMessage(names);
    }
    catch(e){
        app.alert(e.message);
    }
}

我不知道这是否有效.我自己从未尝试过.我的答案基于规格.

我不知道你是否真的需要使用FDF.您是否尝试过在您的submitForm()方法添加JavaScript?就像是:

this.myPDF.submitForm({
    cURL: "http://localhost/Handler.ashx?EmpNo=12345",cSubmitAs: "FDF",oJavaScript: {
        Before: 'app.alert("before!")',After: 'app.alert("after")',Doc: ["MyDocScript1","myFunc1()","MyDocScript2","myFunc2()" ]
    }
});

这仅在您提交为FDF时才有效.如果您提交HTML查询字符串,我认为没有解决方案.

如果您想知道MyDocScript1和MyDocScript2是什么:

Doc defines an array defining additional JavaScript scripts to be
added to those defined in the JavaScript entry of the document’s name
dictionary. The array contains an even number of elements,organized
in pairs. The first element of each pair is a name and the second
is a text string or text stream defining the script corresponding
to that name. Each of the defined scripts is added to those already
defined in the name dictionary and then executed before the script
defined in the Before entry is executed. (ISO-32000-1 Table 245)

我不确定这一切是否都能在实践中发挥作用.请以任何方式告诉我.

相关文章

什么是深拷贝与浅拷贝?深拷贝与浅拷贝是js中处理对象或数据...
前言 今天复习了一些前端算法题,写到一两道比较有意思的题:...
最近在看回JavaScript的面试题,this 指向问题是入坑前端必须...
js如何实现弹出form提交表单?(图文+视频)
js怎么获取复选框选中的值
js如何实现倒计时跳转页面