TitleTOC.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. require_once '../PHPWord.php';
  3. // New Word Document
  4. $PHPWord = new PHPWord();
  5. // New portrait section
  6. $section = $PHPWord->createSection();
  7. // Define the TOC font style
  8. $fontStyle = array('spaceAfter'=>60, 'size'=>12);
  9. // Add title styles
  10. $PHPWord->addTitleStyle(1, array('size'=>20, 'color'=>'333333', 'bold'=>true));
  11. $PHPWord->addTitleStyle(2, array('size'=>16, 'color'=>'666666'));
  12. // Add text elements
  13. $section->addText('Table of contents:');
  14. $section->addTextBreak(2);
  15. // Add TOC
  16. $section->addTOC($fontStyle);
  17. // Add Titles
  18. $section->addPageBreak();
  19. $section->addTitle('I am Title 1', 1);
  20. $section->addText('Some text...');
  21. $section->addTextBreak(2);
  22. $section->addTitle('I am a Subtitle of Title 1', 2);
  23. $section->addTextBreak(2);
  24. $section->addText('Some more text...');
  25. $section->addTextBreak(2);
  26. $section->addTitle('Another Title (Title 2)', 1);
  27. $section->addText('Some text...');
  28. $section->addPageBreak();
  29. $section->addTitle('I am Title 3', 1);
  30. $section->addText('And more text...');
  31. $section->addTextBreak(2);
  32. $section->addTitle('I am a Subtitle of Title 3', 2);
  33. $section->addText('Again and again, more text...');
  34. echo 'Note: The pagenumbers in the TOC doesnt refresh automatically.';
  35. // Save File
  36. $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
  37. $objWriter->save('TitleTOC.docx');
  38. ?>