Skip to content
New issue

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

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

Already on GitHub? # to your account

automatic update of the TOC to fix page number missing issues #2556

Merged
merged 8 commits into from
Feb 20, 2025
Merged
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
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 @@ -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)
- 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);
}
}
}