Search code examples
windowsdelphiprintingdelphi-7

Font Size Difference between Printer Canvas and Form Canvas


I use

Printer.BeginDoc;
Printer.Canvas.Font := Self.Font;
Printer.Canvas.Font.Name := 'Verdana';
Printer.Canvas.Font.Size := 10;
Printer.Canvas.TextOut(10,0,'Nom : Prenom Nom');
Printer.Canvas.TextOut(10,100,'Nom2 : Prenom2 Nom2');
Printer.EndDoc;

Now I wanted to make some kind of preview so decided to make a preview form. But, I have to set the font size a lot bigger.

Form2.Canvas.Font := Self.Font;
Form2.Canvas.Font.Name := 'Verdana';
Form2.Canvas.Font.Size := 25;  //<=======Have to set this a lot higher to resemble what is on the print.
Form2.Canvas.TextOut(10,0,'Nom : Prenom Nom');
Form2.Canvas.TextOut(10,100,'Nom2 : Prenom2 Nom2');

How come the font size doesn't behave the same on the printer.canvas and form.canvas?


Solution

  • this occur because depends of printer DPI, for example

    the Printer.Canvas.Font.Size := 8 works on a printer with 300 DPI a printer with 600 DPI needs .Size := 4 for the same result.

    try this before Printer.beginDoc

    Printer.Canvas.Font.PixelsPerInch := GetDeviceCaps(Printer.Handle,LOGPIXELSY);
    

    or try this function always you need to change font size:

    procedure TForm1.SetPixelsPerInch;
    var
     FontSize: integer;
    begin
      FontSize := Printer.Canvas.Font.Size;
      Printer.Canvas.Font.PixelsPerInch := GetDeviceCaps(Printer.Handle,LOGPIXELSY);
      Printer.Canvas.Font.Size := FontSize;
    end;
    

    See more details in http://www.delphigroups.info/2/e7/314459.html