Skip to content

Commit 4d94d57

Browse files
committed
(1) new width property for table; (2) allow table cell width to be null
1 parent d55db9d commit 4d94d57

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

Classes/PHPWord/Section/Table.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ class PHPWord_Section_Table
6666
*/
6767
private $_pCount;
6868

69+
/**
70+
* Table width
71+
*
72+
* @var int
73+
*/
74+
private $_width = null;
75+
6976

7077
/**
7178
* Create a new table
@@ -113,7 +120,7 @@ public function addRow($height = null)
113120
* @param mixed $style
114121
* @return PHPWord_Section_Table_Cell
115122
*/
116-
public function addCell($width, $style = null)
123+
public function addCell($width = null, $style = null)
117124
{
118125
$cell = new PHPWord_Section_Table_Cell($this->_insideOf, $this->_pCount, $width, $style);
119126
$i = count($this->_rows) - 1;
@@ -150,4 +157,25 @@ public function getStyle()
150157
{
151158
return $this->_style;
152159
}
160+
161+
/**
162+
* Set table width
163+
*
164+
* @var int $width
165+
*/
166+
public function setWidth($width)
167+
{
168+
$this->_width = $width;
169+
}
170+
171+
/**
172+
* Get table width
173+
*
174+
* @return int
175+
*/
176+
public function getWidth()
177+
{
178+
return $this->_width;
179+
}
180+
153181
}

Classes/PHPWord/Style/Table.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
*/
3131
class PHPWord_Style_Table
3232
{
33+
const WIDTH_TYPE_NIL = 'nil'; // No Width
34+
const WIDTH_TYPE_PERCENT = 'pct'; // Width in Fiftieths of a Percent
35+
const WIDTH_TYPE_POINT = 'dxa'; // Width in Twentieths of a Point
36+
const WIDTH_TYPE_AUTO = 'auto'; // Automatically Determined Width
3337

3438
private $_cellMarginTop;
3539
private $_cellMarginLeft;

Classes/PHPWord/Writer/Word2007/Base.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ protected function _writeTable(PHPWord_Shared_XMLWriter $objWriter = null, PHPWo
412412
if ($_cRows > 0) {
413413
$objWriter->startElement('w:tbl');
414414
$tblStyle = $table->getStyle();
415+
$tblWidth = $table->getWidth();
415416
if ($tblStyle instanceof PHPWord_Style_Table) {
416417
$this->_writeTableStyle($objWriter, $tblStyle);
417418
} else {
@@ -420,6 +421,12 @@ protected function _writeTable(PHPWord_Shared_XMLWriter $objWriter = null, PHPWo
420421
$objWriter->startElement('w:tblStyle');
421422
$objWriter->writeAttribute('w:val', $tblStyle);
422423
$objWriter->endElement();
424+
if (!is_null($tblWidth)) {
425+
$objWriter->startElement('w:tblW');
426+
$objWriter->writeAttribute('w:w', $tblWidth);
427+
$objWriter->writeAttribute('w:type', 'pct');
428+
$objWriter->endElement();
429+
}
423430
$objWriter->endElement();
424431
}
425432
}

0 commit comments

Comments
 (0)