Search code examples
c#windowspdf-conversion

process can not open pdf file c#


In a Windows application I need to run another one application that's tetpdflib. That tetpdflib runs in command prompt only. When I drag and drop exe to the command prompt it will execute. Here is my code:

Process tetmlProcess = new Process();
tetmlProcess.StartInfo.CreateNoWindow = true;
tetmlProcess.StartInfo.UseShellExecute = false;
tetmlProcess.StartInfo.RedirectStandardError = true;
tetmlProcess.StartInfo.RedirectStandardInput = true;
tetmlProcess.StartInfo.WorkingDirectory = @"C:\Users\sw_chn\Documents\PDFlib\TET 5.0 32-bit\bin";
tetmlProcess.StartInfo.FileName = @"C:\Users\sw_chn\Documents\PDFlib\TET 5.0 32-bit\bin\tet.exe";
string args1 = @"tet -m wordplus D:\DailyWork\March\JOURNAL-ISSUE_6_3924-3930.pdf";
tetmlProcess.StartInfo.Arguments = args1;
tetmlProcess.Start();
StreamReader news = tetmlProcess.StandardError;
string err = news.ReadToEnd();
Console.WriteLine(err);
Console.ReadLine();

I had following error:

could not open PDF file 'tet' for reading

How to recover from this?


Solution

  • Your Start Arguments contains the Program Name again which leads to this error.

    Simply change your code

    Process tetmlProcess = new Process();
    // ...
    tetmlProcess.StartInfo.WorkingDirectory = @"C:\Users\sw_chn\Documents\PDFlib\TET 5.0 32-bit\bin";
    tetmlProcess.StartInfo.FileName = @"C:\Users\sw_chn\Documents\PDFlib\TET 5.0 32-bit\bin\tet.exe";
    // removing "tet" in Arguments
    string args1 = @"-m wordplus D:\DailyWork\March\JOURNAL-ISSUE_6_3924-3930.pdf";
    tetmlProcess.StartInfo.Arguments = args1;
    tetmlProcess.Start();
    // ...
    

    Conclusion

    The manual contains example like this

    tet --format utf16 --outfile file.utf16 file.pdf

    Here is tet mapped as environment variable in the system and stands for the full path of the application.