Aspose.Pdf.Text.TextAbsorber.Visit NullReferenceException only in RELEASE

I have the strangest problem!

I have this simple PDf file (for example):
4625633.pdf (33.4 KB)

When trying to run the code below in RELEASE mode I get this error:

System.NullReferenceException: Object reference not set to an instance of an object.
   at #=zq3aZBvkdHjxXrbtpuLj3yA0Ym2ayOkCSxosnhtIMopDjv3nN8IN2ryo=.#=zFQaYzcw=(SelectFont #=zi3rmTaw=)
   at #=zq3aZBvkdHjxXrbtpuLj3yA0Ym2ayOkCSxosnhtIMopDjv3nN8IN2ryo=.#=zNBaolN0=(Page #=ziIdMjzs=)
   at #=z9wHlJhWp5unhutIFsTPjgRbqNLUsnwSIsu3zHYxXYf3ACs1UtXN1vOZRucbr.#=zzj4MaqakBuxG(BaseOperatorCollection #=zCxXzFe8=, Resources #=zP1naPC0=, Page #=ziIdMjzs=, Rectangle #=z5UfMonoKXnpw)
   at #=z9wHlJhWp5unhutIFsTPjgRbqNLUsnwSIsu3zHYxXYf3ACs1UtXN1vOZRucbr.#=zzj4MaqakBuxG(BaseOperatorCollection #=zCxXzFe8=, Resources #=zP1naPC0=, Rectangle #=z5UfMonoKXnpw)
   at #=z9wHlJhWp5unhutIFsTPjgRbqNLUsnwSIsu3zHYxXYf3ACs1UtXN1vOZRucbr.#=zx_HoY9w=(Boolean #=zhF4j8loTVfu6)
   at #=z9wHlJhWp5unhutIFsTPjgRbqNLUsnwSIsu3zHYxXYf3ACs1UtXN1vOZRucbr..ctor(Page #=ziIdMjzs=, TextSearchOptions #=zZc7Ti8_P_2rm, Boolean #=zrHXOTndZTTL7)
   at Aspose.Pdf.Text.TextAbsorber.Visit(Page page)
   at Odcanit.Platinum.ContentIndexer.PDF.ContentEnginePDF.ExtractTextFromFile(String filePath)

When running the same code on the same file in DEBUG mode it works perfectly!

Here is the code:

public string ExtractTextFromFile(string filePath)
        {
            const int MAX_PAGES_EACH_SCAN = 100;

            StringBuilder documentText = new StringBuilder();
            int totalPages = -1;
            int scannedPages = 0;
            while (totalPages == -1 || scannedPages < totalPages)
            {
                //open document
                using (Document pdfDocument = new Document(filePath))
                {
                    try
                    {
                        if (totalPages == -1) totalPages = pdfDocument.Pages.Count;
                        //create TextAbsorber object to extract text
                        TextAbsorber textAbsorber = new TextAbsorber { ExtractionOptions = new TextExtractionOptions(TextExtractionOptions.TextFormattingMode.MemorySaving) };
                        int currentScanCount = 0;
                        for (int p = scannedPages + 1; p <= Math.Min(totalPages, scannedPages + MAX_PAGES_EACH_SCAN); p++)
                        {
                            // get the extracted text
                            pdfDocument.Pages[p].Accept(textAbsorber);
                            currentScanCount++;
                        }
                        // Append the text to the buffer
                        documentText.Append(textAbsorber.Text);
                        scannedPages += currentScanCount;
                    }
                    finally
                    {
                        pdfDocument.FreeMemory();
                    }
                }
            }

            return documentText.ToString();
        }

I have found that the RELEASE build must have the .config files distributed with the .exe and .dll files - otherwise it fails with this error…

No idea why.

@nir-1
I tried to reproduce this in my environment (Windows 10, VS 2022, .Net 6) - it works for me in both release and debug mode. What do you use (OS, IDE, sdk)?

I’m using Dot net franework 4.8
Try to remove the .config file from the result compiled files

@nir-1

In this case, an exception occurs.
In my case, it also occurs if I delete this file from the Debug folder - is it different for you?
Why do you need to delete this file (.config)?

As I have written in my first comment, it works in debug even without it for some reason… but I’m trying to figure out what is the definition in this config file that is a must for the procedure to work.
I’m trying to minimize the amount of files distributed.
Can I add some lines of code and exclude this config file with my distribution?

@nir-1
Can you say that this appeared with a specific version of the library?
I checked for a project on Framework 4.8.1 - it works without this file (only with appname.exe and Аspose.PDF.dll). Perhaps this is due to the fact that support for 4.8 was removed in the latest packages, I can’t say for sure yet.

@nir-1

That’s right.

If you take the DLL from Aspose.PDF for .NET Framework 4.0 24.9 (from the site Download .NET Component DLL to Process PDF | Aspose.PDF API)
FW.png (46.8 KB)

then the exe file created in the 4.8 release project runs together with this DLL without any other files.
Please pay attention to the warning (underlined on the attached screenshot).