0
0

HeaderFooter.php 13 KB

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