I want to print a PDF file from C# code without user interaction.
I tried this accepted answer but it is not working for me.
This is the code I tried:
Process p = new Process();
p.StartInfo = new ProcessStartInfo()
{
CreateNoWindow = true,
Verb = "print",
FileName = @"G:\Visual Studio Projects\PrintWithoutGUI\PrintWithoutGUI\Courses.pdf" //put the correct path here
};
p.Start();
I get this exception :
System.ComponentModel.Win32Exception: 'The specified executable is not a valid application for this OS platform.'`
try this by add UseShellExecute=true it will print to default printer but if you want to print to a specific print change the verb print to verb printTo by spefiying the name of the printer Arguments proprety.
private static void PrintByProcess()
{
using (Process p = new Process())
{
p.StartInfo = new ProcessStartInfo()
{
CreateNoWindow = true,
UseShellExecute=true,
Verb = "print",
FileName = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\doc.pdf"
};
p.Start();
}
this is the correct way to write your code
Process p = new Process();
p.StartInfo = new ProcessStartInfo()
{
CreateNoWindow = true,
Verb = "print",
FileName = "PDfReader.exe", //put the path to the pdf reading software e.g. Adobe Acrobat
Arguments = "PdfFile.pdf" // put the path of the pdf file you want to print
};
p.Start();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With