diff --git a/src/PhpWord/Element/AbstractContainer.php b/src/PhpWord/Element/AbstractContainer.php index abf23c6ed7..0ac30ebd25 100644 --- a/src/PhpWord/Element/AbstractContainer.php +++ b/src/PhpWord/Element/AbstractContainer.php @@ -31,7 +31,7 @@ * @method Footnote addFootnote(mixed $pStyle = null) * @method Endnote addEndnote(mixed $pStyle = null) * @method CheckBox addCheckBox(string $name, $text, mixed $fStyle = null, mixed $pStyle = null) - * @method Title addTitle(mixed $text, int $depth = 1) + * @method Title addTitle(mixed $text, int $depth = 1, $pageNum = null) * @method TOC addTOC(mixed $fontStyle = null, mixed $tocStyle = null, int $minDepth = 1, int $maxDepth = 9) * @method PageBreak addPageBreak() * @method Table addTable(mixed $style = null) diff --git a/src/PhpWord/Element/Title.php b/src/PhpWord/Element/Title.php index d01f7f339d..1daaecff5c 100644 --- a/src/PhpWord/Element/Title.php +++ b/src/PhpWord/Element/Title.php @@ -53,13 +53,21 @@ class Title extends AbstractElement */ protected $collectionRelation = true; + /** + * Bookmark page number + * + * @var int + */ + private $pageNum; + /** * Create a new Title Element * * @param string|TextRun $text * @param int $depth + * @param int|null $pageNum */ - public function __construct($text, $depth = 1) + public function __construct($text, $depth = 1, $pageNum = null) { if (is_string($text)) { $this->text = CommonText::toUTF8($text); @@ -74,6 +82,10 @@ public function __construct($text, $depth = 1) if (array_key_exists($styleName, Style::getStyles())) { $this->style = str_replace('_', '', $styleName); } + + if($pageNum !== null) { + $this->pageNum = $pageNum; + } } /** @@ -105,4 +117,14 @@ public function getStyle() { return $this->style; } + + /** + * Get page number + * + * @return int + */ + public function getPageNum() + { + return $this->pageNum; + } } diff --git a/src/PhpWord/Writer/Word2007/Element/TOC.php b/src/PhpWord/Writer/Word2007/Element/TOC.php index 94437cbf0d..0004169ea8 100644 --- a/src/PhpWord/Writer/Word2007/Element/TOC.php +++ b/src/PhpWord/Writer/Word2007/Element/TOC.php @@ -118,6 +118,20 @@ private function writeTitle(XMLWriter $xmlWriter, TOCElement $element, $title, $ $xmlWriter->endElement(); $xmlWriter->endElement(); + if($title->getPageNum() !== null) { + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:fldChar'); + $xmlWriter->writeAttribute('w:fldCharType', 'separate'); + $xmlWriter->endElement(); + $xmlWriter->endElement(); + + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:t'); + $xmlWriter->text($title->getPageNum()); + $xmlWriter->endElement(); + $xmlWriter->endElement(); + } + $xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:fldChar'); $xmlWriter->writeAttribute('w:fldCharType', 'end');