Printing content from a webservice

Calling a RESTfull webservice, or requesting any document for printing is trivial - just give PrintHtml the url and it will download and print it.

As for printing dynamically created content 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.

We then call PrintHtml() with the required url.

Putting it all together:

// Print an html document
private void PrintDocument(string url)
{
   // 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";

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

      factory.Shutdown();

   }     
}

The print queue

The above code takes a "fire and forget" approach. The downloading of the content and printing is performed by a separate process so the application could exit and the print would still continue and complete.

If the PrintLabel() function were called many times then multiple instances of the print process would result with each one exiting when its print is complete and the garbage collector runs. This is not an issue in many scenarios where there are few prints. 

However, this could become an issue where many prints are required. The implementation of PrintHTML() uses a queue so many print jobs can be started and they will download and print in turn.

Again a "fire and forget" approach can be taken. The use of WaitForSpoolingComplete() is not recommended here as it could lead to re-entrancy in the application UI and a potential dead-lock. If you have a requirement to know when jobs are complete and/or the whole queue is complete we recommend you use PrintHTMLEx() instead of PrintHTML(). (The sample console application on GitHub illustrates using PrintHTMLEx)

Use in console application / service

Use in a console application / service follows the same pattern of apply the license to the process and use PrintHTML/PrintHTMLEx to queue printing of content - either downloaded or supplied as a dynamically created string.

When developing such applications it is required that the main thead has the [STAThread] attribute applied.

Please see the sample console application on GitHub for examples of using threads within the application and also various utility classes for wrapping access to the ScriptX objects.

 

::> Walkthrough :: Developing a WPF application with WebBrowser