Главная страница | назад





Article #17394: Printing to file.

 Question and Answer Database

FAQ2394D.txt - Printing to file.

Category   :Printing
Platform   :All
Product    :

Question:
How do I print to a file without changing the printer's setup, or
getting a prompt for a file to print to?


Answer:
To do this you simply need to change the initialization of the
DocInfo structure in TPrinter from:

  lpszOutput := nil;

 to:

 lpszOutput := 'C:\SomePath\SomeFileName';

You can make a permanent change to TPrinter, but for the ultimate in
flexibility, you can create an interface unit to make the filename
easy to change from your application.

{  Note:  Modifications to the VCL are made at the developers risk and 
any effects caused by modifications are not the responsibility of 
Borland International. }
The safest way to make changes to the VCL is to create a new directory
called "patched", and copy the Printers.Pas unit to the new directory,
then make your changes (with comments) to the copied file. The you
must add the path to your patched directory to the beginning of your
library path, then restart Delphi/C++ Builder, and rebuild your
project after making the following changes. Note that the library path
setting can be found off the main menu of:

Delphi 1 : Options...  Environment... Library
Delphi 2 : Tools... Options... Library
Delphi 3 :  Tools... Environment Options... Library
C++ Builder : Options...  Environment... Library

Instructions for the patch:

1)  Create a new unit in the patched directory called "PrnPage.pas"
and include the following code in the unit:

unit PrnPage;

interface

var FileNameToPrintTo : Pointer;

implementation

begin
  FileNameToPrintTo := nil;
end.

2) Now open the Printers.Pas file in your patched directory.
Search for the line that reads "uses Consts;" (or in Delphi 16 it will
read "uses WinProcs, Consts;");

and add the PrnPage unit to the uses clause:

uses Consts, PrnPage;

3) Now search for line that reads "lpszOutput := nil;"

and change it to read: lpszOutput := FileNameToPrintTo;

4) Save your changes, and restart Delphi / C++ Builder.

5) To test your changes, try the following code. Note: Be sure to use
the "Build All" option when compiling your project for the first time.

uses Printers, PrnPage;

{$IFNDEF WIN32}
 const MAX_PATH = 144;
{$ENDIF}

procedure TForm1.Button1Click(Sender: TObject);
var
  lpPrnFile : pChar;
begin
  GetMem(lpPrnFile, MAX_PATH +1);
  
  try
    StrPCopy(lpPrnFile, 'C:\Download\TestIt.Prn');
    FileNameToPrintTo := @lpPrnFile^;
    
    with Printer do begin 
      BeginDoc;
      Canvas.TextOut(10, 10, 'Test');
      EndDoc;
    end;
   
    FileNameToPrintTo := nil;

  finally
    FreeMem(lpPrnFile, MAX_PATH +1);
  end;
end;

4/2/99 11:25:03 AM
 

Last Modified: 01-SEP-99