Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'The specified executable is not a valid application for this OS platform

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.'`

like image 936
The None Avatar asked Sep 25 '19 16:09

The None


2 Answers

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();
        }
like image 94
bigtheo Avatar answered Oct 23 '22 20:10

bigtheo


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();
like image 22
Mostafa Mahmoud Avatar answered Oct 23 '22 19:10

Mostafa Mahmoud