简体   繁体   English

无法使用JavaScript从iTextSharp访问PDF文档页面/事件

[英]Cannot access PDF doc pages/events from iTextSharp with JavaScript

Ideally, I am trying to trigger an event in a PDF, using iTextSharp. 理想情况下,我尝试使用iTextSharp触发PDF中的事件。 Failing that, I would like to know if I can trigger a function. 如果不这样做,我想知道我是否可以触发一个功能。

Basically, I'm just trying to get a handle of the document through javascript. 基本上,我只是想通过javascript来处理文档。

This works in the PDF 这适用于PDF

(Using LiveCycle): (使用LiveCycle):

eApp.Page1.FormPurpose_PolicyNumber::click - 

(JavaScript, client) (JavaScript,客户端)

this.rawValue = "hello";

From C# I can't get a handle on the doc, eApp in this case. 从C#开始,在这种情况下我无法处理doc, eApp

var reader = new PdfReader(pdfFileStream);
var writer = new PdfStamper(reader, outputStream);

THIS WORKS:(app object) 这个工作:( app对象)

PdfAction js =
    PdfAction.JavaScript(
        "app.alert('hello');",
        writer.Writer);

writer.Writer.AddJavaScript(js);

THIS DOES NOT: 这不是:

PdfAction js =
    PdfAction.JavaScript(
        "eApp.Page1.FormPurpose_PolicyNumber.execEvent('click');",
        writer.Writer);

writer.Writer.AddJavaScript(js);

Also doesn't work: 也不起作用:

PdfAction js =
    PdfAction.JavaScript(
        "eApp.Page1.FormPurpose_PolicyNumber.rawValue= 'hello'",
        writer.Writer);

writer.Writer.AddJavaScript(js);

I have looked for the answer high and low, but have not been able to find it, yes there are similar questions, but either they are not answered or are very different than what I'm doing. 我已经找到了高低的答案,但却找不到答案,是的,有类似的问题,但要么他们没有回答,要么与我正在做的非常不同。

According to your latest comment - that won't work. 根据你的最新评论 - 这是行不通的。 iTextSharp will only add some scripts into a document. iTextSharp只会在文档中添加一些脚本。 And that script will be started in some client app, like Adobe Reader. 该脚本将在一些客户端应用程序中启动,如Adobe Reader。 It will not execte any scripts inside of your document. 它不会在您的文档中执行任何脚本。 Suppose in your case it will be easier to do whatewer you want with document using c# and than save it with form flattening set to true. 假设在你的情况下使用c#更容易用文档做你想要的文件,而不是将表格展平设置为true保存。

In my case working solution is: 在我看来,工作解决方案是:

PdfReader reader = new PdfReader(this.pathToPDFTemplate);
...
PdfStamper stamper = new PdfStamper(reader, stream);
...
PdfAction action = PdfAction.JavaScript("this.xfa.form.form1.DesignArea.sfPart3.sfPart3_5.ck2.presence = 'hidden';", stamper.Writer);
stamper.Writer.SetAdditionalAction(PdfWriter.WILL_PRINT, action);
...

The trick is to use this.xfa.form. 诀窍是使用this.xfa.form。 before your path to the field. 在你前往田野之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM