How to get number of printed pages using system.printing?

2.2k Views Asked by At

I am writing simple app which will monitor how many pages I print every day. I am using .Net and I found System.printing namespace which seems easy to use.

Below is my code.

public static void NumberOfPagesPrintedTest()
    {
        PrintServer ps = new PrintServer();

        PrintQueueCollection printQueues = ps.GetPrintQueues();

        foreach (var item in printQueues)
        {
            //if (item.QueueStatus == PrintQueueStatus.Printing)
            //{
                item.Refresh();
                PrintJobInfoCollection coll = item.GetPrintJobInfoCollection();
                if (coll != null)
                {
                    foreach (var jobinfo in coll)
                    {
                        Console.WriteLine(jobinfo.NumberOfPages + " printed " + jobinfo.NumberOfPagesPrinted + " printing " + jobinfo.IsPrinting + " printed " + jobinfo.IsPrinted);
                    }
                }
            //}
        }
    }

but during printing it shows number of pages printing, but it doesn't change after some pages are already printed and and jobinfo.NumberOfPagesPrinted always equals to 0. How to get the number of pages actually printed and how to find out how many pages were printed if printer is stopped by some reason.

2

There are 2 best solutions below

0
On

What about writing method as below for getting page count :-

public static int PrintedPageCount(PrintDocument printDocument)
{
    int counter = 0;
    printDocument.PrintController = new PreviewPrintController();
    printDocument.PrintPage += (sender, e) => counter ++;
    printDocument.Print();
    return counter;
}
0
On

You need to refresh your job (with job.refresh()) until the IsSpooling flag equals false.