0

I´m trying to print a PDF file into any physical printer using GhostScript.

I´m getting an black and white results at the printer.

So, reading the Ghostscript documentation I read about PostScript file. So I´m giving it a try (but without success).

Here´s my C# code:

public bool printGhostScript(string firstPage, string lastPage, string printerName, string pdfFileName)
    {
        int numberOfCopies = 1;
        string ghostScriptPath = @"C:\PrinterBatch\gs9.19\bin\gswin64.exe";
        string postScriptPath = @"C:\PrinterBatch\gs9.19\lib\setup.ps";
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.Arguments = " -dSAFER -dFirstPage=" + firstPage + " -dLastPage=" + lastPage + " " + postScriptPath + " -dBATCH  -dNOPAUSE -sFONTPATH=C:\\Windows\\Fonts -sPAPERSIZE=letter -sOutputFile=\"\\\\spool\\" + printerName + "\" \"" + pdfFileName + "\"  ";

        startInfo.FileName = ghostScriptPath;
        startInfo.UseShellExecute = false;
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;

        startInfo.RedirectStandardError = true;
        startInfo.RedirectStandardOutput = true;

        Process process = Process.Start(startInfo);

        process.WaitForExit(30000);
        if (process.HasExited == false)
            process.Kill();


        return process.ExitCode == 0;
    }

As we can see, I´m calling the command to print and setting my PostScript file called "setup.ps".

Here is the code of my Setup.ps:

mark 
/BitsPerPixel  4 
/NoCancel true 
(mswinpr2) finddevice 
putdeviceprops 
setdevice

The problem I´m getting is this:

Error: /invalidaccess in --setdevice-- Does anyone has an idea to help me?

I just need to print coloured and with quality.

Sorry for the bad english.

EDIT

I forgot to write the command line "/BitsPerPixel 4" at PostScript file. (Already fixed it).

According to the documentation this line is the magical one and makes everything print coloured and with quality.

The command line that is loaded at "arguments" is this:

-dSAFER -dFirstPage=1 -dLastPage=11 C:\PrinterBatch\gs9.19\lib\setup.ps -dBATCH  -dNOPAUSE -sFONTPATH=C:\Windows\Fonts -sPAPERSIZE=letter -sOutputFile="\\spool\PDFCreator" "C:\Users\JUNIOR\Desktop\_ABC.pdf"  

Before I started to use the PostScript (when I was able to print but only b/w) the command was like this:

-dSAFER -dFirstPage=1 -dLastPage=11 -sDEVICE=mswinpr2 -dBATCH  -dNOPAUSE -sFONTPATH=C:\Windows\Fonts -sPAPERSIZE=letter -sOutputFile="\\spool\PDFCreator" "C:\Users\JUNIOR\Desktop\_ABC.pdf"  
velloso
  • 17
  • 2
  • 5
  • Hmm I thought if you use like mswinpr** stuff you don't have to set an output file? Is it b/w if you omit your setup.ps too? Does it print in colour if you use something like acroread? – Stefan Hegny Jun 18 '16 at 20:46
  • We would need to see the fill command line being passed to Ghostscript to be able to help. Adding -sOutputFile to a device which doesn't accept it might cause an error but not the quoted error, the mswinpr2 device does accept an OutputFile parameter. Of course this is only a very small part of the PostScript error transcript, seeing the whole thing might help. Why on earth are you using the 'setup.ps' file ? Its much easier just to add -sDEVICE=mswinpr2 to the command line. It looks to me like this is the source of the problem. – KenS Jun 19 '16 at 11:10
  • @StefanHegny I first used the setup.ps because it was b/w. Don´t know what you mean by using "acroread". :( – velloso Jun 19 '16 at 22:48
  • @KenS the command with postscript is like this: `-dSAFER -dFirstPage=1 -dLastPage=11 C:\PrinterBatch\gs9.19\lib\setup.ps -dBATCH -dNOPAUSE -sFONTPATH=C:\Windows\Fonts -sPAPERSIZE=letter -sOutputFile="\\spool\PDFCreator" "C:\Users\JUNIOR\Desktop\_ABC.pdf"` Before I start to use the PostScript file it was like this: `-dSAFER -dFirstPage=1 -dLastPage=11 -sDEVICE=mswinpr2 -dBATCH -dNOPAUSE -sFONTPATH=C:\Windows\Fonts -sPAPERSIZE=letter -sOutputFile="\\spool\PDFCreator" "C:\Users\JUNIOR\Desktop\_ABC.pdf" ` I´ll edit the question to explain why I am using the setup.ps. – velloso Jun 19 '16 at 22:52
  • You can simply add -dBitsPerPixel=4 to the command line, instead of trying to use the setup.ps file. Since your error is 'invalidaccess in setdevice' and your setup.ps file ends in 'setdevice' I'd have to be suspicious that your problem is right there. – KenS Jun 20 '16 at 07:03
  • @KenS OH! You are a life saver. Now its coloured. But it it´s "fuzzy" and not filled at the paper. :( Take a look at the [image](http://i.stack.imgur.com/eTtd0.png). My new command is like this: `-dSAFER -dFirstPage=1 -dLastPage=3 -sDEVICE=mswinpr2 -sPAPERSIZE=a4 -dFIXEDMEDIA -dPDFFitPage -dPrinted=false -dBitsPerPixel=24 -dBATCH -dNOPAUSE -sFONTPATH=C:\Windows\Fonts -sPAPERSIZE=a4 -sPAPERSIZE=letter -sOutputFile="\\spool\doPDF 8" "C:\Users\JUNIOR\Desktop\_ABC.pdf"` I´d be glad if you help me. Thanks in advance. – velloso Jun 21 '16 at 00:07
  • Well, generally printers can't print right to the edge of the paper anyway, so I'm not totally surprised that it doesn't do so here. There is very little I can offer to help you. The way the mswinpr2 device works is to create a DeviceContext for printer, then render the input to a bitmap, Blit the image to the device context, and then tell the DeviceContext to print itself. So the fuzziness may be due to either the GS rendering, or the Windows printing (you would have to test GS to see which) and the printing of that image to the paper is down to the WIndows printer driver. – KenS Jun 21 '16 at 07:06
  • The fuzziness looks like JPEG artefacts, can't really comment without seeing the original PDF file. Do you realise you've put -sPAPERSIZ in there three times ? With two different values ? – KenS Jun 21 '16 at 10:33
  • In fact there are there times the -sPAPERSIZ, but I did it because of this tread [here](http://stackoverflow.com/questions/8141412/fit-to-page-size-in-ghostscript-with-a-possibly-corrupt-input/8152325#8152325) Anyway, my new command is this: `-sFONTPATH=C:\Windows\Fonts -dBitsPerPixel=24 -dFIXEDMEDIA -dPDFFitPage -dPrinted=false -sPAPERSIZE=a4 -sPAPERSIZE=letter -dFirstPage=1 -dLastPage=1 -sDEVICE=mswinpr2 -sOutputFile="\\spool\PDFCreator" "C:\Users\u6034244\Desktop\ABC.pdf" -dSAFER -dBATCH -dNOPAUSE`. – velloso Jun 21 '16 at 17:07
  • I printed it to a physical printer via Windows (right one)and via My Software (left one). I got this: http://i.stack.imgur.com/QMjVK.jpg. My software is printing bigger. :( – velloso Jun 21 '16 at 17:07
  • Still no example PDF to look at, its impossible to give any clue about what's going on without seeing the original – KenS Jun 30 '16 at 09:33

0 Answers0