Printing bar codes using Printer.Canvas

- Create a new form.

- Add a TButton control Button1 and a TEdit control Edit1.

- Double-click Button1 to view the event code.

procedure TForm1.Button1Click(Sender: TObject);
begin


end;

- Edit the code to look like the code below.

Replace Data with a string or variable, which is the data for the bar code. For example, '234739274893' will print a bar code containing 234739274893. Use PAnsiChar(Edit1.Text) to print a bar code containing data the user has entered in Edit1 text box.

Replace BarCodeType with the type of bar code you want. See Specifying a bar code type for more details. For example, to produce a Code 128 bar codes use C128-0.BA.

Replace FontName and FontSize with the bar code font name and szie you want to use. See Bar code font name and font size to use for more details. For example, to print Code 128 bar codes use font name CIA Code 128 Medium and font size 14.

Replace DisplayErrors with 1 to display any error messages in a message box. Use 0 to disable error messages.

{$I CIAXCOM.PAS}
procedure TForm1.Button1Click(Sender: TObject);
var Value: String;
begin

	Printer.BeginDoc;
	Printer.Canvas.Font.Name := 'FontName';
	Printer.Canvas.Font.Size := FontSize;

	Value := Barcode(Data, 'BarCodeType', DisplayErrors);

	Printer.Canvas.TextOut(100, 100, Value);
	Printer.EndDoc;

end;

Example: Here is an example, which prints a Code 128 bar code.

{$I CIAXCOM.PAS}
procedure TForm1.Button1Click(Sender: TObject);
var Value: String;
begin

	Printer.BeginDoc;
	Printer.Canvas.Font.Name := 'CIA Code 128 Medium';
	Printer.Canvas.Font.Size := 14;

	Value := Barcode(PAnsiChar(Edit1.Text), 'C128-0.BA', 1);

	Printer.Canvas.TextOut(100, 100, Value);
	Printer.EndDoc;

end;

Because the example uses the Printer object, you must add the Printers unit to the uses clause of your unit.

- At the top of your unit add Printers to the uses list.

uses
	Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
	StdCtrls, Printers;

- Choose menu Run | Run (F9) to run the project;

- Enter a number and click the button to print the bar code to the default printer.

You can easily extend this example to print the bar codes you require.


Copyright 2000 CIA (BAR CODES) UK. All rights reserved. Reproduction prohibited.