KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > Barcode


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

49 package com.lowagie.text.pdf;
50
51 import java.awt.Color JavaDoc;
52
53 import com.lowagie.text.ExceptionConverter;
54 import com.lowagie.text.Image;
55 import com.lowagie.text.Rectangle;
56 /** Base class containing properties and methods commom to all
57  * barcode types.
58  *
59  * @author Paulo Soares (psoares@consiste.pt)
60  */

61 public abstract class Barcode {
62     /** A type of barcode */
63     public static final int EAN13 = 1;
64     /** A type of barcode */
65     public static final int EAN8 = 2;
66     /** A type of barcode */
67     public static final int UPCA = 3;
68     /** A type of barcode */
69     public static final int UPCE = 4;
70     /** A type of barcode */
71     public static final int SUPP2 = 5;
72     /** A type of barcode */
73     public static final int SUPP5 = 6;
74     /** A type of barcode */
75     public static final int POSTNET = 7;
76     /** A type of barcode */
77     public static final int PLANET = 8;
78     /** A type of barcode */
79     public static final int CODE128 = 9;
80     /** A type of barcode */
81     public static final int CODE128_UCC = 10;
82     /** A type of barcode */
83     public static final int CODE128_RAW = 11;
84     /** A type of barcode */
85     public static final int CODABAR = 12;
86
87     /** The minimum bar width.
88      */

89     protected float x;
90
91     /** The bar multiplier for wide bars or the distance between
92      * bars for Postnet and Planet.
93      */

94     protected float n;
95     
96     /** The text font. <CODE>null</CODE> if no text.
97      */

98     protected BaseFont font;
99
100     /** The size of the text or the height of the shorter bar
101      * in Postnet.
102      */

103     protected float size;
104     
105     /** If positive, the text distance under the bars. If zero or negative,
106      * the text distance above the bars.
107      */

108     protected float baseline;
109     
110     /** The height of the bars.
111      */

112     protected float barHeight;
113     
114     /** The text alignment. Can be <CODE>Element.ALIGN_LEFT</CODE>,
115      * <CODE>Element.ALIGN_CENTER</CODE> or <CODE>Element.ALIGN_RIGHT</CODE>.
116      */

117     protected int textAlignment;
118     
119     /** The optional checksum generation.
120      */

121     protected boolean generateChecksum;
122     
123     /** Shows the generated checksum in the the text.
124      */

125     protected boolean checksumText;
126     
127     /** Show the start and stop character '*' in the text for
128      * the barcode 39 or 'ABCD' for codabar.
129      */

130     protected boolean startStopText;
131     
132     /** Generates extended barcode 39.
133      */

134     protected boolean extended;
135     
136     /** The code to generate.
137      */

138     protected String JavaDoc code = "";
139     
140     /** Show the guard bars for barcode EAN.
141      */

142     protected boolean guardBars;
143     
144     /** The code type.
145      */

146     protected int codeType;
147     
148     /** The ink spreading. */
149     protected float inkSpreading = 0;
150     
151     /** Gets the minimum bar width.
152      * @return the minimum bar width
153      */

154     public float getX() {
155         return x;
156     }
157     
158     /** Sets the minimum bar width.
159      * @param x the minimum bar width
160      */

161     public void setX(float x) {
162         this.x = x;
163     }
164     
165     /** Gets the bar multiplier for wide bars.
166      * @return the bar multiplier for wide bars
167      */

168     public float getN() {
169         return n;
170     }
171     
172     /** Sets the bar multiplier for wide bars.
173      * @param n the bar multiplier for wide bars
174      */

175     public void setN(float n) {
176         this.n = n;
177     }
178     
179     /** Gets the text font. <CODE>null</CODE> if no text.
180      * @return the text font. <CODE>null</CODE> if no text
181      */

182     public BaseFont getFont() {
183         return font;
184     }
185     
186     /** Sets the text font.
187      * @param font the text font. Set to <CODE>null</CODE> to suppress any text
188      */

189     public void setFont(BaseFont font) {
190         this.font = font;
191     }
192     
193     /** Gets the size of the text.
194      * @return the size of the text
195      */

196     public float getSize() {
197         return size;
198     }
199     
200     /** Sets the size of the text.
201      * @param size the size of the text
202      */

203     public void setSize(float size) {
204         this.size = size;
205     }
206     
207     /** Gets the text baseline.
208      * If positive, the text distance under the bars. If zero or negative,
209      * the text distance above the bars.
210      * @return the baseline.
211      */

212     public float getBaseline() {
213         return baseline;
214     }
215     
216     /** Sets the text baseline.
217      * If positive, the text distance under the bars. If zero or negative,
218      * the text distance above the bars.
219      * @param baseline the baseline.
220      */

221     public void setBaseline(float baseline) {
222         this.baseline = baseline;
223     }
224     
225     /** Gets the height of the bars.
226      * @return the height of the bars
227      */

228     public float getBarHeight() {
229         return barHeight;
230     }
231     
232     /** Sets the height of the bars.
233      * @param barHeight the height of the bars
234      */

235     public void setBarHeight(float barHeight) {
236         this.barHeight = barHeight;
237     }
238     
239     /** Gets the text alignment. Can be <CODE>Element.ALIGN_LEFT</CODE>,
240      * <CODE>Element.ALIGN_CENTER</CODE> or <CODE>Element.ALIGN_RIGHT</CODE>.
241      * @return the text alignment
242      */

243     public int getTextAlignment() {
244         return textAlignment;
245     }
246     
247     /** Sets the text alignment. Can be <CODE>Element.ALIGN_LEFT</CODE>,
248      * <CODE>Element.ALIGN_CENTER</CODE> or <CODE>Element.ALIGN_RIGHT</CODE>.
249      * @param textAlignment the text alignment
250      */

251     public void setTextAlignment(int textAlignment) {
252         this.textAlignment = textAlignment;
253     }
254     
255     /** Gets the optional checksum generation.
256      * @return the optional checksum generation
257      */

258     public boolean isGenerateChecksum() {
259         return generateChecksum;
260     }
261     
262     /** Setter for property generateChecksum.
263      * @param generateChecksum New value of property generateChecksum.
264      */

265     public void setGenerateChecksum(boolean generateChecksum) {
266         this.generateChecksum = generateChecksum;
267     }
268     
269     /** Gets the property to show the generated checksum in the the text.
270      * @return value of property checksumText
271      */

272     public boolean isChecksumText() {
273         return checksumText;
274     }
275     
276     /** Sets the property to show the generated checksum in the the text.
277      * @param checksumText new value of property checksumText
278      */

279     public void setChecksumText(boolean checksumText) {
280         this.checksumText = checksumText;
281     }
282     
283     /** Sets the property to show the start and stop character '*' in the text for
284      * the barcode 39.
285      * @return value of property startStopText
286      */

287     public boolean isStartStopText() {
288         return startStopText;
289     }
290     
291     /** Gets the property to show the start and stop character '*' in the text for
292      * the barcode 39.
293      * @param startStopText new value of property startStopText
294      */

295     public void setStartStopText(boolean startStopText) {
296         this.startStopText = startStopText;
297     }
298     
299     /** Gets the property to generate extended barcode 39.
300      * @return value of property extended.
301      */

302     public boolean isExtended() {
303         return extended;
304     }
305     
306     /** Sets the property to generate extended barcode 39.
307      * @param extended new value of property extended
308      */

309     public void setExtended(boolean extended) {
310         this.extended = extended;
311     }
312     
313     /** Gets the code to generate.
314      * @return the code to generate
315      */

316     public String JavaDoc getCode() {
317         return code;
318     }
319     
320     /** Sets the code to generate.
321      * @param code the code to generate
322      */

323     public void setCode(String JavaDoc code) {
324         this.code = code;
325     }
326     
327     /** Gets the property to show the guard bars for barcode EAN.
328      * @return value of property guardBars
329      */

330     public boolean isGuardBars() {
331         return guardBars;
332     }
333     
334     /** Sets the property to show the guard bars for barcode EAN.
335      * @param guardBars new value of property guardBars
336      */

337     public void setGuardBars(boolean guardBars) {
338         this.guardBars = guardBars;
339     }
340     
341     /** Gets the code type.
342      * @return the code type
343      */

344     public int getCodeType() {
345         return codeType;
346     }
347     
348     /** Sets the code type.
349      * @param codeType the code type
350      */

351     public void setCodeType(int codeType) {
352         this.codeType = codeType;
353     }
354     
355     /** Gets the maximum area that the barcode and the text, if
356      * any, will occupy. The lower left corner is always (0, 0).
357      * @return the size the barcode occupies.
358      */

359     public abstract Rectangle getBarcodeSize();
360     
361     /** Places the barcode in a <CODE>PdfContentByte</CODE>. The
362      * barcode is always placed at coodinates (0, 0). Use the
363      * translation matrix to move it elsewhere.<p>
364      * The bars and text are written in the following colors:<p>
365      * <P><TABLE BORDER=1>
366      * <TR>
367      * <TH><P><CODE>barColor</CODE></TH>
368      * <TH><P><CODE>textColor</CODE></TH>
369      * <TH><P>Result</TH>
370      * </TR>
371      * <TR>
372      * <TD><P><CODE>null</CODE></TD>
373      * <TD><P><CODE>null</CODE></TD>
374      * <TD><P>bars and text painted with current fill color</TD>
375      * </TR>
376      * <TR>
377      * <TD><P><CODE>barColor</CODE></TD>
378      * <TD><P><CODE>null</CODE></TD>
379      * <TD><P>bars and text painted with <CODE>barColor</CODE></TD>
380      * </TR>
381      * <TR>
382      * <TD><P><CODE>null</CODE></TD>
383      * <TD><P><CODE>textColor</CODE></TD>
384      * <TD><P>bars painted with current color<br>text painted with <CODE>textColor</CODE></TD>
385      * </TR>
386      * <TR>
387      * <TD><P><CODE>barColor</CODE></TD>
388      * <TD><P><CODE>textColor</CODE></TD>
389      * <TD><P>bars painted with <CODE>barColor</CODE><br>text painted with <CODE>textColor</CODE></TD>
390      * </TR>
391      * </TABLE>
392      * @param cb the <CODE>PdfContentByte</CODE> where the barcode will be placed
393      * @param barColor the color of the bars. It can be <CODE>null</CODE>
394      * @param textColor the color of the text. It can be <CODE>null</CODE>
395      * @return the dimensions the barcode occupies
396      */

397     public abstract Rectangle placeBarcode(PdfContentByte cb, Color JavaDoc barColor, Color JavaDoc textColor);
398     
399     /** Creates a template with the barcode.
400      * @param cb the <CODE>PdfContentByte</CODE> to create the template. It
401      * serves no other use
402      * @param barColor the color of the bars. It can be <CODE>null</CODE>
403      * @param textColor the color of the text. It can be <CODE>null</CODE>
404      * @return the template
405      * @see #placeBarcode(PdfContentByte cb, Color barColor, Color textColor)
406      */

407     public PdfTemplate createTemplateWithBarcode(PdfContentByte cb, Color JavaDoc barColor, Color JavaDoc textColor) {
408         PdfTemplate tp = cb.createTemplate(0, 0);
409         Rectangle rect = placeBarcode(tp, barColor, textColor);
410         tp.setBoundingBox(rect);
411         return tp;
412     }
413     
414     /** Creates an <CODE>Image</CODE> with the barcode.
415      * @param cb the <CODE>PdfContentByte</CODE> to create the <CODE>Image</CODE>. It
416      * serves no other use
417      * @param barColor the color of the bars. It can be <CODE>null</CODE>
418      * @param textColor the color of the text. It can be <CODE>null</CODE>
419      * @return the <CODE>Image</CODE>
420      * @see #placeBarcode(PdfContentByte cb, Color barColor, Color textColor)
421      */

422     public Image createImageWithBarcode(PdfContentByte cb, Color JavaDoc barColor, Color JavaDoc textColor) {
423         try {
424             return Image.getInstance(createTemplateWithBarcode(cb, barColor, textColor));
425         }
426         catch (Exception JavaDoc e) {
427             throw new ExceptionConverter(e);
428         }
429     }
430     
431     /** Creates a <CODE>java.awt.Image</CODE>. This image only
432      * contains the bars without any text.
433      * @param foreground the color of the bars
434      * @param background the color of the background
435      * @return the image
436      */

437     public abstract java.awt.Image JavaDoc createAwtImage(Color JavaDoc foreground, Color JavaDoc background);
438     
439     /** Gets the amount of ink spreading.
440      * @return the ink spreading
441      *
442      */

443     public float getInkSpreading() {
444         return this.inkSpreading;
445     }
446     
447     /** Sets the amount of ink spreading. This value will be subtracted
448      * to the width of each bar. The actual value will depend on the ink
449      * and the printing medium.
450      * @param inkSpreading the ink spreading
451      *
452      */

453     public void setInkSpreading(float inkSpreading) {
454         this.inkSpreading = inkSpreading;
455     }
456
457     /**
458      * The alternate text to be used, if present.
459      */

460     protected String JavaDoc altText;
461
462     /**
463      * Gets the alternate text.
464      * @return the alternate text
465      */

466     public String JavaDoc getAltText() {
467         return this.altText;
468     }
469
470     /**
471      * Sets the alternate text. If present, this text will be used instead of the
472      * text derived from the supplied code.
473      * @param altText the alternate text
474      */

475     public void setAltText(String JavaDoc altText) {
476         this.altText = altText;
477     }
478     
479 }
480
Popular Tags