Dart Requires Chilkat v11.7.0+
Dart
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 Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// 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
final pdf = CkPdf();
try {
pdf.loadFile('qa_data/pdf/four_pages.pdf');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
print('pages in the document: ${pdf.numPages}');
final json = CkJsonObject();
final jsonOut = CkJsonObject();
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);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
print('every page but the cover: ${jsonOut.stringOf('pages')}');
// A list of pages and ranges, on a second copy of the document.
final pdf2 = CkPdf();
try {
pdf2.loadFile('qa_data/pdf/four_pages.pdf');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
final json2 = CkJsonObject();
json2.updateString('type', 'text');
json2.updateString('text', 'APPENDIX');
json2.updateString('id', 'appendix');
json2.updateString('pages', '2,4-');
try {
pdf2.watermark('add', json2, jsonOut);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
print('page 2 and 4 onwards: ${jsonOut.stringOf('pages')}');
try {
pdf.writeFile('qa_output/four_pages_internal.pdf');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Sample output:
// pages in the document: 4
// every page but the cover: 2-4
// page 2 and 4 onwards: 2,4
}