GroupDocs.Watermark lets you replace the text or image of watermarks that already exist in a document. Search for the watermarks first, then assign a new value to each match.
Replace text
The example below opens a document that contains a “CONFIDENTIAL” watermark, finds it, and replaces the text with “APPROVED”.
fromgroupdocs.watermarkimportWatermarkerfromgroupdocs.watermark.search.search_criteriaimportTextSearchCriteriadefmodify_watermark():withWatermarker("./watermarked-document.pdf")aswatermarker:possible=watermarker.search(TextSearchCriteria("CONFIDENTIAL"))forwminpossible:try:wm.text="APPROVED"exceptException:# The entity may not support text editing, or the value is invalidpasswatermarker.save("./output.pdf")if__name__=="__main__":modify_watermark()
watermarked-document.pdf is the sample file used in this example. Click here to download it.
Use formatted_text_fragments to replace the text with styled fragments — choose the font, foreground color, and background color.
fromgroupdocs.watermarkimportWatermarkerfromgroupdocs.watermark.watermarksimportFont,FontStyle,Colorfromgroupdocs.watermark.search.search_criteriaimportTextSearchCriteriadefmodify_watermark_with_formatting():withWatermarker("./watermarked-document.pdf")aswatermarker:possible=watermarker.search(TextSearchCriteria("CONFIDENTIAL"))forwminpossible:try:wm.formatted_text_fragments.clear()wm.formatted_text_fragments.add("APPROVED",Font("Calibri",19.0,FontStyle.BOLD),Color.red,Color.aqua,)exceptException:# The entity may not support formatted text, or values may be invalidpasswatermarker.save("./output.pdf")if__name__=="__main__":modify_watermark_with_formatting()
watermarked-document.pdf is the sample file used in this example. Click here to download it.
Swap the image data of matched watermarks by assigning new bytes to image_data.
fromgroupdocs.watermarkimportWatermarkerfromgroupdocs.watermark.search.search_criteriaimportImageDctHashSearchCriteriadefreplace_watermark_image():withopen("./stamp.png","rb")asf:image_data=f.read()withWatermarker("./watermarked-document.docx")aswatermarker:criteria=ImageDctHashSearchCriteria("./logo.png")criteria.max_difference=0.9possible=watermarker.search(criteria)forwminpossible:try:wm.image_data=image_dataexceptException:# The entity may not support image replacement, or the image format is invalidpasswatermarker.save("./output.docx")if__name__=="__main__":replace_watermark_image()