From 79effbe7108625e9e5c88e3f7999357e9c20c2a6 Mon Sep 17 00:00:00 2001 From: jgiacomello Date: Thu, 18 Jan 2024 14:33:19 +0100 Subject: [PATCH 1/7] fix: TOC pages numbers --- src/PhpWord/Writer/Word2007/Element/TOC.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpWord/Writer/Word2007/Element/TOC.php b/src/PhpWord/Writer/Word2007/Element/TOC.php index 2cf76155e4..6607dc93f3 100644 --- a/src/PhpWord/Writer/Word2007/Element/TOC.php +++ b/src/PhpWord/Writer/Word2007/Element/TOC.php @@ -113,7 +113,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(); From d5d6b9c98a7e062a551c54d327af8051a4fab576 Mon Sep 17 00:00:00 2001 From: jgiacomello Date: Tue, 27 Aug 2024 14:43:53 +0200 Subject: [PATCH 2/7] fix: TOC pages numbers --- docs/changes/2.x/2.0.0.md | 1 + .../Writer/Word2007/Element/TOCTest.php | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/docs/changes/2.x/2.0.0.md b/docs/changes/2.x/2.0.0.md index 575510b222..47f9a48335 100644 --- a/docs/changes/2.x/2.0.0.md +++ b/docs/changes/2.x/2.0.0.md @@ -5,6 +5,7 @@ ## Enhancements - IOFactory : Added extractVariables method to extract variables from a document [@sibalonat](https://github.com/sibalonat) in [#2515](https://github.com/PHPOffice/PHPWord/pull/2515) +- TOC : Corrected generating TOC to fix page number missing issues [@jgiacomello](https://github.com/jgiacomello) in [#2556](https://github.com/PHPOffice/PHPWord/pull/2556) ### Bug fixes diff --git a/tests/PhpWordTests/Writer/Word2007/Element/TOCTest.php b/tests/PhpWordTests/Writer/Word2007/Element/TOCTest.php index 537fb93d1a..f6bf088274 100644 --- a/tests/PhpWordTests/Writer/Word2007/Element/TOCTest.php +++ b/tests/PhpWordTests/Writer/Word2007/Element/TOCTest.php @@ -53,4 +53,29 @@ 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 <= 1; $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')); + } + } } From 40dfdec95456a5b4ec44524474dbccf86bf16dc3 Mon Sep 17 00:00:00 2001 From: Jessica <122348613+jgiacomello@users.noreply.github.com> Date: Wed, 12 Feb 2025 16:01:04 +0100 Subject: [PATCH 3/7] Update tests/PhpWordTests/Writer/Word2007/Element/TOCTest.php Co-authored-by: Progi1984 --- tests/PhpWordTests/Writer/Word2007/Element/TOCTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/PhpWordTests/Writer/Word2007/Element/TOCTest.php b/tests/PhpWordTests/Writer/Word2007/Element/TOCTest.php index f6bf088274..6aef594779 100644 --- a/tests/PhpWordTests/Writer/Word2007/Element/TOCTest.php +++ b/tests/PhpWordTests/Writer/Word2007/Element/TOCTest.php @@ -76,6 +76,7 @@ public function testWriteTitleWithoutpageNumber(): void 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 \\h', $doc->getElement('/w:document/w:body/w:p[' . $i . ']/w:hyperlink/w:r[4]/w:instrText')->nodeValue); } } } From f6f65daa3e438c97dde6bdcd9f908a9d5fc9d4c4 Mon Sep 17 00:00:00 2001 From: jgiacomello Date: Wed, 12 Feb 2025 16:49:58 +0100 Subject: [PATCH 4/7] changelog update --- docs/changes/1.x/1.4.0.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/changes/1.x/1.4.0.md b/docs/changes/1.x/1.4.0.md index b5102f25fa..9c45131152 100644 --- a/docs/changes/1.x/1.4.0.md +++ b/docs/changes/1.x/1.4.0.md @@ -28,6 +28,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) +- TOC : Corrected generating TOC to fix page number missing issues [@jgiacomello](https://github.com/jgiacomello) in [#2556](https://github.com/PHPOffice/PHPWord/pull/2556) ### Miscellaneous From 2e97f854bb469c35d127bfddffa0891b63dcdf78 Mon Sep 17 00:00:00 2001 From: jgiacomello Date: Fri, 14 Feb 2025 12:41:46 +0100 Subject: [PATCH 5/7] fix testWriteTitleWithoutpageNumber --- tests/PhpWordTests/Writer/Word2007/Element/TOCTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/PhpWordTests/Writer/Word2007/Element/TOCTest.php b/tests/PhpWordTests/Writer/Word2007/Element/TOCTest.php index 9f69912e9f..fc9164f83f 100644 --- a/tests/PhpWordTests/Writer/Word2007/Element/TOCTest.php +++ b/tests/PhpWordTests/Writer/Word2007/Element/TOCTest.php @@ -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; @@ -72,12 +73,12 @@ public function testWriteTitleWithoutpageNumber(): void $doc = TestHelperDOCX::getDocument($phpWord); - for ($i = 1; $i <= 1; $i++) { + 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 \\h', $doc->getElement('/w:document/w:body/w:p[' . $i . ']/w:hyperlink/w:r[4]/w:instrText')->nodeValue); + self::assertEquals('PAGEREF ' . ($i - 1) . ' \\h', $doc->getElement('/w:document/w:body/w:p[' . $i . ']/w:hyperlink/w:r[4]/w:instrText')->nodeValue); } } } From acdb4b67f63f1ac4f6000fe55b22255b2c712b95 Mon Sep 17 00:00:00 2001 From: jgiacomello Date: Fri, 14 Feb 2025 12:47:11 +0100 Subject: [PATCH 6/7] fix testWriteTitleWithoutpageNumber --- tests/PhpWordTests/Writer/Word2007/Element/TOCTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/PhpWordTests/Writer/Word2007/Element/TOCTest.php b/tests/PhpWordTests/Writer/Word2007/Element/TOCTest.php index fc9164f83f..95e79114aa 100644 --- a/tests/PhpWordTests/Writer/Word2007/Element/TOCTest.php +++ b/tests/PhpWordTests/Writer/Word2007/Element/TOCTest.php @@ -64,7 +64,7 @@ public function testWriteTitleWithoutpageNumber(): void $section->addTOC(); //more than one title and random text for create more than one page - for ($i = 1; $i <= 10; $i++) { + 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); @@ -73,7 +73,7 @@ public function testWriteTitleWithoutpageNumber(): void $doc = TestHelperDOCX::getDocument($phpWord); - for ($i = 1; $i <= 10; $i++) { + 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')); From 6179b97cf73251992f6f34a343682f8f3fbd5ee8 Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Fri, 14 Feb 2025 22:06:13 +0100 Subject: [PATCH 7/7] Fixed Changelog --- docs/changes/1.x/1.4.0.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/changes/1.x/1.4.0.md b/docs/changes/1.x/1.4.0.md index 9c45131152..68bf0e4ec8 100644 --- a/docs/changes/1.x/1.4.0.md +++ b/docs/changes/1.x/1.4.0.md @@ -28,7 +28,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) -- TOC : Corrected generating TOC to fix page number missing issues [@jgiacomello](https://github.com/jgiacomello) in [#2556](https://github.com/PHPOffice/PHPWord/pull/2556) +- 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