eDocEngine VCL Professional Help » Symbol Reference » Namespaces of Helper Classes » gtCstDocEng Namespace » gtCstDocEng.TgtOnCalcVariablesEvent Type

Gnostice eDocEngine VCL Developer Guide
gtCstDocEng.TgtOnCalcVariablesEvent Type

This is the type of the OnCalcVariables event.

Pascal
TgtOnCalcVariablesEvent = procedure (Sender: TgtCustomDocumentEngine; Variable: String; var Value: String) of object;
C++
(Sender: TgtCustomDocumentEngine; Variable: String; var Value: String) ( TgtOnCalcVariablesEvent)();
Description

To provide run-time value for placeholders on a page, set a procedure of this type as the event handler.

Parameters
Parameters 
Description 
Sender 
Engine that caused the event. 
Variable 
User-defined custom placeholder. 
Value 
Value with which the placeholder needs to be replaced. 

gtCstDocEng

To illustrate the use of this event handler, try this sample mail-merge example that renders content for a post card including a bogus stamp.

  1. Open the IDE and create a VCL forms application project.
  2. From the tool palette, drop the following controls on the form.
    1. TDBGrid
    2. TDataSource
    3. TADODataSet
    4. TgtPDFEngine
    5. TButton
  3. Ensure that the NorthWind sample Access database is in the project directory. You may use any other database but you will have to make appropriate changes to the code.
  4. Set the DataSource property of the TDBGrid object is set to the TDataSource object.
  5. Set the DataSet property of the TDataSource object to the TADODataSet object.
  6. Set the following click-event handler for the button. In the textouts, the placeholders need to be delimited by <% and %>.
procedure TForm13.Button1Click(Sender: TObject);
var
  N: Integer;
  Bitmap1: TBitmap;
begin
  // Create an image
  Bitmap1 := TBitmap.Create;
  Bitmap1.LoadFromFile('UFO_Stamp.bmp');

  with gtPDFEngine1 do begin

    Preferences.ShowSetupDialog := False;
    Preferences.CalculateVariables := true;
    Preferences.ProcessAfterEachPage := true;

    Page.PaperSize := Custom;

    OnCalcVariables := gtPDFEngine1CalcVariablesEventHandler;
    FileName := 'edocengine_mail_merge.pdf';
    Font.Size := 18;
    Page.Width := 8;
    Page.Height := 4;

    BeginDoc;
    N := DBGrid1.DataSource.DataSet.RecordCount;

    for I := 1 to N do begin
      // Render address on post card
      DrawImage(5.6,0.2, Bitmap1);
      Font.Size := 18;
      Font.Color := clBlack;
      Font.Style := [];
      TextOut(1.5,1.4, 'To');
      TextOut(2,1.8, '<%Name%>');
      TextOut(2,2.2, '<%Address%>');
      TextOut(2,2.6, '<%Country%>');
      Font.Size := 9;
      Font.Style := [fsItalic];
      Font.Color := clRed;
      TextOut(0.2,3.3,'If undelivered, please return to:');
      TextOut(0.3,3.45,'Superdude');
      TextOut(0.3,3.6,'Crypton');

      // Render message on post card
      Font.Size := 20;
      Font.Color := clBlack;
      Font.Style := [];
      NewPage;
      TextOut(0.5,0.6, 'Dear Mr./Ms. <%Name%>,');
      TextOut(0.5,1.4, 'Last month, we a received a rare container shipment of');
      TextOut(0.5,1.8, 'cryptonite. As a loyal member of <%City%> Superdude ');
      TextOut(0.5,2.2, 'Fan Club, you are eligible for a vial of this space ');
      TextOut(0.5,2.6, 'junk engraved with your name "<%Name%>."');
      TextOut(0.5,3,   'Please find it enclosed with this card. Thank you.');
      if I <> N then begin
        NewPage;
      end;
    end;
    EndDoc;
    Close;
  end;
end;
  1. Set the following OnCalcVariables event handler for the PDF engine.
// OnCalcVariables eventhandler that supplies values
// for placeholders
procedure TForm13.gtPDFEngine1CalcVariablesEventHandler(
             Sender: TgtCustomDocumentEngine;
             Variable: String;
             var Value: String);
begin
  // Move the DB cursor
  DBGrid1.DataSource.DataSet.RecNo := I;

  // Set values for the placeholders
  if Variable = 'Name' then begin
    Value := DBGrid1.DataSource.DataSet.FieldByName('CONTACTNAME').Value;
  end;
  if Variable = 'Address' then begin
    Value := Trim(DBGrid1.DataSource.DataSet.FieldByName('ADDRESS').Value) +
             ', ' +
             DBGrid1.DataSource.DataSet.FieldByName('CITY').Value;
  end;
  if Variable = 'City' then begin
    Value := Trim(DBGrid1.DataSource.DataSet.FieldByName('CITY').Value);
  end;

  if Variable = 'Country' then begin
    Value := DBGrid1.DataSource.DataSet.FieldByName('COUNTRY').Value;
  end;
end;
  1. Set the dataset in the form-creation event. This allows you to keep the template intact but easily change the dataset when required.
procedure TForm13.FormCreate(Sender: TObject);
begin
ADODataSet1.ConnectionString :=
  'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' +
  GetCurrentDir +
  'FPNWIND.MDB;Persist Security Info=False';
ADODataSet1.CommandText :=
  'SELECT CONTACTTITLE, CONTACTNAME, ADDRESS, CITY, COUNTRY, POSTALCODE ' +
  'FROM CUSTOMERS';
ADODataSet1.Active := true;
end;