How print dialog box selected printer to printing process - c#

I am trying to print PDF file using Process
PrintDialog pdf = new PrintDialog();
if (pdf.ShowDialog() == DialogResult.OK)
{
pdf.AllowSelection = true;
pdf.AllowSomePages = true;
ProcessStartInfo info = new ProcessStartInfo();
info.Arguments = pdf.PrinterSettings.PrinterName;
info.CreateNoWindow = true;
info.Verb = "print";
info.FileName = filename;
//info.WindowStyle = ProcessWindowStyle.Hidden;
try
{
Process p = new Process();
p.StartInfo = info;
p.EnableRaisingEvents = true; //Important line of code
//p.PriorityBoostEnabled = true;
p.Start();
p.WaitForExit();
p.Close();
}
catch (Exception ex){}
}
else
{
MessageBox.Show("Print Canceled");
}
}
catch (Exception ex){}
But this code not take user selected printer for print process. It print pdf by default printer.
what would be the fault?
Thanks.

#RiksonTool,
Your code is printing to pdf by default printer as it is reading the settings from control panel of windows.
This is not a fault, it is a manifestation of the default settings in windows.
Hope it helps

Related

C# - Print a PDF Error (System.ComponentModel.Win32Exception:...)

I am trying to write an application to print PDFs.
Now I get an error message where I can't get any further after a long search.
The goal would be to print a PDF file without opening the PDF-Reader.
My Code:
using System.Diagnostics;
private void button1_Click(object sender, EventArgs e)
{
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "print";
info.FileName = #"C:\testFile.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();
}
The error condition occurs at p.Start(); line.
Error message:
System.ComponentModel.Win32Exception: "The system cannot find the
specified file".
New experience: 20.01.2021
If I comment out info.Verb = "print";. Then the PDF is opened.
Is this a sign that it finds the PDF but has no access to printer?
use this code it works for me :
public static void PrintToASpecificPirnter()
{
using (PrintDialog printDialog = new PrintDialog())
{
if (printDialog.ShowDialog() == DialogResult.OK)
{
var StartInfo = new ProcessStartInfo();
StartInfo.CreateNoWindow = true;
StartInfo.UseShellExecute = true;
StartInfo.Verb = "printTo";
StartInfo.Arguments = "\"" + printDialog.PrinterSettings.PrinterName + "\"";
StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
StartInfo.FileName = fileName;
Process.Start(StartInfo);
}
}
}

Printing PDF file with Custom Page size

How can I print PDF file without changing the page size? I use the code below but it changes the paper size.
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "print";
info.FileName = FilePath;
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();

How to print only 1 copy of document when using "Process" in c#

I am trying to print a pdf in a c# console application, by starting a process.
Instead of printing only 1 copy of the document it prints multiple copies (3, 4, 5 or 6 unpredictable). This is my code...
var p = new Process
{
StartInfo = new ProcessStartInfo
{
CreateNoWindow = true,
Verb = "PrintTo",
Arguments = printerName,
FileName = filePath
}
};
p.Start();
Please can you tell me what I'm doing wrong?
You can do that with this code.
private void SendToPrinter(string prepDok)
{
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "print";
info.FileName = prepDok;
info.CreateNoWindow = false;
info.WindowStyle = ProcessWindowStyle.Hidden;
System.Windows.Forms.PrintDialog pd = new System.Windows.Forms.PrintDialog();
pd.PrinterSettings.Copies = 1;
Process p = new Process();
p.StartInfo = info;
p.StartInfo.Arguments = pd.PrinterSettings.PrinterName;
p.Start();
p.CloseMainWindow();
Thread.Sleep(4000);
if (!p.WaitForExit(5000))
{
if (!p.HasExited)
{
p.Kill();
}
}
}
This will set the number of copies to one and print dialog won't be shown.

Process .Start() runs locally but not on server

I have code that I took from How can I send a file document to the printer and have it print?. When I was running this on my machine, it worked flawlessly. Once I put it on a VM for testing, it is no longer printing. I discovered this is probably due to the application not opening Adobe. I have given access to the folder where the PDFs reside in, and changed the security settings in Adobe.
My code:
try
{
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "print";
info.FileName = #"properFilePath.pdf";
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Normal; // so I know Adobe is opening
TimeSpan tp = new TimeSpan(0, 0, 10); // I thought this did something different than what it really does
Process p = new Process();
p.StartInfo = info;
p.Start();
p.WaitForInputIdle();
System.Threading.Thread.Sleep(tp);
if (false == p.CloseMainWindow())
{
p.Kill();
}
Message m = new Message() { Msg = "Worked, yo", MsgType = Message.MessageType.Success };
Logger.Log(m);
return true;
}
catch (Exception ex)
{
//I log this I swear
return false;
}
I'm looking to be pointed in the right direction for solutions/help/answers.
Thanks

Why Ghostscript (gswin32c.exe) prints only first page of my multipage PDF document

I am trying to print some multipage PDF files from NET 2.0 x86 console app using Ghostscipt (gswin32c.exe + gsdll32.dll). Everything is working just fine, but only first page of my PDF file is printed. Practicaly in our production environment almost any PDF document will contain multiple pages so I need to print all of them.
Here is the code I am using to send PDF-file to printer with GS:
using (PrintDialog dialog = new PrintDialog())
{
dialog.AllowPrintToFile = false;
dialog.AllowCurrentPage = false;
dialog.AllowSomePages = false;
dialog.PrinterSettings.Copies = 1;
DialogResult result = dialog.ShowDialog();
if (result == DialogResult.OK)
{
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "PDF silent print (gswin32c)";
info.WorkingDirectory = tempDir;
info.FileName = tempDir + #"\gswin32c.exe";
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
info.UseShellExecute = false;
info.Arguments = String.Format(" -dPrinted -dBATCH -dNPAUSE -dNOSAFER -dNOPROMPT -dFIXEDMEDIA -dPDFFitPage -dNumCopies=1 -sDEVICE=mswinpr2 -sOutputFile=\"%printer%{0}\" \"{1}\"", new object[] { dialog.PrinterSettings.printerName, pdfFilename });
using (Process p = Process.Start(info))
{
p.WaitForExit(1 * 1000);
if (!p.HasExited)
p.Kill();
}
}
Am I missing some GhostScript command line arguments?
Please help.

Categories