KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: PdfBarcode.java,v 1.47 2005/03/15 08:15:28 blowagie Exp $
3  * $Name: $
4  *
5  * Copyright 2001, 2002 Bruno Lowagie
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.pdf;
52
53
54 /**
55  * A barcode is a Chunk with a certain type of barcode font.
56  * <P>
57  * With this class you can construct several types of barcode
58  * in different sizes, representing any 'product' or 'article' number.
59  */

60
61 public class PdfBarcode extends com.lowagie.text.Chunk {
62     
63     /** This is a type of barcode. */
64     public static final int CODE39 = 1;
65     
66     /** This is a type of barcode. */
67     public static final int UPCA = 2;
68     
69     /** This is a type of barcode. */
70     public static final int EAN13 = 3;
71     
72     /** This is a type of barcode. */
73     public static final int INTERLEAVED_2_OF_5 = 4;
74     
75     /** The variable parity table in EAN 13 */
76     public static final int[][] variableParity =
77     { {0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2},
78     {0, 0, 1, 0, 1, 1, 2, 2, 2, 2, 2, 2},
79     {0, 0, 1, 1, 0, 1, 2, 2, 2, 2, 2, 2},
80     {0, 0, 1, 1, 1, 0, 2, 2, 2, 2, 2, 2},
81     {0, 1, 0, 0, 1, 1, 2, 2, 2, 2, 2, 2},
82     {0, 1, 1, 0, 0, 1, 2, 2, 2, 2, 2, 2},
83     {0, 1, 1, 1, 0, 0, 2, 2, 2, 2, 2, 2},
84     {0, 1, 0, 1, 0, 1, 2, 2, 2, 2, 2, 2},
85     {0, 1, 0, 1, 1, 0, 2, 2, 2, 2, 2, 2},
86     {0, 1, 1, 0, 1, 0, 2, 2, 2, 2, 2, 2} };
87     
88     /**
89      * Creates a new Barcode.
90      *
91      * @param ttf the ttf file representing the barcode font
92      * @param size the size of the barcode
93      * @param number the number you want to convert to a barcode in String format
94      */

95     
96     public PdfBarcode(String ttf, int type, int size, String number) throws com.lowagie.text.BadElementException, com.lowagie.text.DocumentException, java.io.IOException {
97         super(convertToCode(type, number), new com.lowagie.text.Font(BaseFont.createFont(ttf, "winansi", true), size));
98     }
99     
100     /**
101      * Creates a new Barcode.
102      *
103      * @param ttf the ttf file representing the barcode font
104      * @param size the size of the barcode
105      * @param number the number you want to convert to a barcode in long format
106      */

107     
108     public PdfBarcode(String ttf, int type, int size, long number) throws com.lowagie.text.BadElementException, com.lowagie.text.DocumentException, java.io.IOException {
109         super(convertToCode(type, String.valueOf(number)), new com.lowagie.text.Font(BaseFont.createFont(ttf, "winansi", true), size));
110     }
111     
112     /**
113      * Converts a String representing a number to a barcode with a specific barcode font.
114      *
115      * @param type the type of barcode
116      * @param number the number you want to convert to a barcode in long format
117      */

118     
119     private static String convertToCode(int type, String number) throws com.lowagie.text.BadElementException {
120         StringBuffer code = new StringBuffer();
121         int length = number.length();
122         int digit;
123         int pos = 0;
124         try {
125             switch(type) {
126                 case CODE39:
127                     code.append('*');
128                     while (pos < length) {
129                         code.append(number.substring(pos, ++pos));
130                     }
131                     code.append('*');
132                     break;
133                 case UPCA:
134                     if (length > 12) throw new com.lowagie.text.BadElementException("An UPC-A barcode can only encode a 12 digit number (your number was " + number + ").");
135                     number = addZero(number, 12);
136                     digit = Integer.parseInt(number.substring(pos, ++pos));
137                     code.append((char) (digit + 80));
138                     while (pos < 6) {
139                         digit = Integer.parseInt(number.substring(pos, ++pos));
140                         code.append((char) (digit + 48));
141                     }
142                     code.append((char) 112);
143                     while (pos < 11) {
144                         digit = Integer.parseInt(number.substring(pos, ++pos));
145                         code.append((char) (digit + 64));
146                     }
147                     digit = Integer.parseInt(number.substring(11));
148                     code.append((char) (digit + 96));
149                     break;
150                 case EAN13:
151                     if (length > 13) throw new com.lowagie.text.BadElementException("An EAN-13 barcode can only encode a 13 digit number (your number was " + number + ").");
152                     number = addZero(number, 13);
153                     int firstdigit = Integer.parseInt(number.substring(pos, ++pos));
154                     code.append((char) (firstdigit + 33));
155                     digit = Integer.parseInt(number.substring(pos, ++pos));
156                     code.append((char) (digit + 96));
157                     while (pos < 7) {
158                         digit = Integer.parseInt(number.substring(pos, ++pos));
159                         code.append((char) (digit + 48 + 16 * variableParity[firstdigit][pos - 2]));
160                     }
161                     code.append((char) 124);
162                     while (pos < 12) {
163                         digit = Integer.parseInt(number.substring(pos, ++pos));
164                         code.append((char) (digit + 48 + 16 * variableParity[firstdigit][pos - 2]));
165                     }
166                     digit = Integer.parseInt(number.substring(12));
167                     code.append((char) (digit + 112));
168                     break;
169                 case INTERLEAVED_2_OF_5:
170                     if (length % 2 == 1) {
171                         number = addZero(number, length + 1);
172                     }
173                     code.append('(');
174                     while (number.length() > 2) {
175                         digit = Integer.parseInt(number.substring(0, 2));
176                         code.append(convertInterleaved(digit));
177                         pos += 2;
178                         number = number.substring(2);
179                     }
180                     digit = Integer.parseInt(number);
181                     code.append(convertInterleaved(digit));
182                     code.append(')');
183                     break;
184                 default:
185                     throw new com.lowagie.text.BadElementException("This type of barcode is not supported yet: " + type);
186             }
187         }
188         catch(NumberFormatException nfe) {
189             throw new com.lowagie.text.BadElementException("NumberFormatException at position " + pos + " in " + number + ": " + nfe.getMessage());
190         }
191         return code.toString();
192     }
193     
194     /**
195      * Converts an int to a convert interleaved character.
196      */

197     
198     private static char convertInterleaved(int digits) throws NumberFormatException {
199         int i;
200         if (digits < 50) {
201             i = 48;
202         }
203         else if (digits < 100) {
204             i = 142;
205         }
206         else {
207             throw new NumberFormatException(String.valueOf(digits));
208         }
209         return (char)(digits + i);
210     }
211     
212     /**
213      * Adds leading zeros.
214      */

215     
216     private static String addZero(String number, int length) {
217         StringBuffer buf = new StringBuffer();
218         int zeros = length - number.length();
219         for (int i = 0; i < zeros; i++) {
220             buf.append('0');
221         }
222         buf.append(number);
223         return buf.toString();
224     }
225 }
Popular Tags