2

I am trying to build a system that will download formatted text from a web server, print the formatted text, confirm that the print job completed successfully and then respond to the web server to let it know the text was printed. All without user input.

I have had success using the Web Browser control to download HTML and then print it without needing user input. This falls short, however, on the ability to confirm the printing.

It looks like in System.Printing you can access a PrintServer and a PrintQueue and use that both to start print jobs and also find the status of print jobs.

I haven't yet been able to confirm a print job, but I have been able to initiate simple printing. However, this does not carry any of the HTML formatting from the web server. I'm not tied to HTML, but it has to be some formatting that can be produced by the web server so it can be altered without needing to update the client application.

How can I print the output from the web server, properly formatted, and know whether the print job succeeds or fails?

CLo
  • 3,650
  • 3
  • 26
  • 44
  • These two links [(SO)Confirm successful print programmatically in windows](http://stackoverflow.com/questions/1921487/confirm-successful-print-programmatically-in-windows) and [(C#.NET)Print Job Status](http://tech.groups.yahoo.com/group/CSharpNET/message/20223) might give you enough clues. – Attila Mar 26 '12 at 15:23
  • @Attila Thanks for the links, though if you look at System.Printing it gives you access to the PrintServer, PrintQueue and PrintSystemJobInfo. The latter having several different status properties. So, I'm expecting there to be a fully .Net approach to that part. – CLo Mar 26 '12 at 21:07
  • The [docs](http://msdn.microsoft.com/en-us/library/system.printing.printsystemjobinfo.iscompleted.aspx) indicate you can access PrintSystemJobInfo.isCompleted, which seems to be what you are after. [This link](http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/4913e5e8-ecb1-4b41-8b5d-a58b01689ad1/) might give you more ideas (some of it is in VB.NET, though, but should not be too hard to convert to C#) – Attila Mar 26 '12 at 21:16
  • You mentioned that you've had success using the `web browser` control to download the HTML. Does that mean you are willing to use a windows form approach? – Reinaldo Mar 27 '12 at 13:30
  • @Reinaldo I'm open to any suggestions at this point. The .Net web browser object has a `Print()` method that handles printing HTML without user input. The issue there is that there is no information beyond that about if the print was successful or even when it finished. – CLo Mar 28 '12 at 03:08
  • I assume this is the case, but want to double-check, that you need to generate the content dynamically? And that this is not something that can be accomplished just by hosting files, correct? – Mike Guthrie Mar 29 '12 at 18:56
  • @GuthMD Yes, the process is basically, 1. get the latest order placed on the web. 2. print it 3. if print successful mark it as received. – CLo Mar 29 '12 at 23:47

1 Answers1

1

I'm assuming that you are willing to use the WebBrowser control. Here is a solution to confirm the printing. Basically you need to handle the PrintTemplateTeardown event to wait for the print job to complete.

The following is a sample code extracted from answer in: Print html document from Windows Service without print dialog

using System.Reflection;
using System.Threading;
using SHDocVw;

namespace HTMLPrinting
{
  public class HTMLPrinter
  {
    private bool documentLoaded;
    private bool documentPrinted;

    private void ie_DocumentComplete(object pDisp, ref object URL)
    {
      documentLoaded = true;
    }

    private void ie_PrintTemplateTeardown(object pDisp)
    {
      documentPrinted = true;
    }

    public void Print(string htmlFilename)
    {
      documentLoaded = false;
      documentPrinted = false;

      InternetExplorer ie = new InternetExplorerClass();
      ie.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(ie_DocumentComplete);
      ie.PrintTemplateTeardown += new DWebBrowserEvents2_PrintTemplateTeardownEventHandler(ie_PrintTemplateTeardown);

      object missing = Missing.Value;

      ie.Navigate(htmlFilename, ref missing, ref missing, ref missing, ref missing);
      while (!documentLoaded && ie.QueryStatusWB(OLECMDID.OLECMDID_PRINT) != OLECMDF.OLECMDF_ENABLED)
        Thread.Sleep(100);

      ie.ExecWB(OLECMDID.OLECMDID_PRINT, OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, ref missing, ref missing);
      while (!documentPrinted)
        Thread.Sleep(100);

      ie.DocumentComplete -= ie_DocumentComplete;
      ie.PrintTemplateTeardown -= ie_PrintTemplateTeardown;
      ie.Quit();
    }
  }
}

You can also reference: https://jiangsheng.net/2021/03/24/how-to-determine-when-a-page-is-done-printing-in-webbrowser-control/

Hope it helps!

Sheng Jiang 蒋晟
  • 15,125
  • 2
  • 28
  • 46
Reinaldo
  • 4,556
  • 3
  • 24
  • 24
  • I'm gathering that the .Net WebBrowser doesn't have the PrintTemplateTearDown Event while the Windows Forms version does? – CLo Mar 28 '12 at 03:12
  • Actually the example above doesn't use the WebBrowser control. Instead it creates an instance of the `InternetExplorer` Object to load and print the file. You can use `ie.Visible = false` if you prefer not to make the IE instance invisible. You can read more about the `InternetExplorer Object` here: http://msdn.microsoft.com/en-us/library/aa752084(v=vs.85).aspx – Reinaldo Mar 28 '12 at 13:17
  • I'm working on getting this to work, I'm getting an error on `InternetExplorer ie = new InternetExplorerClass();` **Interop type 'SHDocVw.InternetExplorerClass' cannot be embedded. Use the applicable interface instead.** I'm digging now for an answer, but any thoughts? – CLo Mar 31 '12 at 20:14
  • http://blogs.msdn.com/b/mshneer/archive/2009/12/07/interop-type-xxx-cannot-be-embedded-use-the-applicable-interface-instead.aspx For now I turned off the 'Embed Interop Types' option and it's not giving me the error. – CLo Mar 31 '12 at 20:17
  • It's connecting and printing what it gets, but unfortunately now it's having trouble connecting to the web site because it's using an unauthenticated certificate. So it's printing a security error. **Opened a separate question on the issue: http://stackoverflow.com/questions/9959421/how-to-automatically-accept-security-certificates-when-using-internet-explorer-o** – CLo Mar 31 '12 at 20:33
  • Got this working by testing on a different site. It looks like the PrintTemplateTearDown is called when the print job enters the Queue. With my printer off it still calls PrintTemplateTearDown, so it doesn't tell me that the print job succeeded or failed. – CLo Mar 31 '12 at 20:38
  • You are correct. The `PrintTemplateTearDown` is called after the print job is sent to the printer. If you really want to make sure the print job is completed successfully I recommend you do so by using the Win32 spooler functions. See: http://support.microsoft.com/kb/322091 . Hope it helps. – Reinaldo Mar 31 '12 at 22:04
  • Unfortunately what that specifically is covering looks like a step backwards. I'd have to ensure what I was sending to the printer was converted from HTML to raw data the printer understands, and I don't see info on there about confirming the success of the print, only the success of sending the data to the printer. – CLo Apr 01 '12 at 16:21
  • Perhaps that's not a good example. Like I said, the only idea that comes to mind is by monitoring the spooler. Here is another example: http://blogs.msdn.com/b/martijnh/archive/2009/08/05/printmonitor-a-c-print-spooler-monitor.aspx?wa=wsignin1.0 and one more using WMI instead to monitor the printer jobs: http://www.codeproject.com/Articles/6592/A-simple-approach-for-controlling-print-jobs-using You can try querying the print jobs status and see if yours printed successfully. Hope it helps. – Reinaldo Apr 01 '12 at 20:06
  • I'm awarding the bounty to the answer so it doesn't get lost, but still not quite there. I've read over what you have there, but I'm going to try to revise a previous method. The .Net System.Printing looks to have access to the print servers, queues and jobs. I'm working under the assumption that if I can manage the print confirmation that way, that it should be easier to work a different solution for formatting. **Summary: Use .Net for Print Confirmation, find some other way to format the PrintJob** – CLo Apr 01 '12 at 20:41