Skip to content

添加文本水印

添加文字水印的步骤如下:

​ 1.初始化CPDFWatermark对象,指定水印类型为文本。

​ 2.设置文字水印需要的属性,包含内容,字体,颜色,字号。

​ 3.设置水印通用属性。

​ 4.在文档中创建水印。

以下是创建文字水印的关键代码:

swift
// 初始化 CPDFWatermark 对象,指定类型为文本。
let watermark = CPDFWatermark(document: document, type: .text)

// 设置文本内容,字体,颜色,字号属性。
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)

// 在文档中创建水印。
document.write(to: self.addTextWatermarkURL)
objective-c
//初始化CPDFWatermark对象,指定类型为文本。
CPDFWatermark *watermark = [[CPDFWatermark alloc] initWithDocument:document type:CPDFWatermarkTypeText];
//设置文本内容,字体,颜色,字号属性。
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];
//在文档中创建水印。
[document writeToURL:self.addTextWatermarkURL];