The main use of a textbox is to float or absolutely position some arbitrary content within a document.

Docxpresso allows you to insert textboxes that can contain arbitrary content like:

  • paragraphs,
  • textboxs,
  • tables,
  • lists,
  • charts,
  • forms,
  • etcetera.

Beware that the RTF standard itself has not support for textboxes so they should be avoided when RTF is the target output format. This is no limitation of Docxpresso but it is intrinsic to Rich Text Format documents.

The core method responsible of inserting an textbox is the textbox method which public API is given by:

Signature

public textBox ($options)

Parameters

  • $options (type: array). This array has the following available keys and values:

    • pageNumber (type: string). This optional parameter only applies to absolutely positiones textboxes.
    • style (type: string). A string of styles in CSS format.

The relevant CSS style type styles that one may apply to a textbox include (where ‘*’ in this context stands for top, right, bottom or left).

  • Size (if not set the package will automatically compute the dimensions out of the textbox size and dpi):
    • width: the textbox width.
    • height: the textbox height. If not decalred Docxpresso will try to assign the minimum required height.
  • Borders (individual borders are not supported):
    • border: shorthand for border properties.
    • border-color: sets the border color.
    • border-style: sets the border line style.
    • border-width: sets the border width.
  • Display:
    • float: it can be left, right and also center.
    • margin: specifies all margin widths in one shot.
    • margin-*: specifies one margin at a time.
    • padding: specifies all padding widths in one shot.
    • padding-*: specifies one padding at a time.
    • position: absolute or relative.
    • top: distance from the top for absolutely positioned textboxes.
    • left: distance from the left for absolutely positioned textboxes.

Let us start inserting a simple centered textbox:

<?php
/**
* This sample script inserts a centered textbox
 */
require_once 'pathToDOCXPRESSO/CreateDocument.inc';
$doc = new Docxpresso\CreateDocument();
$format = '.pdf';//.pdf, .doc, .docx, .odt, .rtf
//insert some text
$doc->paragraph(array('text' => 'A centered textbox:'));
//insert a textbox
$doc->paragraph()->style('text-align: center;')
        ->textbox()->style('width: 6cm')
           ->paragraph(array('text' => 'First paragraph within the textbox.'))
           ->unorderedList(array('List item 1', 'List item 2'))
           ->paragraph(array('text' => 'Last paragraph within the textbox.'));
//insert some text
$doc->paragraph(array('text' => 'A final sentence.'));
//include in the render method the path where you want your document to be saved
$doc->render('simple_centered_textbox' . $format); 
//echo a link to the generated document
echo 'You may download the generated document from the link below:<br/>';
echo '<a href="' . 'simple_centered_textbox' . $format . '">Download document</a>';

DOWNLOAD:download pdfdownload docdownload docxdownload odtdownload rtf

Let us now work out another simple example where the textbox is floating with some text wrapping around it:

<?php
/**
* This sample script inserts a floating textbox
 */
require_once 'pathToDOCXPRESSO/CreateDocument.inc';
$doc = new Docxpresso\CreateDocument();
$format = '.pdf';//.pdf, .doc, .docx, .odt, .rtf
//insert some text
$doc->paragraph(array('text' => 'A paragraph with a floating textbox:'));
//insert the containing paragraph
$p = $doc->paragraph()->style('text-align: justify;');
//insert floating image in the middle of the paragraph
for ($j = 0; $j < 3; $j++) {
    $p->text(array('text' => 'This text content should be wrapping the textbox. We repeat it quite a few times so you can see the effect. '));
}
//insert a textbox
$p->textbox()->style('float: right; width: 7cm; height: 4cm; border: 2px solid blue; padding: 0.5cm; margin: 0.3cm; margin-right: 0')
        ->paragraph(array('text' => 'First paragraph within the textbox.'))
        ->unorderedList(array('items' => array('List item 1', 'List item 2')))->end('list')
        ->paragraph(array('text' => 'Last paragraph within the textbox.'));
$doc->paragraph(array('text' => 'And an extra paragraph that should also be wrapping the textbox in order to illustated how the flaoting of textboxes do work. '));
//include in the render method the path where you want your document to be saved
$doc->render('floating_textbox' . $format); 
//echo a link to the generated document
echo 'You may download the generated document from the link below:<br/>';
echo '<a href="' . 'floating_textbox' . $format . '">Download document</a>';

DOWNLOAD:download pdfdownload docdownload docxdownload odtdownload rtf

If Word (.doc) is your target output there is no support for textbox margins. You can overcome this limitation besides by using the padding textbox property and including, when needed a bordered table or by triggering the legacy option in the render method (you should expect in a small degradation in rendering performance).

Let us finish with an absolutely positioned example with respect the page layout:

<?php
/**
 * This sample script inserts an absolutely positioned textbox
 */
require_once 'pathToDOCXPRESSO/CreateDocument.inc';
$doc = new Docxpresso\CreateDocument();
$format = '.pdf';//.pdf, .doc, .docx, .odt, .rtf
//insert some text
$doc->paragraph(array('text' => 'A paragraph with an absolutely positioned textbox:'));
//insert the containing paragraph
$p = $doc->paragraph()->style('text-align: justify;');
//insert floating image in the middle of the paragraph
for ($j = 0; $j < 30; $j++) {
    $p->text(array('text' => 'This text content should be wrapping the textbox. We repeat it quite a few times so you can see the effect. '));
}
//insert a textbox
$doc->textbox()->style('position: absolute; top: 2cm; left: 4cm; width: 7cm; height: 4cm; border: 2px solid blue; padding: 0.5cm; margin: 0 0.5cm;')
        ->paragraph(array('text' => 'First paragraph within the textbox.'))
        ->unorderedList(array('items' => array('List item 1', 'List item 2')))->end('list')
        ->paragraph(array('text' => 'Last paragraph within the textbox.'));

$doc->paragraph(array('text' => 'And an extra paragraph that should also be wrapping the textbox in order to illustrated how the flaoting of textboxes do work. '));

//include in the render method the path where you want your document to be saved
$doc->render('absolutely_positioned_textbox' . $format); 
//echo a link to the generated document
echo 'You may download the generated document from the link below:<br/>';
echo '<a href="' . 'absolutely_positioned_textbox' . $format . '">Download document</a>';

DOWNLOAD:download pdfdownload docdownload docxdownload odtdownload rtf