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

Article #17394: Printing to file.

 Question and Answer Database

FAQ2394D.txt - Printing to file.

Category   :Printing
Platform   :All
Product    :

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

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

  lpszOutput := nil;


 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;


var FileNameToPrintTo : Pointer;


  FileNameToPrintTo := nil;

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;

 const MAX_PATH = 144;

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

    FreeMem(lpPrnFile, MAX_PATH +1);

4/2/99 11:25:03 AM

Last Modified: 01-SEP-99