Skip to content

Commit 7ddaed2

Browse files
aoloetroosan
authored andcommitted
table->setStretch() optionally avoids the table to stretch to the page width (only for word output)
1 parent e846602 commit 7ddaed2

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/PhpWord/Style/Table.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class Table extends Border
2828
const WIDTH_AUTO = 'auto'; // Automatically determined width
2929
const WIDTH_PERCENT = 'pct'; // Width in fiftieths (1/50) of a percent (1% = 50 unit)
3030
const WIDTH_TWIP = 'dxa'; // Width in twentieths (1/20) of a point (twip)
31+
const STRETCH_AUTO = 'autofit'; // Automatically stretch the table to fit the page width
32+
const STRETCH_FIXED = 'fixed'; // Do not stretch the table to fit the page width
3133

3234
/**
3335
* Is this a first row style?
@@ -121,6 +123,11 @@ class Table extends Border
121123
*/
122124
private $unit = self::WIDTH_AUTO;
123125

126+
/**
127+
* @var string Stretch the table to the page width
128+
*/
129+
private $stretch = self::STRETCH_AUTO;
130+
124131
/**
125132
* Create new table style
126133
*
@@ -582,6 +589,32 @@ public function setUnit($value = null)
582589
return $this;
583590
}
584591

592+
/**
593+
* Get stretch
594+
*
595+
* @return string
596+
*/
597+
public function getStretch()
598+
{
599+
return $this->stretch;
600+
}
601+
602+
/**
603+
* Set stretch
604+
*
605+
* Stretch the table to the page width
606+
*
607+
* @param string $value
608+
* @return self
609+
*/
610+
public function setStretch($value = null)
611+
{
612+
$enum = array(self::STRETCH_AUTO, self::STRETCH_FIXED);
613+
$this->stretch = $this->setEnumVal($value, $enum, $this->stretch);
614+
615+
return $this;
616+
}
617+
585618
/**
586619
* Get table style only property by checking if it's a firstRow
587620
*

src/PhpWord/Writer/Word2007/Style/Table.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ private function writeStyle(XMLWriter $xmlWriter, TableStyle $style)
7777
}
7878

7979
$this->writeWidth($xmlWriter, $style->getWidth(), $style->getUnit());
80+
$this->writeLayout($xmlWriter, $style->getStretch());
8081
$this->writeMargin($xmlWriter, $style);
8182
$this->writeBorder($xmlWriter, $style);
8283

@@ -106,6 +107,20 @@ private function writeWidth(XMLWriter $xmlWriter, $width, $unit)
106107
$xmlWriter->endElement(); // w:tblW
107108
}
108109

110+
/**
111+
* Enable/Disable automatic resizing of the table
112+
*
113+
* @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
114+
* @param string $layout autofit / fixed
115+
* @return void
116+
*/
117+
private function writeLayout(XMLWriter $xmlWriter, $stretch)
118+
{
119+
$xmlWriter->startElement('w:tblLayout');
120+
$xmlWriter->writeAttribute('w:type', $stretch);
121+
$xmlWriter->endElement(); // w:tblLayout
122+
}
123+
109124
/**
110125
* Write margin.
111126
*

0 commit comments

Comments
 (0)