When trying to identify the text fragments enclosed in square brackets and replace the [] with empty string, we are getting the below null reference exception if the text fragment contains \r\n or \n or nbsp characters.
Error-System.NullReferenceException: Object reference not set to an instance of an object.
at #=ztxquhYRowjnsZQxP8GiFPpTJwpx3QJg93V2QFsDU09CBI92XJxVn4ao=.#=zW_Vfbi8Nj3PByiegGg==(#=zdfPIt15GsqAtGWNwksq3JS0= #=zUw0fpKg=, String #=zPMPoiS0=, Int32 #=zxOqyBoQ=, Int32 #=zJHlnqi8=)
at #=ztxquhYRowjnsZQxP8GiFPpTJwpx3QJg93V2QFsDU09CBI92XJxVn4ao=.#=zW_Vfbi8Nj3PByiegGg==(#=zdfPIt15GsqAtGWNwksq3JS0= #=zUw0fpKg=, String #=zPMPoiS0=)
at #=ztxquhYRowjnsZQxP8GiFPpTJwpx3QJg93V2QFsDU09CBI92XJxVn4ao=.#=zISNiZ5j016Ue3U4lnq2kWet9EkvS(String #=zPMPoiS0=, Font #=zUw0fpKg=, Font& #=z$B71qFUm5SM2EGdhq6POH1s=)
at #=z$66iMVMGcSZZ6hCn5zcs8teks$4yDhsZhA==.#=z5FM23$lxFBDDOsRNCA==(Double #=z7pPY6dFIxvI$, TextState #=zMeUYjhU=, String& #=zQ6fxqec=)
at Aspose.Pdf.Text.TextFragment.#=z6z_Y4Sm0lzuS(String #=zQIf$T38=)
at Aspose.Pdf.Text.TextFragment.set_Text(String value)
Attached the PDF pdf-cbcf3cda72e341d8a43cd9c58539f74a.pdf (57.3 KB)
In cell A7, getting error when trying to remove square brackets [].
Below is the code for your reference
var doc = new Aspose.Pdf.Document(path);
var searchTerm = “\[(?s)(.*?)\]”;
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(searchTerm);
TextSearchOptions textSearchOptions = new TextSearchOptions(true);
textFragmentAbsorber.TextSearchOptions = textSearchOptions;
doc.Pages.Accept(textFragmentAbsorber);
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
foreach (var textFragment in textFragmentCollection)
{
textFragment.Text = textFragment.Text.Replace("[", “”).Replace("]", “”);
textFragment.TextState.ForegroundColor = Color.Black;
textFragment.TextState.BackgroundColor = Color.Gray;
}