React Native Requires Chilkat v11.7.0+
React Native
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 React Native Downloads
import { JsonObject, Pdf } from '@chilkat/react-native'
function chilkatExample() {
// 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
const pdf = new Pdf();
try {
pdf.loadFile('qa_data/pdf/two_page_memo.pdf');
} catch {
console.log(pdf.lastErrorText);
return;
}
const json = new JsonObject();
json.updateString('type', 'text');
json.updateString('text', 'UNCONTROLLED WHEN PRINTED');
json.updateString('id', 'printonly');
// As a layer: off on screen, on when printing, and locked so the reader cannot switch it on.
json.updateString('approach', 'layer');
json.updateString('layer.name', 'Print marking');
json.updateString('layer.view', 'off');
json.updateString('layer.print', 'on');
json.updateBool('layer.locked', true);
json.updateString('rotation', 'diagonal');
json.updateString('scale.fit', 'page');
json.updateNumber('scale.fitPercent', '70');
json.updateNumber('opacity', '0.3');
const jsonOut = new JsonObject();
try {
pdf.watermark('add', json, jsonOut);
} catch {
console.log(pdf.lastErrorText);
return;
}
console.log(`layer id: ${jsonOut.stringOf('id')} pages: ${jsonOut.stringOf('pages')}`);
try {
pdf.writeFile('qa_output/memo_print_only.pdf');
} catch {
console.log(pdf.lastErrorText);
return;
}
// The same thing as an annotation instead.
const pdf2 = new Pdf();
try {
pdf2.loadFile('qa_data/pdf/two_page_memo.pdf');
} catch {
console.log(pdf2.lastErrorText);
return;
}
const json2 = new JsonObject();
json2.updateString('type', 'text');
json2.updateString('text', 'UNCONTROLLED WHEN PRINTED');
json2.updateString('id', 'printonly2');
json2.updateString('approach', 'annotation');
json2.updateBool('annotation.view', false);
json2.updateBool('annotation.print', true);
json2.updateBool('annotation.fixedPrint', true);
json2.updateString('rotation', 'diagonal');
try {
pdf2.watermark('add', json2, jsonOut);
} catch {
console.log(pdf2.lastErrorText);
return;
}
console.log(`annotation id: ${jsonOut.stringOf('id')}`);
// Sample output:
// layer id: printonly pages: 1-2
// annotation id: printonly2
}