首页  编辑  

Print a textfile to the generic/text printer

Tags: /超级猛料/Print.打印/   Date Created:

Print a textfile to the generic/text printer

Uses WinSpool;

Const

 GenericPrinter: Pchar = 'Universal/Nur Text';

 // Change to systems generic drivers name

Procedure PrintFileToGeneric(Const sFileName: string; ejectPage: boolean );

Const

 BufSize = 16384;

Var

 Count : Integer;

 BytesWritten: DWORD;

 hPrinter: THandle;

 DocInfo: TDocInfo1;

 f: file;

 Buffer: array [1..Bufsize] of char;

 ch: Char;

Begin

 If not WinSpool.OpenPrinter(GenericPrinter, hPrinter, nil) Then

   raise exception.create('Printer not found');

 Try

   DocInfo.pDocName := 'MyDocument';

   DocInfo.pOutputFile := Nil;

   DocInfo.pDatatype := 'RAW';

   If StartDocPrinter(hPrinter, 1, @DocInfo) = 0 Then

     Abort;

   Try

     If not StartPagePrinter(hPrinter) Then

       Abort;

     System.Assign(f, sFileName);

     Try

       Reset(f, 1);

       Try

         While not eof(f) Do Begin

           Blockread(f, Buffer, BufSize, Count);

           If Count > 0 Then Begin

             If not WritePrinter(hPrinter, @Buffer, Count, BytesWritten) Then

               Abort;

           End;

         End;

       Finally

         If ejectPage Then Begin

           ch:= #12;

           WritePrinter( hPrinter, @ch, 1, BytesWritten );

         End;

       End;

     Finally

       EndPagePrinter(hPrinter);

       System.Closefile( f );

     End;

   Finally

     EndDocPrinter(hPrinter);

   End;

 Finally

   WinSpool.ClosePrinter(hPrinter);

 End;

End;

Procedure TForm1.Button1Click(Sender: TObject);

Begin

 printfiletogeneric( extractfilepath( paramstr(0))+'unit1.pas', True );

End;