import aspose.pdf as apdf
from os import path
path_infile = path.join(self.data_dir, infile)
path_outfile = path.join(self.data_dir, outfile)
document = apdf.Document(path_infile)
searchTerm = "Secret word"
textFragmentAbsorber = apdf.text.TextFragmentAbsorber(searchTerm)
textSearchOptions = apdf.text.TextSearchOptions(True)
textFragmentAbsorber.text_search_options = textSearchOptions
document.pages.accept(textFragmentAbsorber)
textFragmentCollection = textFragmentAbsorber.text_fragments
for textFragment in textFragmentCollection:
page = textFragment.page
annotationRectangle = textFragment.rectangle
annot = apdf.annotations.RedactionAnnotation(page, annotationRectangle)
annot.fill_color = apdf.Color.black
document.pages[page.number].annotations.add(annot, True)
annot.redact()
document.save(path_outfile)