HeaderFooter.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2013 PHPExcel
  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 PHPExcel
  22. * @package PHPExcel_Worksheet
  23. * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.9, 2013-06-02
  26. */
  27. /**
  28. * PHPExcel_Worksheet_HeaderFooter
  29. *
  30. * <code>
  31. * Header/Footer Formatting Syntax taken from Office Open XML Part 4 - Markup Language Reference, page 1970:
  32. *
  33. * There are a number of formatting codes that can be written inline with the actual header / footer text, which
  34. * affect the formatting in the header or footer.
  35. *
  36. * Example: This example shows the text "Center Bold Header" on the first line (center section), and the date on
  37. * the second line (center section).
  38. * &CCenter &"-,Bold"Bold&"-,Regular"Header_x000A_&D
  39. *
  40. * General Rules:
  41. * There is no required order in which these codes must appear.
  42. *
  43. * The first occurrence of the following codes turns the formatting ON, the second occurrence turns it OFF again:
  44. * - strikethrough
  45. * - superscript
  46. * - subscript
  47. * Superscript and subscript cannot both be ON at same time. Whichever comes first wins and the other is ignored,
  48. * while the first is ON.
  49. * &L - code for "left section" (there are three header / footer locations, "left", "center", and "right"). When
  50. * two or more occurrences of this section marker exist, the contents from all markers are concatenated, in the
  51. * order of appearance, and placed into the left section.
  52. * &P - code for "current page #"
  53. * &N - code for "total pages"
  54. * &font size - code for "text font size", where font size is a font size in points.
  55. * &K - code for "text font color"
  56. * RGB Color is specified as RRGGBB
  57. * Theme Color is specifed as TTSNN where TT is the theme color Id, S is either "+" or "-" of the tint/shade
  58. * value, NN is the tint/shade value.
  59. * &S - code for "text strikethrough" on / off
  60. * &X - code for "text super script" on / off
  61. * &Y - code for "text subscript" on / off
  62. * &C - code for "center section". When two or more occurrences of this section marker exist, the contents
  63. * from all markers are concatenated, in the order of appearance, and placed into the center section.
  64. *
  65. * &D - code for "date"
  66. * &T - code for "time"
  67. * &G - code for "picture as background"
  68. * &U - code for "text single underline"
  69. * &E - code for "double underline"
  70. * &R - code for "right section". When two or more occurrences of this section marker exist, the contents
  71. * from all markers are concatenated, in the order of appearance, and placed into the right section.
  72. * &Z - code for "this workbook's file path"
  73. * &F - code for "this workbook's file name"
  74. * &A - code for "sheet tab name"
  75. * &+ - code for add to page #.
  76. * &- - code for subtract from page #.
  77. * &"font name,font type" - code for "text font name" and "text font type", where font name and font type
  78. * are strings specifying the name and type of the font, separated by a comma. When a hyphen appears in font
  79. * name, it means "none specified". Both of font name and font type can be localized values.
  80. * &"-,Bold" - code for "bold font style"
  81. * &B - also means "bold font style".
  82. * &"-,Regular" - code for "regular font style"
  83. * &"-,Italic" - code for "italic font style"
  84. * &I - also means "italic font style"
  85. * &"-,Bold Italic" code for "bold italic font style"
  86. * &O - code for "outline style"
  87. * &H - code for "shadow style"
  88. * </code>
  89. *
  90. * @category PHPExcel
  91. * @package PHPExcel_Worksheet
  92. * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  93. */
  94. class PHPExcel_Worksheet_HeaderFooter
  95. {
  96. /* Header/footer image location */
  97. const IMAGE_HEADER_LEFT = 'LH';
  98. const IMAGE_HEADER_CENTER = 'CH';
  99. const IMAGE_HEADER_RIGHT = 'RH';
  100. const IMAGE_FOOTER_LEFT = 'LF';
  101. const IMAGE_FOOTER_CENTER = 'CF';
  102. const IMAGE_FOOTER_RIGHT = 'RF';
  103. /**
  104. * OddHeader
  105. *
  106. * @var string
  107. */
  108. private $_oddHeader = '';
  109. /**
  110. * OddFooter
  111. *
  112. * @var string
  113. */
  114. private $_oddFooter = '';
  115. /**
  116. * EvenHeader
  117. *
  118. * @var string
  119. */
  120. private $_evenHeader = '';
  121. /**
  122. * EvenFooter
  123. *
  124. * @var string
  125. */
  126. private $_evenFooter = '';
  127. /**
  128. * FirstHeader
  129. *
  130. * @var string
  131. */
  132. private $_firstHeader = '';
  133. /**
  134. * FirstFooter
  135. *
  136. * @var string
  137. */
  138. private $_firstFooter = '';
  139. /**
  140. * Different header for Odd/Even, defaults to false
  141. *
  142. * @var boolean
  143. */
  144. private $_differentOddEven = false;
  145. /**
  146. * Different header for first page, defaults to false
  147. *
  148. * @var boolean
  149. */
  150. private $_differentFirst = false;
  151. /**
  152. * Scale with document, defaults to true
  153. *
  154. * @var boolean
  155. */
  156. private $_scaleWithDocument = true;
  157. /**
  158. * Align with margins, defaults to true
  159. *
  160. * @var boolean
  161. */
  162. private $_alignWithMargins = true;
  163. /**
  164. * Header/footer images
  165. *
  166. * @var PHPExcel_Worksheet_HeaderFooterDrawing[]
  167. */
  168. private $_headerFooterImages = array();
  169. /**
  170. * Create a new PHPExcel_Worksheet_HeaderFooter
  171. */
  172. public function __construct()
  173. {
  174. }
  175. /**
  176. * Get OddHeader
  177. *
  178. * @return string
  179. */
  180. public function getOddHeader() {
  181. return $this->_oddHeader;
  182. }
  183. /**
  184. * Set OddHeader
  185. *
  186. * @param string $pValue
  187. * @return PHPExcel_Worksheet_HeaderFooter
  188. */
  189. public function setOddHeader($pValue) {
  190. $this->_oddHeader = $pValue;
  191. return $this;
  192. }
  193. /**
  194. * Get OddFooter
  195. *
  196. * @return string
  197. */
  198. public function getOddFooter() {
  199. return $this->_oddFooter;
  200. }
  201. /**
  202. * Set OddFooter
  203. *
  204. * @param string $pValue
  205. * @return PHPExcel_Worksheet_HeaderFooter
  206. */
  207. public function setOddFooter($pValue) {
  208. $this->_oddFooter = $pValue;
  209. return $this;
  210. }
  211. /**
  212. * Get EvenHeader
  213. *
  214. * @return string
  215. */
  216. public function getEvenHeader() {
  217. return $this->_evenHeader;
  218. }
  219. /**
  220. * Set EvenHeader
  221. *
  222. * @param string $pValue
  223. * @return PHPExcel_Worksheet_HeaderFooter
  224. */
  225. public function setEvenHeader($pValue) {
  226. $this->_evenHeader = $pValue;
  227. return $this;
  228. }
  229. /**
  230. * Get EvenFooter
  231. *
  232. * @return string
  233. */
  234. public function getEvenFooter() {
  235. return $this->_evenFooter;
  236. }
  237. /**
  238. * Set EvenFooter
  239. *
  240. * @param string $pValue
  241. * @return PHPExcel_Worksheet_HeaderFooter
  242. */
  243. public function setEvenFooter($pValue) {
  244. $this->_evenFooter = $pValue;
  245. return $this;
  246. }
  247. /**
  248. * Get FirstHeader
  249. *
  250. * @return string
  251. */
  252. public function getFirstHeader() {
  253. return $this->_firstHeader;
  254. }
  255. /**
  256. * Set FirstHeader
  257. *
  258. * @param string $pValue
  259. * @return PHPExcel_Worksheet_HeaderFooter
  260. */
  261. public function setFirstHeader($pValue) {
  262. $this->_firstHeader = $pValue;
  263. return $this;
  264. }
  265. /**
  266. * Get FirstFooter
  267. *
  268. * @return string
  269. */
  270. public function getFirstFooter() {
  271. return $this->_firstFooter;
  272. }
  273. /**
  274. * Set FirstFooter
  275. *
  276. * @param string $pValue
  277. * @return PHPExcel_Worksheet_HeaderFooter
  278. */
  279. public function setFirstFooter($pValue) {
  280. $this->_firstFooter = $pValue;
  281. return $this;
  282. }
  283. /**
  284. * Get DifferentOddEven
  285. *
  286. * @return boolean
  287. */
  288. public function getDifferentOddEven() {
  289. return $this->_differentOddEven;
  290. }
  291. /**
  292. * Set DifferentOddEven
  293. *
  294. * @param boolean $pValue
  295. * @return PHPExcel_Worksheet_HeaderFooter
  296. */
  297. public function setDifferentOddEven($pValue = false) {
  298. $this->_differentOddEven = $pValue;
  299. return $this;
  300. }
  301. /**
  302. * Get DifferentFirst
  303. *
  304. * @return boolean
  305. */
  306. public function getDifferentFirst() {
  307. return $this->_differentFirst;
  308. }
  309. /**
  310. * Set DifferentFirst
  311. *
  312. * @param boolean $pValue
  313. * @return PHPExcel_Worksheet_HeaderFooter
  314. */
  315. public function setDifferentFirst($pValue = false) {
  316. $this->_differentFirst = $pValue;
  317. return $this;
  318. }
  319. /**
  320. * Get ScaleWithDocument
  321. *
  322. * @return boolean
  323. */
  324. public function getScaleWithDocument() {
  325. return $this->_scaleWithDocument;
  326. }
  327. /**
  328. * Set ScaleWithDocument
  329. *
  330. * @param boolean $pValue
  331. * @return PHPExcel_Worksheet_HeaderFooter
  332. */
  333. public function setScaleWithDocument($pValue = true) {
  334. $this->_scaleWithDocument = $pValue;
  335. return $this;
  336. }
  337. /**
  338. * Get AlignWithMargins
  339. *
  340. * @return boolean
  341. */
  342. public function getAlignWithMargins() {
  343. return $this->_alignWithMargins;
  344. }
  345. /**
  346. * Set AlignWithMargins
  347. *
  348. * @param boolean $pValue
  349. * @return PHPExcel_Worksheet_HeaderFooter
  350. */
  351. public function setAlignWithMargins($pValue = true) {
  352. $this->_alignWithMargins = $pValue;
  353. return $this;
  354. }
  355. /**
  356. * Add header/footer image
  357. *
  358. * @param PHPExcel_Worksheet_HeaderFooterDrawing $image
  359. * @param string $location
  360. * @throws PHPExcel_Exception
  361. * @return PHPExcel_Worksheet_HeaderFooter
  362. */
  363. public function addImage(PHPExcel_Worksheet_HeaderFooterDrawing $image = null, $location = self::IMAGE_HEADER_LEFT) {
  364. $this->_headerFooterImages[$location] = $image;
  365. return $this;
  366. }
  367. /**
  368. * Remove header/footer image
  369. *
  370. * @param string $location
  371. * @throws PHPExcel_Exception
  372. * @return PHPExcel_Worksheet_HeaderFooter
  373. */
  374. public function removeImage($location = self::IMAGE_HEADER_LEFT) {
  375. if (isset($this->_headerFooterImages[$location])) {
  376. unset($this->_headerFooterImages[$location]);
  377. }
  378. return $this;
  379. }
  380. /**
  381. * Set header/footer images
  382. *
  383. * @param PHPExcel_Worksheet_HeaderFooterDrawing[] $images
  384. * @throws PHPExcel_Exception
  385. * @return PHPExcel_Worksheet_HeaderFooter
  386. */
  387. public function setImages($images) {
  388. if (!is_array($images)) {
  389. throw new PHPExcel_Exception('Invalid parameter!');
  390. }
  391. $this->_headerFooterImages = $images;
  392. return $this;
  393. }
  394. /**
  395. * Get header/footer images
  396. *
  397. * @return PHPExcel_Worksheet_HeaderFooterDrawing[]
  398. */
  399. public function getImages() {
  400. // Sort array
  401. $images = array();
  402. if (isset($this->_headerFooterImages[self::IMAGE_HEADER_LEFT])) $images[self::IMAGE_HEADER_LEFT] = $this->_headerFooterImages[self::IMAGE_HEADER_LEFT];
  403. if (isset($this->_headerFooterImages[self::IMAGE_HEADER_CENTER])) $images[self::IMAGE_HEADER_CENTER] = $this->_headerFooterImages[self::IMAGE_HEADER_CENTER];
  404. if (isset($this->_headerFooterImages[self::IMAGE_HEADER_RIGHT])) $images[self::IMAGE_HEADER_RIGHT] = $this->_headerFooterImages[self::IMAGE_HEADER_RIGHT];
  405. if (isset($this->_headerFooterImages[self::IMAGE_FOOTER_LEFT])) $images[self::IMAGE_FOOTER_LEFT] = $this->_headerFooterImages[self::IMAGE_FOOTER_LEFT];
  406. if (isset($this->_headerFooterImages[self::IMAGE_FOOTER_CENTER])) $images[self::IMAGE_FOOTER_CENTER] = $this->_headerFooterImages[self::IMAGE_FOOTER_CENTER];
  407. if (isset($this->_headerFooterImages[self::IMAGE_FOOTER_RIGHT])) $images[self::IMAGE_FOOTER_RIGHT] = $this->_headerFooterImages[self::IMAGE_FOOTER_RIGHT];
  408. $this->_headerFooterImages = $images;
  409. return $this->_headerFooterImages;
  410. }
  411. /**
  412. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  413. */
  414. public function __clone() {
  415. $vars = get_object_vars($this);
  416. foreach ($vars as $key => $value) {
  417. if (is_object($value)) {
  418. $this->$key = clone $value;
  419. } else {
  420. $this->$key = $value;
  421. }
  422. }
  423. }
  424. }