Skip to content
Guides

Add Text Watermark

The steps to add a text watermark are as follows:

  1. Initialize a CPDFWatermark object, specifying the watermark type as text.

  2. Set the properties required for the text watermark, including content, font, color, and font size.

  3. Set the general properties for the watermark.

  4. Create the watermark in the document.

This example shows how to add a text watermark:

swift
// Initialize a CPDFWatermark object, specifying the type as text.
let watermark = CPDFWatermark(document: document, type: .text)

// Set the text content, font, color, and font size properties.
watermark.text = "ComPDFKit"
watermark.textFont = UIFont(name: "Helvetica", size: 30)
watermark.textColor = UIColor.red
watermark.scale = 2.0
watermark.rotation = 45
watermark.opacity = 0.5
watermark.verticalPosition = .center
watermark.horizontalPosition = .center
watermark.tx = 0.0
watermark.ty = 0.0
watermark.isFront = true
watermark.isTilePage = false
watermark.pageString = "0-4"

document.addWatermark(watermark)
document.updateWatermark(watermark)

// Create a watermark in the document.
document.write(to: self.addTextWatermarkURL)
objective-c
//Initialize a CPDFWatermark object, specifying the type as text.
CPDFWatermark *watermark = [[CPDFWatermark alloc] initWithDocument:document type:CPDFWatermarkTypeText];
//Set the text content, font, color, and font size properties.
watermark.text = @"ComPDFKit"; 
watermark.textFont = [UIFont fontWithName:@"Helvetica" size:30]; 
watermark.textColor = [UIColor redColor]; 
watermark.scale = 2.0; 

watermark.rotation = 45; 
watermark.opacity = 0.5; 
watermark.verticalPosition = CPDFWatermarkVerticalPositionCenter; 
watermark.horizontalPosition = CPDFWatermarkHorizontalPositionCenter; 
watermark.tx = 0.0; 
watermark.ty = 0.0; 
watermark.isFront = YES; 
watermark.isTilePage = NO; 
watermark.pageString = @"0-4";
[document addWatermark:watermark];
[document updateWatermark:watermark];
//Create a watermark in the document.
[document writeToURL:self.addTextWatermarkURL];