Unit MainForm;

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

Interface

Uses
{$IFDEF XE2+}
  Winapi.Windows,
  System.Classes,
  Vcl.Controls,
  System.SysUtils,
  Vcl.StdCtrls,
  Vcl.Forms,
  Vcl.Dialogs,
{$ELSE}
  Windows,
  Classes,
  Controls,
  SysUtils,
  StdCtrls,
  Forms,
  Dialogs,
{$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
  i, PCount: Integer;
  CapPageID: Array Of Integer;
Begin
  With TPDFlib.Create Do
  Begin
    Try
      LoadFromFile('A3Sample.pdf', '');
      SetOrigin(1); // Set top left
      SetMeasurementUnits(1); // Set unit to mm
      SelectPage(1);
      ShowMessage('Old PageSize: ' + IntToStr(Trunc(PageWidth)) + ' * ' + IntToStr(Trunc(PageHeight)));
      PCount := PageCount;
      SetLength(CapPageID, PCount);
      For i := 1 To PCount Do
      Begin
        NewPage;
        CapPageID[i - 1] := CapturePage(1);
      End;

      If PageWidth > PageHeight Then
      Begin
        For i := 1 To PCount Do
        Begin
          SelectPage(i);
          SetPageDimensions(297, 210); // A4 landscape
          DrawCapturedPage(CapPageID[i - 1], 0, 0, 297, 210);
        End;
      End
      Else
      Begin
        For i := 1 To PCount Do
        Begin
          SelectPage(i);
          SetPageDimensions(210, 297); // A4 portrait
          DrawCapturedPage(CapPageID[i - 1], 0, 0, 210, 297);
        End;
      End;
      SelectPage(1);
      ShowMessage('New PageSize: ' + IntToStr(Trunc(PageWidth)) + ' * ' + IntToStr(Trunc(PageHeight)));
      SaveToFile('A4NewSize.pdf');
    Finally
      Free;
    End;
  End;
End;

End.
