Search code examples
c#tesseractoneclick

C# Windows Form Application Tesseract problem


I have a big project, and an issue. The program is supposed to use the Tesseract OCR to search for invoices on the local network. The problem is, that the program is doing fine while using Visual Studio, but when I publish it, it keeps on telling me this error:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.ArgumentNullException: Value cannot be null. (Parameter 'path1')
   at System.ArgumentNullException.Throw(String paramName)
   at System.IO.Path.Combine(String path1, String path2)
   at InteropDotNet.LibraryLoader.InternalLoadLibrary(String baseDirectory, String platformName, String fileName)
   at InteropDotNet.LibraryLoader.LoadLibrary(String fileName, String platformName)
   at InteropRuntimeImplementer.LeptonicaApiSignaturesInstance.LeptonicaApiSignaturesImplementation..ctor(LibraryLoader loader)
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span1 copyOfArgs, BindingFlags invokeAttr)

And here is my code:

private void SearchAndCopyPDFsWithOCR(
ing sourceDirectory,
ing targetDirectory,
ing searchTerm,
aTable dataTableValues,
ing partnerPrefix,
ing wetenr)
{
    string tessdataPath = @"C:\Program Files\Tesseract-OCR\tessdata";

    if (!Directory.Exists(tessdataPath))
    {
        MessageBox.Show($"A tessdata mappa {tessdataPath} nem található!", "Hiba", Messag
        return;
    }

    if (string.IsNullOrEmpty(searchTerm) || string.IsNullOrEmpty(partnerPrefix))
        return;

    var filteredDirectories = Directory.GetDirectories(sourceDirectory, $"{partnerPrefix}

    // A TesseractEngine csak egyszer jön létre a metódus elején.
    using (var ocrEngine = new TesseractEngine(tessdataPath, "eng", EngineMode.LstmOnly))

I struggle this for about a week now every day, and I think I tried everything to get over this. Maybe you guys can provide something that I missed. Much appreciated,

  • I tried to log the parameters of every Path.Combine, but the paths were displayed just fine.

  • I tried to make exception handling if-s to log the error, the program doesn't seemed to enter that partion of the code, so no luck here either.

  • Checked that all the parameters have values other than null, no luck

  • Now I run Tesseract from a pre-installed folder on drive C

  • Checked if there is some policy for the network folders, so gave my Desktop for sourcePath

    Other properties of the project:

  • Using ClickOne to publish the project

  • I am using Tesseract engine in a method

The program displays the first sourceDirectory just fine, as I mentoined, but right after that the error pops up. I provide you a part of the calling code of the method below:

// Mappa ellenőrzése és PDF keresés
if (sourceDirectory == null || evszam == null)
{
    MessageBox.Show($"sourceDirectory vagy evszam null {sourceDirectory}, {evszam}");
}
else
{
    if (sourceDirectory == null || Path.GetFileName(evszam) == null)
    {
        throw new ArgumentNullException($"{sourceDirectory} vagy {Path.GetFileName(evszam)} nem lehet null.");
    }
    specificSourceDirectory = Path.Combine(sourceDirectory, evszam);

    MessageBox.Show(specificSourceDirectory);
}

if (Directory.Exists(specificSourceDirectory))
{
    SearchAndCopyPDFsWithOCR(
        specificSourceDirectory,
        targetDirectory,
        szamlaszam,
        dataTableValues,
        partnerPrefix,
        wetenr);
}
else
{

    SearchAndCopyPDFsWithOCR(
        sourceDirectory,
        targetDirectory,
        szamlaszam,
        dataTableValues,
        partnerPrefix,
        wetenr);
}

Solution

  • I found the solution. The OneClick extension was the problem. I found out that when you publish your project, the path of the installation is something like the following:

    C:\Users\UserName\AppData\Local\Apps\2.0\J9OYLD59.Q85\32RP9PX7.B8W\brun...exe_0000000000000000_0001.000b_none_56c17bd12b8e1751
    

    Probably the length, but more likely the exact name (maybe the '.'s?) of the path caused some inconsistency in the program.

    I solved it by using another installer program, where the user can choose where to install the program.