I am trying to use Aspose.Pdf to find every instance of the word “full” in a pdf document I’m passing through and replace it with a custom sized, multiline textbox. However, when I run it through this and save it, it is not replacing the fields. How can I update this to work as desired?
public Aspose.Pdf.Document ChangeBoxSize(System.IO.MemoryStream ms)
{
Aspose.Pdf.Document doc = new Aspose.Pdf.Document(ms);
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("full");
doc.Pages.Accept(textFragmentAbsorber);
TextFragmentCollection textFragments = textFragmentAbsorber.TextFragments;
TextBoxField text = new TextBoxField(doc, new Aspose.Pdf.Rectangle(100, 200, 300, 300));
text.Value = "Insert Text Here";
Aspose.Pdf.Annotations.Border border = new Aspose.Pdf.Annotations.Border(text);
foreach (TextFragment textFragment in textFragments)
{
textFragment.ReplaceOptions.Equals(border);
}
return doc;
}
@amber.louk
If you do it as you intended, it should be code like this (although this is not a replacement, but adding Annotations in places where the text is located):
var doc = new Aspose.Pdf.Document(dataDir + "example.pdf");
var textFragmentAbsorber = new TextFragmentAbsorber("match");
doc.Pages.Accept(textFragmentAbsorber);
TextFragmentCollection textFragments = textFragmentAbsorber.TextFragments;
foreach (TextFragment textFragment in textFragments)
{
var text = new TextBoxField(textFragment.Page, new Aspose.Pdf.Rectangle(textFragment.Rectangle.LLX, textFragment.Rectangle.LLY, textFragment.Rectangle.LLX + 200, textFragment.Rectangle.LLY + 50));
text.Value = "Insert Text Here";
text.Border = new Aspose.Pdf.Annotations.Border(text);
doc.Form.Add(text);
}
//return doc;
doc.Save(dataDir + "example_with_changes.pdf");
example.pdf (72.7 KB)
How would I edit this to replace the words with the textbox?
@amber.louk
I don’t quite understand your goals. Please attach the document you are working with and describe in more detail what you want to achieve (a drawing would be better, of course).