Saturday 5 October 2013

Monitor Printing Jobs in .Net

You may be in need of Monitoring different Print Jobs submitted to printer in our local network.

For that, PrintQueueWatch printer monitoring component is the one stop for you.

  • PrintQueueWatch is a .NET component (as class library) that facilitates monitoring one or more printers from a WinForms application and gathering information from the print system above and beyond that provided by the .NET Framework
Download dll files from there and add reference to your project.

And below is the code you need.


        public void StartWatching(string PrinterDeviceName)
        {
             PrintMonitor[] objPrint = new PrintMonitor[2];
             mPr = new PrinterMonitorComponent();
             mPr.MonitorPrinterChangeEvent = false;
             mPr.JobAdded += Addedtest;
             try
             {
                 mPr.AddPrinter(PrinterDeviceName);
             }
             catch (Exception e)
             {
                 clsErrorTrace.CreateLog("StartWatching", e);
             }
        }
        public void Addedtest(object sender, PrintJobEventArgs e)
        {
            try
            {   string str = "";
                str += "Printer Name: " + e.PrintJob.PrinterName + Environment.NewLine;
                str += "User Name: " + e.PrintJob.UserName + Environment.NewLine;
                str += "Document Name: " + e.PrintJob.Document + Environment.NewLine;
                str += "Total Pages Printed: " + e.PrintJob.TotalPages + Environment.NewLine;
                str += "Date Time: " + e.PrintJob.Submitted + Environment.NewLine;
            }
            catch (Exception ex)
            {
                clsErrorTrace.CreateLog("Addedtest", ex);
            }
        }
Here, you have to pass the Printer Name which you want to monitor for it's jobs.

Ex., I have one printer installed on my PC that is "HP Prolient 2022" then I will Monitor it by this:
StartWatching("HP Prolient 2022");

So "Addedtest" function will be called once any Job will be added and you will be able to get the details related to that job like Printer Name, Document Name, Total Pages Printed, etc.

2 comments:

  1. it's compilling without any errors but not functioning as u specified pls help me

    ReplyDelete
  2. Total Page method is giving wrong value

    ReplyDelete