Skip to content

Commit

Permalink
automatic update of the TOC to fix page number missing issues (#2556)
Browse files Browse the repository at this point in the history
* fix: TOC pages numbers

* fix: TOC pages numbers

* Update tests/PhpWordTests/Writer/Word2007/Element/TOCTest.php

Co-authored-by: Progi1984 <[email protected]>

* changelog update

* fix testWriteTitleWithoutpageNumber

* fix testWriteTitleWithoutpageNumber

* Fixed Changelog

---------

Co-authored-by: Progi1984 <[email protected]>
  • Loading branch information
jgiacomello and Progi1984 authored Feb 20, 2025
1 parent c92ee4d commit 6ca8c9f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/changes/1.x/1.4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
- Reader HTML: Support for inherit value for property line-height by [@Progi1984](https://github.com/Progi1984) fixing [#2683](https://github.com/PHPOffice/PHPWord/issues/2683) in [#2733](https://github.com/PHPOffice/PHPWord/pull/2733)
- Writer HTML: Fixed null string for Text Elements by [@armagedon007](https://github.com/armagedon007) and [@Progi1984](https://github.com/Progi1984) in [#2738](https://github.com/PHPOffice/PHPWord/pull/2738)
- Template Processor: Fix 0 considered as empty string by [@cavasinf](https://github.com/cavasinf), [@SnipsMine](https://github.com/SnipsMine) and [@Progi1984](https://github.com/Progi1984) fixing [#2572](https://github.com/PHPOffice/PHPWord/issues/2572), [#2703](https://github.com/PHPOffice/PHPWord/issues/2703) in [#2748](https://github.com/PHPOffice/PHPWord/pull/2748)
- Word2007 Writer : Corrected generating TOC to fix page number missing issues [@jgiacomello](https://github.com/jgiacomello) in [#2556](https://github.com/PHPOffice/PHPWord/pull/2556)

### Miscellaneous

Expand Down
2 changes: 1 addition & 1 deletion src/PhpWord/Writer/Word2007/Element/TOC.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private function writeTitle(XMLWriter $xmlWriter, TOCElement $element, Title $ti
$xmlWriter->startElement('w:r');
$xmlWriter->startElement('w:instrText');
$xmlWriter->writeAttribute('xml:space', 'preserve');
$xmlWriter->text("PAGEREF _Toc{$rId} \\h");
$xmlWriter->text("PAGEREF $rId \\h");
$xmlWriter->endElement();
$xmlWriter->endElement();

Expand Down
27 changes: 27 additions & 0 deletions tests/PhpWordTests/Writer/Word2007/Element/TOCTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/

declare(strict_types=1);

namespace PhpOffice\PhpWordTests\Writer\Word2007\Element;
Expand Down Expand Up @@ -54,4 +55,30 @@ public function testWriteTitlePageNumber(): void
self::assertTrue($doc->elementExists('/w:document/w:body/w:p[1]/w:hyperlink/w:r[6]/w:t'));
self::assertEquals($expectedPageNum, $doc->getElement('/w:document/w:body/w:p[1]/w:hyperlink/w:r[6]/w:t')->textContent);
}

public function testWriteTitleWithoutpageNumber(): void
{
$phpWord = new PhpWord();

$section = $phpWord->addSection();
$section->addTOC();

//more than one title and random text for create more than one page
for ($i = 1; $i <= 10; ++$i) {
$section->addTitle('Title ' . $i, 1);
$content = file_get_contents('https://loripsum.net/api/10/long');
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $content ? $content : '', false, false);
$section->addPageBreak();
}

$doc = TestHelperDOCX::getDocument($phpWord);

for ($i = 1; $i <= 10; ++$i) {
self::assertTrue($doc->elementExists('/w:document/w:body/w:p[' . $i . ']/w:hyperlink/w:r[1]/w:t'));
self::assertEquals('Title ' . $i, $doc->getElement('/w:document/w:body/w:p[' . $i . ']/w:hyperlink/w:r[1]/w:t')->textContent);
self::assertTrue($doc->elementExists('/w:document/w:body/w:p[' . $i . ']/w:hyperlink/w:r[4]/w:instrText'));
self::assertEquals('preserve', $doc->getElementAttribute('/w:document/w:body/w:p[' . $i . ']/w:hyperlink/w:r[4]/w:instrText', 'xml:space'));
self::assertEquals('PAGEREF ' . ($i - 1) . ' \\h', $doc->getElement('/w:document/w:body/w:p[' . $i . ']/w:hyperlink/w:r[4]/w:instrText')->nodeValue);
}
}
}

0 comments on commit 6ca8c9f

Please sign in to comment.