/samples/Sample_17_TitleTOC.php

https://github.com/cyrillkalita/PHPWord · PHP · 75 lines · 51 code · 14 blank · 10 comment · 1 complexity · 1d490ff4510cca0c3097ccc90479263d MD5 · raw file

  1. <?php
  2. include_once 'Sample_Header.php';
  3. // New Word document
  4. echo date('H:i:s'), " Create new PhpWord object", EOL;
  5. $phpWord = new \PhpOffice\PhpWord\PhpWord();
  6. // Begin code
  7. $section = $phpWord->addSection();
  8. // Define the TOC font style
  9. $fontStyle = array('spaceAfter' => 60, 'size' => 12);
  10. $fontStyle2 = array('size' => 10);
  11. // Add title styles
  12. $phpWord->addTitleStyle(1, array('size' => 20, 'color' => '333333', 'bold' => true));
  13. $phpWord->addTitleStyle(2, array('size' => 16, 'color' => '666666'));
  14. $phpWord->addTitleStyle(3, array('size' => 14, 'italic' => true));
  15. $phpWord->addTitleStyle(4, array('size' => 12));
  16. // Add text elements
  17. $section->addText('Table of contents 1');
  18. $section->addTextBreak(2);
  19. // Add TOC #1
  20. $toc = $section->addTOC($fontStyle);
  21. $section->addTextBreak(2);
  22. // Filler
  23. $section->addText('Text between TOC');
  24. $section->addTextBreak(2);
  25. // Add TOC #1
  26. $section->addText('Table of contents 2');
  27. $section->addTextBreak(2);
  28. $toc2 = $section->addTOC($fontStyle2);
  29. $toc2->setMinDepth(2);
  30. $toc2->setMaxDepth(3);
  31. // Add Titles
  32. $section->addPageBreak();
  33. $section->addTitle('I am Title 1', 1);
  34. $section->addText('Some text...');
  35. $section->addTextBreak(2);
  36. $section->addTitle('I am a Subtitle of Title 1', 2);
  37. $section->addTextBreak(2);
  38. $section->addText('Some more text...');
  39. $section->addTextBreak(2);
  40. $section->addTitle('Another Title (Title 2)', 1);
  41. $section->addText('Some text...');
  42. $section->addPageBreak();
  43. $section->addTitle('I am Title 3', 1);
  44. $section->addText('And more text...');
  45. $section->addTextBreak(2);
  46. $section->addTitle('I am a Subtitle of Title 3', 2);
  47. $section->addText('Again and again, more text...');
  48. $section->addTitle('Subtitle 3.1.1', 3);
  49. $section->addText('Text');
  50. $section->addTitle('Subtitle 3.1.1.1', 4);
  51. $section->addText('Text');
  52. $section->addTitle('Subtitle 3.1.1.2', 4);
  53. $section->addText('Text');
  54. $section->addTitle('Subtitle 3.1.2', 3);
  55. $section->addText('Text');
  56. echo date('H:i:s'), " Note: Please refresh TOC manually.", EOL;
  57. // Save file
  58. echo write($phpWord, basename(__FILE__, '.php'), $writers);
  59. if (!CLI) {
  60. include_once 'Sample_Footer.php';
  61. }