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);
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,对象……等).
解决方法
我假设您已经知道如何使用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)
我不确定这一切是否都能在实践中发挥作用.请以任何方式告诉我.