Sample code for 30+ languages & platforms
Objective-C Requires Chilkat v11.7.0+

A PDF Watermark That Only Appears When Printed

See more PDF Watermark Examples

Demonstrates a watermark that is invisible on screen and appears on paper, which is what a "printed copies are uncontrolled" marking needs. Two approaches can do it: an optional-content layer with layer.view off and layer.print on, which also gives the reader an entry in the layers panel of the viewer (layer.locked stops them switching it), and a Watermark annotation with annotation.view false and annotation.print true. annotation.fixedPrint keeps the printed size and position the same whatever paper it goes on.

Chilkat Objective-C Downloads

Objective-C
#import <CkoPdf.h>
#import <CkoJsonObject.h>

BOOL success = NO;

//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

//  The PDF used in this example is available at two_page_memo.pdf

CkoPdf *pdf = [[CkoPdf alloc] init];
success = [pdf LoadFile: @"qa_data/pdf/two_page_memo.pdf"];
if (success == NO) {
    NSLog(@"%@",pdf.LastErrorText);
    return;
}

CkoJsonObject *json = [[CkoJsonObject alloc] init];
[json UpdateString: @"type" value: @"text"];
[json UpdateString: @"text" value: @"UNCONTROLLED WHEN PRINTED"];
[json UpdateString: @"id" value: @"printonly"];

//  As a layer: off on screen, on when printing, and locked so the reader cannot switch it on.
[json UpdateString: @"approach" value: @"layer"];
[json UpdateString: @"layer.name" value: @"Print marking"];
[json UpdateString: @"layer.view" value: @"off"];
[json UpdateString: @"layer.print" value: @"on"];
[json UpdateBool: @"layer.locked" value: YES];

[json UpdateString: @"rotation" value: @"diagonal"];
[json UpdateString: @"scale.fit" value: @"page"];
[json UpdateNumber: @"scale.fitPercent" numericStr: @"70"];
[json UpdateNumber: @"opacity" numericStr: @"0.3"];

CkoJsonObject *jsonOut = [[CkoJsonObject alloc] init];
success = [pdf Watermark: @"add" jsonIn: json jsonOut: jsonOut];
if (success == NO) {
    NSLog(@"%@",pdf.LastErrorText);
    return;
}

NSLog(@"%@%@%@%@",@"layer id: ",[jsonOut StringOf: @"id"],@" pages: ",[jsonOut StringOf: @"pages"]);

success = [pdf WriteFile: @"qa_output/memo_print_only.pdf"];
if (success == NO) {
    NSLog(@"%@",pdf.LastErrorText);
    return;
}

//  The same thing as an annotation instead.
CkoPdf *pdf2 = [[CkoPdf alloc] init];
success = [pdf2 LoadFile: @"qa_data/pdf/two_page_memo.pdf"];
if (success == NO) {
    NSLog(@"%@",pdf2.LastErrorText);
    return;
}

CkoJsonObject *json2 = [[CkoJsonObject alloc] init];
[json2 UpdateString: @"type" value: @"text"];
[json2 UpdateString: @"text" value: @"UNCONTROLLED WHEN PRINTED"];
[json2 UpdateString: @"id" value: @"printonly2"];
[json2 UpdateString: @"approach" value: @"annotation"];
[json2 UpdateBool: @"annotation.view" value: NO];
[json2 UpdateBool: @"annotation.print" value: YES];
[json2 UpdateBool: @"annotation.fixedPrint" value: YES];
[json2 UpdateString: @"rotation" value: @"diagonal"];
success = [pdf2 Watermark: @"add" jsonIn: json2 jsonOut: jsonOut];
if (success == NO) {
    NSLog(@"%@",pdf2.LastErrorText);
    return;
}

NSLog(@"%@%@",@"annotation id: ",[jsonOut StringOf: @"id"]);

//  Sample output:

//  layer id: printonly pages: 1-2
//  annotation id: printonly2