Create a Signature Field

Introduction

A signature in a PDF document requires a signature field. This field may be hidden or displayed through a widget annotation on a specific page.

The SetaPDF-Signer component offers a class that allows you to access or create signature fields: The SetaPDF_Signer_SignatureField class.

Create Hidden Signature Fields

Simple hidden signature fields can be created by just some lines of code. The document instance could be created by an existing document as well! 

PHP
<?php
require_once('library/SetaPDF/Autoload.php');

// create a writer
$writer = new \SetaPDF_Core_Writer_Http('hidden-field.pdf', true);
// create a new document instance
$document = new \SetaPDF_Core_Document($writer);
// create at least a single page
$document->getCatalog()->getPages()->create(\SetaPDF_Core_PageFormats::A4);

// add some hidden signature fields on page 1
\SetaPDF_Signer_SignatureField::add($document, 'Signature field 1');
\SetaPDF_Signer_SignatureField::add($document, 'Signature field 2');

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

To automatically get or create a signature field instance the class also offers a get() method, that will create a hidden signature field automatically if no signature field was found: 

PHP
$field = SetaPDF_Signer_SignatureField::get($document, 'Signature');

Creating Visible Signature Fields

The SetaPDF_Signer_SignatureField class allows you to create visible signature fields by absolute or relative positions. 

PHP
<?php
require_once('library/SetaPDF/Autoload.php');

// create a writer
$writer = new \SetaPDF_Core_Writer_Http('visible-field.pdf', true);
// create a new document instance
$document = new \SetaPDF_Core_Document($writer);
// create at least a single page
$document->getCatalog()->getPages()->create(\SetaPDF_Core_PageFormats::A4);

// add a field left top with an offset of 10 points
\SetaPDF_Signer_SignatureField::add(
    $document,
    'Signature field 1',
    1,
    \SetaPDF_Signer_SignatureField::POSITION_LEFT_TOP,
    array('x' => 10, 'y' => -10),
    180,
    70
);


// add a field absolutely position on the bottom left with an offset of 10 points
\SetaPDF_Signer_SignatureField::add($document, 'Signature field 1', 1, 10, 10, 180, 70);

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

Naming of Signature Fields

If no name parameter is passed to either the add() or get() methods, the default field name will be used: 

public const string SetaPDF_Signer_SignatureField::DEFAULT_FIELD_NAME = 'Signature'

The default signature field name

If a name that is passed to the add() method already exists it will be suffixed with a numeric incrementing value.

You can check the final name with the getQualifiedName() method.

So a common logic to have a unique signature field would be:

PHP
$field = $signer->addSignatureField();
$signer->setSignatureFieldName($field->getQualifiedName());