Printing dynamically created content

In this scenario, there is no WebBrowser control from which we can obtain a ScriptX Factory or inject one, so we need to new a Factory in the code:

var factory = new ScriptX.Factory();
if ( factory != null )
{
   // get the printing object and configure required parameters
   ScriptX.printing printer = factory.printing;

   ...
}

The printer object can be configured with headers, footers, the required printer, papersize etc in the usual way.

To create html on the fly, simply build the complete html as a string (it should be well formed, but given the flexibility of the Windows Web Browsing Platform you can get away with quite a lot). The built html can reference images and external css files but all such references must be absolute since there is no base url for the document (unless you include a definition within the html string).

Printing

Having configured the printer and built the content to print it simply remains to pass that string in with the psuedo protocol html:// to PrintHtml().

Putting it all together:

// Print a very simplistic label with the persons name on it
private void PrintLabel(string PersonName)
{
   // license the app so we can use advanced features
   if ( ApplyScriptXLicense() ) 
   {
      // create a scriptx factory
      var factory = new ScriptX.Factory();

      if ( factory != null )
      {
         // get the printing object and configure required parameters
         ScriptX.printing printer = factory.printing;

         printer.header = this.Title;
         printer.footer = "&D&b&b&P of &p";

         // use some advanced features ...
         printer.SetMarginMeasure(2); // set units to inches
         printer.leftMargin = 1.5f;
         printer.topMargin = 1;
         printer.rightMargin = 1;
         printer.bottomMargin = 1;

         // select the printer and paper size.
         printer.printer = "Microsoft XPS Document Writer";
         printer.paperSize = "A4";

         // build the html to print
         StringBuilder sHtml = new StringBuilder("html://");
         sHtml.Append("<!DOCTYPEe html><html><head>
            <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">
            <title>ScriptX Sample</title></head><body>");
         sHtml.Append("<table border=\"0\" width=\"100%\" height=\"100%\"><tr>");
         sHtml.Append("<td align=\"center\">");
         sHtml.Append("<h1>ScriptX Printing of HTML</h1><p>This label is for:</p><h2>");
         sHtml.Append(PersonName);
         sHtml.Append("</h2><p>and was printed on:</p><h4>");
         sHtml.Append(System.DateTime.Now.ToLongDateString());
         sHtml.Append("</h4></td></tr></table></body></html>");

         // and print it -- this job will be queued and printed
         // in an external process.
         printer.PrintHTML(sHtml.ToString(),0);
      }

      factory.Shutdown();

   }     
}

 

::> Printing content from a webservice