Header.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /**
  3. * PHPWord
  4. *
  5. * Copyright (c) 2011 PHPWord
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPWord
  22. * @package PHPWord
  23. * @copyright Copyright (c) 010 PHPWord
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version Beta 0.6.3, 08.07.2011
  26. */
  27. /**
  28. * PHPWord_Section_Header
  29. *
  30. * @category PHPWord
  31. * @package PHPWord_Section
  32. * @copyright Copyright (c) 2011 PHPWord
  33. */
  34. class PHPWord_Section_Header {
  35. /**
  36. * Header Count
  37. *
  38. * @var int
  39. */
  40. private $_headerCount;
  41. /**
  42. * Header Relation ID
  43. *
  44. * @var int
  45. */
  46. private $_rId;
  47. /**
  48. * Header Element Collection
  49. *
  50. * @var int
  51. */
  52. private $_elementCollection = array();
  53. /**
  54. * Create a new Header
  55. */
  56. public function __construct($sectionCount) {
  57. $this->_headerCount = $sectionCount;
  58. }
  59. /**
  60. * Add a Text Element
  61. *
  62. * @param string $text
  63. * @param mixed $styleFont
  64. * @param mixed $styleParagraph
  65. * @return PHPWord_Section_Text
  66. */
  67. public function addText($text, $styleFont = null, $styleParagraph = null) {
  68. //$givenText = utf8_encode($text);
  69. //$givenText = iconv('gbk', 'utf-8', $givenText);
  70. //$givenText = iconv('GB2312', 'utf-8//IGNORE', $givenText);
  71. $givenText = PHPWord_Media::GBKToUTF8($givenText);
  72. $text = new PHPWord_Section_Text($givenText, $styleFont, $styleParagraph);
  73. $this->_elementCollection[] = $text;
  74. return $text;
  75. }
  76. /**
  77. * Add a TextBreak Element
  78. *
  79. * @param int $count
  80. */
  81. public function addTextBreak($count = 1) {
  82. for($i=1; $i<=$count; $i++) {
  83. $this->_elementCollection[] = new PHPWord_Section_TextBreak();
  84. }
  85. }
  86. /**
  87. * Create a new TextRun
  88. *
  89. * @return PHPWord_Section_TextRun
  90. */
  91. public function createTextRun($styleParagraph = null) {
  92. $textRun = new PHPWord_Section_TextRun($styleParagraph);
  93. $this->_elementCollection[] = $textRun;
  94. return $textRun;
  95. }
  96. /**
  97. * Add a Table Element
  98. *
  99. * @param mixed $style
  100. * @return PHPWord_Section_Table
  101. */
  102. public function addTable($style = null) {
  103. $table = new PHPWord_Section_Table('header', $this->_headerCount, $style);
  104. $this->_elementCollection[] = $table;
  105. return $table;
  106. }
  107. /**
  108. * Add a Image Element
  109. *
  110. * @param string $src
  111. * @param mixed $style
  112. * @return PHPWord_Section_Image
  113. */
  114. public function addImage($src, $style = null) {
  115. $image = new PHPWord_Section_Image($src, $style);
  116. if(!is_null($image->getSource())) {
  117. $rID = PHPWord_Media::addHeaderMediaElement($this->_headerCount, $src);
  118. $image->setRelationId($rID);
  119. $this->_elementCollection[] = $image;
  120. return $image;
  121. } else {
  122. trigger_error('Src does not exist or invalid image type.', E_ERROR);
  123. }
  124. }
  125. /**
  126. * Add a by PHP created Image Element
  127. *
  128. * @param string $link
  129. * @param mixed $style
  130. * @return PHPWord_Section_MemoryImage
  131. */
  132. public function addMemoryImage($link, $style = null) {
  133. $memoryImage = new PHPWord_Section_MemoryImage($link, $style);
  134. if(!is_null($memoryImage->getSource())) {
  135. $rID = PHPWord_Media::addHeaderMediaElement($this->_headerCount, $link, $memoryImage);
  136. $memoryImage->setRelationId($rID);
  137. $this->_elementCollection[] = $memoryImage;
  138. return $memoryImage;
  139. } else {
  140. trigger_error('Unsupported image type.');
  141. }
  142. }
  143. /**
  144. * Add a PreserveText Element
  145. *
  146. * @param string $text
  147. * @param mixed $styleFont
  148. * @param mixed $styleParagraph
  149. * @return PHPWord_Section_Footer_PreserveText
  150. */
  151. public function addPreserveText($text, $styleFont = null, $styleParagraph = null) {
  152. //$text = utf8_encode($text);
  153. //$text = iconv('gbk', 'utf-8', $text);
  154. //$text = iconv('GB2312', 'utf-8//IGNORE', $text);
  155. $text = PHPWord_Media::GBKToUTF8($text);
  156. $ptext = new PHPWord_Section_Footer_PreserveText($text, $styleFont, $styleParagraph);
  157. $this->_elementCollection[] = $ptext;
  158. return $ptext;
  159. }
  160. /**
  161. * Add a Watermark Element
  162. *
  163. * @param string $src
  164. * @param mixed $style
  165. * @return PHPWord_Section_Image
  166. */
  167. public function addWatermark($src, $style = null) {
  168. $image = new PHPWord_Section_Image($src, $style, true);
  169. if(!is_null($image->getSource())) {
  170. $rID = PHPWord_Media::addHeaderMediaElement($this->_headerCount, $src);
  171. $image->setRelationId($rID);
  172. $this->_elementCollection[] = $image;
  173. return $image;
  174. } else {
  175. trigger_error('Src does not exist or invalid image type.', E_ERROR);
  176. }
  177. }
  178. /**
  179. * Get Header Relation ID
  180. */
  181. public function getRelationId() {
  182. return $this->_rId;
  183. }
  184. /**
  185. * Set Header Relation ID
  186. *
  187. * @param int $rId
  188. */
  189. public function setRelationId($rId) {
  190. $this->_rId = $rId;
  191. }
  192. /**
  193. * Get all Header Elements
  194. */
  195. public function getElements() {
  196. return $this->_elementCollection;
  197. }
  198. /**
  199. * Get Header Count
  200. */
  201. public function getHeaderCount() {
  202. return $this->_headerCount;
  203. }
  204. }
  205. ?>