Forum


Replies: 2   Views: 2828
Adjust column size issue
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 dev@ram  · 11-05-2017 - 18:40

I have a table with 3 columns. I'm trying to make the 3 columns the same size.

I used the following code as per docs. I get the table rendered but the column widths are

not equal. Is something wrong ?

//the column data

$image = new WordFragment($docx);
    $options = array(
    'src' => $tImg,
    'imageAlign' => 'center',
    'scaling' => 80,
    'spacingTop' => 10,
    'spacingBottom' => 0,
    'spacingLeft' => 0,
    'cellSpacing' => 15,    
    'spacingRight' => 0,
    'textWrap' => 0,
    );
    $image->addImage($options);
    $tblRows[] = array(array('colspan' => 3,'value' => $image));
    $tlabel = new WordFragment($docx);
    $tlabel->addText(array(array('text' => 'Stat 1 ','bold' => true,'fontSize' => 10)));
    $ilabel = new WordFragment($docx);
    $ilabel->addText(array(array('text' => 'Stat 2 ','bold' => true,'fontSize' => 10)));
    $mlabel = new WordFragment($docx);
    $mlabel->addText(array(array('text' => 'Stat3','bold' => true,'fontSize' => 10)));
    $tblRows[] = array($tlabel,$ilabel,$mlabel);

//table config

    $tblConfig = array(
        'border' => 'single',
        'cellMargin' => 125,
        'tableStyle' => 'TableGridPHPDOCX',
        'tableAlign' => 'center',
        'columnWidths' => array(1000,1000,1000),
        'textProperties' => array('font' => 'Arial', 'fontSize' => 10),
    );    
    $docx->addTable($tblRows, $tblConfig);

 

Posted by admin  · 12-05-2017 - 07:20

Hello,

The default behaviour of MS Word is autofit table widths. You can set column widths but if the content of a cell doesn't fit a width then MS Word increase it width automatically.

You can force column widths setting tableLayout as fixed:

$tblConfig = array(
        'border' => 'single',
        'cellMargin' => 125,
        'tableStyle' => 'TableGridPHPDOCX',
        'tableAlign' => 'center',
        'columnWidths' => array(1000,1000,1000),
        'textProperties' => array('font' => 'Arial', 'fontSize' => 10),
        'tableLayout' => 'fixed',
);
$docx->addTable($tblRows, $tblConfig);

This option is explained on the addTable API page:

https://www.phpdocx.com/api-documentation/word-content/add-table-Word-document-with-PHP

Regards.

Posted by dev@ram  · 12-05-2017 - 13:11

Brilliant !

Exactly what I needed. Thank you for the precise information.
The support is excellent.