Show On Page
The $showOnPage parameter of the addStamp() method accepts various values defining the
page or pages to show the stamp on. This demo shows you some examples.
PHP
<?php
use setasign\SetaPDF2\Core\Document;
use setasign\SetaPDF2\Core\Font\TrueType\Subset;
use setasign\SetaPDF2\Core\PageFormats;
use setasign\SetaPDF2\Core\Writer\HttpWriter;
use setasign\SetaPDF2\Stamper\Stamp\TextStamp;
use setasign\SetaPDF2\Stamper\Stamper;
// load and register the autoload function
require_once __DIR__ . '/../../../../../bootstrap.php';
$showOnPageOptions = require 'options.php';
$value = displaySelect('Show on page:', $showOnPageOptions);
$data = $showOnPageOptions[$value];
$writer = new HttpWriter('positioning-and-translate.pdf', true);
$document = new Document($writer);
// let's add some pages for demonstration purpose
$pages = $document->getCatalog()->getPages();
for ($i = 100; $i > 0; $i--) {
$pages->create(
PageFormats::A4,
($i & 1) ? PageFormats::ORIENTATION_PORTRAIT : PageFormats::ORIENTATION_LANDSCAPE
);
}
// create a stamper instance
$stamper = new Stamper($document);
// create a font instance which is needed for the text stamp instance
$font = new Subset(
$document,
$assetsDirectory . '/fonts/DejaVu/ttf/DejaVuSans.ttf'
);
// create a stamp instance
$stamp = new TextStamp($font, 12);
$stamp->setBackgroundColor([0.5, 1, 1]);
$stamp->setBorderWidth(1);
$stamp->setPadding(2);
$stamp->setTextWidth(180);
$stamp->setText('A simple example text to demonstrate showOnPage parameter.');
// add the stamp object on all pages on the given position
$stamper->addStamp(
$stamp,
Stamper::POSITION_LEFT_TOP,
$data['showOnPage']
);
// execute the stamp process
$stamper->stamp();
// save and finish the document instance
$document->save()->finish();
PHP
<?php
use setasign\SetaPDF2\Stamper\Stamper;
return [
[
'displayValue' => '\setasign\SetaPDF2\Stamper\Stamper::PAGES_ALL',
'showOnPage' => Stamper::PAGES_ALL
],
[
'displayValue' => '\setasign\SetaPDF2\Stamper\Stamper::PAGES_EVEN',
'showOnPage' => Stamper::PAGES_EVEN
],
[
'displayValue' => '\setasign\SetaPDF2\Stamper\Stamper::PAGES_ODD',
'showOnPage' => Stamper::PAGES_ODD
],
[
'displayValue' => '\setasign\SetaPDF2\Stamper\Stamper::PAGES_FIRST',
'showOnPage' => Stamper::PAGES_FIRST
],
[
'displayValue' => '\setasign\SetaPDF2\Stamper\Stamper::PAGES_LAST',
'showOnPage' => Stamper::PAGES_LAST
],
[
'displayValue' => '4',
'showOnPage' => 4
],
[
'displayValue' => "'2-' (2nd page until the last page)",
'showOnPage' => '2-'
],
[
'displayValue' => "'1-5' (page 1 to 5)",
'showOnPage' => '1-5'
],
[
'displayValue' => '[3, 5, 8, 99]',
'showOnPage' => [3, 5, 8, 99]
],
[
'displayValue' => 'second last page (callback function)',
'showOnPage' => function($pageNumber, $pageCount) {
return $pageNumber === ($pageCount - 1);
}
]
];
