KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > rtf > document > RtfPageSetting


1 /*
2  * $Id: RtfPageSetting.java 2796 2007-05-27 09:42:20Z psoares33 $
3  * $Name: $
4  *
5  * Copyright 2003, 2004 by Mark Hall
6  *
7  * The contents of this file are subject to the Mozilla Public License Version 1.1
8  * (the "License"); you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the License.
14  *
15  * The Original Code is 'iText, a free JAVA-PDF library'.
16  *
17  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19  * All Rights Reserved.
20  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
22  *
23  * Contributor(s): all the names of the contributors are added in the source code
24  * where applicable.
25  *
26  * Alternatively, the contents of this file may be used under the terms of the
27  * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the
28  * provisions of LGPL are applicable instead of those above. If you wish to
29  * allow use of your version of this file only under the terms of the LGPL
30  * License and not to allow others to use your version of this file under
31  * the MPL, indicate your decision by deleting the provisions above and
32  * replace them with the notice and other provisions required by the LGPL.
33  * If you do not delete the provisions above, a recipient may use your version
34  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Library General Public License as published by the Free Software Foundation;
39  * either version 2 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
44  * details.
45  *
46  * If you didn't download this code from the following link, you should check if
47  * you aren't using an obsolete version:
48  * http://www.lowagie.com/iText/
49  */

50
51 package com.lowagie.text.rtf.document;
52
53 import java.io.ByteArrayOutputStream JavaDoc;
54 import java.io.IOException JavaDoc;
55 import java.io.OutputStream JavaDoc;
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 /**
64  * The RtfPageSetting stores the page size / page margins for a RtfDocument.
65  * INTERNAL CLASS - NOT TO BE USED DIRECTLY
66  *
67  * @version $Id: RtfPageSetting.java 2796 2007-05-27 09:42:20Z psoares33 $
68  * @author Mark Hall (mhall@edu.uni-klu.ac.at)
69  * @author Thomas Bickel (tmb99@inode.at)
70  */

