To add only the heading of the last page to the Table of Contents in PHPWord, you can add a bookmark to the heading on the last page and then add it to the Table of Contents using the addLink
method.
Here's an example code snippet:
// Create PHPWord object
$phpWord = new \PhpOffice\PhpWord\PhpWord();
// Add a section
$section = $phpWord->addSection();
// Add some content to the section
$section->addText('Some content');
$section->addPageBreak();
// Add the last page content with a bookmark
$lastPageSection = $phpWord->addSection();
$lastPageSection->addText('Last page content', array('size' => 14, 'bold' => true));
$lastPageSection->addBookmark('last-page');
// Add a Table of Contents
$tocStyle = array('tabLeader' => \PhpOffice\PhpWord\Style\TOC::TAB_LEADER_DOT, 'textAlignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER);
$section->addTOC($tocStyle);
// Add the bookmarked heading to the Table of Contents
$toc = $section->getTOC();
$toc->addLink('Last page heading', 'last-page', null, \PhpOffice\PhpWord\SimpleType\TblWidth::AUTO);
In this example, the addBookmark
method is used to add a bookmark named "last-page" to the heading on the last page. Then, the addLink
method is used to add the heading to the Table of Contents, with the bookmark name and other optional parameters. Only the last page heading will be added to the Table of Contents, not the entire page.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-05-02 09:54:15 +0000
Seen: 9 times
Last updated: May 02 '23
How can a HTTP header be inserted in WordPress?
How can data be stored in a data attribute and utilized through Ajax?
How can an array be passed using typo3 flexform xml and itemsProcConfig?
Does JSON encode fail to retrieve data from the database?
Can the GS1 128 barcode decoder in PHP or Jquery be utilized?
How can Xdebug be used in conjunction with VSCode for Laravel on Sail and WSL2?
In PHP, what is the method for finding encrypted data using the "like" operator?
What is the method to pass a variable from PHP back to JavaScript?
Why do PHP variable variables display unexpected behavior when used with arrays?