I am attempting to replace a paragraph of text with a new one. I am finding the text using a textfragmentabsorber and a rectangle. I find that there’s portions of text still there even if I just set it to empty text, and which text is left varies by the ReplaceAdjustment flag…
Here’s a link to the orignal file:
https://1drv.ms/b/s!AgfCWFajPdC67VqUhMEgJTykZKyn
A link to an example after running above code with ShiftRestOfLine:
https://1drv.ms/b/s!AgfCWFajPdC67VwzP-TLsoHaHxiO?e=7PhyTv
And a link to example with None:
https://1drv.ms/b/s!AgfCWFajPdC67VtIj_P_d1o-oYsT?e=BBsj3S
Aspose.Pdf.Rectangle rectForDescription = new Aspose.Pdf.Rectangle(20, 461, 149, 525);
// iterate through individual TextFragment
Aspose.Pdf.Text.TextFragmentAbsorber textFragmentAbsorber2 = new Aspose.Pdf.Text.TextFragmentAbsorber();
// search text within page bound
textFragmentAbsorber2.TextSearchOptions.LimitToPageBounds = true;
// specify the page region for TextSearch Options
textFragmentAbsorber2.TextSearchOptions.Rectangle = rectForDescription; // Docs are wrong - should be lX, lY, ux, uy not height and width
//int currentDescrLength = GetSegmentsTextLength(pdf, rectForDescription);
//if (currentDescrLength < introText.Length)
// textFragmentAbsorber2.TextReplaceOptions.ReplaceAdjustmentAction = Aspose.Pdf.Text.TextReplaceOptions.ReplaceAdjustment.AdjustSpaceWidth;
//else
//textFragmentAbsorber2.TextReplaceOptions.ReplaceAdjustmentAction = Aspose.Pdf.Text.TextReplaceOptions.ReplaceAdjustment.WholeWordsHyphenation;
textFragmentAbsorber2.TextReplaceOptions.ReplaceAdjustmentAction = Aspose.Pdf.Text.TextReplaceOptions.ReplaceAdjustment.ShiftRestOfLine;
Aspose.Pdf.Text.TextFragmentCollection textFragmentCollection2 = textFragmentAbsorber2.TextFragments;
// search text from first page of PDF file
pdf.Pages[1].Accept(textFragmentAbsorber2);
string txtInfo = "";
bool isFirst = true;
foreach (Aspose.Pdf.Text.TextFragment tf in textFragmentCollection2)
{
tf.Text = " ";
foreach (Aspose.Pdf.Text.TextSegment seg in tf.Segments)
{
seg.Text = " ";
}
}
pdf.Save();