eDocEngine VCL Professional Help » Symbol Reference » Namespaces of Helper Classes » gtCstDocEng Namespace » gtCstDocEng.TgtOnCalcVariablesEvent Type
Gnostice eDocEngine VCL Developer Guide
|
This is the type of the OnCalcVariables event.
TgtOnCalcVariablesEvent = procedure (Sender: TgtCustomDocumentEngine; Variable: String; var Value: String) of object;
(Sender: TgtCustomDocumentEngine; Variable: String; var Value: String) ( TgtOnCalcVariablesEvent)();
To provide run-time value for placeholders on a page, set a procedure of this type as the event handler.
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.
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;
// 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;
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;