开发者

parse pdf document javascript

开发者 https://www.devze.com 2023-02-28 18:39 出处:网络
I have a pdf document embedd开发者_高级运维ed inside a webpage in ASP.net and want to get a specific field inside the pdf document using Javascript...plain Javascript...JavaScript in a PDF can call JS

I have a pdf document embedd开发者_高级运维ed inside a webpage in ASP.net and want to get a specific field inside the pdf document using Javascript...plain Javascript...


JavaScript in a PDF can call JS in a web page and visa versa, if BOTH are set up for it. You can see Acrobat's documentation here.

Check out the HostContainer specification, starting on page 486. In the PDF you'd need script something like:

var document = this; // hurray for closures.
this.hostContainer.messageHandler = { onDisclose: function() {return true;},
  onMessage: function(msgArrayIgnored) {
    // build a JSON string of field/value pairs
    var outgoingMessage = "{ ";
    for (var i = 0; i < this.numFields; ++i) {
      var fldName = document.getNthFieldName(i);
      var fld = document.getField(fld);
      var val = fld.value;
      // you'll probably need to escape 'val' to be legal JSON
      outgoingMessage += fldName + ": \"" + val + "\";

      // stick in a comma unless this is the last field
      if (i != this.numFields-1) {
        outgoingMessage += ", ";
      }

    }
    outgoingMessage += "};";
    this.hostContainer.postMessage( [outgoingMessage] );
  };

In the HTML, you need to set up something similar. Lets assume your pdf is embedded in an object tag, and that element's id is "pdfElem". Your HTML script might look something like:

var pdf = document.getElementById("pdfElem");
pdf.messageHandler = function(message) {
  var fldValPairs = eval(message);
  doStuffWithFieldInfo(fldValPairs);
};

Later, any time you want to inspect the PDF's field info you post a message, and the PDF will call back to pdf.messageHandler with its JSON string wrapped in an array:

pdf.postMessage(["this string is ignored"]);

There's probably a bug or two lurking in there somewhere, but this will put you on the right track.


Webpage JavaScript will not be able to interact with the PDF form fields. You can however make a PDF form post to a web page form processor and then obtain the values in the form fields.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号