Question

I have a table cell where I would like to place a few list items. Below is my code.

Basically, I have a whole bunch of styles defined and then I add rows and cells. Inside one of the cells, I am adding an unordered list.

Note: If you are wondering why I have the "exact" parameter here, $table->addRow(250, "exact"); it is because I've used this fix for controlling table row height.

// Table
$styleTable = array(    'borderSize'        =>  7,
                        'cellMarginTop'     =>  0,
                        'cellMarginLeft'    =>  100,
                        'valign'            =>  'center',
                        'spaceAfter'        =>  0
                                                        );

$styleCell = array(     'spaceAfter'        =>  0               
                                                        );


$cellTextStyle = array(     'bold'  =>  false, 
                            'size'  =>  10, 
                            'name'  =>  'Calibri'
                                                        );

$cellTextStyleBold = array( 'bold'  =>  true, 
                            'size'  =>  10, 
                            'name'  =>  'Calibri'   
                                                        );

$listStyleText = array(     'spaceAfter'    =>  0,
                            'spaceBefore'   =>  0,
                            'spacing'       =>  0,
                            'size'          =>  10       
                                                                );  

$listStyle = array(         'spaceAfter'    =>  0,
                            'spaceBefore'   =>  0,
                            'spacing'       =>  0
                                                                );  

$listStyleParagraph = array(        'spaceAfter'    =>  0,
                                    'spaceBefore'   =>  0,
                                    'spacing'       =>  0       
                                                                );          

$PHPWord->addTableStyle('myTable', $styleTable);

$table = $section->addTable('myTable');
$table->addRow(250, "exact");
$table->addCell(5760, $styleCell)->addText('Type:', $cellTextStyleBold);
$table->addCell(5760, $styleCell)->addText('Kazvin', $cellTextStyle);

$table->addRow(250, "null");
$table->addCell(5760, $styleCell)->addText('Description:', $cellTextStyleBold);
$cell = $table->addCell(5760, $styleCell);

// Add listitem elements inside table cell
$PHPWord->addParagraphStyle('listStyle', array('spaceAfter'=>0));
$cell->addListItem('100% wool pile on a cotton foundation', 0, null, null, 'listStyle');
$cell->addListItem('Semi-open ivory field', 0, null, null, 'listStyle');
$cell->addListItem('Coral and powder blue floral medallion', 0, null, null, 'listStyle');
$cell->addListItem('Formal coral, powder blue and ivory border', 0, null, null, 'listStyle');

The problem is that the the List Item Paragraph Style does not get used when doing something like $cell->addListItem....

If I were to use $section-> instead of $cell->

// Add listitem elements inside table cell
$PHPWord->addParagraphStyle('listStyle', array('spaceAfter'=>0));
$section->addListItem('100% wool pile on a cotton foundation', 0, null, null, 'listStyle');
$section->addListItem('Semi-open ivory field', 0, null, null, 'listStyle');
$section->addListItem('Coral and powder blue floral medallion', 0, null, null, 'listStyle');
$section->addListItem('Formal coral, powder blue and ivory border', 0, null, null, 'listStyle');

Then the 'spaceAfter' => 0 works fine. However, the unordered list appears outside of the table cell.

I've tried for days trying to find a way to apply a 'spaceAfter' => 0 Paragraph Style to my list items inside the table cell but with no luck.

Does anyone know how something like this could be accomplished?

Was it helpful?

Solution

Make a change in Cell.php.

Replace :

public function addListItem($text, $depth = 0, $styleText = null, $styleList = null) {
    $text = utf8_encode($text);
    $listItem = new PHPWord_Section_ListItem($text, $depth, $styleText, $styleList, $styleParagraph);
    $this->_elementCollection[] = $listItem;
    return $listItem;
}

By :

public function addListItem($text, $depth = 0, $styleText = null, $styleList = null, $styleParagraph = null) {
    $text = utf8_encode($text);
    $listItem = new PHPWord_Section_ListItem($text, $depth, $styleText, $styleList, $styleParagraph);
    $this->_elementCollection[] = $listItem;
    return $listItem;
}

Work like a charm for me !

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top