Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can Send to Print a Test Page to a Printer using delphi?

Actually i'm working in a project which need print a test page to a particular printer. the test page must be the same which of prints Windows in the option printer properties -> print test page.

How i can do this in delphi?

like image 660
Salvador Avatar asked Dec 02 '22 03:12

Salvador


2 Answers

This code will print the test page for the default printer:

uses ShellAPI, printers;
{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
var
  Device, Driver, Port: Array [0..255] of Char;
  hDevMode: THandle;
begin
  Printer.GetPrinter(Device, Driver, Port, hDevmode);
  ShInvokePrinterCommand(handle, PRINTACTION_TESTPAGE, Device, nil, true );
end;

Result: Printer test page

Enjoy :)

like image 83
jachguate Avatar answered Dec 04 '22 22:12

jachguate


You can use the PrintTestPage method the from the Win32_Printer wmi class

check this sample

{$APPTYPE CONSOLE}

uses
  SysUtils,
  ActiveX,
  ComObj;

procedure  PrintTestPage(const PrinterName:string);
var
  FSWbemLocator : OLEVariant;
  FWMIService   : OLEVariant;
  FWbemObjectSet: OLEVariant;
  FWbemObject   : OLEVariant;
  oEnum         : IEnumvariant;
  iValue        : LongWord;
  PrintResult   : Integer;
begin;
  FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  FWMIService   := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', '');
  FWbemObjectSet:= FWMIService.ExecQuery(Format('SELECT * FROM Win32_Printer Where Name="%s"',[PrinterName]),'WQL',0);
  oEnum         := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
  if oEnum.Next(1, FWbemObject, iValue) = 0 then
  begin
    PrintResult:=FWbemObject.PrintTestPage;
    if PrintResult=0 then
     Writeln('Success')
    else
     Writeln('An error occurred');
  end;
end;


begin
 try
    CoInitialize(nil);
    try
      PrintTestPage('MyPrinter');
    finally
      CoUninitialize;
    end;
 except
    on E:Exception do
        Writeln(E.Classname, ':', E.Message);
 end;

 Readln;
end.
like image 26
RRUZ Avatar answered Dec 04 '22 23:12

RRUZ