001/* 002 * $Id: Barcode.java 4784 2011-03-15 08:33:00Z blowagie $ 003 * 004 * This file is part of the iText (R) project. 005 * Copyright (c) 1998-2011 1T3XT BVBA 006 * Authors: Bruno Lowagie, Paulo Soares, et al. 007 * 008 * This program is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU Affero General Public License version 3 010 * as published by the Free Software Foundation with the addition of the 011 * following permission added to Section 15 as permitted in Section 7(a): 012 * FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY 1T3XT, 013 * 1T3XT DISCLAIMS THE WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS. 014 * 015 * This program is distributed in the hope that it will be useful, but 016 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 017 * or FITNESS FOR A PARTICULAR PURPOSE. 018 * See the GNU Affero General Public License for more details. 019 * You should have received a copy of the GNU Affero General Public License 020 * along with this program; if not, see http://www.gnu.org/licenses or write to 021 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 022 * Boston, MA, 02110-1301 USA, or download the license from the following URL: 023 * http://itextpdf.com/terms-of-use/ 024 * 025 * The interactive user interfaces in modified source and object code versions 026 * of this program must display Appropriate Legal Notices, as required under 027 * Section 5 of the GNU Affero General Public License. 028 * 029 * In accordance with Section 7(b) of the GNU Affero General Public License, 030 * a covered work must retain the producer line in every PDF that is created 031 * or manipulated using iText. 032 * 033 * You can be released from the requirements of the license by purchasing 034 * a commercial license. Buying such a license is mandatory as soon as you 035 * develop commercial activities involving the iText software without 036 * disclosing the source code of your own applications. 037 * These activities include: offering paid services to customers as an ASP, 038 * serving PDFs on the fly in a web application, shipping iText with a closed 039 * source product. 040 * 041 * For more information, please contact iText Software Corp. at this 042 * address: sales@itextpdf.com 043 */ 044package com.itextpdf.text.pdf; 045 046import com.itextpdf.text.ExceptionConverter; 047import com.itextpdf.text.Image; 048import com.itextpdf.text.Rectangle; 049import com.itextpdf.text.BaseColor; 050/** Base class containing properties and methods common to all 051 * barcode types. 052 * 053 * @author Paulo Soares 054 */ 055public abstract class Barcode { 056 /** A type of barcode */ 057 public static final int EAN13 = 1; 058 /** A type of barcode */ 059 public static final int EAN8 = 2; 060 /** A type of barcode */ 061 public static final int UPCA = 3; 062 /** A type of barcode */ 063 public static final int UPCE = 4; 064 /** A type of barcode */ 065 public static final int SUPP2 = 5; 066 /** A type of barcode */ 067 public static final int SUPP5 = 6; 068 /** A type of barcode */ 069 public static final int POSTNET = 7; 070 /** A type of barcode */ 071 public static final int PLANET = 8; 072 /** A type of barcode */ 073 public static final int CODE128 = 9; 074 /** A type of barcode */ 075 public static final int CODE128_UCC = 10; 076 /** A type of barcode */ 077 public static final int CODE128_RAW = 11; 078 /** A type of barcode */ 079 public static final int CODABAR = 12; 080 081 /** The minimum bar width. 082 */ 083 protected float x; 084 085 /** The bar multiplier for wide bars or the distance between 086 * bars for Postnet and Planet. 087 */ 088 protected float n; 089 090 /** The text font. <CODE>null</CODE> if no text. 091 */ 092 protected BaseFont font; 093 094 /** The size of the text or the height of the shorter bar 095 * in Postnet. 096 */ 097 protected float size; 098 099 /** If positive, the text distance under the bars. If zero or negative, 100 * the text distance above the bars. 101 */ 102 protected float baseline; 103 104 /** The height of the bars. 105 */ 106 protected float barHeight; 107 108 /** The text alignment. Can be <CODE>Element.ALIGN_LEFT</CODE>, 109 * <CODE>Element.ALIGN_CENTER</CODE> or <CODE>Element.ALIGN_RIGHT</CODE>. 110 */ 111 protected int textAlignment; 112 113 /** The optional checksum generation. 114 */ 115 protected boolean generateChecksum; 116 117 /** Shows the generated checksum in the the text. 118 */ 119 protected boolean checksumText; 120 121 /** Show the start and stop character '*' in the text for 122 * the barcode 39 or 'ABCD' for codabar. 123 */ 124 protected boolean startStopText; 125 126 /** Generates extended barcode 39. 127 */ 128 protected boolean extended; 129 130 /** The code to generate. 131 */ 132 protected String code = ""; 133 134 /** Show the guard bars for barcode EAN. 135 */ 136 protected boolean guardBars; 137 138 /** The code type. 139 */ 140 protected int codeType; 141 142 /** The ink spreading. */ 143 protected float inkSpreading = 0; 144 145 /** Gets the minimum bar width. 146 * @return the minimum bar width 147 */ 148 public float getX() { 149 return x; 150 } 151 152 /** Sets the minimum bar width. 153 * @param x the minimum bar width 154 */ 155 public void setX(float x) { 156 this.x = x; 157 } 158 159 /** Gets the bar multiplier for wide bars. 160 * @return the bar multiplier for wide bars 161 */ 162 public float getN() { 163 return n; 164 } 165 166 /** Sets the bar multiplier for wide bars. 167 * @param n the bar multiplier for wide bars 168 */ 169 public void setN(float n) { 170 this.n = n; 171 } 172 173 /** Gets the text font. <CODE>null</CODE> if no text. 174 * @return the text font. <CODE>null</CODE> if no text 175 */ 176 public BaseFont getFont() { 177 return font; 178 } 179 180 /** Sets the text font. 181 * @param font the text font. Set to <CODE>null</CODE> to suppress any text 182 */ 183 public void setFont(BaseFont font) { 184 this.font = font; 185 } 186 187 /** Gets the size of the text. 188 * @return the size of the text 189 */ 190 public float getSize() { 191 return size; 192 } 193 194 /** Sets the size of the text. 195 * @param size the size of the text 196 */ 197 public void setSize(float size) { 198 this.size = size; 199 } 200 201 /** Gets the text baseline. 202 * If positive, the text distance under the bars. If zero or negative, 203 * the text distance above the bars. 204 * @return the baseline. 205 */ 206 public float getBaseline() { 207 return baseline; 208 } 209 210 /** Sets the text baseline. 211 * If positive, the text distance under the bars. If zero or negative, 212 * the text distance above the bars. 213 * @param baseline the baseline. 214 */ 215 public void setBaseline(float baseline) { 216 this.baseline = baseline; 217 } 218 219 /** Gets the height of the bars. 220 * @return the height of the bars 221 */ 222 public float getBarHeight() { 223 return barHeight; 224 } 225 226 /** Sets the height of the bars. 227 * @param barHeight the height of the bars 228 */ 229 public void setBarHeight(float barHeight) { 230 this.barHeight = barHeight; 231 } 232 233 /** Gets the text alignment. Can be <CODE>Element.ALIGN_LEFT</CODE>, 234 * <CODE>Element.ALIGN_CENTER</CODE> or <CODE>Element.ALIGN_RIGHT</CODE>. 235 * @return the text alignment 236 */ 237 public int getTextAlignment() { 238 return textAlignment; 239 } 240 241 /** Sets the text alignment. Can be <CODE>Element.ALIGN_LEFT</CODE>, 242 * <CODE>Element.ALIGN_CENTER</CODE> or <CODE>Element.ALIGN_RIGHT</CODE>. 243 * @param textAlignment the text alignment 244 */ 245 public void setTextAlignment(int textAlignment) { 246 this.textAlignment = textAlignment; 247 } 248 249 /** Gets the optional checksum generation. 250 * @return the optional checksum generation 251 */ 252 public boolean isGenerateChecksum() { 253 return generateChecksum; 254 } 255 256 /** Setter for property generateChecksum. 257 * @param generateChecksum New value of property generateChecksum. 258 */ 259 public void setGenerateChecksum(boolean generateChecksum) { 260 this.generateChecksum = generateChecksum; 261 } 262 263 /** Gets the property to show the generated checksum in the the text. 264 * @return value of property checksumText 265 */ 266 public boolean isChecksumText() { 267 return checksumText; 268 } 269 270 /** Sets the property to show the generated checksum in the the text. 271 * @param checksumText new value of property checksumText 272 */ 273 public void setChecksumText(boolean checksumText) { 274 this.checksumText = checksumText; 275 } 276 277 /** Sets the property to show the start and stop character '*' in the text for 278 * the barcode 39. 279 * @return value of property startStopText 280 */ 281 public boolean isStartStopText() { 282 return startStopText; 283 } 284 285 /** Gets the property to show the start and stop character '*' in the text for 286 * the barcode 39. 287 * @param startStopText new value of property startStopText 288 */ 289 public void setStartStopText(boolean startStopText) { 290 this.startStopText = startStopText; 291 } 292 293 /** Gets the property to generate extended barcode 39. 294 * @return value of property extended. 295 */ 296 public boolean isExtended() { 297 return extended; 298 } 299 300 /** Sets the property to generate extended barcode 39. 301 * @param extended new value of property extended 302 */ 303 public void setExtended(boolean extended) { 304 this.extended = extended; 305 } 306 307 /** Gets the code to generate. 308 * @return the code to generate 309 */ 310 public String getCode() { 311 return code; 312 } 313 314 /** Sets the code to generate. 315 * @param code the code to generate 316 */ 317 public void setCode(String code) { 318 this.code = code; 319 } 320 321 /** Gets the property to show the guard bars for barcode EAN. 322 * @return value of property guardBars 323 */ 324 public boolean isGuardBars() { 325 return guardBars; 326 } 327 328 /** Sets the property to show the guard bars for barcode EAN. 329 * @param guardBars new value of property guardBars 330 */ 331 public void setGuardBars(boolean guardBars) { 332 this.guardBars = guardBars; 333 } 334 335 /** Gets the code type. 336 * @return the code type 337 */ 338 public int getCodeType() { 339 return codeType; 340 } 341 342 /** Sets the code type. 343 * @param codeType the code type 344 */ 345 public void setCodeType(int codeType) { 346 this.codeType = codeType; 347 } 348 349 /** Gets the maximum area that the barcode and the text, if 350 * any, will occupy. The lower left corner is always (0, 0). 351 * @return the size the barcode occupies. 352 */ 353 public abstract Rectangle getBarcodeSize(); 354 355 /** Places the barcode in a <CODE>PdfContentByte</CODE>. The 356 * barcode is always placed at coordinates (0, 0). Use the 357 * translation matrix to move it elsewhere.<p> 358 * The bars and text are written in the following colors:<p> 359 * <P><TABLE BORDER=1> 360 * <TR> 361 * <TH><P><CODE>barColor</CODE></TH> 362 * <TH><P><CODE>textColor</CODE></TH> 363 * <TH><P>Result</TH> 364 * </TR> 365 * <TR> 366 * <TD><P><CODE>null</CODE></TD> 367 * <TD><P><CODE>null</CODE></TD> 368 * <TD><P>bars and text painted with current fill color</TD> 369 * </TR> 370 * <TR> 371 * <TD><P><CODE>barColor</CODE></TD> 372 * <TD><P><CODE>null</CODE></TD> 373 * <TD><P>bars and text painted with <CODE>barColor</CODE></TD> 374 * </TR> 375 * <TR> 376 * <TD><P><CODE>null</CODE></TD> 377 * <TD><P><CODE>textColor</CODE></TD> 378 * <TD><P>bars painted with current color<br>text painted with <CODE>textColor</CODE></TD> 379 * </TR> 380 * <TR> 381 * <TD><P><CODE>barColor</CODE></TD> 382 * <TD><P><CODE>textColor</CODE></TD> 383 * <TD><P>bars painted with <CODE>barColor</CODE><br>text painted with <CODE>textColor</CODE></TD> 384 * </TR> 385 * </TABLE> 386 * @param cb the <CODE>PdfContentByte</CODE> where the barcode will be placed 387 * @param barColor the color of the bars. It can be <CODE>null</CODE> 388 * @param textColor the color of the text. It can be <CODE>null</CODE> 389 * @return the dimensions the barcode occupies 390 */ 391 public abstract Rectangle placeBarcode(PdfContentByte cb, BaseColor barColor, BaseColor textColor); 392 393 /** Creates a template with the barcode. 394 * @param cb the <CODE>PdfContentByte</CODE> to create the template. It 395 * serves no other use 396 * @param barColor the color of the bars. It can be <CODE>null</CODE> 397 * @param textColor the color of the text. It can be <CODE>null</CODE> 398 * @return the template 399 * @see #placeBarcode(PdfContentByte cb, BaseColor barColor, BaseColor textColor) 400 */ 401 public PdfTemplate createTemplateWithBarcode(PdfContentByte cb, BaseColor barColor, BaseColor textColor) { 402 PdfTemplate tp = cb.createTemplate(0, 0); 403 Rectangle rect = placeBarcode(tp, barColor, textColor); 404 tp.setBoundingBox(rect); 405 return tp; 406 } 407 408 /** Creates an <CODE>Image</CODE> with the barcode. 409 * @param cb the <CODE>PdfContentByte</CODE> to create the <CODE>Image</CODE>. It 410 * serves no other use 411 * @param barColor the color of the bars. It can be <CODE>null</CODE> 412 * @param textColor the color of the text. It can be <CODE>null</CODE> 413 * @return the <CODE>Image</CODE> 414 * @see #placeBarcode(PdfContentByte cb, BaseColor barColor, BaseColor textColor) 415 */ 416 public Image createImageWithBarcode(PdfContentByte cb, BaseColor barColor, BaseColor textColor) { 417 try { 418 return Image.getInstance(createTemplateWithBarcode(cb, barColor, textColor)); 419 } 420 catch (Exception e) { 421 throw new ExceptionConverter(e); 422 } 423 } 424 425 /** Creates a <CODE>java.awt.Image</CODE>. This image only 426 * contains the bars without any text. 427 * @param foreground the color of the bars 428 * @param background the color of the background 429 * @return the image 430 */ 431 public abstract java.awt.Image createAwtImage(java.awt.Color foreground, java.awt.Color background); 432 433 /** Gets the amount of ink spreading. 434 * @return the ink spreading 435 * 436 */ 437 public float getInkSpreading() { 438 return this.inkSpreading; 439 } 440 441 /** Sets the amount of ink spreading. This value will be subtracted 442 * to the width of each bar. The actual value will depend on the ink 443 * and the printing medium. 444 * @param inkSpreading the ink spreading 445 * 446 */ 447 public void setInkSpreading(float inkSpreading) { 448 this.inkSpreading = inkSpreading; 449 } 450 451 /** 452 * The alternate text to be used, if present. 453 */ 454 protected String altText; 455 456 /** 457 * Gets the alternate text. 458 * @return the alternate text 459 */ 460 public String getAltText() { 461 return this.altText; 462 } 463 464 /** 465 * Sets the alternate text. If present, this text will be used instead of the 466 * text derived from the supplied code. 467 * @param altText the alternate text 468 */ 469 public void setAltText(String altText) { 470 this.altText = altText; 471 } 472 473}