title: Adding document level JavaScript to PdfResultStamper using iTextSharp
body: <p>I am trying to add a document level JavaScript function to a PDF document using iTextSharp. I am adding the function to the document via a PdfStamper per some of the documentation I have found. The line in particular is:</p>
  
  <pre><code>_pdfResultStamper.JavaScript = GetJS();
  </code></pre>
  
  <p>The GetJS just returns a string of JavaScript that I copied from the prototype function I wrote in Acrobat Pro. This isn't the exact function but its close enough to get the idea of what I am doing:</p>
  
  <pre><code>public string GetJS()
  {
      return "function sayHello(fieldObj){" +
                   "app.alert(fieldObj.value);" +
              "}";
  }
  </code></pre>
  
  <p>When I open the generated PDF I do not see this function anywhere in the document JS (even when I choose "Edit All JavaScripts" in Acrobat). </p>
  
  <p>Later on I am adding several checkboxes to the PDF form and binding to their mouseup events to call sayHello. This code does get added to the PDF as expected.</p>
  
  <pre><code>                PdfFormField Field = checkbox.CheckField;
                  Field.Action = PdfAction.JavaScript("sayHello(event.target);", _pdfResultStamper.Writer);
  </code></pre>
  
  <p>However nothing happens (obviously) because the global/document level function is not present.</p>
  
  <p>How do I go about adding the sayHello function at the document level so all the checkboxes can call it? I have tried a couple of other approaches and nothing seems to write the global function to the resulting PDF.</p>