简体   繁体   中英

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

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.

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;
}

您需要刷新您的工作(使用job.refresh() ),直到IsSpooling标志等于false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM