title: How to "postMessage" to host from within pdf when viewed in IE9 object element
body: <p>I am attempting to do a small PoC with PDFs and have run into an issue.  I am looking to post a message to a PDF and have the PDF post a message to the browser.</p>
  
  <p><strong>The deets:</strong></p>
  
  <p>I am viewing the PDF in an "object" element in IE9.  I am using itextsharp to prefill a pdf template on the server, inject some app level javascript (post message and on message stuff) and then serve that up to the browser via a filestreamresult.  I am using Reader 10 to view the PDF in IE9.</p>
  
  <p><strong>What works:</strong></p>
  
  <p>So far, everything works except for the PDF posting a message to the browser.  I can post a message to the PDF, from the browser, no problem and all of the fields are prefilled as desired.</p>
  
  <p><strong>What doesn't work:</strong></p>
  
  <p>When I try using this.hostContainer.postMessage(["something","somethingmore"]) I get an Acrobat Escript window that says "hostContainer is not defined".  I have also tried using "event.target.hostContainer" but I get "event.target is not defined".  I am at a loss of what to do and any insight would be super helpful.</p>
  
  <p><strong>Reference links:</strong></p>
  
  <ol>
  <li><a href="http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/js_api_reference.pdf" rel="nofollow noreferrer">Acrobat Javascript API</a> </li>
  <li><a href="https://stackoverflow.com/questions/5934211/getting-notified-when-the-user-clicks-a-link-in-an-embedded-pdf">Stackoverflow How-To on this topic</a>  </li>
  <li><a href="http://www.javabeat.net/javascript-communication-between-html-and-pdf-in-itext/" rel="nofollow noreferrer">Original guide I used</a></li>
  </ol>
  
  <p><strong>The code:</strong></p>
  
  <p><em>My form view:</em></p>
  
  <pre><code>&lt;object id="pdfFrame" style="width:100%;height: 100%;" data="@Url.Action("LoadForm")"&gt;No luck :(&lt;/object&gt;
  </code></pre>
  
  <p><em>My custom javascript string method:</em></p>
  
  <pre><code>private static string GetCustomJavascript(string existingJavaScript)
      {
          const string newJs = 
          "this.disclosed = true; " +
          "if (this.external &amp;&amp; this.hostContainer) { " +
              "function onMessageFunc( stringArray ) { " +
  //                    "var name = this.myDoc.getField(personal.name); " +
  //                    "var login = this.myDoc.getField(personal.loginname); " +
                      "try{" +
                          "app.alert(doc.xfa);" +
                          "console.println('Doc xfa value = ' + doc.xfa);" +
  //                            "event.target.hostContainer.postMessage(['hello from pdf!']);" +
  //                        "this.hostContainer.postMessage(['hello from pdf!']);"+
  //                        "name.value = stringArray[0]; " +
  //                        "login.value = stringArray[1]; " +
                      "} catch(e){ onErrorFunc(e); } " +
                  "} " +
                  "function onErrorFunc( e ) { " +
                      "console.show(); " +
                      "console.println(e.toString()); " +
                  "} " +
                  "try {" +
                      "if(!this.hostContainer.messageHandler) { " +
                          "this.hostContainer.messageHandler = new Object(); " +
                          "this.hostContainer.messageHandler.myDoc = this; " +
                          "this.hostContainer.messageHandler.onMessage = onMessageFunc; " +
                          "this.hostContainer.messageHandler.onError = onErrorFunc; " +
                          "this.hostContainer.messageHandler.onDisclose = function(){ return true; }; " +
                      "}" +
                  "} catch(e){onErrorFunc(e);}" +
              "}";
              var jsToReturn = existingJavaScript + newJs;
              return jsToReturn;
          }
  </code></pre>
  
  <p><em>My method for filling and sending the form to the browser:</em></p>
  
  <pre><code>public MemoryStream GetFilledRequestForm(string fileDirectory, User user, FormView formView)
          {
              var pdfStream = new MemoryStream();
              var templateFilePath = GetRequestTypeTemplateFilePath(fileDirectory, _requestModule.FormTemplateFileName);
              var pdfReader = new PdfReader(templateFilePath);
  //            pdfReader.RemoveUsageRights();
              var stamper = new PdfStamper(pdfReader, pdfStream);
              var formFields = GetFormFields(user, formView, pdfReader);
              foreach (var field in formFields.Where(f =&gt; f.Value != null))
              {
                  stamper.AcroFields.SetField(field.Name, field.Value);
              }
              stamper.FormFlattening = false;
              var newJs =  GetCustomJavascript(stamper.Reader.JavaScript);
              stamper.AddJavaScript("newJs", newJs);
              stamper.Close();
              byte[] byteInfo = pdfStream.ToArray();
              var outputStream = new MemoryStream();
              outputStream.Write(byteInfo, 0, byteInfo.Length);
              outputStream.Position = 0;
              return outputStream;
          }
  </code></pre>