Skip to content

Commit

Permalink
Word2007 Reader : Support for table cell borders and margins
Browse files Browse the repository at this point in the history
  • Loading branch information
kernusr authored and Progi1984 committed Aug 31, 2023
1 parent c17c4c7 commit 676dda9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -13,6 +13,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Improved TextDirection for styling a cell by @terryzwt in #2429
- Word2007 Reader : Added option to disable loading images by @aelliott1485 in #2450
- HTML Writer : Added border-spacing to default styles for table by @kernusr in #2451
- Word2007 Reader : Support for table cell borders and margins by @kernusr in #

### Bug fixes

Expand Down
31 changes: 26 additions & 5 deletions src/PhpWord/Reader/Word2007/AbstractPart.php
Expand Up @@ -385,9 +385,8 @@ protected function readTable(XMLReader $xmlReader, DOMElement $domNode, $parent,
} elseif ('w:tc' == $rowNode->nodeName) { // Cell
$cellWidth = $xmlReader->getAttribute('w:w', $rowNode, 'w:tcPr/w:tcW');
$cellStyle = null;
$cellStyleNode = $xmlReader->getElement('w:tcPr', $rowNode);
if (null !== $cellStyleNode) {
$cellStyle = $this->readCellStyle($xmlReader, $cellStyleNode);
if ($xmlReader->elementExists('w:tcPr', $rowNode)) {
$cellStyle = $this->readCellStyle($xmlReader, $rowNode);
}

$cell = $row->addCell($cellWidth, $cellStyle);
Expand Down Expand Up @@ -573,7 +572,7 @@ private function readTableIndent(XMLReader $xmlReader, DOMElement $domNode)
/**
* Read w:tcPr.
*
* @return array
* @return array|null
*/
private function readCellStyle(XMLReader $xmlReader, DOMElement $domNode)
{
Expand All @@ -585,8 +584,30 @@ private function readCellStyle(XMLReader $xmlReader, DOMElement $domNode)
'bgColor' => [self::READ_VALUE, 'w:shd', 'w:fill'],
'noWrap' => [self::READ_VALUE, 'w:noWrap', null, null, true],
];
$style = null;

return $this->readStyleDefs($xmlReader, $domNode, $styleDefs);
if ($xmlReader->elementExists('w:tcPr', $domNode)) {
$styleNode = $xmlReader->getElement('w:tcPr', $domNode);

$margins = ['top', 'left', 'bottom', 'right'];
foreach ($margins as $side) {
$ucfSide = ucfirst($side);
$styleDefs['cellMargin' . $ucfSide] = [self::READ_VALUE, 'w:tcMar/w:' . $side, 'w:w'];
}

$borders = array_merge($margins, ['insideH', 'insideV']);
foreach ($borders as $side) {
$ucfSide = ucfirst($side);

$styleDefs['border' . $ucfSide . 'Size'] = [self::READ_VALUE, 'w:tcBorders/w:' . $side, 'w:sz'];
$styleDefs['border' . $ucfSide . 'Color'] = [self::READ_VALUE, 'w:tcBorders/w:' . $side, 'w:color'];
$styleDefs['border' . $ucfSide . 'Style'] = [self::READ_VALUE, 'w:tcBorders/w:' . $side, 'w:val'];
}

$style = $this->readStyleDefs($xmlReader, $styleNode, $styleDefs);
}

return $style;
}

/**
Expand Down

0 comments on commit 676dda9

Please sign in to comment.