Skip to content

How to get printing status in C#

Mohit Jagtap

Asked Jan 18, 2017, 6:53 PM EST1 answer
Share
LinkedInX
Hi i am Mohit,
 
i have my simple function to print the document like :
 
C#
public string PrintMyFile(string fileName)
{
     //which gets the default printer..
    PrintQueue printQueue = LocalPrintServer.GetDefaultPrintQueue();

     PrintSystemJobInfo printSystemJobInfo =        printQueue.AddJob(Path.GetFileNameWithoutExtension(fileName), fileName, false);

  string status =  printSystemJobInfo.JobStatus;
  return status;
}
 
Here in 'status' i am getting the status as 'spooling' but i need to get the status once the document is printed. How can i do that?
 I read about PrintEventArgs OnEndPrint()  but i dont know how to use it. Please guide me on this. 

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • +1
    For this problem you have to use PrintDocument object in  System.Drawing.Printing.PrintDocument.
    After declaring the PrintDocument object you will also create the three events of Print Document which are:
    Code
     _printDocument.BeginPrint += new PrintEventHandler(pd_BeginPrint);
                _printDocument.PrintPage += new PrintPageEventHandler(pd_PrintPage);
                _printDocument.EndPrint += new PrintEventHandler(pd_EndPrint);
     
    Sample Code
     
    public PrintDocument _printDocument;
    1. public PrintDocument _printDocument;  
    2.  _printDocument = new PrintDocument  
    3.             {  
    4.                 PrinterSettings = _settings,  
    5.                 DefaultPageSettings = { PrinterResolution = { X = 300, Y = 300 } }  
    6.             };  
    7.             _printDocument.BeginPrint += new PrintEventHandler(pd_BeginPrint);  
    8.             _printDocument.PrintPage += new PrintPageEventHandler(pd_PrintPage);  
    9.  _printDocument.EndPrint += new PrintEventHandler(pd_EndPrint);  
    10.     
    11.    private void pd_BeginPrint(object beginPrintSender, PrintEventArgs beginPrintE)  
    12.         {  
    13.            //Write your logic when printing starts  
    14.         }  
    15.    private void pd_PrintPage(object printPageSender, PrintPageEventArgs printPageE)  
    16.         {  
    17.            // write your logic while printing  
    18.         }  
    19.   
    20.   
    21. private void pd_EndPrint(object sender, PrintEventArgs endPrintE)  
    22.         {  
    23.             try  
    24.             {  
    25.               //write your logic when printing ends  
    26.   
    27.             }  
    28.             catch (Exception e)  
    29.             {  
    30.                   
    31.             }  
    32.         }  
    Code
     
    +1