TAdvPDFLib Font size problems in created PDF file

When I create PDF files the font sizes do not seem to match. So for example if I use

var
TMS: TAdvPDFLib;
......

  TMS.Graphics.Font.Name:= 'Times New Roman';
  TMS.Graphics.Font.Size:= 9;

  TMS.Graphics.DrawText('Some 9 point text', PointF(100, 100));

I end up with 12 point Times New Roman text.

This seems such a simple and fundamental aspect that I did not expect to see a mismatch, but I do.

Thanks in anticipation.

Dave Martel

Decided to make a very simple demo of the problem.

Create a simple VCL app and add a button, a file save dialogue and a TAdvPDFLib component from the palette.

This is the form file

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 144
  ClientWidth = 356
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -12
  Font.Name = 'Segoe UI'
  Font.Style = []
  TextHeight = 15
  object Button1: TButton
    Left = 120
    Top = 96
    Width = 129
    Height = 25
    Caption = 'Create Demo PDF File'
    TabOrder = 0
    OnClick = Button1Click
  end
  object TMS: TAdvPDFLib
    Left = 96
    Top = 8
    Width = 26
    Height = 26
    Visible = True
  end
  object FileSaveDialog1: TFileSaveDialog
    FavoriteLinks = <>
    FileTypes = <>
    Options = []
    Left = 16
    Top = 8
  end
end

and this is the code

unit TMSFontDemo;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, AdvCustomComponent,
  AdvPDFLib;

type
  TForm1 = class(TForm)
    Button1: TButton;
    FileSaveDialog1: TFileSaveDialog;
    TMS: TAdvPDFLib;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses
  System.Types;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
  procedure SomeText(FontName: String; FontSize: Single; X, Y: Single; cl: TColor);
  begin
    TMS.Graphics.Font.Name:= FontName;
    TMS.Graphics.Font.Size:= FontSize;
    TMS.Graphics.Font.Color:= cl;
    var s: String := Format('Text in Font: %s; Size: %d', [FontName, round(FontSize)]);
    TMS.Graphics.DrawText(s, PointF(X, Y))
  end;
begin
  if FileSaveDialog1.Execute(self.Handle) then
  begin
    TMS.BeginDocument(FileSaveDialog1.FileName);
    try
      TMS.NewPage;
      SomeText('Times New Roman', 6, 50, 30, clFuchsia);
      SomeText('Times New Roman', 8, 50, 80, clFuchsia);
      SomeText('Times New Roman', 9, 50, 130, clFuchsia);
      SomeText('Times New Roman', 12, 50, 180, clFuchsia);
    finally
      TMS.EndDocument(true);
    end;
  end;
end;

end.

Run the application, click the button and pick a file name. It will create the PDF and should then open it in Acrobat PDF reader (if you have it, no idea what happens if not).

If you then copy the text from PDF reader and paste it in to MS word in 2 ways, firstly in "Keep Text only", and then "Keep Source formatting"

Now select each of the first 4 lines in word from the unformatted text, and change the font to "Times New Roman", and the appropriate size. This is what I see.

image

The top lines are formatted in Word, and the Fuchsia lines, direct from the PDF file.

The factor difference in size is 96/72 and I can see in the code where this is done. I just don't understand why, and there doesn't seem to be any obvious way to stop or control that, other than the very kludgy way of setting a font like 8 point like this...

TMS.Graphics.Font.Size:= (8 * 96 / 72);

which seems like defeat to me for now. I may have to do it, but would like to know why in case that also breaks at some point.

Maybe I should not be using TMS.Graphics.Font, but it looks like the right way...

Thanks

Hi,

a PDF is 72 DPI and a screen is 96 DPI by default. We scale the font so the font sizes matches what you see on the screen. If you don't want this, you can use Graphics.Font.SizeNoScale.

Pieter,

Thanks for the reply. This does indeed resolve it for me.

I guess I am a little surprised. It may be that screens are traditionally 96spi, but these days 120 dpi and 150 dpi are common. My understandring was that the unit "point" when referring to fonts was that it is always 1/72 of an inch. It should be up to the rendering system as to how to render that on a device. So to have the default scaling to screen at 96dpi and have to verride it seems a bit backwards to me.

Anyway whatever I will move on with this. In fact I have my own reporting wrapper which maitains the font knowledge and passes that in to the PDF engine as needed, so it will be a single line change.

Thanks again.

Dave Martel

1 Like