How to print PDF on default network printer using GhostScript (gswin32c.exe) shell command
I've finally made it working and easy for debugging.
My final method code for those interested:
/// <summary>
/// Prints the PDF.
/// </summary>
/// <param name="ghostScriptPath">The ghost script path. Eg "C:\Program Files\gs\gs8.71\bin\gswin32c.exe"</param>
/// <param name="numberOfCopies">The number of copies.</param>
/// <param name="printerName">Name of the printer. Eg \\server_name\printer_name</param>
/// <param name="pdfFileName">Name of the PDF file.</param>
/// <returns></returns>
public bool PrintPDF (string ghostScriptPath, int numberOfCopies, string printerName, string pdfFileName) {
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.Arguments = " -dPrinted -dBATCH -dNOPAUSE -dNOSAFER -q -dNumCopies=" + Convert.ToString(numberOfCopies) + " -sDEVICE=ljet4 -sOutputFile=\"\\\\spool\\" + printerName + "\" \"" + pdfFileName + "\" ";
startInfo.FileName = ghostScriptPath;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardOutput = true;
Process process = Process.Start(startInfo);
Console.WriteLine( process.StandardError.ReadToEnd() + process.StandardOutput.ReadToEnd() );
process.WaitForExit(30000);
if (process.HasExited == false) process.Kill();
return process.ExitCode == 0;
}
printing pdf using ghostscript via IIS
for this i got an work around. the post i did here for the different logic. holds goood for this requirement also.
html or pdf printing on server side c#
Regards,
Pavan N
GhostScript does not print PDF file to a real printer correctly, but works fine with PDFCreator
It turns out to be an issue with the soft mask of the PDF file itself. This issue has been resolved in version 9.16 of GhostScript.
Batch printing PDF files on Ricoh MP 4000 printer using Ghostscript without Adobe
Ghostscript doesn't have a 'jobtype' switch, nor a suserid switch, so its not surprising that they have no effect. Possibly the post you reference has some more information, but I can't find any such post, maybe you can point to it (URL)
[later]
Setup.ps needs to be a PostScript file (because that's what Ghostscript understands, its a PostScript interpreter). From the documentation you want to set DocumentName, which is a member of the User Settings dictionary, so:
mark
/UserSettings <<
/DocumentName (My name goes in here)
>>
(mswinpr2) finddevice
putdeviceprops
setdevice
The white space is just for clarity. This is lifted pretty much verbatim from the example.
So, you need to modify the 'setup.ps' you send for each job. You can either write a custom setup.ps for each one, or use the 'PostScript input' capability of GS and supply the whole content of setup.ps on the command line using the -c and -f switches. All setup.ps does is run the PostScript in there before you run your own PostScript program (assuming you put setup.ps before your PostScript program on the command line).
This is pretty nasty actually, its not the way devices are normally configured, but the mswinpr2 device was originally written by someone outside the Ghostscript team, and presumably adopted wholesale.
How can I send a file document to the printer and have it print?
You can tell Acrobat Reader to print the file using (as someone's already mentioned here) the 'print' verb. You will need to close Acrobat Reader programmatically after that, too:
private void SendToPrinter()
{
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "print";
info.FileName = @"c:\output.pdf";
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process p = new Process();
p.StartInfo = info;
p.Start();
p.WaitForInputIdle();
System.Threading.Thread.Sleep(3000);
if (false == p.CloseMainWindow())
p.Kill();
}
This opens Acrobat Reader and tells it to send the PDF to the default printer, and then shuts down Acrobat after three seconds.
If you are willing to ship other products with your application then you could use GhostScript (free), or a command-line PDF printer such as http://www.commandlinepdf.com/ (commercial).
Note: the sample code opens the PDF in the application current registered to print PDFs, which is the Adobe Acrobat Reader on most people's machines. However, it is possible that they use a different PDF viewer such as Foxit (http://www.foxitsoftware.com/pdf/reader/). The sample code should still work, though.
Related Topics
How to Format Timespan in Xaml
How to Convert an Array of Floats to a Byte[] and Back
Is Casting the Same Thing as Converting
Why Is This Code Invalid in C#
Adding Elements to an Xml File in C#
How to Test Smtpclient Before Calling Client.Send()
Unity3D Ui, Calculation for Position Dragging an Item
Getting Hwnd Off of Corewindow Object in Uwp
Httpcontext.Current.Session Is Null in Ashx File
Why Won't Anyone Accept Public Fields in C#
Exporting Datagridview to CSV File
Is There a .Net Way to Enumerate All Available Network Printers
Lambda Expressions in Immediate Window for VS2015
Testing Equality of Arrays in C#