Barcode images may be drawn on any graphics device, one of the most important of which is the printer. The drawBarcode() method causes the barcode to be draw on the graphics surface (g2 in the example below), positioned at the left and top coordinates (in points) specified by the 2nd and 3rd parameters. For printing the remaining parameters may be 0. In fact the 4th parameter specifies the required resolution (in dpi) of the image – but for a printer this information is generally provided thought the graphic surface properties and need not be specified in the method call.
public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException {
if (pageIndex >= 1) {
return Printable.NO_SUCH_PAGE;
}
Graphics2D g2=(Graphics2D)g;
barcode.drawBarcode(g2,72.0,120.0,0,0);
g2.setFont(new Font("Arial",Font.BOLD,12));
g2.setColor(java.awt.Color.BLACK);
g2.drawString("dBarcode for Java",72,100);
return Printable.PAGE_EXISTS;
}
More: