SetaPDF Demos

Appearance on Several Pages

To display a signature appearance on all pages of a PDF document is a common request. But the PDF specification (at least in PDF 2.0 - before this was only documented in the "Digital Signature Appearance specifications" of Adobe) officially states that:

The location of a signature within a document can have a bearing on its legal meaning. For this reason, signature fields shall never refer to more than one annotation.

Because of this and according to viewer implementations the SetaPDF-Signer component does not support severals widget annotations for a single signature field.

This demo simply makes use of individual stamp annotations which are added to all other pages when the signature appearance is created.

It uses a proxy class that wraps the original appearance instance and adds the stamp annotations.

PLEASE NOTE: THIS DEMO ONLY WORKS FOR DOCUMENTS WHERE ALL PAGES HAVE THE SAME ROTATION.

PHP
<?php

use com\setasign\SetaPDF\Demos\Signer\Appearance\OnAllPages as AppearanceOnAllPages;

// load and register the autoload function
require_once __DIR__ . '/../../../../../bootstrap.php';
// load the wrapper class
require_once __DIR__ . '/../../../../../classes/Signer/Appearance/OnAllPages.php';

$writer = new SetaPDF_Core_Writer_Http('several-appearances.pdf');
$document = SetaPDF_Core_Document::loadByFilename(
    $assetsDirectory . '/pdfs/Brand-Guide.pdf',
//    $assetsDirectory . '/pdfs/misc/rotated/all.pdf',
    $writer
);

// create a signer instance
$signer = new SetaPDF_Signer($document);

// add a visible signature field
$field = $signer->addSignatureField(
    SetaPDF_Signer_SignatureField::DEFAULT_FIELD_NAME,
    1,
    SetaPDF_Signer_SignatureField::POSITION_RIGHT_BOTTOM,
    ['x' => -10, 'y' => 10],
    180,
    60
);

// and define that you want to use this field
$signer->setSignatureFieldName($field->getQualifiedName());

// The name property is used by the appearance module as the author of the stamp annotation
$signer->setName('www.setasign.com');

$certificatePath = $assetsDirectory . '/certificates/setapdf-no-pw.pem';

// now create a signature module
$module = new SetaPDF_Signer_Signature_Module_Pades();
// pass the path to the certificate
$module->setCertificate('file://' . $certificatePath);
// set the path to the private key (in this demo the key is also saved in the certificate file)
$module->setPrivateKey('file://' . $certificatePath, '');

// now create the appearance module and pass the signature module along
$appearance = new SetaPDF_Signer_Signature_Appearance_Dynamic($module);
// let's create a font instance to not use standard fonts (not embedded)
$font = new SetaPDF_Core_Font_Type0_Subset(
    $document,
    $assetsDirectory . '/fonts/DejaVu/ttf/DejaVuSans.ttf'
);
// and pass it to the appearance module
$appearance->setFont($font);

// pass the appearance module wrapped into the proxy class to the signer instance
$signer->setAppearance(new AppearanceOnAllPages($appearance));

// sign the document
$signer->sign($module);
PHP
<?php

namespace com\setasign\SetaPDF\Demos\Signer\Appearance;

use SetaPDF_Core_Document;
use SetaPDF_Signer;
use SetaPDF_Signer_SignatureField;

class OnAllPages extends \SetaPDF_Signer_Signature_Appearance_AbstractAppearance
{
    /**
     * @var \SetaPDF_Core_XObject_Form
     */
    protected $_formXObject;

    /**
     * @var \SetaPDF_Signer_Signature_Appearance_AbstractAppearance
     */
    protected $_mainAppearance;

    /**
     * @param \SetaPDF_Signer_Signature_Appearance_AbstractAppearance $mainAppearance
     */
    public function __construct(\SetaPDF_Signer_Signature_Appearance_AbstractAppearance $mainAppearance)
    {
        $this->_mainAppearance = $mainAppearance;
    }

    /**
     * Proxy method to the main appearance instance.
     *
     * Internally it adds stamp annotations to all other pages on the same position with the same appearance.
     *
     * @param \SetaPDF_Signer_SignatureField $field
     * @param \SetaPDF_Core_Document $document
     * @param \SetaPDF_Signer $signer
     */
    public function createAppearance(
        \SetaPDF_Signer_SignatureField $field,
        \SetaPDF_Core_Document $document,
        \SetaPDF_Signer $signer
    ) {
        $this->_mainAppearance->createAppearance($field, $document, $signer);

        $pages = $document->getCatalog()->getPages();
        $pageOfRealSignature = $pages->getPageByAnnotation($field);

        for ($pageNo = 1, $pageCount = $pages->count(); $pageNo <= $pageCount; $pageNo++) {
            $page = $pages->getPage($pageNo);

            if ($page === $pageOfRealSignature) {
                continue;
            }

            $annotation = new \SetaPDF_Core_Document_Page_Annotation_Stamp($field->getRect());
            $annotation->setName(\uniqid('', true));
            $annotation->setModificationDate(new \DateTime());
            $annotation->setPrintFlag();
            $annotation->setLocked();
            $annotation->setLockedContents();
            $annotation->setSubject(\sprintf(
                'Copy of signature appearance of signature field "%s"',
                \SetaPDF_Core_Encoding::convertPdfString($field->getQualifiedName())
            ));
            $annotation->setTextLabel($signer->getName());

            $annotation->setAppearance($this->_getFormXObject($field, $document, $signer));
            $page->getAnnotations()->add($annotation);
        }
    }

    /**
     * Get a reusable form XObject.
     *
     * @param \SetaPDF_Signer_SignatureField $field
     * @param \SetaPDF_Core_Document $document
     * @param \SetaPDF_Signer $signer
     * @return \SetaPDF_Core_XObject_Form
     */
    protected function _getFormXObject(
        \SetaPDF_Signer_SignatureField $field,
        \SetaPDF_Core_Document $document,
        \SetaPDF_Signer $signer
    ) {
        if ($this->_formXObject === null) {
            $this->_formXObject = $this->_mainAppearance->_getN2XObject($field, $document, $signer);

            $matrix = $field->getAppearance()->getIndirectObject()->ensure()->getValue()->getValue('Matrix');
            if ($matrix) {
                $this->_formXObject->setMatrix($matrix->toPhp(true));
            }
        }

        return $this->_formXObject;
    }

    /**
     * @param SetaPDF_Signer_SignatureField $field
     * @param SetaPDF_Core_Document $document
     * @param SetaPDF_Signer $signer
     * @return \SetaPDF_Core_XObject_Form
     */
    protected function _getN2XObject(
        SetaPDF_Signer_SignatureField $field,
        SetaPDF_Core_Document $document,
        SetaPDF_Signer $signer
    ) {
        return $this->_mainAppearance->_getN2XObject($field, $document, $signer);
    }
}