Free Support Forum - aspose.com

Getting object reference exception while changing color of the text using textFragmentAbsorber


#1

Hello team,

When i changing the color of the text, if the fragments count more than 1, i getting an exception “object reference not set to an instance of an object”.
i am using the following code for changing color.

Aspose.Pdf.Text.TextFragmentAbsorber textFragmentAbsorbernew = new Aspose.Pdf.Text.TextFragmentAbsorber();
textFragmentAbsorbernew.TextSearchOptions = new Aspose.Pdf.Text.TextSearchOptions(Rect);
textFragmentAbsorbernew.Visit(PDFPage);
if (action == “gotor”)
{
foreach (Aspose.Pdf.Text.TextFragment TextFrag in textFragmentAbsorbernew.TextFragments)
{
System.Drawing.Color color1 = System.Drawing.ColorTranslator.FromHtml(obj.TextColor);
TextFrag.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(color1);
}
}

i am searching keyword like “reviewer” in the page for creating link, but in page text present like “un-reviewer”
and while i try to change color of text reviewer, absorber extracts two fragments like “-” and “reviewer”.

i observed during changing color is, for “-” color is changed and for changing color of “reviewer” i am getting exception.

Please help me to solve this issue.

Thanks
Regards
Vijaykumar L S


#2

@vijiannabond

Thanks for contacting support.

Would you please share your sample PDF document with us. We will test the scenario in our environment and address it accordingly.


#3

the following regular expression i am used for extracting text from the pdf page.

textFragmentAbsorber = new Aspose.Pdf.Text.TextFragmentAbsorber(@"\b(?<=|^)" + keyWord + @"(?=|$)\b", new Aspose.Pdf.Text.TextSearchOptions(true));

above expression is used for case sensitive and whole words.

In page number 1, if i try to extract word like “reviewed” and when we try to change color of the text i getting an exception.

Thnaks
Regards
Vijaykumar L S


#4

object-refeerence-1.pdf (206.9 KB)
please find Above document regarding the issue.


#5

@vijiannabond

Thanks for providing requested details.

We have tested the scenario in our environment using Aspose.PDF for .NET 19.1 and following code snippet. We were unable to replicate the issue which you have mentioned. For your kind reference, an output PDF is also attached.

var regex = @"\b(?<=|^)reviewed(?=|$)\b";
var textFragmentAbsorber = new TextFragmentAbsorber(regex);
var textSearchOptions = new TextSearchOptions(true);
textFragmentAbsorber.TextSearchOptions = textSearchOptions;
Document pdfDocument = new Document(dataDir + "object-refeerence-1.pdf");
pdfDocument.Pages.Accept(textFragmentAbsorber);
var textFragmentCollection = textFragmentAbsorber.TextFragments;
foreach (TextFragment textFragment in textFragmentCollection)
{
 textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.Red;
}
pdfDocument.Save(dataDir + "test19.1.out.pdf");

test19.1.out.pdf (202.0 KB)

Would you please try using latest version of the API and in case you still face any issue, please share a sample console application which is able to reproduce the issue. We will again test the scenario in our environment and address it accordingly.