Individual Appearance Create an Individual Field Appearance
Table of Contents
Each form field has a so called appearance stream which should represent the visible appearance of the field value.
You can access this appearance stream to create an individual appearance by e.g. using an image, a page of another PDF document or by drawing the appearance directly through the canvas instance. The result can be flattened later.
A field appearance is represented by a form XObject which offers you a canvas instance to interact with it. You can simply receive the canvas instance of a form field this way:
$field = $fields->get('fieldname'); $canvas = $field->getAppearanceCanvas();
This gives you direct access to a fields canvas and you can use all available methods documented here.
It is also possible to create a form XObject by hand and pass this to the widget annotation of the form field:
// Get the field $field = $fields->get('fieldname'); // Get the annotation $annotation = $field->getAnnotation(); // Create a form XObject to which we are going to write the image // This form XObject will be the resulting appearance of our form field $xObject = SetaPDF_Core_XObject_Form::create( $document, [0, 0, $annotation->getWidth(), $annotation->getHeight()] ); // Get the canvas for this XObject $canvas = $xObject->getCanvas(); // ...draw the appearance // Now add the appearance back to the annotation $annotation->setAppearance($xObject);
Let's demonstrate this with a simple example: We want to fill in a form field with a company logo. It should be centered and use the full height or width of the form field.
Same example but the company logo is taken from a PDF page:
The above examples already made use of the canvas object but it is not limited to draw images or pages of existing PDF documents. Let's try to build a logo by using the canvas directly: