This guide provides a quick overview of how to set up and start using GroupDocs.Watermark for Python via .NET. The library opens a document, lets you add text or image watermarks, search for or remove existing ones, and saves the result back to the original format.
For best practices, use a virtual environment to manage dependencies in Python applications. Learn more about virtual environments at the Create and Use Virtual Environments documentation topic.
Create and Activate a Virtual Environment
Create a virtual environment:
py-mvenv.venv
python3 -m venv .venv
python3 -m venv .venv
Activate a virtual environment:
.venv\Scripts\activate
source .venv/bin/activate
source .venv/bin/activate
Install groupdocs-watermark-net Package
After activating the virtual environment, run the following command in your terminal to install the latest version of the package:
py-mpipinstallgroupdocs-watermark-net
python3 -m pip install groupdocs-watermark-net
python3 -m pip install groupdocs-watermark-net
Ensure the package is installed successfully. You should see the message
Successfully installed groupdocs-watermark-net-*
Example 1: Add a text watermark
To quickly test the library, let’s open a PDF, add a diagonal “CONFIDENTIAL” text watermark, and save the result.
importosfromgroupdocs.watermarkimportWatermarker,Licensefromgroupdocs.watermark.watermarksimportTextWatermark,Font,Colordefadd_text_watermark():# Optionally set a licenselicense_path=os.path.abspath("./GroupDocs.Watermark.lic")ifos.path.exists(license_path):License().set_license(license_path)# Open the document, add a text watermark, and save the resultwithWatermarker("./sample.pdf")aswatermarker:watermark=TextWatermark("CONFIDENTIAL",Font("Arial",48))watermark.foreground_color=Color.redwatermark.opacity=0.5watermark.rotate_angle=45.0watermarker.add(watermark)watermarker.save("./watermarked.pdf")if__name__=="__main__":add_text_watermark()
sample.pdf is the sample file used in this example. Click here to download it.
watermarker.add(watermark) then watermarker.save("./watermarked.pdf"): Applies the watermark and writes the result.
Example 2: Add an image watermark
In this example we stamp a logo image onto a Word document.
importosfromgroupdocs.watermarkimportWatermarker,Licensefromgroupdocs.watermark.watermarksimportImageWatermarkdefadd_image_watermark():# Optionally set a licenselicense_path=os.path.abspath("./GroupDocs.Watermark.lic")ifos.path.exists(license_path):License().set_license(license_path)# Open the document, add an image watermark, and save the resultwithWatermarker("./sample.docx")aswatermarker:watermark=ImageWatermark("./logo.png")watermark.opacity=0.5watermarker.add(watermark)watermarker.save("./watermarked.docx")if__name__=="__main__":add_image_watermark()
sample.docx and logo.png are the sample files used in this example. Download sample.docx and logo.png.
ImageWatermark("./logo.png"): Builds an image watermark from a logo file. You can also pass a stream.
watermark.opacity = 0.5: Makes the logo half-transparent so the underlying content stays readable.
watermarker.add(watermark) then watermarker.save("./watermarked.docx"): Applies the watermark and writes the result.
Example 3: Read document information
Sometimes you only need a document’s metadata. get_document_info() returns the file type, page count, and size without modifying the document.
importosfromgroupdocs.watermarkimportWatermarker,Licensedefget_document_info():# Optionally set a licenselicense_path=os.path.abspath("./GroupDocs.Watermark.lic")ifos.path.exists(license_path):License().set_license(license_path)# Open the document and read its metadatawithWatermarker("./sample.pdf")aswatermarker:info=watermarker.get_document_info()print("File type:",info.file_type)print("Pages:",info.page_count)print("Size, bytes:",info.size)if__name__=="__main__":get_document_info()
sample.pdf is the sample file used in this example. Click here to download it.
File type: Pdf (.pdf) - Pdf
Pages: 3
Size, bytes: 271889