Hey,
We are using the Pdf.Text.TextFragmentAbsorber to search and replace text. When we pass in a paragraph, it doesn’t find the text, but does work when we pass through the paragraph as single lines?
I have included the code that we are using below. We are using version Aspose.PDF for .NET 22.10
// Create the Aspose PDF document
Aspose.Pdf.Document pdfDoc = new Aspose.Pdf.Document(message.Documents[0].FilePath);
foreach (MessageDocumentPhrase phrase in message.Documents[0].Phrases)
{
try
{
// Create the required objects
TextFragmentAbsorber textFragmentAbsorber = (phrase.IsExpression) ?
new TextFragmentAbsorber(new System.Text.RegularExpressions.Regex(phrase.SearchText), new Aspose.Pdf.Text.TextSearchOptions(true)) : // Is a regular expression
new TextFragmentAbsorber(Regex.Escape(phrase.SearchText), new Aspose.Pdf.Text.TextSearchOptions(true)); // Not a regular expression
// Process the search
pdfDoc.Pages.Accept(textFragmentAbsorber);
TextFragmentCollection textFragments = textFragmentAbsorber.TextFragments;
if (textFragments.Count != 0)
{
foreach (TextFragment textFragment in textFragments)
{
try
{
// Update text and other properties
textFragment.Text = phrase.ReplacementText;
textFragment.TextState.RenderingMode = TextRenderingMode.FillText;
textFragment.TextState.Font = FontRepository.FindFont(phrase.Font);
textFragment.TextState.FontSize = phrase.FontSize;
textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.ColorTranslator.FromHtml(phrase.FontColour));
textFragment.TextState.BackgroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.ColorTranslator.FromHtml(phrase.BackgroundColour));
textFragment.TextState.Underline = phrase.Underline;
if (phrase.WordSpacing != 0)
textFragment.TextState.WordSpacing = phrase.WordSpacing;
if (phrase.LineSpacing != 0)
textFragment.TextState.LineSpacing = phrase.LineSpacing;
}
catch
{
throw new Exception($"Unable to apply text replacement: {phrase.ReplacementText}");
}
}
}
}
catch (Exception ex)
{
_telemetry.TrackException(ex);
}
}
// Apply the changes
pdfDoc.Save(message.Documents[0].FilePath);