001package com.hfg.xml.msofficexml.docx.wordprocessingml; 002 003 004import com.hfg.graphics.units.GfxSize; 005import com.hfg.graphics.units.GfxUnits; 006import com.hfg.graphics.units.Twips; 007import com.hfg.util.StringUtil; 008import com.hfg.xml.XMLName; 009import com.hfg.xml.XMLTag; 010 011// Section 17.6.11 (pg. 571) of Ecma Office Open XML Part 1 012 013// <w:pgMar w:top="1440" w:right="1152" w:bottom="1440" 014// w:left="1152" w:header="720" w:footer="720" w:gutter="0"/> 015 016//------------------------------------------------------------------------------ 017/** 018 * Page margins tag (<pgMar>) of a OfficeOpenXML Docx document. 019 * 020 * @author J. Alex Taylor, hairyfatguy.com 021 */ 022//------------------------------------------------------------------------------ 023// com.hfg XML/HTML Coding Library 024// 025// This library is free software; you can redistribute it and/or 026// modify it under the terms of the GNU Lesser General Public 027// License as published by the Free Software Foundation; either 028// version 2.1 of the License, or (at your option) any later version. 029// 030// This library is distributed in the hope that it will be useful, 031// but WITHOUT ANY WARRANTY; without even the implied warranty of 032// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 033// Lesser General Public License for more details. 034// 035// You should have received a copy of the GNU Lesser General Public 036// License along with this library; if not, write to the Free Software 037// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 038// 039// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 040// jataylor@hairyfatguy.com 041//------------------------------------------------------------------------------ 042 043public class WmlPageMargins extends XMLTag 044{ 045 //--------------------------------------------------------------------------- 046 public WmlPageMargins() 047 { 048 super(WmlXML.PAGE_MARGINS); 049 } 050 051 052 //--------------------------------------------------------------------------- 053 /** 054 * Specifies the distance (in twentieths of a point) between the bottom of the text 055 * margins for the main document and the bottom of the page for all pages in this section. 056 * @param inValue distance specified in generic GfxSize which will be converted to twisps (twentieths of a point) 057 * @return this WmlPageMargins object to enable method chaining 058 */ 059 public WmlPageMargins setBottom(GfxSize inValue) 060 { 061 if (inValue != null) 062 { 063 setAttribute(WmlXML.BOTTOM_ATT, inValue.toInt(GfxUnits.dxa)); 064 } 065 else 066 { 067 removeAttribute(WmlXML.BOTTOM_ATT); 068 } 069 070 return this; 071 } 072 073 //--------------------------------------------------------------------------- 074 public GfxSize getBottom() 075 { 076 return getTwispAttribute(WmlXML.BOTTOM_ATT); 077 } 078 079 //--------------------------------------------------------------------------- 080 /** 081 * Specifies the distance (in twips or twentieths of a point) from the bottom edge of the page to the bottom edge of the footer. 082 * @param inValue distance specified in generic GfxSize which will be converted to twisps (twentieths of a point) 083 * @return this WmlPageMargins object to enable method chaining 084 */ 085 public WmlPageMargins setFooter(GfxSize inValue) 086 { 087 if (inValue != null) 088 { 089 setAttribute(WmlXML.FOOTER_ATT, inValue.toInt(GfxUnits.dxa)); 090 } 091 else 092 { 093 removeAttribute(WmlXML.FOOTER_ATT); 094 } 095 096 return this; 097 } 098 099 //--------------------------------------------------------------------------- 100 public GfxSize getFooter() 101 { 102 return getTwispAttribute(WmlXML.FOOTER_ATT); 103 } 104 105 //--------------------------------------------------------------------------- 106 /** 107 * Specifies the page gutter (the extra space added to the margin, typically to account for binding). 108 * @param inValue distance specified in generic GfxSize which will be converted to twisps (twentieths of a point) 109 * @return this WmlPageMargins object to enable method chaining 110 */ 111 public WmlPageMargins setGutter(GfxSize inValue) 112 { 113 if (inValue != null) 114 { 115 setAttribute(WmlXML.GUTTER_ATT, inValue.toInt(GfxUnits.dxa)); 116 } 117 else 118 { 119 removeAttribute(WmlXML.GUTTER_ATT); 120 } 121 122 return this; 123 } 124 125 //--------------------------------------------------------------------------- 126 public GfxSize getGutter() 127 { 128 return getTwispAttribute(WmlXML.GUTTER_ATT); 129 } 130 131 //--------------------------------------------------------------------------- 132 /** 133 * Specifies the distance (in twips or twentieths of a point) from the top edge of the page to the top edge of the header. 134 * @param inValue distance specified in generic GfxSize which will be converted to twisps (twentieths of a point) 135 * @return this WmlPageMargins object to enable method chaining 136 */ 137 public WmlPageMargins setHeader(GfxSize inValue) 138 { 139 if (inValue != null) 140 { 141 setAttribute(WmlXML.HEADER_ATT, inValue.toInt(GfxUnits.dxa)); 142 } 143 else 144 { 145 removeAttribute(WmlXML.HEADER_ATT); 146 } 147 148 return this; 149 } 150 151 //--------------------------------------------------------------------------- 152 public GfxSize getHeader() 153 { 154 return getTwispAttribute(WmlXML.HEADER_ATT); 155 } 156 157 //--------------------------------------------------------------------------- 158 /** 159 * Specifies the distance (in twips or twentieths of a point) from the left edge of the page to the left edge of the text. 160 * @param inValue distance specified in generic GfxSize which will be converted to twisps (twentieths of a point) 161 * @return this WmlPageMargins object to enable method chaining 162 */ 163 public WmlPageMargins setLeft(GfxSize inValue) 164 { 165 if (inValue != null) 166 { 167 setAttribute(WmlXML.LEFT_ATT, inValue.toInt(GfxUnits.dxa)); 168 } 169 else 170 { 171 removeAttribute(WmlXML.LEFT_ATT); 172 } 173 174 return this; 175 } 176 177 //--------------------------------------------------------------------------- 178 public GfxSize getLeft() 179 { 180 return getTwispAttribute(WmlXML.LEFT_ATT); 181 } 182 183 //--------------------------------------------------------------------------- 184 /** 185 * Specifies the distance (in twips or twentieths of a point) from the right edge of the page to the right edge of the text. 186 * @param inValue distance specified in generic GfxSize which will be converted to twisps (twentieths of a point) 187 * @return this WmlPageMargins object to enable method chaining 188 */ 189 public WmlPageMargins setRight(GfxSize inValue) 190 { 191 if (inValue != null) 192 { 193 setAttribute(WmlXML.RIGHT_ATT, inValue.toInt(GfxUnits.dxa)); 194 } 195 else 196 { 197 removeAttribute(WmlXML.RIGHT_ATT); 198 } 199 200 return this; 201 } 202 203 //--------------------------------------------------------------------------- 204 public GfxSize getRight() 205 { 206 return getTwispAttribute(WmlXML.RIGHT_ATT); 207 } 208 209 //--------------------------------------------------------------------------- 210 /** 211 * Specifies the distance (in twips or twentieths of a point) from the top edge of the page to the top edge of the text. 212 * @param inValue distance specified in generic GfxSize which will be converted to twisps (twentieths of a point) 213 * @return this WmlPageMargins object to enable method chaining 214 */ 215 public WmlPageMargins setTop(GfxSize inValue) 216 { 217 if (inValue != null) 218 { 219 setAttribute(WmlXML.TOP_ATT, inValue.toInt(GfxUnits.dxa)); 220 } 221 else 222 { 223 removeAttribute(WmlXML.TOP_ATT); 224 } 225 226 return this; 227 } 228 229 //--------------------------------------------------------------------------- 230 public GfxSize getTop() 231 { 232 return getTwispAttribute(WmlXML.TOP_ATT); 233 } 234 235 236 //--------------------------------------------------------------------------- 237 private GfxSize getTwispAttribute(XMLName inAttribute) 238 { 239 GfxSize value = null; 240 String stringValue = getAttributeValue(inAttribute); 241 if (StringUtil.isSet(stringValue)) 242 { 243 value = new Twips(Integer.parseInt(stringValue)); 244 } 245 246 return value; 247 } 248 249}