0

I'm creating an application using C# and WinForm to control files being printed by the user. As soon as I print, I run the program with the following code. I have used the instuction shown in the link below to create this app:

private void OnJobStatusChanged(object Sender, PrintJobChangeEventArgs e)
        {
            MethodInvoker invoker = () =>
            {
                if (e.JobStatus == JOBSTATUS.JOB_STATUS_SPOOLING)
                {
                    bool res = PausePrintJob(printerList.Text.Trim(), e.JobID);
                    MessageBox.Show(res.ToString());
                }
                txtLogs.Text += e.JobID + " - " + e.JobName + " - " + e.JobStatus + '\n';
            };

            if (txtLogs.InvokeRequired)
            {
                Invoke(invoker);
            }
            else
            {
                invoker();
            }

        }

public static bool PausePrintJob(string printerName, int printJobID)
        {
            bool isActionPerformed = false;
            string searchQuery = "SELECT * FROM Win32_PrintJob";
            ManagementObjectSearcher searchPrintJobs =
                     new ManagementObjectSearcher(searchQuery);
            ManagementObjectCollection prntJobCollection = searchPrintJobs.Get();
            foreach (ManagementObject prntJob in prntJobCollection)
            {
                System.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 prnterName = jobName.Split(splitArr)[0];
                int prntJobID = Convert.ToInt32(jobName.Split(splitArr)[1]);
                string documentName = prntJob.Properties["Document"].Value.ToString();
                if (String.Compare(prnterName, printerName, true) == 0)
                {
                    if (prntJobID == printJobID)
                    {
                        prntJob.InvokeMethod("Pause", null);
                        isActionPerformed = true;
                        break;
                    }
                }
            }
            return isActionPerformed;
        }`

Now the problem with the program is that after the user gives the command to print, I sometimes need to print the pages in black and white and sometimes in color. I used the following command through powershell. However, once the page receives the print command, the result is unchanged.

Set-PrintConfiguration -PrinterName "PRINTER NAME" -Color $false //or 0

This command worked for new printable files. But the files ordered to print remain in the old settings. I need to change the page properties after giving the print command. I looked through the Win32_PrintJob API and other documentation, but I couldn't find a solution. Can anyone help?

Oktam Yaqubov
  • 113
  • 12

0 Answers0