Guides
Add Watermarks in PDF
This sample shows how to add watermarks, including text, image and tile watermarks, and delete watermarks
objective-c
// Init Methods
- (instancetype)initWithDocument:(CPDFDocument *)document {
CPDFDocument *myDocument = document;
[self addTextWatermark:myDocument];
[self addImageWatermark:myDocument];
[self addTilesWatermark:myDocument];
[self deletetWatermark];
}
// Insert text watermark
- (void)addTextWatermark:(CPDFDocument *)oldDocument {
NSString *commandLineStr = @"";
commandLineStr = [commandLineStr stringByAppendingString:@"-------------------------------------\n"];
commandLineStr = [commandLineStr stringByAppendingString:@"Samples 1: Insert text watermark\n"];
// Get Sandbox path for saving the PDFFile
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"Watermark"];
if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
[[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"AddTextWatermarkTest"];
// Save the document in the test PDF file
NSURL *addTextWatermarkURL = [NSURL fileURLWithPath:writeFilePath];
[oldDocument writeToURL:addTextWatermarkURL];
// Create a new document for test PDF file
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:addTextWatermarkURL];
// Create text watermark
CPDFWatermark *watermark = [[CPDFWatermark alloc] initWithDocument:document type:CPDFWatermarkTypeText];
watermark.text = @"ComPDFKit"; // The text for the watermark (image watermark does not work).
watermark.textFont = [UIFont fontWithName:@"Helvetica" size:30]; // The text font for the watermark (image watermark does not work). Default Font : Helvetica 24.
watermark.textColor = [UIColor redColor]; // The text color for the watermark (image watermark does not work).
watermark.scale = 2.0; // Watermark scaling with default `1`, if it is a picture watermark `1` represents the original size of the picture, if it is a text watermark `1` represents the `textFont` font size.
watermark.rotation = 45; // Watermark rotation angle, the range of 0~360, with the default of 0.
watermark.opacity = 0.5; // Watermark transparency, the range of 0~1, with the default of 1.
watermark.verticalPosition = CPDFWatermarkVerticalPositionCenter; // Vertical alignment of the watermark.
watermark.horizontalPosition = CPDFWatermarkHorizontalPositionCenter; // Horizontal alignment of the watermark.
watermark.tx = 0.0; // The translation relative to the horizontal position. Positive numbers are shifted to the right, negative numbers are shifted to the left.
watermark.ty = 0.0; // The translation relative to the vertical position. Positive numbers are shifted downwards, negative numbers are shifted upwards.
watermark.isFront = YES; // Set watermark to locate in front of the content.
watermark.isTilePage = NO; // Set tiled watermark for the page(image watermark does not work).
watermark.pageString = @"0-4";
[document addWatermark:watermark];
[document updateWatermark:watermark];
[document writeToURL:addTextWatermarkURL];
// Print text watermark object message
commandLineStr = [commandLineStr stringByAppendingFormat:@"Text :%@", watermark.text];
CGFloat red, green, blue, alpha;
[watermark.textColor getRed:&red green:&green blue:&blue alpha:&alpha];
commandLineStr = [commandLineStr stringByAppendingFormat:@"Color : red:%.1f, green:%.1f, blue:%.1f, alpha:%.1f\n", red, green, blue, alpha];
commandLineStr = [commandLineStr stringByAppendingFormat:@"FontSize :%.1f\n", watermark.textFont.pointSize];
commandLineStr = [commandLineStr stringByAppendingFormat:@"Opacity :%.1f\n", watermark.opacity];
commandLineStr = [commandLineStr stringByAppendingFormat:@"Vertalign :%@\n", [self getStringFromEnumVertalign:watermark.verticalPosition]];
commandLineStr = [commandLineStr stringByAppendingFormat:@"Horizalign :%@\n", [self getStringFromEnumHorizalign:watermark.horizontalPosition]];
commandLineStr = [commandLineStr stringByAppendingFormat:@"VertOffset :%.1f\n", watermark.tx];
commandLineStr = [commandLineStr stringByAppendingFormat:@"HorizOffset :%.1f\n", watermark.ty];
commandLineStr = [commandLineStr stringByAppendingFormat:@"Pages :%@\n", watermark.pageString];
commandLineStr = [commandLineStr stringByAppendingFormat:@"VerticalSpacing :%.1f\n", watermark.verticalSpacing];
commandLineStr = [commandLineStr stringByAppendingFormat:@"HorizontalSpacing :%.1f\n", watermark.horizontalSpacing];
commandLineStr = [commandLineStr stringByAppendingString:@"Done. Results saved in AddTextWatermarkTest.pdf\n"];
}
// Insert Image Watermark
- (void)addImageWatermark:(CPDFDocument *)oldDocument {
NSString *commandLineStr = @"";
commandLineStr = [commandLineStr stringByAppendingString:@"-------------------------------------\n"];
commandLineStr = [commandLineStr stringByAppendingString:@"Samples 2: Insert Image Watermark\n"];
// Get Sandbox path for saving the PDFFile
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"Watermark"];
if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
[[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"AddImageWatermarkTest"];
// Save the document in the test PDF file
NSURL *addImageWatermarkURL = [NSURL fileURLWithPath:writeFilePath];
[oldDocument writeToURL:addImageWatermarkURL];
// Create a new document for test PDF file
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:addImageWatermarkURL];
// Create a image watermark
CPDFWatermark *watermark = [[CPDFWatermark alloc] initWithDocument:document type:CPDFWatermarkTypeImage];
watermark.image = [UIImage imageNamed:@"Logo"];
watermark.scale = 2.0; // Watermark scaling with default `1`, if it is a picture watermark `1` represents the original size of the picture, if it is a text watermark `1` represents the `textFont` font size.
watermark.rotation = 45; // Watermark rotation angle, the range of 0~360, with the default of 0.
watermark.opacity = 0.5; // Watermark transparency, the range of 0~1, with the default of 1.
watermark.verticalPosition = CPDFWatermarkVerticalPositionCenter; // Vertical alignment of the watermark.
watermark.horizontalPosition = CPDFWatermarkHorizontalPositionCenter; // Horizontal alignment of the watermark.
watermark.tx = 0.0; // The translation relative to the horizontal position. Positive numbers are shifted to the right, negative numbers are shifted to the left.
watermark.ty = 0.0; // The translation relative to the vertical position. Positive numbers are shifted downwards, negative numbers are shifted upwards.
watermark.isFront = YES; // Set watermark to locate in front of the content.
watermark.pageString = @"0-4";
[document addWatermark:watermark];
[document writeToURL:addImageWatermarkURL];
// Print text watermark object message
commandLineStr = [commandLineStr stringByAppendingFormat:@"Opacity :%.1f\n", watermark.opacity];
commandLineStr = [commandLineStr stringByAppendingFormat:@"Vertalign :%@\n", [self getStringFromEnumVertalign:watermark.verticalPosition]];
commandLineStr = [commandLineStr stringByAppendingFormat:@"Horizalign :%@\n", [self getStringFromEnumHorizalign:watermark.horizontalPosition]];
commandLineStr = [commandLineStr stringByAppendingFormat:@"VertOffset :%.1f\n", watermark.tx];
commandLineStr = [commandLineStr stringByAppendingFormat:@"HorizOffset :%.1f\n", watermark.ty];
commandLineStr = [commandLineStr stringByAppendingFormat:@"Pages :%@\n", watermark.pageString];
commandLineStr = [commandLineStr stringByAppendingFormat:@"VerticalSpacing :%.1f\n", watermark.verticalSpacing];
commandLineStr = [commandLineStr stringByAppendingFormat:@"HorizontalSpacing :%.1f\n", watermark.horizontalSpacing];
commandLineStr = [commandLineStr stringByAppendingString:@"Done. Results saved in AddImageWatermarkTest.pdf\n"];
}
// Insert Text Tiles Watermark
- (void)addTilesWatermark:(CPDFDocument *)oldDocument {
NSString *commandLineStr = @"";
commandLineStr = [commandLineStr stringByAppendingString:@"-------------------------------------\n"];
commandLineStr = [commandLineStr stringByAppendingString:@"Samples 3: Insert Text Tiles Watermark\n"];
// Get Sandbox path for saving the PDFFile
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"Watermark"];
if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
[[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"AddTilesWatermarkTest"];
// Save the document in the test PDF file
NSURL *addTilesWatermarkURL = [NSURL fileURLWithPath:writeFilePath];
[oldDocument writeToURL:addTilesWatermarkURL];
// Create a new document for test PDF file
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:addTilesWatermarkURL];
// Create text tiles watermark
CPDFWatermark *watermark = [[CPDFWatermark alloc] initWithDocument:document type:CPDFWatermarkTypeText];
watermark.text = @"ComPDFKit"; // The text for the watermark (image watermark does not work).
watermark.textFont = [UIFont fontWithName:@"Helvetica" size:30]; // The text font for the watermark (image watermark does not work). Default Font : Helvetica 24.
watermark.textColor = [UIColor redColor]; // The text color for the watermark (image watermark does not work).
watermark.scale = 2.0; // Watermark scaling with default `1`, if it is a picture watermark `1` represents the original size of the picture, if it is a text watermark `1` represents the `textFont` font size.
watermark.rotation = 45; // Watermark rotation angle, the range of 0~360, with the default of 0.
watermark.opacity = 0.5; // Watermark transparency, the range of 0~1, with the default of 1.
watermark.verticalPosition = CPDFWatermarkVerticalPositionCenter; // Vertical alignment of the watermark.
watermark.horizontalPosition = CPDFWatermarkHorizontalPositionCenter; // Horizontal alignment of the watermark.
watermark.tx = 0.0; // The translation relative to the horizontal position. Positive numbers are shifted to the right, negative numbers are shifted to the left.
watermark.ty = 0.0; // The translation relative to the vertical position. Positive numbers are shifted downwards, negative numbers are shifted upwards.
watermark.isFront = YES; // Set watermark to locate in front of the content.
watermark.isTilePage = YES; // Set tiled watermark for the page(image watermark does not work).
watermark.verticalSpacing = 10; // Set the vertical spacing for the tiled watermark.
watermark.horizontalSpacing = 10; // Set the horizontal spacing for the tiled watermark.
watermark.pageString = @"0-4";
[document addWatermark:watermark];
[document writeToURL:addTilesWatermarkURL];
// Print text tiles watermark message
commandLineStr = [commandLineStr stringByAppendingFormat:@"Text :%@\n", watermark.text];
CGFloat red, green, blue, alpha;
[watermark.textColor getRed:&red green:&green blue:&blue alpha:&alpha];
commandLineStr = [commandLineStr stringByAppendingFormat:@"Color : red:%.1f, green:%.1f, blue:%f, alpha:%.1f\n", red, green, blue, alpha];
commandLineStr = [commandLineStr stringByAppendingFormat:@"FontSize :%.1f\n", watermark.textFont.pointSize];
commandLineStr = [commandLineStr stringByAppendingFormat:@"Opacity :%.1f\n", watermark.opacity];
commandLineStr = [commandLineStr stringByAppendingFormat:@"Vertalign :%@\n", [self getStringFromEnumVertalign:watermark.verticalPosition]];
commandLineStr = [commandLineStr stringByAppendingFormat:@"Horizalign :%@\n", [self getStringFromEnumHorizalign:watermark.horizontalPosition]];
commandLineStr = [commandLineStr stringByAppendingFormat:@"VertOffset :%.1f\n", watermark.tx];
commandLineStr = [commandLineStr stringByAppendingFormat:@"HorizOffset :%.1f\n", watermark.ty];
commandLineStr = [commandLineStr stringByAppendingFormat:@"Pages :%@\n", watermark.pageString];
commandLineStr = [commandLineStr stringByAppendingFormat:@"VerticalSpacing :%.1f\n", watermark.verticalSpacing];
commandLineStr = [commandLineStr stringByAppendingFormat:@"HorizontalSpacing :%.1f\n", watermark.horizontalSpacing];
commandLineStr = [commandLineStr stringByAppendingString:@"Done. Results saved in AddTilesWatermarkTest.pdf\n"];
}
// Delete Watermark
- (void)deletetWatermark {
NSString *commandLineStr = @"";
commandLineStr = [commandLineStr stringByAppendingString:@"-------------------------------------\n"];
commandLineStr = [commandLineStr stringByAppendingString:@"Samples 4:Delete Watermark\n"];
// Get Sandbox path for saving the PDF File
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"Watermark"];
NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@/%@.pdf", @"Documents",@"Watermark",@"AddTextWatermarkTest"];
// Copy file
if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
[[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"DeleteWatermarkTest"];
if ([[NSFileManager defaultManager] fileExistsAtPath:documentFolder])
[[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:documentFolder] toURL:[NSURL fileURLWithPath:writeFilePath] error:nil];
NSURL *deleteWatermarkURL = [NSURL fileURLWithPath:writeFilePath];
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:deleteWatermarkURL];
NSArray *waterArray = [document watermarks];
[document removeWatermark:waterArray[0]];
[document writeToURL:deleteWatermarkURL];
commandLineStr = [commandLineStr stringByAppendingString:@"Done. Results saved in DeleteWatermarkTest.pdf\n"];
}
- (NSString *)getStringFromEnumVertalign:(NSInteger)enums {
switch (enums) {
case 0:
return @"CPDFWatermarkVerticalPositionTop";
break;
case 1:
return @"WATERMARK_VERTALIGN_CENTER";
break;
case 2:
return @"CPDFWatermarkVerticalPositionBottom";
break;
default:
return @" ";
break;
}
}
- (NSString *)getStringFromEnumHorizalign:(NSInteger)enums {
switch (enums) {
case 0:
return @"CPDFWatermarkHorizontalPositionLeft";
break;
case 1:
return @"CPDFWatermarkHorizontalPositionCenter";
break;
case 2:
return @"CPDFWatermarkHorizontalPositionRight";
break;
default:
return @" ";
break;
}
}