Printing webbrowser control content

For controlled printing of content displayed within the web browser control, the techniques discussed for basic use may be employed; using the ScriptX object declared within the HTML page or a ScriptX object injected into a page.

The presence of the license allows all the basic and advanced methods and properties and properties to be used. For example:

///
/// PrintOrPreview the document displayed in the WebBrowser.
/// Adds ScriptX to the page if required, or uses it if already there
/// 
private void PrintOrPreview(PrintOperation operation)
{
var document = (IHTMLDocument3) WebBrowser.Document;

// 'de-facto' id is 'factory'
var factoryElement = document.getElementById("factory");

// does the factory object exist?
if (factoryElement == null)
{
   // If not then create it.
   ((IHTMLDocument2)WebBrowser.Document).body.insertAdjacentHTML("beforeEnd",
       factoryObjectHtml);

   factoryElement = document.getElementById("factory");
}

if (factoryElement != null)
{
   var factoryObject = (IHTMLObjectElement) factoryElement;

   // an element 'factory' exists, but is the object loaded (it may not be installed)?
   ScriptX.Factory factory = (ScriptX.Factory) factoryObject.@object;
   if (factory != null)
   {
      ScriptX.printing printer = factory.printing;
      printer.header = this.Title;
      printer.footer = "&D&b&b&P of &p";

      // Advanced features .. make sure we are using the required prnter and 
      // and paper ..
      printer.CurrentPrinter = "\\\\PrinterServer\\LabelPrinter";
      printer.PaperSize = "A5";

      switch (operation)
      {
         case PrintOperation.Print:
            printer.Print(false); // prompt will only be obeyed on intranet
            break;

         case PrintOperation.Preview:
            printer.Preview();
            break;
      }
   }
   else
   {
      MessageBox.Show("Unable to find or create MeadCo ScriptX."
           "\n\nIs MeadCo ScriptX installed?",this.Title);
   }
}
}

 

::> Printing dynamically created content