Forum


Replies: 2   Views: 203
How can i set a table column or table cell to align the text to the right
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  · 30-12-2023 - 17:07

Hello,

In a table, vertical align is a cell style, but horizontal align is a paragraph style. You can set a custom horizontal align creating a WordFragment and adding it to the table.

For example:

$docx = new CreateDocx();

$textFragmentLeft = new WordFragment($docx, 'document');
$textFragmentLeft->addText('Default alignment');

$textFragmentCenter = new WordFragment($docx, 'document');
$textFragmentCenter->addText('Center alignment', ['textAlign' => 'center']);

$textFragmentRight = new WordFragment($docx, 'document');
$textFragmentRight->addText('Right alignment', ['textAlign' => 'right']);

$valuesTable = array(
  array(
    $textFragmentLeft,
    $textFragmentCenter,
    $textFragmentRight,
  ),
  array(
    'Other cell 1',
    'Other cell 2',
    'Other cell 3',
  ),
);

$docx->addTable($valuesTable);

$docx->createDocx('output');

You can also apply other styles (such as bold, font size...) to the WordFragments and also add more than one content to the same WordFragment if needed (https://www.phpdocx.com/documentation/practical/wordfragments-and-wordml).

In the package you can find a more complex sample that creates a table with WordFragments in the following path: examples/Contents/addTable/sample_4.php

Regards.