0

I've got this code:

 using (GhostscriptProcessor processor = new GhostscriptProcessor())
                {
                    List<string> switches = new List<string>();
                    if (!printSettings.DefaultPageSettings.Color)
                    {
                        switches.Add("-sProcessColorModel=DeviceGray");
                        switches.Add("-sColorConversionStrategy=Gray");
                        switches.Add("-dOverrideICC");
                    }
                    switches.Add("-empty");
                    switches.Add("-dPrinted");
                    switches.Add("-dBATCH");
                    switches.Add("-dNOPAUSE");
                    switches.Add("-dNOSAFER");
                    switches.Add("-dNumCopies=" + printSettings.Copies.ToString());
                    switches.Add("-sDEVICE=mswinpr2");
                    switches.Add("-dDEVICEWIDTHPOINTS="+printSettings.DefaultPageSettings.PaperSize.Width.ToString());
                    switches.Add("-dDEVICEHEIGHTPOINTS="+printSettings.DefaultPageSettings.PaperSize.Height.ToString());
                    switches.Add("-dPDFFitPage");
                    switches.Add("-sOutputFile=%printer%" + printSettings.PrinterName);
                    switches.Add("-f");
                    switches.Add(filePath);

                    processor.StartProcessing(switches.ToArray(), null);
                }

And the switches supposedly used for printing in grayscale I got from this question, however they are not working. Is there a way to print a PDF to printer in grayscale using Ghostscript?


Update

Updated code to write convert pdf to grayscale before sending to printer:

/*Call to print PDF*/
private void pdfPrint(String filePath) {
    try {

        using (GhostscriptProcessor processor = new GhostscriptProcessor())
        {
            List<string> switches = new List<string>();
            if (!printSettings.DefaultPageSettings.Color)
            {
                filePath = pdfRenderBlackWhite(filePath);
                if (!File.Exists(filePath))
                {
                    return;
                }
            }

            switches.Add("-empty");
            switches.Add("-dPrinted");
            switches.Add("-dBATCH");
            switches.Add("-dNOPAUSE");
            switches.Add("-dNOSAFER");
            switches.Add("-dNumCopies=" + printSettings.Copies.ToString());
            switches.Add("-sDEVICE=mswinpr2");
            switches.Add("-dDEVICEWIDTHPOINTS="+printSettings.DefaultPageSettings.PaperSize.Width.ToString());
            switches.Add("-dDEVICEHEIGHTPOINTS="+printSettings.DefaultPageSettings.PaperSize.Height.ToString());
            switches.Add("-dPDFFitPage");
            switches.Add("-sOutputFile=%printer%" + printSettings.PrinterName);
            switches.Add("-f");
            switches.Add(filePath);

            processor.StartProcessing(switches.ToArray(), null);
        }

    } catch(Exception e) {
        MessageBox.Show(e.GetType().ToString() + ' ' + e.StackTrace + '\n' + e.Message);
    }
}

private string pdfRenderBlackWhite(string filePath)
{
    String bwPDFFilePath = tempdir + "\\" + Path.GetFileName(filePath) + DateTime.Now.ToString("yyyyMMddhhmmssfff") + ".PDF";

    try
    {
        using (GhostscriptProcessor processor = new GhostscriptProcessor())
        {
            List<string> switches = new List<string>();
            switches.Add("-empty");
            switches.Add("-dPrinted");
            switches.Add("-dBATCH");
            switches.Add("-dNOPAUSE");
            switches.Add("-dNOSAFER");
            switches.Add("-sProcessColorModel=DeviceGray");
            switches.Add("-sColorConversionStrategy=Gray");
            switches.Add("-dOverrideICC");
            switches.Add("-sDEVICE=pdfwrite");
            switches.Add("-o");
            switches.Add(bwPDFFilePath);
            switches.Add("-f");
            switches.Add(filePath);
            processor.StartProcessing(switches.ToArray(), null);
        }

        return bwPDFFilePath;
    }
    catch (Exception e)
    {
        MessageBox.Show(e.GetType().ToString() + ' ' + e.StackTrace + '\n' + e.Message);
        return bwPDFFilePath;
    }
}
Community
  • 1
  • 1
MDMoore313
  • 3,233
  • 1
  • 23
  • 38

1 Answers1

1

You've picked a question to copy the answer from which is not appropriate; the question is specific to the pdfwrite device, which does not do any rendering, and you need rendered output. The 'ColorConversionStrategy' switch only works on the pdfwrite device (along with aa whole host of other PDF-specific switches)

I believe that the mswinpr2 device doesn't care what you set as the ProcessColorModel, it always works in RGB. If the printer is monochrome or gray scale then the Windows print system takes care of the conversion (just as it does for CMYK).

So what you need to do is convert the input to gray scale first, and you can use the controls from the previous question you found to create a gray scale representation of the original PDF file which you can then print.

Or if your printer supports being told to print in grayscale, then you can set it that way from the print dialog.

KenS
  • 30,202
  • 3
  • 34
  • 51
  • Ugh, so it would be a 2 step process then, but it's possible? I was afraid of that, but at least it's possible, thanks. – MDMoore313 Oct 22 '16 at 13:08
  • My worry would be that, if the intermediate is always RGB, it might be that a colour printer (especially a CMYK colour printer) would still print in 'colour'. That's because the conversion gray->RGB->CMYK often does not result in 100% black, it can produce a mixture of CMY and K. But it depends so much on the colour management that its not possible to be sure. Best to just try it and see. – KenS Oct 22 '16 at 13:21
  • I know what you mean, there are similar questions on SO and they report the printer still using the color ink to print in grayscale. I'm coding it right now so we'll see. – MDMoore313 Oct 22 '16 at 13:24
  • That did the trick, from what I can tell it still uses CMYK to produce gray, but this should be fine for now. Thanks. – MDMoore313 Oct 22 '16 at 14:55