PHP ActiveX Requires Chilkat v11.7.0+
PHP ActiveX
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 PHP ActiveX Downloads
<?php
$success = 0;
// 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
$pdf = new COM("Chilkat.Pdf");
$success = $pdf->LoadFile('qa_data/pdf/four_pages.pdf');
if ($success == 0) {
print $pdf->LastErrorText . "\n";
exit;
}
print 'pages in the document: ' . $pdf->NumPages . "\n";
$json = new COM("Chilkat.JsonObject");
$jsonOut = new COM("Chilkat.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');
$success = $pdf->Watermark('add',$json,$jsonOut);
if ($success == 0) {
print $pdf->LastErrorText . "\n";
exit;
}
print 'every page but the cover: ' . $jsonOut->stringOf('pages') . "\n";
// A list of pages and ranges, on a second copy of the document.
$pdf2 = new COM("Chilkat.Pdf");
$success = $pdf2->LoadFile('qa_data/pdf/four_pages.pdf');
if ($success == 0) {
print $pdf2->LastErrorText . "\n";
exit;
}
$json2 = new COM("Chilkat.JsonObject");
$json2->UpdateString('type','text');
$json2->UpdateString('text','APPENDIX');
$json2->UpdateString('id','appendix');
$json2->UpdateString('pages','2,4-');
$success = $pdf2->Watermark('add',$json2,$jsonOut);
if ($success == 0) {
print $pdf2->LastErrorText . "\n";
exit;
}
print 'page 2 and 4 onwards: ' . $jsonOut->stringOf('pages') . "\n";
$success = $pdf->WriteFile('qa_output/four_pages_internal.pdf');
if ($success == 0) {
print $pdf->LastErrorText . "\n";
exit;
}
// Sample output:
// pages in the document: 4
// every page but the cover: 2-4
// page 2 and 4 onwards: 2,4
?>