1 50 51 package com.lowagie.text.pdf; 52 53 54 60 61 public class PdfBarcode extends com.lowagie.text.Chunk { 62 63 64 public static final int CODE39 = 1; 65 66 67 public static final int UPCA = 2; 68 69 70 public static final int EAN13 = 3; 71 72 73 public static final int INTERLEAVED_2_OF_5 = 4; 74 75 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 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 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 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 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 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 |