PdfContentEditor ReplaceText returns Object reference not set to the instance of an object error

Latest Version of Aspose.Pdf

Using the PdfContentEditor and attempting to replace text on certain files throws an “Object reference not set to the instance of an object” error.

image.png (190.1 KB)

image.png (120.3 KB)

image.png (63.6 KB)

private async Task ReplaceAttachmentWithSignatures(CompileAttachment compileAttachment, string filePath)
{
    await _azureProvider.DownloadToFileAsync(compileAttachment.AzurePdfPath, filePath);
    var document = new Aspose.Pdf.Document(filePath);
    var editor = new PdfContentEditor();
    editor.BindPdf(document);
    if (compileAttachment.MagicTags != null)
    {
        List<MagicTag> magicTags = compileAttachment.MagicTags.Where(x => x.Tag == "{{item.number}}" || x.Tag == "{{section.number}}").ToList();

        editor.ReplaceTextStrategy.ReplaceScope = ReplaceTextStrategy.Scope.ReplaceAll;

        foreach (var magicTag in magicTags)
        {
            //Replace all the matching keys in the text
            editor.ReplaceText(magicTag.Tag, magicTag.Value ?? "");
        }

        ELSLogHelper.InsertInfoLog(ELSLogHelper.AsposeLogMessage("Save"), MethodBase.GetCurrentMethod()?.Name, MethodBase.GetCurrentMethod().DeclaringType?.Name, Environment.StackTrace);
        editor.Save(filePath);
        editor.Document.FreeMemory();
        editor.Document.Dispose();
        editor.Dispose();
        editor = null;
    }
}

Here is the attachment it is attempting to process.

Bad_ITB FAF FY24 263 C17006 Building K Analyzer_Plan.pdf

@ST2YKE2

We are checking it and will get back to you shortly.

1 Like

@ST2YKE2

We regret to share that we lost the file that was shared by you and now the link has been expired. Would you please be kind enough to share the file again with us?

@asad.ali

Bad_ITB FAF FY24 263 C17006 Building K Analyzer_Plan.pdf

@ST2YKE2

We tested using below two code snippet and 24.8 version of the API and faced the same exception.

var document = new Aspose.Pdf.Document(dataDir + "Bad_ITB FAF FY24 263 C17006 Building K Analyzer_Plan.pdf");
var editor = new PdfContentEditor();
editor.BindPdf(document);
//Replace all the matching keys in the text
editor.ReplaceText("{{item.number}}", "1"); // Exception occurs here
editor.Save(dataDir + "output.pdf");
editor.Document.FreeMemory();
editor.Document.Dispose();
editor.Dispose();
editor = null;
var doc = new Aspose.Pdf.Document($"{dataDir}Bad_ITB FAF FY24 263 C17006 Building K Analyzer_Plan.pdf");
var textFragmentAbsorber = new TextFragmentAbsorber("{{item.number}}");
doc.Pages.Accept(textFragmentAbsorber); // Exception occurs here
var textFragmentCollection = textFragmentAbsorber.TextFragments;
foreach (var textFragment in textFragmentCollection)
{
    //If add this line, it's OK.
    textFragment.Text = "1";
}
doc.Save($"{dataDir}out.pdf");

The issue has been generated in the issue tracking system under the ticket ID PDFNET-58082. We will let you know via this forum thread as soon as it is resolved. We apologize for the inconvenience.

The issues you have found earlier (filed as PDFNET-58082) have been fixed in Aspose.PDF for .NET 24.12.