Forum


Replies: 2   Views: 385
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 crewscontrol  · 30-12-2023 - 16:53

I've been going through the samples folder as well as looking on the website but somehow I'm missing how to align cell contents to the right.  I see a 'vAlign' option but don't know how to set the horizontal align.

Posted by crewscontrol  · 30-12-2023 - 17:06

Okay, I searched from Google and found this post.  Looks like I have to create a text fragment with textAlign = 'right' and then set that as the cell's value.

 

https://www.phpdocx.com/en/forum/default/topic/658

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.