We help IT Professionals succeed at work.

How to know if a document is printed successfully?

egyptotsuka
egyptotsuka used Ask the Experts™
on
I'm writing a software in C# for printing reports using Xtrareports.

I need to print each report just once and insert a record in a database indicating that the report has been printed so that it doesn't get printed again.

But the problem is if the printer was out of paper or had a paper jam or any physical problem, my software will not recognize this situation and will consider the report as printed and I won't be able to print it again.

Is there a method to get feedback from the printer indicating the status of printing process to make sure the report is printed successfully?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Anuradha GoliSystems Development / Support Specialist

Commented:
   #region GetPrintJobsCollection

        /// <summary>
        /// Returns the jobs in printer queue
        /// </summary>
        /// <param name="printerName">Printer Name.</param>
        /// <returns>StringCollection</returns>
        public StringCollection GetPrintJobsCollection(string printerName)
        {
            StringCollection printJobCollection = new StringCollection();
            try
            {
                //Query the printer to get the files waiting to print.
                string searchQuery = "SELECT * FROM Win32_PrintJob";

                ManagementObjectSearcher searchPrintJobs = new ManagementObjectSearcher(searchQuery);
                ManagementObjectCollection prntJobCollection = searchPrintJobs.Get();


                foreach (ManagementObject prntJob in prntJobCollection)
                {
                    String jobName = prntJob.Properties["Name"].Value.ToString();

                    //Job name would be of the format [Printer name], [Job ID]
                    char[] splitArr = new char[1];
                    splitArr[0] = Convert.ToChar(",");
                    string prnName = jobName.Split(splitArr)[0];
                    string documentName = prntJob.Properties["Document"].Value.ToString();
                    if (String.Compare(prnName, printerName, true) == 0)
                    {
                        printJobCollection.Add(documentName);
                    }
                }
            }
            catch (Exception ex)
            {
                // Log the exception.
            }
            return printJobCollection;
        }

        #endregion GetPrintJobsCollection

Open in new window


Add reference system.management from .Net dlls.
Systems Development / Support Specialist
Commented:
The following code displays all properties for a given printer name. Among them you will find: PrinterStatus, Comment, Location, DriverName, PortName, etc.
string printerName = "YourPrinterName";
string query = string.Format("SELECT * from Win32_Printer WHERE Name LIKE '%{0}'", printerName);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection coll = searcher.Get();

foreach (ManagementObject printer in coll)
{
    foreach (PropertyData property in printer.Properties)
    {
        Console.WriteLine(string.Format("{0}: {1}", property.Name, property.Value));
    }
}

Open in new window

Author

Commented:
Thank you very much anuradhay. It's an excellent solution.

Author

Commented:
The result of the query give many properties including: DetectedErrorState, PrinterStatus, Status and so on. Which one is most precise detecting the status of the printer?