Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pagenumber in TOC #1652

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/PhpWord/Element/AbstractContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 23 additions & 1 deletion src/PhpWord/Element/Title.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
}

/**
Expand Down Expand Up @@ -105,4 +117,14 @@ public function getStyle()
{
return $this->style;
}

/**
* Get page number
*
* @return int
*/
public function getPageNum()
{
return $this->pageNum;
}
}
14 changes: 14 additions & 0 deletions src/PhpWord/Writer/Word2007/Element/TOC.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down