Skip to content

Commit 8ab8d29

Browse files
authored
Merge pull request #2454 from PHPOffice/pr2351
Word2007 Reader : Support for table cell borders and margins
2 parents 08f3fa5 + d1137c7 commit 8ab8d29

File tree

5 files changed

+232
-173
lines changed

5 files changed

+232
-173
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1313
- Improved TextDirection for styling a cell by @terryzwt in #2429
1414
- Word2007 Reader : Added option to disable loading images by @aelliott1485 in #2450
1515
- HTML Writer : Added border-spacing to default styles for table by @kernusr in #2451
16+
- Word2007 Reader : Support for table cell borders and margins by @kernusr in #2454
1617

1718
### Bug fixes
1819

src/PhpWord/Reader/Word2007/AbstractPart.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,8 @@ protected function readTable(XMLReader $xmlReader, DOMElement $domNode, $parent,
385385
} elseif ('w:tc' == $rowNode->nodeName) { // Cell
386386
$cellWidth = $xmlReader->getAttribute('w:w', $rowNode, 'w:tcPr/w:tcW');
387387
$cellStyle = null;
388-
$cellStyleNode = $xmlReader->getElement('w:tcPr', $rowNode);
389-
if (null !== $cellStyleNode) {
390-
$cellStyle = $this->readCellStyle($xmlReader, $cellStyleNode);
388+
if ($xmlReader->elementExists('w:tcPr', $rowNode)) {
389+
$cellStyle = $this->readCellStyle($xmlReader, $rowNode);
391390
}
392391

393392
$cell = $row->addCell($cellWidth, $cellStyle);
@@ -573,7 +572,7 @@ private function readTableIndent(XMLReader $xmlReader, DOMElement $domNode)
573572
/**
574573
* Read w:tcPr.
575574
*
576-
* @return array
575+
* @return null|array
577576
*/
578577
private function readCellStyle(XMLReader $xmlReader, DOMElement $domNode)
579578
{
@@ -585,8 +584,24 @@ private function readCellStyle(XMLReader $xmlReader, DOMElement $domNode)
585584
'bgColor' => [self::READ_VALUE, 'w:shd', 'w:fill'],
586585
'noWrap' => [self::READ_VALUE, 'w:noWrap', null, null, true],
587586
];
587+
$style = null;
588588

589-
return $this->readStyleDefs($xmlReader, $domNode, $styleDefs);
589+
if ($xmlReader->elementExists('w:tcPr', $domNode)) {
590+
$styleNode = $xmlReader->getElement('w:tcPr', $domNode);
591+
592+
$borders = ['top', 'left', 'bottom', 'right'];
593+
foreach ($borders as $side) {
594+
$ucfSide = ucfirst($side);
595+
596+
$styleDefs['border' . $ucfSide . 'Size'] = [self::READ_VALUE, 'w:tcBorders/w:' . $side, 'w:sz'];
597+
$styleDefs['border' . $ucfSide . 'Color'] = [self::READ_VALUE, 'w:tcBorders/w:' . $side, 'w:color'];
598+
$styleDefs['border' . $ucfSide . 'Style'] = [self::READ_VALUE, 'w:tcBorders/w:' . $side, 'w:val'];
599+
}
600+
601+
$style = $this->readStyleDefs($xmlReader, $styleNode, $styleDefs);
602+
}
603+
604+
return $style;
590605
}
591606

592607
/**

src/PhpWord/Style/Border.php

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
*/
2323
class Border extends AbstractStyle
2424
{
25+
const DEFAULT_MARGIN = 1440; // In twips.
26+
2527
/**
2628
* Border Top Size.
2729
*
@@ -106,6 +108,34 @@ class Border extends AbstractStyle
106108
*/
107109
protected $borderBottomStyle;
108110

111+
/**
112+
* Top margin spacing.
113+
*
114+
* @var float|int
115+
*/
116+
protected $marginTop = self::DEFAULT_MARGIN;
117+
118+
/**
119+
* Left margin spacing.
120+
*
121+
* @var float|int
122+
*/
123+
protected $marginLeft = self::DEFAULT_MARGIN;
124+
125+
/**
126+
* Right margin spacing.
127+
*
128+
* @var float|int
129+
*/
130+
protected $marginRight = self::DEFAULT_MARGIN;
131+
132+
/**
133+
* Bottom margin spacing.
134+
*
135+
* @var float|int
136+
*/
137+
protected $marginBottom = self::DEFAULT_MARGIN;
138+
109139
/**
110140
* Get border size.
111141
*
@@ -501,4 +531,100 @@ public function hasBorder()
501531

502532
return $borders !== array_filter($borders, 'is_null');
503533
}
534+
535+
/**
536+
* Get Margin Top.
537+
*
538+
* @return float|int
539+
*/
540+
public function getMarginTop()
541+
{
542+
return $this->marginTop;
543+
}
544+
545+
/**
546+
* Set Margin Top.
547+
*
548+
* @param float|int $value
549+
*
550+
* @return self
551+
*/
552+
public function setMarginTop($value = null)
553+
{
554+
$this->marginTop = $this->setNumericVal($value, self::DEFAULT_MARGIN);
555+
556+
return $this;
557+
}
558+
559+
/**
560+
* Get Margin Left.
561+
*
562+
* @return float|int
563+
*/
564+
public function getMarginLeft()
565+
{
566+
return $this->marginLeft;
567+
}
568+
569+
/**
570+
* Set Margin Left.
571+
*
572+
* @param float|int $value
573+
*
574+
* @return self
575+
*/
576+
public function setMarginLeft($value = null)
577+
{
578+
$this->marginLeft = $this->setNumericVal($value, self::DEFAULT_MARGIN);
579+
580+
return $this;
581+
}
582+
583+
/**
584+
* Get Margin Right.
585+
*
586+
* @return float|int
587+
*/
588+
public function getMarginRight()
589+
{
590+
return $this->marginRight;
591+
}
592+
593+
/**
594+
* Set Margin Right.
595+
*
596+
* @param float|int $value
597+
*
598+
* @return self
599+
*/
600+
public function setMarginRight($value = null)
601+
{
602+
$this->marginRight = $this->setNumericVal($value, self::DEFAULT_MARGIN);
603+
604+
return $this;
605+
}
606+
607+
/**
608+
* Get Margin Bottom.
609+
*
610+
* @return float|int
611+
*/
612+
public function getMarginBottom()
613+
{
614+
return $this->marginBottom;
615+
}
616+
617+
/**
618+
* Set Margin Bottom.
619+
*
620+
* @param float|int $value
621+
*
622+
* @return self
623+
*/
624+
public function setMarginBottom($value = null)
625+
{
626+
$this->marginBottom = $this->setNumericVal($value, self::DEFAULT_MARGIN);
627+
628+
return $this;
629+
}
504630
}

src/PhpWord/Style/Section.php

Lines changed: 0 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ class Section extends Border
4040
*/
4141
const DEFAULT_WIDTH = 11905.511811024; // In twips.
4242
const DEFAULT_HEIGHT = 16837.79527559; // In twips.
43-
const DEFAULT_MARGIN = 1440; // In twips.
4443
const DEFAULT_GUTTER = 0; // In twips.
4544
const DEFAULT_HEADER_HEIGHT = 720; // In twips.
4645
const DEFAULT_FOOTER_HEIGHT = 720; // In twips.
@@ -77,34 +76,6 @@ class Section extends Border
7776
*/
7877
private $pageSizeH = self::DEFAULT_HEIGHT;
7978

80-
/**
81-
* Top margin spacing.
82-
*
83-
* @var float|int
84-
*/
85-
private $marginTop = self::DEFAULT_MARGIN;
86-
87-
/**
88-
* Left margin spacing.
89-
*
90-
* @var float|int
91-
*/
92-
private $marginLeft = self::DEFAULT_MARGIN;
93-
94-
/**
95-
* Right margin spacing.
96-
*
97-
* @var float|int
98-
*/
99-
private $marginRight = self::DEFAULT_MARGIN;
100-
101-
/**
102-
* Bottom margin spacing.
103-
*
104-
* @var float|int
105-
*/
106-
private $marginBottom = self::DEFAULT_MARGIN;
107-
10879
/**
10980
* Page gutter spacing.
11081
*
@@ -344,102 +315,6 @@ public function setPageSizeH($value = null)
344315
return $this;
345316
}
346317

347-
/**
348-
* Get Margin Top.
349-
*
350-
* @return float|int
351-
*/
352-
public function getMarginTop()
353-
{
354-
return $this->marginTop;
355-
}
356-
357-
/**
358-
* Set Margin Top.
359-
*
360-
* @param float|int $value
361-
*
362-
* @return self
363-
*/
364-
public function setMarginTop($value = null)
365-
{
366-
$this->marginTop = $this->setNumericVal($value, self::DEFAULT_MARGIN);
367-
368-
return $this;
369-
}
370-
371-
/**
372-
* Get Margin Left.
373-
*
374-
* @return float|int
375-
*/
376-
public function getMarginLeft()
377-
{
378-
return $this->marginLeft;
379-
}
380-
381-
/**
382-
* Set Margin Left.
383-
*
384-
* @param float|int $value
385-
*
386-
* @return self
387-
*/
388-
public function setMarginLeft($value = null)
389-
{
390-
$this->marginLeft = $this->setNumericVal($value, self::DEFAULT_MARGIN);
391-
392-
return $this;
393-
}
394-
395-
/**
396-
* Get Margin Right.
397-
*
398-
* @return float|int
399-
*/
400-
public function getMarginRight()
401-
{
402-
return $this->marginRight;
403-
}
404-
405-
/**
406-
* Set Margin Right.
407-
*
408-
* @param float|int $value
409-
*
410-
* @return self
411-
*/
412-
public function setMarginRight($value = null)
413-
{
414-
$this->marginRight = $this->setNumericVal($value, self::DEFAULT_MARGIN);
415-
416-
return $this;
417-
}
418-
419-
/**
420-
* Get Margin Bottom.
421-
*
422-
* @return float|int
423-
*/
424-
public function getMarginBottom()
425-
{
426-
return $this->marginBottom;
427-
}
428-
429-
/**
430-
* Set Margin Bottom.
431-
*
432-
* @param float|int $value
433-
*
434-
* @return self
435-
*/
436-
public function setMarginBottom($value = null)
437-
{
438-
$this->marginBottom = $this->setNumericVal($value, self::DEFAULT_MARGIN);
439-
440-
return $this;
441-
}
442-
443318
/**
444319
* Get gutter.
445320
*

0 commit comments

Comments
 (0)