71 public class RtfPageSetting extends RtfElement implements RtfExtendedElement {
72
73     /**
74      * Constant for the page height
75      */

76     private static final byte[] PAGE_WIDTH = "\\paperw".getBytes();
77     /**
78      * Constant for the page width
79      */

80     private static final byte[] PAGE_HEIGHT = "\\paperh".getBytes();
81     /**
82      * Constant for the left margin
83      */

84     private static final byte[] MARGIN_LEFT = "\\margl".getBytes();
85     /**
86      * Constant for the right margin
87      */

88     private static final byte[] MARGIN_RIGHT = "\\margr".getBytes();
89     /**
90      * Constant for the top margin
91      */

92     private static final byte[] MARGIN_TOP = "\\margt".getBytes();
93     /**
94      * Constant for the bottom margin
95      */

96     private static final byte[] MARGIN_BOTTOM = "\\margb".getBytes();
97     /**
98      * Constant for landscape
99      */

100     private static final byte[] LANDSCAPE = "\\lndscpsxn".getBytes();
101     /**
102      * Constant for the section page width
103      */

104     private static final byte[] SECTION_PAGE_WIDTH = "\\pgwsxn".getBytes();
105     /**
106      * Constant for the section page height
107      */

108     private static final byte[] SECTION_PAGE_HEIGHT = "\\pghsxn".getBytes();
109     /**
110      * Constant for the section left margin
111      */

112     private static final byte[] SECTION_MARGIN_LEFT = "\\marglsxn".getBytes();
113     /**
114      * Constant for the section right margin
115      */

116     private static final byte[] SECTION_MARGIN_RIGHT = "\\margrsxn".getBytes();
117     /**
118      * Constant for the section top margin
119      */

120     private static final byte[] SECTION_MARGIN_TOP = "\\margtsxn".getBytes();
121     /**
122      * Constant for the section bottom margin
123      */

124     private static final byte[] SECTION_MARGIN_BOTTOM = "\\margbsxn".getBytes();
125     
126     /**
127      * The page width to use
128      */

129     private int pageWidth = 11906;
130     /**
131      * The page height to use
132      */

133     private int pageHeight = 16840;
134     /**
135      * The left margin to use
136      */

137     private int marginLeft = 1800;
138     /**
139      * The right margin to use
140      */

141     private int marginRight = 1800;
142     /**
143      * The top margin to use
144      */

145     private int marginTop = 1440;
146     /**
147      * The bottom margin to use
148      */

149     private int marginBottom = 1440;
150     /**
151      * Whether the page is portrait or landscape
152      */

153     private boolean landscape = false;
154
155     /**
156      * Constructs a new RtfPageSetting object belonging to a RtfDocument.
157      *
158      * @param doc The RtfDocument this RtfPageSetting belongs to
159      */

160     public RtfPageSetting(RtfDocument doc) {
161         super(doc);
162     }
163     
164     /**
165      * unused
166      * @deprecated replaced by {@link #writeContent(OutputStream)}
167      */

168     public byte[] write()
169     {
170         return(new byte[0]);
171     }
172     /**
173      * unused
174      */

175     public void writeContent(final OutputStream JavaDoc out) throws IOException JavaDoc
176     {
177     }
178     
179     /**
180      * Writes the page size / page margin definition
181      *
182      * @return A byte array with the page size / page margin definition
183      * @deprecated replaced by {@link #writeDefinition(OutputStream)}
184      */

185     public byte[] writeDefinition() {
186         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
187         try {
188             writeDefinition(result);
189         } catch(IOException JavaDoc ioe) {
190             ioe.printStackTrace();
191         }
192         return result.toByteArray();
193     }
194     
195     /**
196      * Writes the page size / page margin definition
197      */

198     public void writeDefinition(final OutputStream JavaDoc result) throws IOException JavaDoc
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     /**
216      * Writes the definition part for a new section
217      *
218      * @return A byte array containing the definition for a new section
219      * @deprecated replaced by {@link #writeSectionDefinition(OutputStream)}
220      */

221     public byte[] writeSectionDefinition() {
222         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
223         try {
224             writeSectionDefinition(result);
225         } catch(IOException JavaDoc ioe) {
226             ioe.printStackTrace();
227         }
228         return result.toByteArray();
229     }
230     /**
231      * Writes the definition part for a new section
232      */

233     public void writeSectionDefinition(final OutputStream JavaDoc result) throws IOException JavaDoc
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     /**
260      * Gets the bottom margin
261      *
262      * @return Returns the bottom margin
263      */

264     public int getMarginBottom() {
265         return marginBottom;
266     }
267     
268     /**
269      * Sets the bottom margin
270      *
271      * @param marginBottom The bottom margin to use
272      */

273     public void setMarginBottom(int marginBottom) {
274         this.marginBottom = marginBottom;
275     }
276     
277     /**
278      * Gets the left margin
279      *
280      * @return Returns the left margin
281      */

282     public int getMarginLeft() {
283         return marginLeft;
284     }
285     
286     /**
287      * Sets the left margin to use
288      *
289      * @param marginLeft The left margin to use
290      */

291     public void setMarginLeft(int marginLeft) {
292         this.marginLeft = marginLeft;
293     }
294     
295     /**
296      * Gets the right margin
297      *
298      * @return Returns the right margin
299      */

300     public int getMarginRight() {
301         return marginRight;
302     }
303     
304     /**
305      * Sets the right margin to use
306      *
307      * @param marginRight The right margin to use
308      */

309     public void setMarginRight(int marginRight) {
310         this.marginRight = marginRight;
311     }
312     
313     /**
314      * Gets the top margin
315      *
316      * @return Returns the top margin
317      */

318     public int getMarginTop() {
319         return marginTop;
320     }
321     
322     /**
323      * Sets the top margin to use
324      *
325      * @param marginTop The top margin to use
326      */

327     public void setMarginTop(int marginTop) {
328         this.marginTop = marginTop;
329     }
330     
331     /**
332      * Gets the page height
333      *
334      * @return Returns the page height
335      */

336     public int getPageHeight() {
337         return pageHeight;
338     }
339     
340     /**
341      * Sets the page height to use
342      *
343      * @param pageHeight The page height to use
344      */

345     public void setPageHeight(int pageHeight) {
346         this.pageHeight = pageHeight;
347     }
348     
349     /**
350      * Gets the page width
351      *
352      * @return Returns the page width
353      */

354     public int getPageWidth() {
355         return pageWidth;
356     }
357     
358     /**
359      * Sets the page width to use
360      *
361      * @param pageWidth The page width to use
362      */

363     public void setPageWidth(int pageWidth) {
364         this.pageWidth = pageWidth;
365     }
366     
367     /**
368      * Set the page size to use. This method will use guessFormat to try to guess the correct
369      * page format. If no format could be guessed, the sizes from the pageSize are used and
370      * the landscape setting is determined by comparing width and height;
371      *
372      * @param pageSize The pageSize to use
373      */

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     /**
383      * This method tries to fit the <code>Rectangle pageSize</code> to one of the predefined PageSize rectangles.
384      * If a match is found the pageWidth and pageHeight will be set according to values determined from files
385      * generated by MS Word2000 and OpenOffice 641. If no match is found the method will try to match the rotated
386      * Rectangle by calling itself with the parameter rotate set to true.
387      *
388      * @param pageSize the page size for which to guess the correct format
389      * @param rotate Whether we should try to rotate the size befor guessing the format
390      * @return <code>True</code> if the format was guessed, <code>false/<code> otherwise
391      */

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     /**
460      * This method compares to Rectangles. They are considered equal if width and height are the same
461      *
462      * @param rect1 The first Rectangle to compare
463      * @param rect2 The second Rectangle to compare
464      * @return <code>True</code> if the Rectangles equal, <code>false</code> otherwise
465      */

466     private boolean rectEquals(Rectangle rect1, Rectangle rect2) {
467         return (rect1.getWidth() == rect2.getWidth()) && (rect1.getHeight() == rect2.getHeight());
468     }
469 }
470
Popular Tags