No Description

Sample_17_TitleTOC.php 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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(htmlspecialchars('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(htmlspecialchars('Text between TOC'));
  24. $section->addTextBreak(2);
  25. // Add TOC #1
  26. $section->addText(htmlspecialchars('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(htmlspecialchars('Foo & Bar'), 1);
  34. $section->addText(htmlspecialchars('Some text...'));
  35. $section->addTextBreak(2);
  36. $section->addTitle(htmlspecialchars('I am a Subtitle of Title 1'), 2);
  37. $section->addTextBreak(2);
  38. $section->addText(htmlspecialchars('Some more text...'));
  39. $section->addTextBreak(2);
  40. $section->addTitle(htmlspecialchars('Another Title (Title 2)'), 1);
  41. $section->addText(htmlspecialchars('Some text...'));
  42. $section->addPageBreak();
  43. $section->addTitle(htmlspecialchars('I am Title 3'), 1);
  44. $section->addText(htmlspecialchars('And more text...'));
  45. $section->addTextBreak(2);
  46. $section->addTitle(htmlspecialchars('I am a Subtitle of Title 3'), 2);
  47. $section->addText(htmlspecialchars('Again and again, more text...'));
  48. $section->addTitle(htmlspecialchars('Subtitle 3.1.1'), 3);
  49. $section->addText(htmlspecialchars('Text'));
  50. $section->addTitle(htmlspecialchars('Subtitle 3.1.1.1'), 4);
  51. $section->addText(htmlspecialchars('Text'));
  52. $section->addTitle(htmlspecialchars('Subtitle 3.1.1.2'), 4);
  53. $section->addText(htmlspecialchars('Text'));
  54. $section->addTitle(htmlspecialchars('Subtitle 3.1.2'), 3);
  55. $section->addText(htmlspecialchars('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. }