SetaPDF Demos PHP libraries to handle, modify or create PDF files

Add Stamp Annotations

If stamp annotations are added without appearances they may not be shown in all PDF viewer applications.
This demo shows you how to add stamp annotations including their appearances.

The appearances are simply stored as pages in a separate PDF file and imported when needed.

PHP
<?php

use setasign\SetaPDF2\Core\Document;
use setasign\SetaPDF2\Core\Document\Page\Annotation\StampAnnotation;
use setasign\SetaPDF2\Core\PageFormats;
use setasign\SetaPDF2\Core\Writer\HttpWriter;

// load and register the autoload function
require_once '../../../../../bootstrap.php';

$iconNameToPageNo = [
    StampAnnotation::ICON_APPROVED => 1,
    StampAnnotation::ICON_NOT_APPROVED => 18,
    StampAnnotation::ICON_EXPERIMENTAL => 20,
    StampAnnotation::ICON_AS_IS => 21,
    StampAnnotation::ICON_EXPIRED => 7,
    StampAnnotation::ICON_NOT_FOR_PUBLIC_RELEASE => 14,
    StampAnnotation::ICON_CONFIDENTIAL => 9,
    StampAnnotation::ICON_FINAL => 6,
    StampAnnotation::ICON_SOLD => 23,
    StampAnnotation::ICON_DEPARTMENTAL => 22,
    StampAnnotation::ICON_FOR_COMMENT => 11,
    StampAnnotation::ICON_TOP_SECRET => 19,
    StampAnnotation::ICON_DRAFT => 8,
    StampAnnotation::ICON_FOR_PUBLIC_RELEASE => 15,
    'Verified' => 2,
    'Revised' => 3,
    'Reviewed' => 4,
    'Received' => 5,
    'Completed' => 10,
    'InformationOnly' => 12,
    'PreliminaryResults' => 13,
    'Void' => 16,
    'Emergency' => 17
];

$iconName = displaySelect('Icon Name:', $iconNameToPageNo, true, true);
$x = 50;
$y = 700;
$width = 180;

$writer = new HttpWriter('stamped.pdf', true);
$document = new Document($writer);

$page = $document->getCatalog()->getPages()->create(PageFormats::A4);

$stampAppearances = Document::loadByFilename($assetsDirectory . '/pdfs/stamps.pdf');
$appearancePageNo = $iconNameToPageNo[$iconName];
if (isset($appearancePageNo)) {
    $appearance = $stampAppearances->getCatalog()->getPages()->getPage($appearancePageNo)->toXObject($document);
    $height = $appearance->getHeight($width);

    $annotation = new StampAnnotation([$x, $y, $x + $width, $y + $height]);
    $annotation->setIconName($iconName);
    $annotation->setAppearance($appearance);
    $annotation->setName(uniqid('', true));

    $page->getAnnotations()->add($annotation);
}

$document->save()->finish();