No Description

Sample_01_SimpleText.php 3.2KB

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. $phpWord->addFontStyle('rStyle', array('bold' => true, 'italic' => true, 'size' => 16, 'allCaps' => true, 'doubleStrikethrough' => true));
  7. $phpWord->addParagraphStyle('pStyle', array('align' => 'center', 'spaceAfter' => 100));
  8. $phpWord->addTitleStyle(1, array('bold' => true), array('spaceAfter' => 240));
  9. // New portrait section
  10. $section = $phpWord->addSection();
  11. // Simple text
  12. $section->addTitle(htmlspecialchars('Welcome to PhpWord'), 1);
  13. $section->addText(htmlspecialchars('Hello World!'));
  14. // Two text break
  15. $section->addTextBreak(2);
  16. // Defined style
  17. $section->addText(htmlspecialchars('I am styled by a font style definition.'), 'rStyle');
  18. $section->addText(htmlspecialchars('I am styled by a paragraph style definition.'), null, 'pStyle');
  19. $section->addText(htmlspecialchars('I am styled by both font and paragraph style.'), 'rStyle', 'pStyle');
  20. $section->addTextBreak();
  21. // Inline font style
  22. $fontStyle['name'] = 'Times New Roman';
  23. $fontStyle['size'] = 20;
  24. $textrun = $section->addTextRun();
  25. $textrun->addText(htmlspecialchars('I am inline styled '), $fontStyle);
  26. $textrun->addText(htmlspecialchars('with '));
  27. $textrun->addText(htmlspecialchars('color'), array('color' => '996699'));
  28. $textrun->addText(htmlspecialchars(', '));
  29. $textrun->addText(htmlspecialchars('bold'), array('bold' => true));
  30. $textrun->addText(htmlspecialchars(', '));
  31. $textrun->addText(htmlspecialchars('italic'), array('italic' => true));
  32. $textrun->addText(htmlspecialchars(', '));
  33. $textrun->addText(htmlspecialchars('underline'), array('underline' => 'dash'));
  34. $textrun->addText(htmlspecialchars(', '));
  35. $textrun->addText(htmlspecialchars('strikethrough'), array('strikethrough' => true));
  36. $textrun->addText(htmlspecialchars(', '));
  37. $textrun->addText(htmlspecialchars('doubleStrikethrough'), array('doubleStrikethrough' => true));
  38. $textrun->addText(htmlspecialchars(', '));
  39. $textrun->addText(htmlspecialchars('superScript'), array('superScript' => true));
  40. $textrun->addText(htmlspecialchars(', '));
  41. $textrun->addText(htmlspecialchars('subScript'), array('subScript' => true));
  42. $textrun->addText(htmlspecialchars(', '));
  43. $textrun->addText(htmlspecialchars('smallCaps'), array('smallCaps' => true));
  44. $textrun->addText(htmlspecialchars(', '));
  45. $textrun->addText(htmlspecialchars('allCaps'), array('allCaps' => true));
  46. $textrun->addText(htmlspecialchars(', '));
  47. $textrun->addText(htmlspecialchars('fgColor'), array('fgColor' => 'yellow'));
  48. $textrun->addText(htmlspecialchars(', '));
  49. $textrun->addText(htmlspecialchars('scale'), array('scale' => 200));
  50. $textrun->addText(htmlspecialchars(', '));
  51. $textrun->addText(htmlspecialchars('spacing'), array('spacing' => 120));
  52. $textrun->addText(htmlspecialchars(', '));
  53. $textrun->addText(htmlspecialchars('kerning'), array('kerning' => 10));
  54. $textrun->addText(htmlspecialchars('. '));
  55. // Link
  56. $section->addLink('http://www.google.com', htmlspecialchars('Google'));
  57. $section->addTextBreak();
  58. // Image
  59. $section->addImage('resources/_earth.jpg', array('width'=>18, 'height'=>18));
  60. // Save file
  61. echo write($phpWord, basename(__FILE__, '.php'), $writers);
  62. if (!CLI) {
  63. include_once 'Sample_Footer.php';
  64. }