While replacing a text using TextFragment.Text Throws System.NullReferenceException

Hi Team,
Im trying to find and replace some texts in a PDF File,
I have tried with TextFragement class Text property, It works well for few inputs but Fails (throws System.NullReferenceException) while replacing a specific text

Below Inputs works fine :
new Inputs {searchText = “MANUFACTURE”, replacementText = “Manufactured By”}
new Inputs {searchText = “00000000000000000”, replacementText = “123456789000”}
Below Input fails :
new Inputs {searchText = “OrderNumber”, replacementText = “Ordes 123”}

But if I give replacement Text within 4 characters,
new Inputs {searchText = “OrderNumber”, replacementText = “Orde”} like this , it works…

My Environment Details are
Windows 10 Enterprise 21H2 64 Bit
Intel® - Xenon® Gold 6254 CPU @ 3.10GHZ
Microsoft Visual Studio Professional 2022 Version 17.2.0
Dot Net 6.0

The following is the code im currently using :

public void ReplaceTexts()
{
string dataDir = @“C:\Users\dell101\Desktop\POC\text”;
Document pdfDocument = new Document(dataDir + Constants.AMG_HOLA + Constants.FILE_EXTENSION_PDF);

        List<Inputs> inputsList = new() {
            new Inputs {searchText = "OrderNumber", replacementText = "Ordes 123"},
            new Inputs {searchText = "EMISSION", replacementText = "Emision"},
            new Inputs {searchText = "VALVE", replacementText = "A Big Line"}
        };

        foreach (var item in inputsList)
        {
            string input = item.searchText;
            TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(input);
            pdfDocument.Pages.Accept(textFragmentAbsorber);

            TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;

            foreach (TextFragment textFragment in textFragmentCollection)
            {
                textFragment.Text = item.replacementText;
            }
        }
        pdfDocument.Save(dataDir + Constants.AMG_HOLA + " 1 " + Constants.FILE_EXTENSION_PDF);
    }

Here is my Source PDF :
Audi VIN 114x42 Germany.pdf (131.1 KB)

@PrabhuDevaGRR
Which version of the Aspose.Pdf library are you using? I haven’t been able to reproduce the issue yet, even though I have pretty much the same environment.

Hi @sergei.shibanov,
I have tried in different machine, Still Im getting the exception.
Im using ASPOSE.PDF latest version 23.5.0

Here I have added the error screenshots , with a sample code.
Error Message.JPG (60.4 KB)
Original Source.JPG (108.8 KB)
Updated Source.JPG (119.9 KB)

public void SimpleFindAndReplace()
{
    string dataDir = @"C:\Users\dell1\Desktop\POC\text\";
    Document pdfDocument = new Document(dataDir + Constants.AUDI + Constants.FILE_EXTENSION_PDF);
    TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("EMAIL");
    pdfDocument.Pages.Accept(textFragmentAbsorber);
    TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;

    foreach (TextFragment textFragment in textFragmentCollection)
    {
        textFragment.Text = "Orde";
    }

    pdfDocument.Save(dataDir + Constants.AUDI + " 1 " + Constants.FILE_EXTENSION_PDF);
}

Works for most of the input.
Getting Exception only for “EMAIL” text.
searchText = “EMAIL”
replacementText = “USER123@GMAIL.COM”

If the replacementText is less than or equal to 4 characters like “USER” or “USE”, its working fine without any exception. when no of characters is greater than 5, it throws the exception.

@PrabhuDevaGRR
Look and write please (or attach in a text file) what fonts are installed in your system.
To get a list of fonts in C# one can use:

var fonts = new InstalledFontCollection();
Console.WriteLine("installed fonts:");
foreach (FontFamily family in fonts.Families)
{
    Console.WriteLine(family.Name);
}

Hi @sergei.shibanov

Please find the list of fonts installed in my system.
Fonts List.docx (8.4 KB)

@PrabhuDevaGRR
image.png (48.5 KB)
Let’s compare the execution of the code for me and for you.
If you set a breakpoint as in the screenshot I attached, do you also have textFragmentAbsorber.TextFragments.Count equal to zero?
(this is to be expected since the “OrderNumber” substrings are not visible in the attached document)

@sergei.shibanov
Kindly try for text EMAIL

searchText = “EMAIL”
replacementText = “USER123@GMAIL.COM”

@PrabhuDevaGRR

I was guided by these given data.

With the data that is given in the last post (“EMAIL” and “USER123@GMAIL.COM”), the exception was reproduced - I will set the task for the development team (with a code fragment just for this data).

@PrabhuDevaGRR
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-54768

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Thank you for the Update @sergei.shibanov