Replies: 3   Views: 464
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 ed.goode  · 01-11-2022 - 21:16

I'm creating a document from a template that has a lot of styles in it. The document that gets created has a lot of images and tables, each with a caption. They are being created fine, but I can't figure out how to set them to use the template's caption style.

This is the code I'm using to create tables:

$table_params = array(
            'tableStyle'=> 'TableGrid',
            'borderColor' => 'BFBFBF',
            'border' => 'single',
            'textProperties' => array('pStyle' => 'TableTextstandard'),
            'caption' => array('text' => $hbd_caption, 'position' => 'above', 'styleName' => 'Table')

What am I missing, or is it just not possible?

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


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'),


$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.


Posted by ed.goode  · 02-11-2022 - 20:54

I thought that the styleName was used to set the labels. I have my styleName set to Table so that the captions are set to SEQ Table. I've got tables and figures that should be numbered separately, and I want them both to use the document's caption style. Is that not possible?

Posted by admin  · 03-11-2022 - 06:25


The same styleName option is applied in both (SEQ and custom paragraph style [pStyle]). Styles can be applied using custom style options (color, lineSpacing, position...) but there's no option to apply a custom paragraph style to the caption. Using a Premium license, DOCXCustomizer can be used to set a new pStyle, but as you are using an Advanced license you'd need to add support for a new option.

We have sent to your email a small update in the CreateImageCaption class with a new option to apply a custom paragraph style to captions.