From 4d94d5763cdd2bf6fa2a4db1985c46cf58c984a1 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Mon, 13 Jan 2014 23:14:05 +0700 Subject: [PATCH] (1) new width property for table; (2) allow table cell width to be null --- Classes/PHPWord/Section/Table.php | 30 +++++++++++++++++++++++- Classes/PHPWord/Style/Table.php | 4 ++++ Classes/PHPWord/Writer/Word2007/Base.php | 7 ++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/Classes/PHPWord/Section/Table.php b/Classes/PHPWord/Section/Table.php index 5f3e1ea5..02aeacb7 100755 --- a/Classes/PHPWord/Section/Table.php +++ b/Classes/PHPWord/Section/Table.php @@ -66,6 +66,13 @@ class PHPWord_Section_Table */ private $_pCount; + /** + * Table width + * + * @var int + */ + private $_width = null; + /** * Create a new table @@ -113,7 +120,7 @@ class PHPWord_Section_Table * @param mixed $style * @return PHPWord_Section_Table_Cell */ - public function addCell($width, $style = null) + public function addCell($width = null, $style = null) { $cell = new PHPWord_Section_Table_Cell($this->_insideOf, $this->_pCount, $width, $style); $i = count($this->_rows) - 1; @@ -150,4 +157,25 @@ class PHPWord_Section_Table { return $this->_style; } + + /** + * Set table width + * + * @var int $width + */ + public function setWidth($width) + { + $this->_width = $width; + } + + /** + * Get table width + * + * @return int + */ + public function getWidth() + { + return $this->_width; + } + } diff --git a/Classes/PHPWord/Style/Table.php b/Classes/PHPWord/Style/Table.php index c3b49ea3..02a794aa 100755 --- a/Classes/PHPWord/Style/Table.php +++ b/Classes/PHPWord/Style/Table.php @@ -30,6 +30,10 @@ */ class PHPWord_Style_Table { + const WIDTH_TYPE_NIL = 'nil'; // No Width + const WIDTH_TYPE_PERCENT = 'pct'; // Width in Fiftieths of a Percent + const WIDTH_TYPE_POINT = 'dxa'; // Width in Twentieths of a Point + const WIDTH_TYPE_AUTO = 'auto'; // Automatically Determined Width private $_cellMarginTop; private $_cellMarginLeft; diff --git a/Classes/PHPWord/Writer/Word2007/Base.php b/Classes/PHPWord/Writer/Word2007/Base.php index c9e81ab8..47efd73a 100755 --- a/Classes/PHPWord/Writer/Word2007/Base.php +++ b/Classes/PHPWord/Writer/Word2007/Base.php @@ -412,6 +412,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart if ($_cRows > 0) { $objWriter->startElement('w:tbl'); $tblStyle = $table->getStyle(); + $tblWidth = $table->getWidth(); if ($tblStyle instanceof PHPWord_Style_Table) { $this->_writeTableStyle($objWriter, $tblStyle); } else { @@ -420,6 +421,12 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart $objWriter->startElement('w:tblStyle'); $objWriter->writeAttribute('w:val', $tblStyle); $objWriter->endElement(); + if (!is_null($tblWidth)) { + $objWriter->startElement('w:tblW'); + $objWriter->writeAttribute('w:w', $tblWidth); + $objWriter->writeAttribute('w:type', 'pct'); + $objWriter->endElement(); + } $objWriter->endElement(); } }