Barcode Colors
iText is a free and open source library for creating
PDF documents and generating Barcodes in Java.
This requires the library iText-5.0.2.jar to be in classpath.
The following example shows generating CODE 3of9 Barcode with colors.
package com.bethecoder.tutorials.itext;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.Barcode39;
import com.itextpdf.text.pdf.PdfWriter;
public class BarCodeColorsTest {
/**
* @param args
* @throws DocumentException
* @throws FileNotFoundException
*/
public static void main ( String [] args ) throws FileNotFoundException, DocumentException {
Document document = new Document ( new Rectangle ( PageSize.A4 )) ;
PdfWriter writer = PdfWriter.getInstance ( document, new FileOutputStream ( "barcode39colors.pdf" )) ;
document.open () ;
//Barcode 39
document.add ( new Paragraph ( "Barcode 39 with Colors" )) ;
Barcode39 code39 = new Barcode39 () ;
code39.setGenerateChecksum ( true ) ;
code39.setCode ( "0123456789" ) ;
BaseColor barColor = BaseColor.BLUE;
BaseColor txtColor = BaseColor.ORANGE;
//Add Barcode to PDF document
document.add ( code39.createImageWithBarcode (
writer.getDirectContent () , barColor, txtColor )) ;
document.close () ;
}
}
It gives the following output,