How to Watermark PDFs with ComPDFKit in Objective-C

Tutorials | Watermark PDFs · Objective-C · How To Thu. 15 Sep. 2022

In this article, we will talk about how to add watermarks on PDFs with the development language — Objective-C. With ComPDFKit, you could add text and image watermarks easily. There are also some methods to guide you on how to adjust the watermark like size, location, etc.



Usage of Watermark


If you look over ComPDFKit’s features list, you can see that the watermark is within the section of security. Watermarks are used to protect our documents. For instance, tell the readers who is the owner of the file, company name, or others.


Nowadays, many companies use watermarks as a way to promote. They could share some useful PDF material with watermarks to tell others something they want to promote like sales numbers and information about their activity.



Add Text Watermark


The topic of this section is text watermark which is one of these two watermark types. You could add a line of words as a watermark. Then, adjust the words such as size, types, and opacity which we will specify in the following part one by one. 


- Text Size & Types: Supports setting words font size and types by CPDFKitPlatformFont *textFont;

- Opacity: Supports setting the opacity from 0 to 100.

- Location: Adjusts the location of words by X and Y coordinate.

- HorizAlign: Sets the current alignment (Left, center, or right) for text watermarks.

- VertAlign: Sets the current alignment (top, bottom, or center) for text watermarks.

- RadAngle: Sets the rotation degrees of text watermarks between 0 and 360.


After adjusting the text watermark, you can choose the pages you want to apply the watermark to. Usually, we put the watermark behind the content of PDF documents. Of course, you can change it and put the watermark in front of the content of the PDF files.


Follow the code sample below to apply the watermarks on pages 3~5 with the text "ComPDFKit" and set them at 24px, black, 50 opacity, and 45-degree angles.


NSURL *url = [NSURL fileURLWithPath:@""];
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];

CPDFWatermark * watermark = [[CPDFWatermark alloc] initWithDocument:document type:CPDFWatermarkTypeText];
watermark.text = @"ComPDFKit";
watermark.textFont = [NSFont systemFontOfSize:24.0];
watermark.textColor = [CPDFKitPlatformColor blackColor];
watermark.opacity = 0.5;
watermark.rotation = 45.0;
watermark.pageString = @"2-4";
[document addWatermark:watermark];

Add Image Watermark


You can add an image to PDFs as a watermark with ComPDFKit. About the image watermark, you can also set the size, opacity, location, alignment, and the radAngle of the image.


Here is how to set an image watermark in the middle of page 6 with 30 opacity.

NSURL *url = [NSURL fileURLWithPath:@""];
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];

CPDFWatermark * watermark = [[CPDFWatermark alloc] initWithDocument:document type:CPDFWatermarkTypeImage];
NSURL *imageUrl = [NSURL fileURLWithPath:@""];            
watermark.image = [[CPDFKitPlatformImage alloc]initWithContentsOfURL:imageUrl];
watermark.opacity = 0.3;
watermark.pageString = @"5";
[document addWatermark:watermark];



Full-Screen Watermarks


Except for a single watermark, you could get a full-screen watermark by interface  @property (nonatomic, assign) BOOL isTilePage;. When isTilePage=YES is set, the watermark will be duplicated and cover the entire page. We can see the differences between the single watermarks and the full-screen watermarks in the picture below.


Watermarks Comparison by ComPDFKit: a single watermark & a full-screen watermark



Watermark Management


ComPDFKit provides some useful processes to manage watermarks like deleting and editing. You can edit the watermarks you set before by the interface -(NSArray<CPDFWatermark*>*)watermarks;. By the following code, you can remove all the watermarks we have mentioned above.


NSURL *url = [NSURL fileURLWithPath:@""];
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];
NSArray * watermarks = document.watermarks;

for (CPDFWatermark *watermark in watermarks) {
     [document removeWatermark:watermark];



Final Words


Watermarks are widely used in PDFs. There is another way for you to get the looks of watermarks — Background in PDFs. Connect to the links here to see more details.

Ready to Get Started?

Download our all-in-one ComPDFKit for free and run it to your project within minutes!