We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Replace multiple text fragments

Using the code below, I can replace a text fragment in an PDF file.

Aspose.Pdf.

Document doc = new Aspose.Pdf.Document(@"test.pdf");

TextFragmentAbsorber tfa = new TextFragmentAbsorber("@CurrentDateTime");

doc.Pages.Accept(tfa);

TextFragmentCollection tfc = tfa.TextFragments;

foreach (TextFragment tf in tfc)

tf.Text =

DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss tt");

doc.Save(

@"test.pdf");

Aspose.Pdf.

Document doc = new Aspose.Pdf.Document(@"test.pdf");

TextFragmentAbsorber tfa = new TextFragmentAbsorber("@CurrentDateTime");

doc.Pages.Accept(tfa);

TextFragmentCollection tfc = tfa.TextFragments;

foreach (TextFragment tf in tfc)

tf.Text =

DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss tt");

doc.Save(

@"test.pdf");

How do I replace multiple text fragments within the document?

Hi Kenneth,


Thanks for your inquiry. Please check following code snippet for search/replace multiple text fragments. Hopefully it will help you to accomplish the task.

DOM Approach:

Document doc = new Document(myDir

  • “Lorem_lpsum.pdf”);<o:p></o:p>

TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("Test");

doc.Pages.Accept(textFragmentAbsorber);

foreach (TextFragment textFragment in textFragmentAbsorber.TextFragments)

{

//textFragment.Text = doc.Pages.Count.ToString();

textFragment.Text = "Aspose";

//textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.Blue;

//textFragment.TextState.BackgroundColor = Aspose.Pdf.Color.Yellow;

}

textFragmentAbsorber = TextFragmentAbsorber("Lorem");

doc.Pages.Accept(textFragmentAbsorber);

foreach (TextFragment textFragment in textFragmentAbsorber.TextFragments)

{ textFragment.Text = "01-01-2014"; }

doc.Save(myDir + "TextReplace_out.pdf");

Facades Approach:

PdfContentEditor pdfContentEditor = new PdfContentEditor();

// pdfContentEditor.BindPdf(myDir + "TableResult1.pdf");

pdfContentEditor.BindPdf(myDir + " Lorem_lpsum.pdf ");

//replace text on all pages

pdfContentEditor.ReplaceTextStrategy.ReplaceScope = ReplaceTextStrategy.Scope.REPLACE_ALL;

pdfContentEditor.ReplaceText("Test", 0, "Aspose");

pdfContentEditor.ReplaceText("[[[appname]]]", 0, "Test Application");

//save output PDF

pdfContentEditor.Save(myDir + "ReplaceTextfac.pdf");

Please feel free to contact us for any further assistance.


Best Regards,