This project is read-only.

TOC not working?

Jul 9, 2010 at 12:41 PM

Anyone got the TOC functionality to work? I use the example from the documentation:

$styleTOC = array('tabLeader'=>PHPWord_Style_TOC::TABLEADER_DOT);
$styleFont = array('spaceAfter'=>60, 'name'=>'Tahoma', 'size'=>12);
$section->addTOC($styleFont, $styleTOC);

The result is empty though..

Jul 15, 2010 at 1:43 PM

Problem solved, I had forgot to use the addTitle function for titles...

However I get no page numbers (I add page numbers in the footer though). And I can manually update the TOC from Word to get page numbers but that is not the smoothest solution.

Jul 15, 2010 at 4:44 PM

Glad to see that your problem is solved.

The automatic update of the page numbers in the TOC isn't possible, because the native word function looks into the entire document and sets the page numbers directly. However, you need to right click the TOC and then update page numbers only.

Aug 21, 2012 at 9:26 AM

I try to add code to PHPWord/Writer/Word2007/Document.php like this:

$objWriter->startElement('w:r');
      $objWriter->startElement('w:instrText');
              $objWriter->writeAttribute('xml:space', 'preserve');
              $objWriter->writeRaw('PAGEREF '.$title['anchor'].'\h');
      $objWriter->endElement();
$objWriter->endElement();
					
										
$objWriter->startElement('w:r');
	$objWriter->startElement('w:fldChar');
		$objWriter->writeAttribute('w:fldCharType', 'end');
	$objWriter->endElement();
$objWriter->endElement();

//I add this
// start TOC page
$objWriter->startElement('w:r');
       $objWriter->startElement('w:t');
               $objWriter->writeRaw($i+2);
       $objWriter->endElement();
$objWriter->endElement();
//end 
...

In word2007 it works,but in 2003 it does not work

Jul 28, 2014 at 9:18 PM
Edited Jul 28, 2014 at 9:18 PM
wanglei_estar, this worked for me:
$objWriter->startElement('w:r');
       $objWriter->startElement('w:t');
               $objWriter->writeRaw($i+2);
       $objWriter->endElement();
$objWriter->endElement();
//end 
thanks
Oct 3, 2014 at 8:53 AM
Edited Oct 3, 2014 at 8:54 AM
Hii wanglei_estar,
I can see the Page number in TOC, but it does not seems to be correct page number. Any help?