Unit MainForm;

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

Interface

Uses
{$IFDEF XE2+}
  Winapi.Windows,
  System.Classes,
  Vcl.Controls,
  Vcl.StdCtrls,
  Vcl.Forms,
{$ELSE}
  Windows,
  Classes,
  Controls,
  StdCtrls,
  Forms,
{$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
  tableID: Integer;
  s: Double;
  StrMessage, output, strPageSize: String;

Begin
  With TPDFlib.Create Do
    Try

      // PDF output filename
      output := 'CreateTable.pdf';

      // Display message containing the library version
      StrMessage := 'PDF library Create Table sample' + chr(10) + 'Library version: ' + LibraryVersion + chr(10);

      // Set the page size of the new document
      SetPageSize(strPageSize);

      // Create the table and specify the number of rows and columns
      tableID := CreateTable(5, 5);

      // Specify color and width of borders
      SetTableBorderColor(tableID, 0, 0.3, 0.204, 0);
      SetTableBorderWidth(tableID, 0, 0.2);

      // Specify table row height and table column width
      SetTableRowHeight(tableID, 1, 5, 0);
      SetTableColumnWidth(tableID, 1, 5, 100);

      // Specify a background color for the column headers
      SetTableCellBackgroundColor(tableID, 1, 1, 1, 5, 0.201, 0.100, 0.235);
      SetTableCellAlignment(tableID, 1, 1, 1, 5, 4);

      // Specify a font color for the column headers
      SetTableCellTextColor(tableID, 1, 1, 1, 5, 1.0, 1.0, 1.0);

      // Setup the column headers for the first row
      SetTableCellContent(tableID, 1, 1, '<b>1st</b>');
      SetTableCellContent(tableID, 1, 2, '<b>2nd</b>');
      SetTableCellContent(tableID, 1, 3, '<b>3rd</b>');
      SetTableCellContent(tableID, 1, 4, '<b>4th</b>');
      SetTableCellContent(tableID, 1, 5, '<b>5th</b>');

      // Insert the content for the second row
      SetTableCellContent(tableID, 2, 1, '1');
      SetTableCellContent(tableID, 2, 2, '2');
      SetTableCellContent(tableID, 2, 3, '3 <b> A ray of hope</b><br>Brighten up the day');
      SetTableCellContent(tableID, 2, 4, '<b>4</b>');
      SetTableCellContent(tableID, 2, 5, '<b>5</b>');

      // Insert the content for the third row
      SetTableCellContent(tableID, 3, 1, 'one');
      SetTableCellContent(tableID, 3, 2, 'two');
      SetTableCellContent(tableID, 3, 3, 'three');
      SetTableCellContent(tableID, 3, 4, 'four');
      SetTableCellContent(tableID, 3, 5, 'five');

      // Insert the content for the fourth row
      SetTableCellContent(tableID, 4, 1, 'ein');
      SetTableCellContent(tableID, 4, 2, 'zwei');
      SetTableCellContent(tableID, 4, 3, 'drei');
      SetTableCellContent(tableID, 4, 4, 'vier');
      SetTableCellContent(tableID, 4, 5, 'f¨¹nf');

      // Insert the content for the fifth row
      SetTableCellContent(tableID, 5, 1, 'un');
      SetTableCellContent(tableID, 5, 2, 'deux');
      SetTableCellContent(tableID, 5, 3, 'trois');
      SetTableCellContent(tableID, 5, 4, 'quatre');
      SetTableCellContent(tableID, 5, 5, 'cinq');

      // Draw the table onto the document
      s := DrawTableRows(tableID, 50, 780, 400, 1, 0);

      If (s = 0) Then
        MessageBox(Handle, PChar('Could not create table!'), 'Error:', MB_OK or MB_ICONSTOP);

      // Attempt to save the file
      If (SaveToFile(output) = 1) Then
      Begin
        // Display message that the file has been written successfully
        StrMessage := StrMessage + output + ' written successfully';
        MessageBox(Handle, PChar(StrMessage), 'Information:', MB_OK or MB_ICONINFORMATION);
      End
      Else
      Begin
        // Display cannot write to file message
        StrMessage := StrMessage + 'Error, file could not be written.';
        MessageBox(Handle, PChar(StrMessage), 'Error:', MB_OK or MB_ICONSTOP);
      End;
    Finally
      Free;
    End;

End;

End.
