Forum


Replies: 3   Views: 686
Set caption style for tables and images
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by admin  · 02-11-2022 - 07:37

Hello,

You can use the styleName option to set the custom paragraph style to be applied to the caption. This custom style (styleId value) must exist in the template or it can also be generated with phpdocx.

For example, using a custom paragraph style generated with createParagraphStyle applied to image and table captions:

// style options
$style = array(
    'color'            => '999999',
    'border'           => 'single',
    'borderLeft'       => 'double',
    'borderRightColor' => '000099',
    'indentLeft'       => 920,
);
// create custom style
$docx->createParagraphStyle('myStyle', $style);

$options = array(
    'src' => 'image.png',
    'imageAlign' => 'center',
    'scaling' => 50,
    'spacingTop' => 10,
    'spacingBottom' => 0,
    'spacingLeft' => 0,
    'spacingRight' => 20,
    'textWrap' => 0,
    'borderStyle' => 'lgDash',
    'borderWidth' => 6,
    'borderColor' => 'FF0000',
    'caption' => array('showLabel' => true, 'text' => ' Sample Image', 'styleName' => 'myStyle', 'bookmarkName' => 'my_bookmark'),
);

$docx->addImage($options);

$valuesTable = array(
    array(11, 12, 13, 14),
    array(21, 22, 23, 24),
    array(31, 32, 33, 34),
);

$paramsTable = array(
    'border' => 'single',
    'tableAlign' => 'center',
    'borderWidth' => 10,
    'borderColor' => 'B70000',
    'textProperties' => array('bold' => true, 'font' => 'Algerian', 'fontSize' => 18),
    'caption' => array('text' => ' Sample Table', 'styleName' => 'myStyle')
);

$docx->addTable($valuesTable, $paramsTable);

If you don't know the style ID to be set please use Indexer or parseStyles or getWordStyles to know the available style IDs.

Regards.