Printing PDF file with Custom Page size - c#

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();

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);
}
}
}

Open pdf file failed

I want to open a pdf file with Adobe Reader. I used this code shown here, but it doesn't open it. I do see the Adobe Acrobt Reader in the proccess list in Task Manager, but it does not show the window with my PDF file:
ProcessStartInfo startInfo = new ProcessStartInfo(AdobPath);
startInfo.UseShellExecute = true;
startInfo.CreateNoWindow = false; ;
startInfo.Arguments = string.Format("/A \"page={0}\" \"{1}\"", pn, PdfPath);
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
if (process.HasExited == false)
process.WaitForExit(10000);
process.Close();

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.

How can i calculate percent end time when below .exe is running on ASP.NET C#

I want to press button and it should continue and display percent time when i click another page, code should on background. After that i click this page it display me results belong running .exe process.
How can i do that? I need your help.
ProcessStartInfo startInfo = new ProcessStartInfo();
Process p = new Process();
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
startInfo.Arguments = "/Scan http://localhost/deneme/sunucu.html /Profile web_applications /SaveToDatabase –ScanningMode=Heuristic –UseAcuSensor=TRUE –EnablePortScanning=TRUE";
startInfo.FileName = #"C:\\Program Files (x86)\\Acunetix\\Web Vulnerability Scanner 9.5\wvs_console.exe";
p.StartInfo = startInfo;
p.OutputDataReceived += (sender, args) => Console.WriteLine("received output: {0}", args.Data);
p.Start();
p.BeginOutputReadLine();
StreamReader output = p.StandardOutput //i tried beginoutputreadline
p.WaitForExit();
ListBox1.Items.Add(output.tostring());
Regards,

Process.Exited event is not be called

I have the following code snippet to call into command line:
p = new Process();
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "cmd.exe";
psi.Arguments = "/C " + "type " + “[abc].pdf”;
psi.UseShellExecute = false;
psi.RedirectStandardInput = false;
psi.RedirectStandardOutput = true;
psi.CreateNoWindow = true;
p.StartInfo = psi;
p.EnableRaisingEvents = true;
p.Exited += new EventHandler(p_Exited);
p.Start();
p.WaitForExit();
Strangely, When [abc] is a small pdf file(8kb) p_Exited is called. But when it's a large pdf file(120kb) it is never called. Any clues?
Thanks,
You need to consume the output stream when the standard output has been redirected:
p.Start();
p.StandardOutput.ReadToEnd();
p.WaitForExit();

Categories