unit MainForm;

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

interface

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

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    btnRemoveLastPage: TButton;
    btnAppendPDF: TButton;
    lblStatus: TLabel;
    OpenDialog1: TOpenDialog;
    procedure FormCreate(Sender: TObject);
    procedure btnRemoveLastPageClick(Sender: TObject);
    procedure btnAppendPDFClick(Sender: TObject);
  private
    { Private declarations }
    procedure ShowStatus(const Msg: string);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  OpenDialog1.Filter := 'PDF Files (*.pdf)|*.pdf';
  ShowStatus('Ready');
end;

procedure TForm1.ShowStatus(const Msg: string);
begin
  lblStatus.Caption := Msg;
  Application.ProcessMessages;
end;

procedure TForm1.btnRemoveLastPageClick(Sender: TObject);
var
  PDFLib: TPDFLib;
  PageCount: Integer;
  SourceFileName, OutputFileName: string;
begin
  if not OpenDialog1.Execute then
    Exit;
    
  SourceFileName := OpenDialog1.FileName;
  OutputFileName := 'LastPageRemoved.pdf';
  
  ShowStatus('Processing...');
  
  PDFLib := TPDFLib.Create;
  try
    // Open source PDF file using LoadFromFile
    if PDFLib.LoadFromFile(SourceFileName, '') <> 1 then
    begin
      ShowStatus('Error: Cannot open source PDF file');
      Exit;
    end;
    
    // Get total page count
    PageCount := PDFLib.PageCount;
    
    if PageCount <= 1 then
    begin
      ShowStatus('Error: PDF file has only one page or no pages, cannot delete');
      Exit;
    end;
    
    // Delete last page
    if PDFLib.DeletePages(PageCount, 1) <> 1 then
    begin
      ShowStatus('Error: Cannot delete the last page');
      Exit;
    end;
    
    // Save the result
    if PDFLib.SaveToFile(OutputFileName) <> 1 then
    begin
      ShowStatus('Error: Cannot save file');
      Exit;
    end;
    
    ShowStatus('Success: Last page deleted and saved to LastPageRemoved.pdf');
  finally
    PDFLib.Free;
  end;
end;

procedure TForm1.btnAppendPDFClick(Sender: TObject);
var
  PDFLib: TPDFLib;
  TargetFileName, SourceFileName, OutputFileName: string;
begin
  // Select target PDF file (append to the end of this file)
  ShowStatus('Please select the first PDF file (target file)');
  OpenDialog1.Title := 'Select first (target) PDF File';
  if not OpenDialog1.Execute then
    Exit;

  TargetFileName := OpenDialog1.FileName;
  
  // Select source PDF file (file to be appended)
  ShowStatus('Please select the second PDF file (file to append)');
  OpenDialog1.Title := 'Select PDF File to Append';
  if not OpenDialog1.Execute then
    Exit;
    
  SourceFileName := OpenDialog1.FileName;

  // Save output file
  OutputFileName := '.\PageMerged.pdf';
  
  ShowStatus('Merging files...');
  
  PDFLib := TPDFLib.Create;
  try
    // Merge the files
    if PDFLib.MergeFiles(TargetFileName, SourceFileName, OutputFileName) <> 1 then
    begin
      ShowStatus('Error: Cannot merge the files');
      Exit;
    end;
    
    ShowStatus('Success: Files merged and saved to PageMerged.pdf');
  finally
    PDFLib.Free;
  end;
end;

end. 