Using text watermarks is an effective way to protect document content. By overlaying sensitive documents with watermarks such as “Confidential” or “Draft”, you can deter unauthorized distribution and reinforce data security.
The following example shows how to add a scaled, rotated text watermark to a document and save the result.
If a document consists of multiple parts (pages, worksheets, slides, frames, etc.), the watermark is added to each of them. You can target specific parts via the PagesSetup property:
You can place a watermark at an absolute position by setting x, y, width, and height. Values are measured in the default units of the document.
fromgroupdocs.watermarkimportWatermarkerfromgroupdocs.watermark.watermarksimportTextWatermark,Fontdefadd_watermark_absolute():withWatermarker("./sample.pdf")aswatermarker:watermark=TextWatermark("Test watermark",Font("Times New Roman",8.0))watermark.x=10watermark.y=20watermark.width=100watermark.height=40watermarker.add(watermark)watermarker.save("./output.pdf")if__name__=="__main__":add_watermark_absolute()
sample.pdf is the sample file used in this example. Click here to download it.
The origin of coordinates differs across document types (relative positioning doesn’t have these specifics and can be used as a unified positioning approach).
The following table lists the unit of measure and origin of coordinates for each document format:
Document Format
Unit of Measure
Origin of Coordinates
PDF
Point
Left bottom corner of page
WordProcessing
Point
Left top corner of page
Spreadsheet
Point
Left top corner of worksheet
Presentation
Point
Left top corner of slide
Image
Pixel
Left top corner of image (frame)
Diagram
Point
Left top corner of page
Relative watermark positioning
Instead of exact coordinates, use parent-relative alignment and set offsets with margins.
Scale the watermark relative to the parent size using sizing_type and scale_factor.
fromgroupdocs.watermarkimportWatermarkerfromgroupdocs.watermark.watermarksimportTextWatermark,Font,SizingTypedefadd_watermark_size_type():withWatermarker("./sample.pdf")aswatermarker:watermark=TextWatermark("This is a test watermark",Font("Calibri",12.0))watermark.sizing_type=SizingType.SCALE_TO_PARENT_DIMENSIONSwatermark.scale_factor=0.5watermarker.add(watermark)watermarker.save("./output.pdf")if__name__=="__main__":add_watermark_size_type()
sample.pdf is the sample file used in this example. Click here to download it.
When a rotation angle is set, the watermark size is taken to be the axis-aligned bounding-box size. The picture below shows the result of the snippet above — the actual watermark bounds are blue and the bounding box is red. The bounding-box size is used to calculate the watermark’s relative size.
Using custom fonts
You can use custom TrueType fonts by specifying a folder path in the Font constructor.
fromgroupdocs.watermarkimportWatermarkerfromgroupdocs.watermark.watermarksimportTextWatermark,Font,Colordefadd_custom_font_watermark():# Point fonts_folder at a directory containing your TrueType (.ttf) fontsfonts_folder=r"c:\CustomFonts"withWatermarker("./sample.pdf")aswatermarker:watermark=TextWatermark("Test watermark",Font("CustomFontName",fonts_folder,36.0))watermark.foreground_color=Color.bluewatermark.opacity=0.5watermark.x=10watermark.y=10watermarker.add(watermark)watermarker.save("./output.pdf")if__name__=="__main__":add_custom_font_watermark()
sample.pdf is the sample file used in this example. Click here to download it. Supply your own TrueType fonts in the folder referenced by fonts_folder.
Considering parent margins
By default, page margins are ignored and the maximum available space is used. To align the watermark with the page margins, set consider_parent_margins to True.
As you can see, the watermark goes beyond the page margins. To change this behavior, set the consider_parent_margins property to True (as shown below).
Now the watermark is aligned with respect to the page margins.
Watermarks in documents of different types
Watermarks are represented by different objects in different formats, and some properties may not be supported for a specific format (for example, background color for WordArt in Word documents). See Features Overview for details.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.
On this page
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.