How to print a test page to a printer using delphi?

I'm actually working on a project that is supposed to print a test page on a specific printer. the test page should be the same as one of the Windows printers in the options of the optional printer β†’ print a test page.

How can i do this in delphi?

+4
source share
2 answers

This code prints a 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 :)

+13
source

You can use the PrintTestPage method from the Win32_Printer 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. 
+6
source

Source: https://habr.com/ru/post/1342358/


All Articles