React Native Requires Chilkat v11.7.0+
React Native
Watermark Only Some of a PDF's Pages
See more PDF Watermark Examples
Demonstrates choosing the pages to stamp. "pages" takes all (the default), first, last, odd, even, or a list of 1-based pages and ranges such as 1,3,5-9,12- where a trailing dash means "to the end". "excludePages" takes the same syntax and is applied afterwards, which is how you say "every page except the cover". The result reports the pages the watermark actually landed 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 four_pages.pdf
const pdf = new Pdf();
try {
pdf.loadFile('qa_data/pdf/four_pages.pdf');
} catch {
console.log(pdf.lastErrorText);
return;
}
console.log(`pages in the document: ${pdf.numPages}`);
const json = new JsonObject();
const jsonOut = new JsonObject();
json.updateString('type', 'text');
json.updateString('text', 'INTERNAL');
json.updateString('id', 'internal');
// Every page except the first.
json.updateString('pages', 'all');
json.updateString('excludePages', '1');
try {
pdf.watermark('add', json, jsonOut);
} catch {
console.log(pdf.lastErrorText);
return;
}
console.log(`every page but the cover: ${jsonOut.stringOf('pages')}`);
// A list of pages and ranges, on a second copy of the document.
const pdf2 = new Pdf();
try {
pdf2.loadFile('qa_data/pdf/four_pages.pdf');
} catch {
console.log(pdf2.lastErrorText);
return;
}
const json2 = new JsonObject();
json2.updateString('type', 'text');
json2.updateString('text', 'APPENDIX');
json2.updateString('id', 'appendix');
json2.updateString('pages', '2,4-');
try {
pdf2.watermark('add', json2, jsonOut);
} catch {
console.log(pdf2.lastErrorText);
return;
}
console.log(`page 2 and 4 onwards: ${jsonOut.stringOf('pages')}`);
try {
pdf.writeFile('qa_output/four_pages_internal.pdf');
} catch {
console.log(pdf.lastErrorText);
return;
}
// Sample output:
// pages in the document: 4
// every page but the cover: 2-4
// page 2 and 4 onwards: 2,4
}