Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was trying to get number of copies to be printed before printing using below source code
C#
class Program
    {
        static void Main(string[] args)
        {
            Printer p = new Printer();
            p.StartWatching("HP LaserJet MFP M725 PCL 6");
            c();
        }
        public static void c()
        {
            int counter = 0;
            string line;

            // Read the file and display it line by line.
            System.IO.StreamReader file = new System.IO.StreamReader("C:\\Users\\a1410043\\Documents\\important.txt");

            while ((line = file.ReadLine()) != null)
            {

                Console.WriteLine(line);

                counter++;

            }



            file.Close();

            // Suspend the screen.

            Console.ReadLine();

        }
    }

    class Printer
    {
        private PrinterMonitorComponent mPr;
        public static int a = 0;
        public void StartWatching(string PrinterDeviceName)
        {

            mPr = new PrinterMonitorComponent();

            mPr.MonitorPrinterChangeEvent = false;
           
            a = totalPages();
            mPr.JobAdded += Addedtest;
            // mPr.JobAdded += c;
            try
            {
                mPr.DeviceName = PrinterDeviceName;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message + "\n==================" + e.StackTrace);
            }
        }

        private void c(object sender, PrintJobEventArgs args)
        {

        }

        public static int totalPages()
        {
            List<PrintSystemJobInfo> jobInfos = new List<PrintSystemJobInfo>();


            LocalPrintServer server = new LocalPrintServer();
            try
            {
                // return server.DefaultPrintQueue.GetPrintJobInfoCollection().ElementAt(server.DefaultPrintQueue.GetPrintJobInfoCollection().Count()).NumberOfPages;
                return (server.DefaultPrintQueue.GetPrintJobInfoCollection().ElementAt(server.DefaultPrintQueue.GetPrintJobInfoCollection().Count() - 1).NumberOfPages);
            }
            catch
            {
                return -1;
            }
        }
        
        public void Addedtest(object sender, PrintJobEventArgs e)
        {
            List<PrintSystemJobInfo> jobInfos = new List<PrintSystemJobInfo>();


            LocalPrintServer server = new LocalPrintServer();


            e.PrintJob.Commit();

            Console.WriteLine("Total Pages: " + a * e.PrintJob.Copies);
            Console.WriteLine("No of Copies: " + e.PrintJob.Copies);
            Console.WriteLine("Printer Name: " + e.PrintJob.PrinterName);
            Console.WriteLine("User Name: " + e.PrintJob.UserName);
            Console.WriteLine("Document Name: " + e.PrintJob.Document);
            Console.WriteLine("Data  submitted: " + e.PrintJob.Submitted + "\n");
            //File.WriteAllText("C:\\Users\\a1410043\\Documents\\copyvalue.txt\\", e.PrintJob.Copies.ToString());
            using (StreamWriter writer =
        new StreamWriter("C:\\Users\\a1410043\\Documents\\important.txt"))
            {
                writer.Write(a * e.PrintJob.Copies);

            }
            ////

            ////






            e.PrintJob.Update();
        }

        
    }


What I have tried:

I could get number of copies using code but I cannot integrate it within windows service which consists wmi event watcher to watch management class specific property "Win32_PrintJob". The problem is that when I call StartWatching like


WqlEventQuery q;
C#
           ManagementScope scope = new ManagementScope("root\\CIMV2");
            scope.Options.EnablePrivileges = true;
try
            {
                q = new WqlEventQuery();
                q.EventClassName = "__InstanceCreationEvent";
                q.WithinInterval = new TimeSpan(1);
                q.Condition = @"TargetInstance ISA 'Win32_PrintJob'";
                
                w = new ManagementEventWatcher(scope, q);
                w.EventArrived += new EventArrivedEventHandler(PausePrinterManagement);
                
               w.EventArrived += new PrintJob<eventhandler>(this.StartWatching);
               
                w.Start();

            }
            catch (Exception e)
            {

                Console.WriteLine(e.Message);
                if (w != null)
                    w.Stop();
            }

I couldn/t get number of copies. Can anyone give me an instruction?
Thanks beforehand!
Posted
Updated 28-Jun-16 20:41pm
v2
Comments
Little@Knight 29-Jun-16 6:01am    
Can anybody have an idea about this?
jayveebishie 30-Jun-16 6:50am    
why not use System.Drawing.Printing.PrintDocument

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900