This logic will print PDF documents, using GhostScript, without opening or using Adobe.
You will need to have Ghostscript installed on the local system for this to work
Finding a PDF Application
This finds the GhostScript exe application path
Create Pool of reg keys
This is the pool of different possibilities of registration keys, which will contain the path to the exe
Get path from the registration
Using data from the pool it will check the reg for the exe path, The ghostscript does not contain the value in the primary key, it within the sub key.
Once within the sub key, the path returned is the root path or dll. So I have done a small hack to the code which will return the exe file, if this pathway is hit (ONLY because, I know that the exe name is correct)
Get installer exe path
Loop through each item within the queue, to see if there is a reg record
Allocation controller method
This method controls the allocation logic
Printing the PDF document
This uses the Allocation logic to find the exe path and then it sends the document to the printer, without any popups
Create the Process start info object
Creates the ProcessStartInfo object, so GhostScript can print the pdf
Get the current default printer
This get the printer name for the default printer within windows
Run the process
Creates controls the creation of the process to send the PDF to the printer
You will need to have Ghostscript installed on the local system for this to work
This finds the GhostScript exe application path
This is the pool of different possibilities of registration keys, which will contain the path to the exe
private static void createRegistrationKey()
{
RegistrationKey = new Liststring, string >>();
RegistrationKey.Add(new Tuple<string , string >("Ghost", @"SOFTWARE\GPL Ghostscript"));
RegistrationKey.Add(new Tuple<string , string >("Ghost",@"SOFTWARE\AFPL Ghostscript"));
}
Using data from the pool it will check the reg for the exe path, The ghostscript does not contain the value in the primary key, it within the sub key.
Once within the sub key, the path returned is the root path or dll. So I have done a small hack to the code which will return the exe file, if this pathway is hit (ONLY because, I know that the exe name is correct)
private static Tuple<string ,string > getRegistryApplicationPath(string keyTitle, string applicationKey)
{
RegistryKey key = Registry.LocalMachine.OpenSubKey(applicationKey);
if (key != null)
{
if (key.ValueCount > 0)
{
return new Tuple<string , string >(keyTitle, key.GetValue("").ToString());
}
else if (key.SubKeyCount > 0)
{
foreach(var item in key.GetSubKeyNames())
{
var inner = key.OpenSubKey(item);
foreach(var valueInner in inner.GetValueNames())
{
string inner1 = inner.GetValue(valueInner).ToString();
if (!inner1.Contains(".exe") && !inner1.Contains(".dll") && inner1.Contains("GPLGS"))
{
//As the folder exists, I will just add the exe name, which I know it will be and return the full path
return new Tuple<string , string >(keyTitle, Path.Combine(inner1, "gswin32c.exe"));
}else if (inner1.Contains(".exe") || inner1.Contains(".dll"))
{
return new Tuple<string , string >(keyTitle, Path.Combine(inner1));
}
}
}
}
}
return new Tuple<string ,string >(String.Empty,String.Empty);
}
Loop through each item within the queue, to see if there is a reg record
private static void getInstalledApplicationPath()
{
foreach (var s in RegistrationKey)
{
allocatedPath = getRegistryApplicationPath(s.Item1, s.Item2);
if (!String.IsNullOrEmpty(allocatedPath.Item1))
{
break;
}
}
}
This method controls the allocation logic
public static Tuple<string , string > Allocate()
{
createRegistrationKey();
getInstalledApplicationPath();
return allocatedPath;
}
This uses the Allocation logic to find the exe path and then it sends the document to the printer, without any popups
Creates the ProcessStartInfo object, so GhostScript can print the pdf
private static void setProcess()
{
startInfo = new ProcessStartInfo();
startInfo.Arguments =
String.Format(
" -dPrinted -dBATCH -dNOPAUSE -dNOSAFER -q -dNumCopies=1 -sDEVICE=mswinpr2 -sOutputFile=\"\\\\spool\\{0}\" \"{1}\"",
printerName, printFileName);
startInfo.FileName = applicationPath;
startInfo.UseShellExecute = false;
}
This get the printer name for the default printer within windows
private static void getPrinterName()
{
PrintDialog pt = new PrintDialog();
printerName = pt.PrinterSettings.PrinterName;
}
Creates controls the creation of the process to send the PDF to the printer
public static void Print(string path, string exePath)
{
var allocation = PDFLibrary.ApplicationEXEPath.Allocate();
printFileName = path;
applicationPath = allocation.Item2;
getPrinterName();
setTracking();
if (!String.IsNullOrEmpty(applicationPath))
{
setProcess();
using (var process = Process.Start(startInfo))
{
Console.WriteLine("Process started");
Console.WriteLine("Process Id: " + process.Id.ToString());
Console.WriteLine("Process Machine name: " + process.MachineName.ToString());
Console.WriteLine("Wait for Process to exit");
process.WaitForExit();
Console.WriteLine( "Closing Process");
closeProcess(process);
}
reJoinThread();
}
else
{
MessageBox.Show(LanguageHelper.GetString("dlgPDFViewer.CantFindPDFApplication"), "Health OptionsĀ®", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private static void closeProcess(Process process)
{
try
{
process.CloseMainWindow();
process.Kill();
}
catch (Exception e)
{
}
}
private static void reJoinThread()
{
try
{
Console.WriteLine(""Try and re-join the Tracking thread");
trackingThread.Join();
Console.WriteLine(""Thread has ended");
}
catch (Exception e)
{
}
}
Created: 18/08/2016 Total Comment: 1
Comments
Hi,
no version of your posted code is in working condition. Parts are misformatted and other parts are missing. Both here and on codeproject.
But thanks for sharing !
regards
Dennis
no version of your posted code is in working condition. Parts are misformatted and other parts are missing. Both here and on codeproject.
But thanks for sharing !
regards
Dennis