1 50 51 package com.lowagie.text.rtf.document; 52 53 import java.io.ByteArrayOutputStream ; 54 import java.io.IOException ; 55 import java.io.OutputStream ; 56 57 import com.lowagie.text.PageSize; 58 import com.lowagie.text.Rectangle; 59 import com.lowagie.text.rtf.RtfElement; 60 import com.lowagie.text.rtf.RtfExtendedElement; 61 62 63 71 public class RtfPageSetting extends RtfElement implements RtfExtendedElement { 72 73 76 private static final byte[] PAGE_WIDTH = "\\paperw".getBytes(); 77 80 private static final byte[] PAGE_HEIGHT = "\\paperh".getBytes(); 81 84 private static final byte[] MARGIN_LEFT = "\\margl".getBytes(); 85 88 private static final byte[] MARGIN_RIGHT = "\\margr".getBytes(); 89 92 private static final byte[] MARGIN_TOP = "\\margt".getBytes(); 93 96 private static final byte[] MARGIN_BOTTOM = "\\margb".getBytes(); 97 100 private static final byte[] LANDSCAPE = "\\lndscpsxn".getBytes(); 101 104 private static final byte[] SECTION_PAGE_WIDTH = "\\pgwsxn".getBytes(); 105 108 private static final byte[] SECTION_PAGE_HEIGHT = "\\pghsxn".getBytes(); 109 112 private static final byte[] SECTION_MARGIN_LEFT = "\\marglsxn".getBytes(); 113 116 private static final byte[] SECTION_MARGIN_RIGHT = "\\margrsxn".getBytes(); 117 120 private static final byte[] SECTION_MARGIN_TOP = "\\margtsxn".getBytes(); 121 124 private static final byte[] SECTION_MARGIN_BOTTOM = "\\margbsxn".getBytes(); 125 126 129 private int pageWidth = 11906; 130 133 private int pageHeight = 16840; 134 137 private int marginLeft = 1800; 138 141 private int marginRight = 1800; 142 145 private int marginTop = 1440; 146 149 private int marginBottom = 1440; 150 153 private boolean landscape = false; 154 155 160 public RtfPageSetting(RtfDocument doc) { 161 super(doc); 162 } 163 164 168 public byte[] write() 169 { 170 return(new byte[0]); 171 } 172 175 public void writeContent(final OutputStream out) throws IOException 176 { 177 } 178 179 185 public byte[] writeDefinition() { 186 ByteArrayOutputStream result = new ByteArrayOutputStream (); 187 try { 188 writeDefinition(result); 189 } catch(IOException ioe) { 190 ioe.printStackTrace(); 191 } 192 return result.toByteArray(); 193 } 194 195 198 public void writeDefinition(final OutputStream result) throws IOException 199 { 200 result.write(PAGE_WIDTH); 201 result.write(intToByteArray(pageWidth)); 202 result.write(PAGE_HEIGHT); 203 result.write(intToByteArray(pageHeight)); 204 result.write(MARGIN_LEFT); 205 result.write(intToByteArray(marginLeft)); 206 result.write(MARGIN_RIGHT); 207 result.write(intToByteArray(marginRight)); 208 result.write(MARGIN_TOP); 209 result.write(intToByteArray(marginTop)); 210 result.write(MARGIN_BOTTOM); 211 result.write(intToByteArray(marginBottom)); 212 result.write((byte)'\n'); 213 } 214 215 221 public byte[] writeSectionDefinition() { 222 ByteArrayOutputStream result = new ByteArrayOutputStream (); 223 try { 224 writeSectionDefinition(result); 225 } catch(IOException ioe) { 226 ioe.printStackTrace(); 227 } 228 return result.toByteArray(); 229 } 230 233 public void writeSectionDefinition(final OutputStream result) throws IOException 234 { 235 if(landscape) { 236 result.write(LANDSCAPE); 237 result.write(SECTION_PAGE_WIDTH); 238 result.write(intToByteArray(pageWidth)); 239 result.write(SECTION_PAGE_HEIGHT); 240 result.write(intToByteArray(pageHeight)); 241 result.write((byte)'\n'); 242 } else { 243 result.write(SECTION_PAGE_WIDTH); 244 result.write(intToByteArray(pageWidth)); 245 result.write(SECTION_PAGE_HEIGHT); 246 result.write(intToByteArray(pageHeight)); 247 result.write((byte)'\n'); 248 } 249 result.write(SECTION_MARGIN_LEFT); 250 result.write(intToByteArray(marginLeft)); 251 result.write(SECTION_MARGIN_RIGHT); 252 result.write(intToByteArray(marginRight)); 253 result.write(SECTION_MARGIN_TOP); 254 result.write(intToByteArray(marginTop)); 255 result.write(SECTION_MARGIN_BOTTOM); 256 result.write(intToByteArray(marginBottom)); 257 } 258 259 264 public int getMarginBottom() { 265 return marginBottom; 266 } 267 268 273 public void setMarginBottom(int marginBottom) { 274 this.marginBottom = marginBottom; 275 } 276 277 282 public int getMarginLeft() { 283 return marginLeft; 284 } 285 286 291 public void setMarginLeft(int marginLeft) { 292 this.marginLeft = marginLeft; 293 } 294 295 300 public int getMarginRight() { 301 return marginRight; 302 } 303 304 309 public void setMarginRight(int marginRight) { 310 this.marginRight = marginRight; 311 } 312 313 318 public int getMarginTop() { 319 return marginTop; 320 } 321 322 327 public void setMarginTop(int marginTop) { 328 this.marginTop = marginTop; 329 } 330 331 336 public int getPageHeight() { 337 return pageHeight; 338 } 339 340 345 public void setPageHeight(int pageHeight) { 346 this.pageHeight = pageHeight; 347 } 348 349 354 public int getPageWidth() { 355 return pageWidth; 356 } 357 358 363 public void setPageWidth(int pageWidth) { 364 this.pageWidth = pageWidth; 365 } 366 367 374 public void setPageSize(Rectangle pageSize) { 375 if(!guessFormat(pageSize, false)) { 376 this.pageWidth = (int) (pageSize.getWidth() * RtfElement.TWIPS_FACTOR); 377 this.pageHeight = (int) (pageSize.getHeight() * RtfElement.TWIPS_FACTOR); 378 this.landscape = pageWidth > pageHeight; 379 } 380 } 381 382 392 private boolean guessFormat(Rectangle pageSize, boolean rotate) { 393 if (rotate) { 394 pageSize = pageSize.rotate(); 395 } 396 if (rectEquals(pageSize, PageSize.A3)) { 397 pageWidth = 16837; 398 pageHeight = 23811; 399 landscape = rotate; 400 return true; 401 } 402 if (rectEquals(pageSize, PageSize.A4)) { 403 pageWidth = 11907; 404 pageHeight = 16840; 405 landscape = rotate; 406 return true; 407 } 408 if (rectEquals(pageSize, PageSize.A5)) { 409 pageWidth = 8391; 410 pageHeight = 11907; 411 landscape = rotate; 412 return true; 413 } 414 if (rectEquals(pageSize, PageSize.A6)) { 415 pageWidth = 5959; 416 pageHeight = 8420; 417 landscape = rotate; 418 return true; 419 } 420 if (rectEquals(pageSize, PageSize.B4)) { 421 pageWidth = 14570; 422 pageHeight = 20636; 423 landscape = rotate; 424 return true; 425 } 426 if (rectEquals(pageSize, PageSize.B5)) { 427 pageWidth = 10319; 428 pageHeight = 14572; 429 landscape = rotate; 430 return true; 431 } 432 if (rectEquals(pageSize, PageSize.HALFLETTER)) { 433 pageWidth = 7927; 434 pageHeight = 12247; 435 landscape = rotate; 436 return true; 437 } 438 if (rectEquals(pageSize, PageSize.LETTER)) { 439 pageWidth = 12242; 440 pageHeight = 15842; 441 landscape = rotate; 442 return true; 443 } 444 if (rectEquals(pageSize, PageSize.LEGAL)) { 445 pageWidth = 12252; 446 pageHeight = 20163; 447 landscape = rotate; 448 return true; 449 } 450 if (!rotate && guessFormat(pageSize, true)) { 451 int x = pageWidth; 452 pageWidth = pageHeight; 453 pageHeight = x; 454 return true; 455 } 456 return false; 457 } 458 459 466 private boolean rectEquals(Rectangle rect1, Rectangle rect2) { 467 return (rect1.getWidth() == rect2.getWidth()) && (rect1.getHeight() == rect2.getHeight()); 468 } 469 } 470 | Popular Tags |