You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

XSSFHeaderFooter.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xssf.usermodel.extensions;
  16. import org.apache.poi.ss.usermodel.HeaderFooter;
  17. import org.apache.poi.xssf.usermodel.helpers.HeaderFooterHelper;
  18. import org.apache.poi.util.Internal;
  19. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
  20. /**
  21. * Parent class of all XSSF headers and footers.
  22. *
  23. * For a list of all the different fields that can be placed into a header or
  24. * footer, such as page number, bold, underline etc, see the follow formatting
  25. * syntax
  26. *
  27. *<b> Header/Footer Formatting Syntax</b>
  28. *<p>
  29. * There are a number of formatting codes that can be written inline with the
  30. * actual header / footer text, which affect the formatting in the header or
  31. * footer.
  32. *</p>
  33. *
  34. * This example shows the text "Center Bold Header" on the first line (center
  35. * section), and the date on the second line (center section). &amp;CCenter
  36. * &amp;"-,Bold"Bold &amp;"-,Regular"Header_x000A_&amp;D
  37. *
  38. * <b>General Rules:</b> There is no required order in which these codes must
  39. * appear. The first occurrence of the following codes turns the formatting ON,
  40. * the second occurrence turns it OFF again:
  41. *
  42. * <dl>
  43. * <dt>&amp;L</dt>
  44. * <dd>code for "left section" (there are three header / footer locations,
  45. * "left", "center", and "right"). When two or more occurrences of this section
  46. * marker exist, the contents from all markers are concatenated, in the order of
  47. * appearance, and placed into the left section.</dd>
  48. * <dt>&amp;P</dt>
  49. * <dd>code for "current page #"</dd>
  50. * <dt>&amp;N</dt>
  51. * <dd>code for "total pages"</dd>
  52. * <dt>&amp;font size</dt>
  53. * <dd>code for "text font size", where font size is a font size in points.</dd>
  54. * <dt>&amp;K</dt>
  55. * <dd>code for "text font color" RGB Color is specified as RRGGBB Theme Color
  56. * is specifed as TTSNN where TT is the theme color Id, S is either "+" or "-"
  57. * of the tint/shade value, NN is the tint/shade value.</dd>
  58. * <dt>&amp;S</dt>
  59. * <dd>code for "text strikethrough" on / off</dd>
  60. * <dt>&amp;X</dt>
  61. * <dd>code for "text super script" on / off</dd>
  62. * <dt>&amp;Y</dt>
  63. * <dd>code for "text subscript" on / off</dd>
  64. * <dt>&amp;C</dt>
  65. * <dd>code for "center section". When two or more occurrences of this section
  66. * marker exist, the contents from all markers are concatenated, in the order of
  67. * appearance, and placed into the center section. SpreadsheetML Reference
  68. * Material - Worksheets 1966</dd>
  69. * <dt>&amp;D</dt>
  70. * <dd>code for "date"</dd>
  71. * <dt>&amp;T</dt>
  72. * <dd>code for "time"</dd>
  73. * <dt>&amp;G</dt>
  74. * <dd>code for "picture as background"</dd>
  75. * <dt>&amp;U</dt>
  76. * <dd>code for "text single underline"</dd>
  77. * <dt>&amp;E</dt>
  78. * <dd>code for "double underline"</dd>
  79. * <dt>&amp;R</dt>
  80. * <dd>code for "right section". When two or more occurrences of this section
  81. * marker exist, the contents from all markers are concatenated, in the order of
  82. * appearance, and placed into the right section.</dd>
  83. * <dt>&amp;Z</dt>
  84. * <dd>code for "this workbook's file path"</dd>
  85. * <dt>&amp;F</dt>
  86. * <dd>code for "this workbook's file name"</dd>
  87. * <dt>&amp;A</dt>
  88. * <dd>code for "sheet tab name"</dd>
  89. * <dt>&amp;+</dt>
  90. * <dd>code for add to page #.</dd>
  91. * <dt>&amp;-</dt>
  92. * <dd>code for subtract from page #.</dd>
  93. * <dt>&amp;"font name,font type" - code for "text font name" and "text font type",
  94. * where font name and font type are strings specifying the name and type of the
  95. * font, separated by a comma. When a hyphen appears in font name, it means
  96. * "none specified". Both of font name and font type can be localized
  97. * values.</dt>
  98. * <dt>&amp;"-,Bold"</dt>
  99. * <dd>code for "bold font style"</dd>
  100. * <dt>&amp;B</dt>
  101. * <dd>also means "bold font style"</dd>
  102. * <dt>&amp;"-,Regular"</dt>
  103. * <dd>code for "regular font style"</dd>
  104. * <dt>&amp;"-,Italic"</dt>
  105. * <dd>code for "italic font style"</dd>
  106. * <dt>&amp;I</dt>
  107. * <dd>also means "italic font style"</dd>
  108. * <dt>&amp;"-,Bold Italic"</dt>
  109. * <dd>code for "bold italic font style"</dd>
  110. * <dt>&amp;O</dt>
  111. * <dd>code for "outline style"</dd>
  112. * <dt>&amp;H</dt>
  113. * <dd>code for "shadow style"</dd>
  114. * </dl>
  115. *
  116. *
  117. */
  118. public abstract class XSSFHeaderFooter implements HeaderFooter {
  119. private final HeaderFooterHelper helper;
  120. private final CTHeaderFooter headerFooter;
  121. private boolean stripFields;
  122. /**
  123. * Create an instance of XSSFAbstractHeaderFooter from the supplied XML bean
  124. */
  125. public XSSFHeaderFooter(CTHeaderFooter headerFooter) {
  126. this.headerFooter = headerFooter;
  127. this.helper = new HeaderFooterHelper();
  128. }
  129. /**
  130. * Returns the underlying CTHeaderFooter xml bean
  131. *
  132. * @return the underlying CTHeaderFooter xml bean
  133. */
  134. @Internal
  135. public CTHeaderFooter getHeaderFooter() {
  136. return this.headerFooter;
  137. }
  138. /**
  139. * Returns the value of the header or footer.
  140. *
  141. * @return the value of the header or footer.
  142. */
  143. public String getValue() {
  144. String value = getText();
  145. if (value == null)
  146. return "";
  147. return value;
  148. }
  149. /**
  150. * Are fields currently being stripped from the text that this
  151. * XSSFHeaderFooter returns? Default is false, but can be changed
  152. */
  153. public boolean areFieldsStripped() {
  154. return stripFields;
  155. }
  156. /**
  157. * Should fields (eg macros) be stripped from the text that this class
  158. * returns? Default is not to strip.
  159. */
  160. public void setAreFieldsStripped(boolean stripFields) {
  161. this.stripFields = stripFields;
  162. }
  163. /**
  164. * Removes any fields (eg macros, page markers etc) from the string.
  165. * Normally used to make some text suitable for showing to humans, and the
  166. * resultant text should not normally be saved back into the document!
  167. */
  168. public static String stripFields(String text) {
  169. return org.apache.poi.hssf.usermodel.HeaderFooter.stripFields(text);
  170. }
  171. public abstract String getText();
  172. protected abstract void setText(String text);
  173. /**
  174. * get the text representing the center part of this element
  175. */
  176. @Override
  177. public String getCenter() {
  178. String text = helper.getCenterSection(getText());
  179. if (stripFields)
  180. return stripFields(text);
  181. return text;
  182. }
  183. /**
  184. * get the text representing the left part of this element
  185. */
  186. @Override
  187. public String getLeft() {
  188. String text = helper.getLeftSection(getText());
  189. if (stripFields)
  190. return stripFields(text);
  191. return text;
  192. }
  193. /**
  194. * get the text representing the right part of this element
  195. */
  196. @Override
  197. public String getRight() {
  198. String text = helper.getRightSection(getText());
  199. if (stripFields)
  200. return stripFields(text);
  201. return text;
  202. }
  203. /**
  204. * set a centered string value for this element
  205. */
  206. @Override
  207. public void setCenter(String newCenter) {
  208. setText(helper.setCenterSection(getText(), newCenter));
  209. }
  210. /**
  211. * set a left string value for this element
  212. */
  213. @Override
  214. public void setLeft(String newLeft) {
  215. setText(helper.setLeftSection(getText(), newLeft));
  216. }
  217. /**
  218. * set a right string value for this element
  219. */
  220. @Override
  221. public void setRight(String newRight) {
  222. setText(helper.setRightSection(getText(), newRight));
  223. }
  224. }