Print PDF File on Network printer using c#
Here i'm going to explain how to print PDF File on Network Printer using c# .
you can develop windows as well as Console Application to test printing.
Step 1
Print PDF File using Adobe Reader.
Download and install adobe reader in which system your application will execute and give complete path while printing as-
C:\Program Files (x86)\Adobe\Reader 9.0\Reader\AcroRd32.exe
We write code as -
Code 1.1
string FilePath = @"E:\abc.pdf";
PrinterProcessName = "TEC B-EV4 (203 dpi)";
string pdfArguments = string.Format("/n /s /o /h /p /t " + "\"" + FilePath + "\" " + "\"" + PrinterProcessName + "\"");
string pdfPrinterLocation = @"C:\Program Files (x86)\Adobe\Reader 9.0\Reader\AcroRd32.exe";
ProcessStartInfo newProcess = new ProcessStartInfo(pdfPrinterLocation, pdfArguments);
newProcess.CreateNoWindow = true;
newProcess.RedirectStandardOutput = true;
newProcess.UseShellExecute = false;
Process pdfProcess = new Process();
pdfProcess.StartInfo = newProcess;
pdfProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
pdfProcess.Start();
Thread.Sleep(1000);
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
Code 1.2
string PathToExecutable = @"C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe";
var PrintFileToPrinter = string.Format("/t \"{0}\" \"{1}\"", txtfilepath.Text, txtPrinterName.Text);
var args = string.Format("{0}", PrintFileToPrinter);
Process process = new Process();
process.StartInfo.FileName = PathToExecutable;
process.StartInfo.Arguments = args;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.ErrorDialog = false;
process.StartInfo.UseShellExecute = false;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start();
Code 1.3
Process proc = new Process();
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo.Verb = "print";
proc.StartInfo.FileName =@"C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe";
//proc.StartInfo.Arguments = String.Format(@"/p /h {0}", filepath);
proc.StartInfo.Arguments = String.Format("/p /h \"{0}\" \"{1}\"", filepath, txtPrinterName.Text);
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
if (proc.HasExited == false)
{
proc.WaitForExit(1000);
}
proc.EnableRaisingEvents = true;
proc.Close();
KillAdobe("AcroRd32");
private static bool KillAdobe(string name)
{
foreach (Process clsProcess in Process.GetProcesses().Where(clsProcess => clsProcess.ProcessName.StartsWith(name)))
{
clsProcess.Kill();
return true;
}
return false;
}
Step 2
without Adobe reader
must set default printer in which printer you want to print.
Code 2.1
Process p = new Process();
p.StartInfo = new ProcessStartInfo()
{
CreateNoWindow = true,
Verb = "print",
FileName = @"D:\QRCodeImages\Print.pdf",
};
p.Start();
Thread.Sleep(1000);
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
Code 2.2
public void printPDF()
{
try
{
dynamic printernamewe = txtPrinterName.Text;
PrinterClass.SetDefaultPrinter(printernamewe);
dynamic printerName = printernamewe;
string pdfFileName = @"D:\QRCodeImages\Print.pdf";
ProcessStartInfo psInfo = new ProcessStartInfo();
psInfo.Verb = "Print"; // or "PrintTo"
psInfo.FileName = pdfFileName;
psInfo.Arguments = String.Format("/p /h \"{0}\" \"{1}\"", pdfFileName, printerName);
psInfo.WindowStyle = ProcessWindowStyle.Hidden;
psInfo.CreateNoWindow = true;
psInfo.UseShellExecute = true;
Process process = Process.Start(psInfo);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public static class PrinterClass
{
[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SetDefaultPrinter(string Printer);
}