1

I have a pdf document embedded inside a webpage in ASP.net and want to get a specific field inside the pdf document using Javascript...plain Javascript...

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.