unit uCreatePdf;
{$I ..\..\..\Lib\PDFiumVcl.inc}

interface

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

const
  // Simple color constants
  clLightBlue= $FFCCAA;
  clLightGreen= $CCFFCC;

type
  TFormMain= class(TForm)
    Pdf: TPdf;
    Image: TImage;
    SaveDialog: TSaveDialog;

    // Main UI
    PanelTop: TPanel;
    PanelBottom: TPanel;

    // Content Controls
    LabelTitle: TLabel;
    EditTitle: TEdit;
    LabelAuthor: TLabel;
    EditAuthor: TEdit;

    // Options
    GroupOptions: TGroupBox;
    CheckWatermark: TCheckBox;

    // Actions
    ButtonCreate: TButton;
    ButtonOpen: TButton;

    // Status
    LabelStatus: TLabel;
    LinkLabel1: TLinkLabel;

    // Events
    procedure FormCreate(Sender: TObject);
    procedure ButtonCreateClick(Sender: TObject);
    procedure ButtonOpenClick(Sender: TObject);
    procedure LinkLabel1LinkClick(
      Sender    : TObject;
      const Link: string;
      LinkType  : TSysLinkType);

  private
    FLastCreatedFile: string;
    procedure CreateSimplePDF;
    procedure AddWatermark;
    procedure UpdateStatus(const Status: string);

  public
  end;

var
  FormMain: TFormMain;

implementation

uses
{$IFDEF XE2+}
  Winapi.ShellAPI;
{$ELSE}
  ShellAPI;
{$ENDIF}
{$R *.dfm}

procedure TFormMain.FormCreate(Sender: TObject);
begin
  EditTitle.Text:= 'Sample PDF Document';
  EditAuthor.Text:= 'PDFium VCL Demo';
  UpdateStatus('Ready to create PDF');
end;

procedure TFormMain.LinkLabel1LinkClick(
  Sender    : TObject;
  const Link: string;
  LinkType  : TSysLinkType);
begin
  // Open the link in default browser
  ShellExecute(Handle, 'open', PChar(Link), nil, nil, SW_SHOWNORMAL);
end;

procedure TFormMain.ButtonCreateClick(Sender: TObject);
begin
  try
    UpdateStatus('Creating PDF...');
    CreateSimplePDF;
    UpdateStatus('PDF created successfully: '+ FLastCreatedFile);
  except
    on E: Exception do
    begin
      UpdateStatus('Error: '+ E.Message);
      ShowMessage('Error creating PDF: '+ E.Message + sLineBreak + 'Copy the DLL to the system or program directory first.');
    end;
  end;
end;

procedure TFormMain.CreateSimplePDF;
begin
  // Create PDF document with compression enabled
  Pdf.CreateDocument;
  Pdf.Compressed:= True;
  Pdf.Active:= True;
  try
    // Add page
    Pdf.AddPage(0, 595, 842); // A4 size (page number, width, height)

    // Title
    Pdf.AddText(EditTitle.Text, 'Arial', 18, 50, 750, clBlack, $FF, 0.0);

    // Author
    Pdf.AddText('By: '+ EditAuthor.Text, 'Arial', 12, 50, 720, clGray, $FF, 0.0);

    // Czech
    Pdf.AddText('Czech: Příliš žluťoučký kůň úpěl ďábelské ódy', 'Arial', 12, 50, 680, clBlack, $FF, 0.0);

    // French
    Pdf.AddText('French: Le cœur déçu mais l''âme plutôt naïve rêva de crapaüter en canoë', 'Arial', 12, 50, 655, clBlack, $FF, 0.0);

    // German
    Pdf.AddText('German: Falsches Üben von Xylophonmusik quält jeden größeren Zwerg', 'Arial', 12, 50, 630, clBlack, $FF, 0.0);

    // Spanish
    Pdf.AddText('Spanish: El pingüino Wenceslao hizo kilómetros bajo exhaustiva lluvia y frío', 'Arial', 12, 50, 605, clBlack, $FF, 0.0);

    // Portuguese
    Pdf.AddText('Portuguese: À noite, vovô põe açúcar no café do pingüim queixoso', 'Arial', 12, 50, 580, clBlack, $FF, 0.0);

    // Polish
    Pdf.AddText('Polish: Pchnąć w tę łódź jeża lub ośm skrzyń fig', 'Arial', 12, 50, 555, clBlack, $FF, 0.0);

    // Turkish
    Pdf.AddText('Turkish: Pijamalı hasta yağız şoföre çabucak güvendi', 'Arial', 12, 50, 530, clBlack, $FF, 0.0);

    // Optional features
    if CheckWatermark.Checked
    then
      AddWatermark;

    // Save
    Pdf.Compressed:= True;
    FLastCreatedFile:= 'Sample.pdf';
    Pdf.SaveAs(FLastCreatedFile);

  finally
    Pdf.Active:= False;
  end;
end;


procedure TFormMain.AddWatermark;
begin
  Pdf.AddText('SAMPLE', 'Arial', 72, 200, 200, clGray, $30, 45.0);
end;

procedure TFormMain.ButtonOpenClick(Sender: TObject);
begin
  if FLastCreatedFile<> ''
  then
  begin
    if FileExists(FLastCreatedFile)
    then
      ShellExecute(0, 'OPEN', PChar(FLastCreatedFile), '', '', SW_SHOWNORMAL)
    else
      ShowMessage('File not found: '+ FLastCreatedFile);
  end
  else
    ShowMessage('No PDF file created yet. Click "Create PDF" first.');
end;

procedure TFormMain.UpdateStatus(const Status: string);
begin
  LabelStatus.Caption:= Status;
  Application.ProcessMessages;
end;

end.
