Search code examples
c#printingsystem.drawing

Retrieve documents printed from printer using c#


This days I've been developing a service that sends a PDF file to a network printer and effectively print it using C# and the GhostscriptProcessor library.

But now I'm really stuck with the next (and last) step I want to make. I need to know wether or not the file was really printed. I tried everything I could (for instance I tried implementing this powershell script but I'm not familiar at all with powershell and I got depressed since I got too many errors I can't solve) but I can't find the answer.

Is there ANY way using C# (any library) to retrieve if a document has been printed. Or retrieve the whole log of documents printed? Any script I can call through C# (or not, I am able to circumvent that) that tells me the information I need?

I'd like to add that I have access to the printer using the System.Drawing.Printing library as follows:

var printServer = new PrintServer();
var myPrintQueues = printServer.GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Local, EnumeratedPrintQueueTypes.Connections });
foreach (PrintQueue pq in myPrintQueues)
    {
        pq.Refresh();
        string printerName = "ES7470 MFP(PCL)";
        if (!pq.Name.ToUpper().Contains(printerName.ToUpper())) break;
        PrintJobInfoCollection jobs = pq.GetPrintJobInfoCollection();
        //And here I can use pq or jobs but I can't retrieve the log at all.
    }

Solution

  • I think the problem is the PrintServer, I'm not in your environment so I cant tell your setup but LocalPrintServer.GetPrintQueue should do the trick.

    string printerName = "ES7470 MFP(PCL)";
    LocalPrintServer localPrintServer = new LocalPrintServer();
    PrintQueueCollection printQueues = localPrintServer.GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Local, EnumeratedPrintQueueTypes.Connections });
    
    if (printQueues == null) return false;
    
    PrintQueue queue = printQueues.Where(x => x.Name.Equals(printerName)).FirstOrDefault();
    

    I'm guessing your application will know if someone printed something, otherwise you'll have to poll the Printer Queue each second or two to find out if a job went to the printer...