Pdf replace text with regex

Hi.
i try to replace Email by regular Expression.

but this is not work.

i am send a code and file

 Dim errText as string = ""  
    Dim fileName As String = Request("fileName")       : if fileName = "" then errText = "Error: fileName is empty"
    Dim logoUrl = ""

     Dim fileArr = split(fileName,".")
       Dim fileType  = fileArr(ubound(fileArr))
       
      
    ' ---==== 1.0 if this binary filel

    if fileName <> "" then 
    
     if lcase(fileType)="pdf" then
       Dim pdfDocument As new Aspose.pdf.Document(fileFolder + fileName)
       
       ' Create TextAbsorber object to find all the phrases matching the regular expression
       
      Dim textFragmentAbsorber  As new TextFragmentAbsorber("[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+")
      
      ' Set text search option to specify regular expression usage
       Dim  textSearchOptions As new TextSearchOptions(true)
       textFragmentAbsorber.TextSearchOptions = textSearchOptions
        
        
        ' Accept the absorber for a single page
        ' pdfDocument.Pages[1].Accept(textFragmentAbsorber)
        
        ' Get the extracted text fragments
           Dim textFragmentCollection As TextFragmentCollection = textFragmentAbsorber.TextFragments
        
          ' Update text and other properties
          For Each textFragment As TextFragment In textFragmentCollection
            textFragment.Text = "New Phrase"
              
          Next
            
 
        ' Dim logoStream As New MemoryStream()  
        ' pdfDocument.Save(logoStream, Aspose.pdf.SaveFormat.pdf)
        
        
        Response.Clear()
        Response.ClearContent()
        Response.ClearHeaders()
        Response.AddHeader("Content-Description", "File Transfer")
        Response.ContentType = "application/pdf" 
        Response.AddHeader("content-disposition", "attachment; filename=test.pdf")
        Response.AddHeader("Content-Transfer-Encoding", "binary")
        ' logoStream.WriteTo(Response.OutputStream)
       pdfDocument.Save(fileFolder + fileName)
      ' response.Write(pdfDocument)
      response.end
      end if

152227CV Aleksandra Tomina.pdf (656.0 KB)
152227CV Aleksandra Tomina.pdf (656.0 KB)

@eranlipi

We used the following code snippet in our environment with Aspose.PDF for .NET 20.10 and were unable to notice any issue. Would you kindly try using latest version of the API and let us know in case you still face any issue:

var doc = new Aspose.Pdf.Document(dataDir + "152227CV Aleksandra Tomina.pdf");

TextFragmentAbsorber absorber = new TextFragmentAbsorber(@"[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+", new TextSearchOptions(true));
absorber.TextReplaceOptions = new TextReplaceOptions(TextReplaceOptions.ReplaceAdjustment.ShiftRestOfLine);
absorber.TextReplaceOptions.ReplaceScope = TextReplaceOptions.Scope.REPLACE_ALL;
doc.Pages.Accept(absorber);
doc.Pages[1].Accept(absorber);
foreach(TextFragment textfragment in absorber.TextFragments)
{
 textfragment.Text = "New Aspose Test Name";
}
doc.Save(dataDir + @"FindAndReplace_out.pdf");

FindAndReplace_out.pdf (689.7 KB)

1 Like