Using text watermarks is an effective way to protect the integrity of document content. By marking sensitive documents with watermarks like “Confidential” or “Draft,” organizations can discourage unauthorized sharing and strengthen data security measures. This visual indicator acts as a continual reminder of the document’s restricted status, encouraging careful handling by anyone who views it.
The following code snippet shows how to add text watermark to a document.
If the document consists of multiple parts (pages, worksheets, slides, frames, etc.), the watermark will be added to each of them. You can configure this behaivior through the PagesSetup property:
Using GroupDocs.Watermark, you can also add watermark to some absolute position in the document. Following example shows how to add a text watermark with absolute positioning using properties X, Y, Width and Height. The values of all properties for absolute sizing and positioning are measured in default document units.
In the example above, absolute margin values are used. This means that margins are measured in document units. But you can set relative margins for a watermark as well (as shown in below example), using the MarginType property
GroupDocs.Watermark API also supports rotation of the watermark. You can use [RotateAngle] property to define watermark rotation angle in degrees. A positive value means clockwise rotation.
constwatermarker=newgroupdocsWatermark.Watermarker('test.docx');constwatermark=newgroupdocsWatermark.TextWatermark('Test watermark',newgroupdocsWatermark.Font('Calibri',32));watermark.setHorizontalAlignment(groupdocsWatermark.HorizontalAlignment.Right);watermark.setVerticalAlignment(groupdocsWatermark.VerticalAlignment.Top);watermark.setSizingType(groupdocsWatermark.SizingType.ScaleToParentDimensions);watermark.setScaleFactor(0.5);// Set rotation angle
watermark.setRotateAngle(45.0);watermarker.add(watermark);watermarker.save("result.docx");watermarker.close();
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
GroupDocs.Watermark API provides ability to use custom TrueType fonts. For this you can configure FolderPath property through the Font class constructor with a folder path which contains fonts files.
constwatermarker=newgroupdocsWatermark.Watermarker('test.pdf');varfontsFolder=@"c:\CustomFonts\";
// Initialize the font to be used for watermark and folder with it
var font = new groupdocsWatermark.Font("OTChekhardaBoldItalic",fontsFolder,36)constwatermark=newgroupdocsWatermark.TextWatermark('top secret',font);watermark.setForegroundColor(groupdocsWatermark.Color.getRed());watermark.setX(10);watermark.setY(100);watermark.setOpacity(0.4);watermarker.add(watermark);watermarker.save(outputFilePath);watermarker.close();
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.