Skip to content

Commit f9a0554

Browse files
anrikuntroosan
authored andcommitted
Added support for Floating Table Positioning (tblpPr) (#639)
Added support for Floating Table Positioning (tblpPr)
1 parent 9affbf4 commit f9a0554

File tree

14 files changed

+725
-1
lines changed

14 files changed

+725
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ v0.15.0 (?? ??? 2018)
1616
- Added support for Vertically Raised or Lowered Text (w:position) @anrikun @troosan #640
1717
- Add support for MACROBUTTON field @phryneas @troosan #1021
1818
- Add support for Hyphenation @Trainmaster #1282 (Document: `autoHyphenation`, `consecutiveHyphenLimit`, `hyphenationZone`, `doNotHyphenateCaps`, Paragraph: `suppressAutoHyphens`)
19+
- Added support for Floating Table Positioning (tblpPr) @anrikun #639
1920

2021
### Fixed
2122
- Fix reading of docx default style - @troosan #1238

docs/elements.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ Track changes can be set on text elements. There are 2 ways to set the change in
482482
Either by calling the `setChangeInfo()`, or by setting the `TrackChange` instance on the element with `setTrackChange()`.
483483

484484
.. code-block:: php
485+
485486
$phpWord = new \PhpOffice\PhpWord\PhpWord();
486487
487488
// New portrait section

docs/styles.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,20 @@ Available Table style options:
108108
- ``unit``. The unit to use for the width. One of ``\PhpOffice\PhpWord\SimpleType\TblWidth``. Defaults to *auto*.
109109
- ``layout``. Table layout, either *fixed* or *autofit* See ``\PhpOffice\PhpWord\Style\Table`` for constants.
110110
- ``cellSpacing`` Cell spacing in *twip*
111+
- ``position`` Floating Table Positioning, see below for options
112+
113+
Floating Table Positioning options:
114+
115+
- ``leftFromText`` Distance From Left of Table to Text in *twip*
116+
- ``rightFromText`` Distance From Right of Table to Text in *twip*
117+
- ``topFromText`` Distance From Top of Table to Text in *twip*
118+
- ``bottomFromText`` Distance From Top of Table to Text in *twip*
119+
- ``vertAnchor`` Table Vertical Anchor, one of ``\PhpOffice\PhpWord\Style\TablePosition::VANCHOR_*``
120+
- ``horzAnchor`` Table Horizontal Anchor, one of ``\PhpOffice\PhpWord\Style\TablePosition::HANCHOR_*``
121+
- ``tblpXSpec`` Relative Horizontal Alignment From Anchor, one of ``\PhpOffice\PhpWord\Style\TablePosition::XALIGN_*``
122+
- ``tblpX`` Absolute Horizontal Distance From Anchorin *twip*
123+
- ``tblpYSpec`` Relative Vertical Alignment From Anchor, one of ``\PhpOffice\PhpWord\Style\TablePosition::YALIGN_*``
124+
- ``tblpY`` Absolute Vertical Distance From Anchorin *twip*
111125

112126
Available Row style options:
113127

samples/Sample_09_Tables.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<?php
2+
use PhpOffice\PhpWord\Shared\Converter;
3+
use PhpOffice\PhpWord\Style\TablePosition;
4+
25
include_once 'Sample_Header.php';
36

47
// New Word Document
@@ -139,6 +142,15 @@
139142
$innerCell = $cell->addTable(array('alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER))->addRow()->addCell();
140143
$innerCell->addText('Inside nested table');
141144

145+
// 6. Table with floating position
146+
147+
$section->addTextBreak(2);
148+
$section->addText('Table with floating positioning.', $header);
149+
150+
$table = $section->addTable(array('borderSize' => 6, 'borderColor' => '999999', 'position' => array('vertAnchor' => TablePosition::VANCHOR_TEXT, 'bottomFromText' => Converter::cmToTwip(1))));
151+
$cell = $table->addRow()->addCell();
152+
$cell->addText('This is a single cell.');
153+
142154
// Save file
143155
echo write($phpWord, basename(__FILE__, '.php'), $writers);
144156
if (!CLI) {

samples/Sample_26_Html.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// New Word Document
55
echo date('H:i:s') , ' Create new PhpWord object' , EOL;
66
$phpWord = new \PhpOffice\PhpWord\PhpWord();
7+
$phpWord->addParagraphStyle('Heading2', array('alignment' => 'center'));
78

89
$section = $phpWord->addSection();
910
$html = '<h1>Adding element via HTML</h1>';
@@ -17,6 +18,8 @@
1718
$html .= '<p style="margin-top: 240pt;">Unordered (bulleted) list:</p>';
1819
$html .= '<ul><li>Item 1</li><li>Item 2</li><ul><li>Item 2.1</li><li>Item 2.1</li></ul></ul>';
1920

21+
$html .= '<h2 style="align: center">centered title</h2>';
22+
2023
$html .= '<p style="margin-top: 240pt;">Ordered (numbered) list:</p>';
2124
$html .= '<ol>
2225
<li><p style="font-weight: bold;">List 1 item 1</p></li>

src/PhpWord/Reader/Word2007/AbstractPart.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,12 +462,42 @@ protected function readTableStyle(XMLReader $xmlReader, \DOMElement $domNode)
462462
$styleDefs['layout'] = array(self::READ_VALUE, 'w:tblLayout', 'w:type');
463463
$styleDefs['cellSpacing'] = array(self::READ_VALUE, 'w:tblCellSpacing', 'w:w');
464464
$style = $this->readStyleDefs($xmlReader, $styleNode, $styleDefs);
465+
466+
$tablePositionNode = $xmlReader->getElement('w:tblpPr', $styleNode);
467+
if ($tablePositionNode !== null) {
468+
$style['position'] = $this->readTablePosition($xmlReader, $tablePositionNode);
469+
}
465470
}
466471
}
467472

468473
return $style;
469474
}
470475

476+
/**
477+
* Read w:tblpPr
478+
*
479+
* @param \PhpOffice\Common\XMLReader $xmlReader
480+
* @param \DOMElement $domNode
481+
* @return array
482+
*/
483+
private function readTablePosition(XMLReader $xmlReader, \DOMElement $domNode)
484+
{
485+
$styleDefs = array(
486+
'leftFromText' => array(self::READ_VALUE, '.', 'w:leftFromText'),
487+
'rightFromText' => array(self::READ_VALUE, '.', 'w:rightFromText'),
488+
'topFromText' => array(self::READ_VALUE, '.', 'w:topFromText'),
489+
'bottomFromText' => array(self::READ_VALUE, '.', 'w:bottomFromText'),
490+
'vertAnchor' => array(self::READ_VALUE, '.', 'w:vertAnchor'),
491+
'horzAnchor' => array(self::READ_VALUE, '.', 'w:horzAnchor'),
492+
'tblpXSpec' => array(self::READ_VALUE, '.', 'w:tblpXSpec'),
493+
'tblpX' => array(self::READ_VALUE, '.', 'w:tblpX'),
494+
'tblpYSpec' => array(self::READ_VALUE, '.', 'w:tblpYSpec'),
495+
'tblpY' => array(self::READ_VALUE, '.', 'w:tblpY'),
496+
);
497+
498+
return $this->readStyleDefs($xmlReader, $domNode, $styleDefs);
499+
}
500+
471501
/**
472502
* Read w:tcPr
473503
*

src/PhpWord/Style/Table.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,13 @@ class Table extends Border
152152
*/
153153
private $layout = self::LAYOUT_AUTO;
154154

155+
/**
156+
* Position
157+
*
158+
* @var \PhpOffice\PhpWord\Style\TablePosition
159+
*/
160+
private $position;
161+
155162
/**
156163
* Create new table style
157164
*
@@ -694,4 +701,27 @@ private function setTableOnlyProperty($property, $value, $isNumeric = true)
694701

695702
return $this;
696703
}
704+
705+
/**
706+
* Get position
707+
*
708+
* @return \PhpOffice\PhpWord\Style\TablePosition
709+
*/
710+
public function getPosition()
711+
{
712+
return $this->position;
713+
}
714+
715+
/**
716+
* Set position
717+
*
718+
* @param mixed $value
719+
* @return self
720+
*/
721+
public function setPosition($value = null)
722+
{
723+
$this->setObjectVal($value, 'TablePosition', $this->position);
724+
725+
return $this;
726+
}
697727
}

0 commit comments

Comments
 (0)