Search code examples
c#pdfocrinvoke

Invoke pdftohtml.exe via C#


I want to convert a pdf file into an html file, so that I can extract the values in a table.

pdftohtml.exe can do this.

If I call the following on a command prompt I get an html page with the content from the pdf file:

pdftohtml.exe test.pdf test.html

This works as expected. Now I want to invoke this exe via C#.

I did the following:

string filename = @"C:\Temp\pdftohtml.exe";
Process proc = Process.Start(filename, "test.pdf test.html");

Unfortunately this does not work. I suspect that somehow the parameters are not past to the exe correctly.

When I call this exe via the command line with -c before the parameters I get an error:

pdftohtml.exe -c test.pdf test.html

leads to an error (rangecheck in .putdeviceprops).

Does someone know how to correctly invoke this program?


Solution

  • You can use the following stuff,

    using System.Diagnostics;
    
    // Prepare the process to run
    ProcessStartInfo start = new ProcessStartInfo();
    // Enter in the command line arguments, everything you would enter after the executable name itself
    start.Arguments = arguments; 
    // Enter the executable to run, including the complete path
    start.FileName = ExeName;
    // Do you want to show a console window?
    start.WindowStyle = ProcessWindowStyle.Hidden;
    start.CreateNoWindow = true;
    
    // Run the external process & wait for it to finish
    using (Process proc = Process.Start(start))
    {
     proc.WaitForExit();
    
     // Retrieve the app's exit code
     exitCode = proc.ExitCode;
    }
    

    Usually /C will be used to execute the command and then terminate. In the above code, do modifications as required.