eDocEngine VCL Professional Help » Symbol Reference » Namespaces of Helper Classes » gtCstDocEng Namespace » TgtCustomDocumentEngine Class » TgtCustomDocumentEngine.OnCalcVariables Event

Gnostice eDocEngine VCL Developer Guide
TgtCustomDocumentEngine.OnCalcVariables Event

Occurs when the engine encounters a placeholder variable in text that is meant to be rendered on a document.

Pascal
published property OnCalcVariables: TgtOnCalcVariablesEvent;
C++
__published: __property TgtOnCalcVariablesEvent OnCalcVariables;
Description

Methods that write text on a page support certain built-in and custom user-defined placeholders. Any placeholders that you put in text strings at design-time will be replaced by the engine with real values at run time. This event occurs each time a placeholder is replaced. To provide a value for the substitution, define a event handler for this event.

Placeholders will be substituted only if the engine's CalculateVariables property is set to true.

{
  This example illustrates autopagination and the use of
  built-in and custom placeholders.

  Drag a button component and a PDF engine component
  on a form. Double-click the button and use the
  following code for the click-event procedure.
}
procedure TForm3.Button1Click(Sender: TObject);
var
 i: Integer;
begin

  with gtPDFEngine1 do begin
    FileName := 'autopagination_placeholders_demo.pdf';
    Font.Size := 16;
    Font.Name := 'Times New Roman';
    TextFormatting.LineSpacing := 2;

    // Enable autopagination
    AutoPaginate := true;

    // Ensure all text placeholders are replaced at run-time
    Preferences.CalculateVariables := true;

    // Render pages after processing all instructions
    Preferences.ProcessAfterEachPage  // Required for generating values
         := false;                    // for built-in placeholders

    // Specify event handler for placeholder substitution events
    OnCalcVariables := gtPDFEngine1CalcVariablesEventHandler;

    BeginDoc;
    for i := 1 to 50 do begin
      // Render paragraph containing built-in and custom placeholders
      BeginPara;
      Textout('Hello, world! - on page #' +
              CphPageNo + ' of ' + CphTotalPages +
              ' - Random #<%my_random_number_placeholder%>');
      EndPara;
    end;
    EndDoc;
  end;
  Close;

end;

// Event handler that gets called whenever a placeholder is substituted.
procedure TForm3.gtPDFEngine1CalcVariablesEventHandler(
                      Sender: TgtCustomDocumentEngine;
                      Variable: String;
                      var Value: String);
begin
  // Provide a value for the custom placeholder
  if Variable = 'my_random_number_placeholder' then begin
    Value := FloatToStr(Random);
  end;
end;