0

I am printing some PDF files through c# code. My code looks like this

myPrinters.SetDefaultPrinter(pdfPrinterName);
var info = new ProcessStartInfo
{
  Verb = "Print",
  FileName = file,
  CreateNoWindow = true,
  WindowStyle = ProcessWindowStyle.Hidden
};

var p = new Process { StartInfo = info };
p.Start();                 
p.WaitForInputIdle();
System.Threading.Thread.Sleep(18000);
if (!p.CloseMainWindow())
  p.Kill();

Print works perfect, But here i don't have any way to know whether print was success or failure.Like there must be cases where user selected wrong printer which won't work, things like that. How can effectively manage these scenarios and know whether my print command was success or not?

4
  • 3
    What about the Process exit code, if it equals zero its a success? msdn.microsoft.com/en-us/library/… Commented Jun 17, 2015 at 9:16
  • 1
    @SelectDistinct the process will finish as soon as the print job is queued, it won't even wait for printing to start. It's the same as selecting "Print" from a pdf file's context menu Commented Jun 17, 2015 at 9:25
  • An added problem is that the process may return with a zero code because it has successfully added the print job to the Print Job Queue - which does not mean it has successfully printed.
    – PaulF
    Commented Jun 17, 2015 at 9:26
  • @SelectDistinct Can you add an answer with proper code?
    – None
    Commented Jun 17, 2015 at 10:05

1 Answer 1

4

This code doesn't actually print something, it's using the Shell to send a Print command to whichever program the user has installed to handle pdfs. This may or may not be Adobe Acrobat. The process will finish as soon as the verb is sent. It won't even wait until the file is added to a print queue.

Printer management is achieved by using the classes in the System.Printing namespace. The PrintServer class represents an actual printer and PrintQueue represents a print queue on that printer. Calling PrintQueue.GetPrintJobInfoCollection will return all jobs on a printer.

Once you find the relevant PrintSystemJobInfo object (by checking the Name or JobName properties) you can check the job's status with the IsCompleted, IsBlocked etc properties. To detect changes you should call Refresh on the job periodically

You could try to find the job with something like the following method, created by copying from the various documentation samples:

public bool TryFindJob(string searchString,out PrintSystemJobInfo actualJob)
{
    var myPrintServer = new PrintServer(@"\\theServer");

    actualJob=null;
    var myPrintQueues = myPrintServer.GetPrintQueues();
    foreach (PrintQueue pq in myPrintQueues)
    {
       pq.Refresh();
       var jobs = pq.GetPrintJobInfoCollection();
       foreach (PrintSystemJobInfo job in jobs)
       {
            if (job.JobName.Contains(searchString) || job.Name.Contains(searchString))
            {
                actualJob=job;
                return true;
            }
        }
    }
    return false;
}

Assuming the job's name will contain the file's name, you can find the job :

var name=Path.GetFileNameWithoutExtension(file);
PrintSystemJobInfo job;
if(TryFindJob(name,out job))
{
   while(...)
   {
        job.Refresh();
        if (job.IsCompleted)
        {
            break;
        }
        else
        {
            //Sleep and retry
        }

}

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .