No Description

Sample_21_TableRowRules.php 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. $section = $phpWord->addSection();
  7. $section->addText(htmlspecialchars('By default, when you insert an image, it adds a textbreak after its content.'));
  8. $section->addText(
  9. htmlspecialchars('If we want a simple border around an image, we wrap the image inside a table->row->cell')
  10. );
  11. $section->addText(
  12. htmlspecialchars(
  13. 'On the image with the red border, even if we set the row height to the height of the image, '
  14. . 'the textbreak is still there:'
  15. )
  16. );
  17. $table1 = $section->addTable(array('cellMargin' => 0, 'cellMarginRight' => 0, 'cellMarginBottom' => 0, 'cellMarginLeft' => 0));
  18. $table1->addRow(3750);
  19. $cell1 = $table1->addCell(null, array('valign' => 'top', 'borderSize' => 30, 'borderColor' => 'ff0000'));
  20. $cell1->addImage('./resources/_earth.jpg', array('width' => 250, 'height' => 250, 'align' => 'center'));
  21. $section->addTextBreak();
  22. $section->addText(
  23. htmlspecialchars(
  24. "But if we set the rowStyle 'exactHeight' to true, the real row height is used, removing the textbreak:"
  25. )
  26. );
  27. $table2 = $section->addTable(
  28. array(
  29. 'cellMargin' => 0,
  30. 'cellMarginRight' => 0,
  31. 'cellMarginBottom' => 0,
  32. 'cellMarginLeft' => 0,
  33. )
  34. );
  35. $table2->addRow(3750, array('exactHeight' => true));
  36. $cell2 = $table2->addCell(null, array('valign' => 'top', 'borderSize' => 30, 'borderColor' => '00ff00'));
  37. $cell2->addImage('./resources/_earth.jpg', array('width' => 250, 'height' => 250, 'align' => 'center'));
  38. $section->addTextBreak();
  39. $section->addText(
  40. htmlspecialchars('In this example, image is 250px height. Rows are calculated in twips, and 1px = 15twips.')
  41. );
  42. $section->addText(htmlspecialchars('So: $' . "table2->addRow(3750, array('exactHeight'=>true));"));
  43. // Save file
  44. echo write($phpWord, basename(__FILE__, '.php'), $writers);
  45. if (!CLI) {
  46. include_once 'Sample_Footer.php';
  47. }