Please use the Print method of the barcode component to print the barcode symbol to paper. The TImage control isn't required.
var
...
TextDefine: TBarcodeTextDefine;
begin
...
Printer.BeginDoc;
... { Print other content }
Font1 := TFont.Create;
try
Font1.Name := 'Comic Sans MS';
Font1.Size := 9;
TextDefine.DisplayText := dtBarcode;
TextDefine.TextPosition := tpBottomOut;
TextDefine.TextAlignment := taJustify;
TextDefine.TextFont := Font1;
TextDefine.ExtraFontSize := 9;
Barcode1D_Code391.Print(20, 20, '1234567890', True, clBlack, clWhite, TextDefine, 2, 0.3);
finally
Font1.Free;
end;
... { Print other content }
Printer.EndDoc;
...
end;
or
var
...
TextDefine: TBarcodeTextDefine;
begin
...
Printer.BeginDoc;
... { Print other content }
with Barcode1D_Code391 do
begin
Ratio := 2;
AutoCheckDigit := True;
BarColor := clBlack;
SpaceColor := clWhite;
DisplayText := dtBarcode;
TextPosition := tpBottomOut;
TextAlignment := taJustify;
TextFont.Name := 'Comic Sans MS';
TextFont.Size := 9;
Barcode := '1234567890';
Print(20, 20, 0.3);
end;
... { Print other content }
Printer.EndDoc;
...
end;
The method uses GetDeviceCaps function to retrieves device-specific information for the specified device. It will fail if a default printer is not set in Windows. Please ensure that a default printer is specified in Windows:
Set default printer for current application:
uses Winapi.Printers;
procedure SetAppPrinter(const PrinterName: string);
var
Idx: Integer;
begin
Idx := Printer.Printers.IndexOf(PrinterName);
if Idx <> -1 then
Printer.PrinterIndex := Idx
else
ShowMessage('Printer not found!');
end;
Set the global default printer.
uses Winapi.WinSpool;
procedure SetWindowsDefaultPrinter(const PrinterName: string);
begin
if SetDefaultPrinter(PChar(PrinterName)) then
SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 0)
else
RaiseLastOSError;
end;