Skip to content

Commit 6ca8c9f

Browse files
automatic update of the TOC to fix page number missing issues (#2556)
* fix: TOC pages numbers * fix: TOC pages numbers * Update tests/PhpWordTests/Writer/Word2007/Element/TOCTest.php Co-authored-by: Progi1984 <progi1984@gmail.com> * changelog update * fix testWriteTitleWithoutpageNumber * fix testWriteTitleWithoutpageNumber * Fixed Changelog --------- Co-authored-by: Progi1984 <progi1984@gmail.com>
1 parent c92ee4d commit 6ca8c9f

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

docs/changes/1.x/1.4.0.md

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
- 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)
3131
- 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)
3232
- 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)
33+
- 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)
3334

3435
### Miscellaneous
3536

src/PhpWord/Writer/Word2007/Element/TOC.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ private function writeTitle(XMLWriter $xmlWriter, TOCElement $element, Title $ti
114114
$xmlWriter->startElement('w:r');
115115
$xmlWriter->startElement('w:instrText');
116116
$xmlWriter->writeAttribute('xml:space', 'preserve');
117-
$xmlWriter->text("PAGEREF _Toc{$rId} \\h");
117+
$xmlWriter->text("PAGEREF $rId \\h");
118118
$xmlWriter->endElement();
119119
$xmlWriter->endElement();
120120

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

+27
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*
1616
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
1717
*/
18+
1819
declare(strict_types=1);
1920

2021
namespace PhpOffice\PhpWordTests\Writer\Word2007\Element;
@@ -54,4 +55,30 @@ public function testWriteTitlePageNumber(): void
5455
self::assertTrue($doc->elementExists('/w:document/w:body/w:p[1]/w:hyperlink/w:r[6]/w:t'));
5556
self::assertEquals($expectedPageNum, $doc->getElement('/w:document/w:body/w:p[1]/w:hyperlink/w:r[6]/w:t')->textContent);
5657
}
58+
59+
public function testWriteTitleWithoutpageNumber(): void
60+
{
61+
$phpWord = new PhpWord();
62+
63+
$section = $phpWord->addSection();
64+
$section->addTOC();
65+
66+
//more than one title and random text for create more than one page
67+
for ($i = 1; $i <= 10; ++$i) {
68+
$section->addTitle('Title ' . $i, 1);
69+
$content = file_get_contents('https://loripsum.net/api/10/long');
70+
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $content ? $content : '', false, false);
71+
$section->addPageBreak();
72+
}
73+
74+
$doc = TestHelperDOCX::getDocument($phpWord);
75+
76+
for ($i = 1; $i <= 10; ++$i) {
77+
self::assertTrue($doc->elementExists('/w:document/w:body/w:p[' . $i . ']/w:hyperlink/w:r[1]/w:t'));
78+
self::assertEquals('Title ' . $i, $doc->getElement('/w:document/w:body/w:p[' . $i . ']/w:hyperlink/w:r[1]/w:t')->textContent);
79+
self::assertTrue($doc->elementExists('/w:document/w:body/w:p[' . $i . ']/w:hyperlink/w:r[4]/w:instrText'));
80+
self::assertEquals('preserve', $doc->getElementAttribute('/w:document/w:body/w:p[' . $i . ']/w:hyperlink/w:r[4]/w:instrText', 'xml:space'));
81+
self::assertEquals('PAGEREF ' . ($i - 1) . ' \\h', $doc->getElement('/w:document/w:body/w:p[' . $i . ']/w:hyperlink/w:r[4]/w:instrText')->nodeValue);
82+
}
83+
}
5784
}

0 commit comments

Comments
 (0)