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 text watermark to a document.
If a document consists of multiple parts (pages, worksheets, slides, frames, etc.), the watermark will be added to each of them. You can configure this behavior via the PagesSetup property:
You can add a watermark to an absolute position by setting x, y, width, and height. Values are measured in default document units.
importgroupdocs.watermarkasgwimportgroupdocs.watermark.watermarksasgwwwithgw.Watermarker("image.png")aswatermarker:font=gww.Font("Times New Roman",8.0)watermark=gww.TextWatermark("Test watermark",font)watermark.x=10watermark.y=20watermark.width=100watermark.height=40watermarker.add(watermark)watermarker.save("image.png")
Warning
Note that the origin of coordinates may be different for different document types (relative positioning doesn’t have these specifics and can be used as a unified positioning approach).
Following are the origin of the coordinates for different formats of the documents.
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 watermark depending on the parent size using sizing_type and scale_factor.
importgroupdocs.watermarkasgwimportgroupdocs.watermark.watermarksasgwwwithgw.Watermarker("image.png")aswatermarker:font=gww.Font("Calibri",12.0)watermark=gww.TextWatermark("This is a test watermark",font)watermark.sizing_type=gww.SizingType.SCALE_TO_PARENT_DIMENSIONSwatermark.scale_factor=0.5watermarker.add(watermark)watermarker.save("image.png")
Note
Using of relative size and positioning is the simplest way to add watermark to a document of any type.
Watermark rotation
Rotate watermark by setting rotate_angle in degrees. Positive values mean clockwise.
If rotation angle is set, it is assumed that watermark size is equal to axis-aligned bounding box size. The following picture illustrates what is the watermark bounding box and how it is used for sizing and positioning. The picture shows a result of execution of the above code snippet. The actual watermark bounds are colored in blue and the bounding box is colored in red. As you can see, the bounding box size is used to calculate watermark relative size.
Using custom fonts
You can use custom TrueType fonts by specifying a folder path in the Font constructor.
By default, page margins are ignored and the maximum available space is used. To align with page margins, set consider_parent_margins to True.
As you can see, the watermark goes beyond page margins. To change this behavior, set ConsiderParentMargins property to true (as shown in below example).
Now, the watermark is aligned with respect to page margins.
Watermark in documents of different types
Watermarks are represented by different objects in different formats. Some properties may not be supported for specific formats (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.