Text Stamp

Introduction

The text stamp class lets you stamp simple text onto existing PDF pages. Internally it encapsulates a text block instance which can be styled in various ways. 

Font Handling

A font is represented as an object instance. The Core component comes with all available 14 standard fonts and lets you create own fonts based on TrueType font files.

Create an Instance

An instance of a text stamp could be created by simply passing a font object to its constructor: 

PHP
$font = \SetaPDF_Core_Font_Standard_Helvetica::create($document);
$stamp = new \SetaPDF_Stamper_Stamp_Text($font);

Configure Properties

Beside the general stamp properties the text stamp class offers following properties to configured the text appearance in various ways: 

Text

Text related properties could be controlled by following methods: 

getCharSpacing()

Get the character spacing value.

getFont()

Get the current font object.

getFontSize()

Get the font size.

getLineHeight()

Get the line height / leading.

getText()

Get the text.

getWordSpacing()

Get the word spacing value.

setCharSpacing()

Set the character spacing value.

setFont()

Set the font object and size.

setFontSize()

Set the font size.

setLineHeight()

Set the line height / leading.

setText()

Set the text.

setWordSpacing()

Set the word spacing value.

Layout

To layout a text stamp following methods are available:

getAlign()

Get the text alignment.

getHeight()

Get the height of this stamp.

getOutlineColor()

Get the texts outline color object.

getOutlineWidth()

Get the outline width.

getPaddingBottom()

Get the bottom padding.

getPaddingLeft()

Get the left padding.

getPaddingRight()

Get the right padding.

getPaddingTop()

Get the top padding.

getRenderingMode()

Get the defined rendering mode.

getTextColor()

Get the text color object.

setAlign()

Set the text alignment.

setOutlineColor()

Set the texts outline color.

setOutlineWidth()

Set the outline width.

setPadding()

Set the padding.

setPaddingBottom()

Set the bottom padding.

setPaddingLeft()

Set the left padding.

setPaddingRight()

Set the right padding.

setPaddingTop()

Set the top padding.

setRenderingMode()

Set the rendering mode.

setTextColor()

Set the text color.

setTextWidth()

Set the width of the text of the stamp.

Border and Background

getBackgroundColor()

Get the background color object.

getBorderColor()

Get the border color object.

getBorderWidth()

Get the border width.

setBackgroundColor()

Set the background color.

setBorderColor()

Set the border color.

setBorderWidth()

Set the border width.

Examples

Following some examples showing you some ideas of how to style a text stamp: 

PHP
<?php
/**
 * Create a transparent watermark on an existing document
 */

require_once('library/SetaPDF/Autoload.php');

// create a writer
$writer = new \SetaPDF_Core_Writer_Http('watermark.pdf', true);
// get a document instance
$document = \SetaPDF_Core_Document::loadByFilename(
    'files/pdfs/lenstown/Laboratory-Report.pdf', $writer
);

// create a stamper instance
$stamper = new \SetaPDF_Stamper($document);

// create a font object as a TrueType subset
$font = new \SetaPDF_Core_Font_TrueType_Subset(
    $document,
    'files/fonts/DejaVuSans.ttf'
);

// create simple text stamp
$stamp = new \SetaPDF_Stamper_Stamp_Text($font, 90);
$stamp->setText('CONFIDENTIAL');
$stamp->setTextColor(new \SetaPDF_Core_DataStructure_Color_Rgb(1, 0, 0));
$stamp->setAlign(\SetaPDF_Core_Text::ALIGN_CENTER);
$stamp->setOpacity(0.5);

// right bottom and callback
$stamper->addStamp($stamp, array(
    'position' => \SetaPDF_Stamper::POSITION_CENTER_MIDDLE,
    'rotation' => 60
));

// stamp the document
$stamper->stamp();

// show the whole page at opening time
$document->getCatalog()->setPageLayout(\SetaPDF_Core_Document_PageLayout::SINGLE_PAGE);

// save and send it to the client
$document->save()->finish();
PHP
<?php
/**
 * Adds a users name and email into the bottom right corner
 */

require_once('library/SetaPDF/Autoload.php');

// create a writer
$writer = new \SetaPDF_Core_Writer_Http('personalization.pdf', true);
// get a document instance
$document = \SetaPDF_Core_Document::loadByFilename(
    'files/pdfs/Brand-Guide.pdf', $writer
);

// create a stamper instance
$stamper = new \SetaPDF_Stamper($document);

// create a font object as a TrueType subset
$font = new \SetaPDF_Core_Font_TrueType_Subset(
    $document,
    'files/fonts/DejaVuSans.ttf'
);

// create simple text stamp
$stamp = new \SetaPDF_Stamper_Stamp_Text($font, 12);
$stamp->setText("Personalized for: John Doe (test@example.com)");
$stamp->setTextColor(new \SetaPDF_Core_DataStructure_Color_Rgb(56/255, 101/255, 174/255));

// right bottom and callback
$stamper->addStamp($stamp, array(
    'showOnPage' => '2-21',
    'position' => \SetaPDF_Stamper::POSITION_RIGHT_BOTTOM,
    'translateX' => -40,
    'translateY' => 20
));

// stamp the document
$stamper->stamp();

// show the whole page at opening time
$document->getCatalog()->setPageLayout(\SetaPDF_Core_Document_PageLayout::SINGLE_PAGE);

// save and send it to the client
$document->save()->finish();