简体   繁体   中英

Print a PDF to Printer in Grayscale Using Ghostscript and .NET

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;
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM