Unit MainForm;

{$I ..\..\..\Lib\PDFlibDefines.inc}

Interface

Uses
{$IFDEF XE2+}
  Winapi.Windows,
  System.Classes,
  System.SysUtils,
  Vcl.Controls,
  Vcl.StdCtrls,
  Vcl.Forms,
  Vcl.Graphics,
{$ELSE}
  Windows,
  Classes,
  SysUtils,
  Controls,
  StdCtrls,
  Forms,
  Graphics,
{$ENDIF}
  PDFlibrary;

Type
  TForm1 = Class(TForm)
    Button1: TButton;
    Procedure Button1Click(Sender: TObject);
  Private
    { Private declarations }
  Public
    { Public declarations }
  End;

Var
  Form1: TForm1;

Implementation

{$R *.dfm}

Procedure TForm1.Button1Click(Sender: TObject);
Var
  PDF: TPDFlib;
  DPI, Page: Integer;
  PCanvas: TCanvas;
  PFont: TFont;
Begin
  PDF := TPDFlib.Create;
  PDF.SetOrigin(1); // set top left mode
  PCanvas := TCanvas.Create;

  PFont := TFont.Create;
  PFont.Name := 'Arial';
  PFont.Height := -10;
  PCanvas.Font := PFont;
  DPI := PCanvas.Font.PixelsPerInch; // Screen DPI, 96

  For Page := 1 To 2 Do
  Begin
    PCanvas.Handle := PDF.GetCanvasDC(595, 852);
    // CanvasWidth & CanvasHeight in points, 852 x 595 is A4

    Case Page Of
      1:
        Begin
          PCanvas.TextOut(100, 100, 'Hello world from TCanvas');
          // Creates a new document from the drawing operations applied to the DC returned by the GetCanvasDC function.
          PDF.LoadFromCanvasDC(DPI, 0);
          // Option 0, Process the drawing commands as vector graphics, fonts are not embedded
        End;
      2:
        Begin
          // Adds a new page to the selected document from the drawing operations applied to the DC returned by the GetCanvasDC.
          PCanvas.TextOut(100, 100, 'Page 2 from TCanvas');
          PDF.NewPageFromCanvasDC(DPI, 0);
          // Add a new page with the canvas text
        End;
    End;

  End;

  PDF.SaveToFile('CanvasText.pdf');
  PCanvas.Free;
  PDF.Free;
End;

End.
