JIT RichText

TECH TIP: Using Delphi’s TRichEd with the JITPipeline

The following function can be used to copy rtf data from a Delphi TRichEd to a Delphi String. Once you have the rtf data in a string you can return the value in the JITPipeline.OnGetFieldAsString and JITPipeline.OnGetFieldValue events.

Note: you can define the JITPipeline’s Field.DataType as dtBlob, dtMemo, or dtString. It really does not matter in this case, because the TppDBRichText component simply calls DataPipeline.GetFieldAsString which for the JITPipeline fires the OnGetFieldAsString event.

function GetRichEdAsString(aRichEd: TRichEdit): String;
var
  lBuf: PChar;
  lRTFStream: TMemoryStream;

begin
  
  {create temp memory stream}  
  lRTFStream := TMemoryStream.Create;

  {save the TRichEd's rtf data to the memory stream}
  aRichEd.Lines.SaveToStream(lRTFStream);

  {allocate memory for the pchar buffer ( size of stream + 1 for the null terminator}
  lBuf := StrAlloc(lRTFStream.Size + 1);
  
  {copy the rtf stream to the buffer}
  lRTFStream.Position := 0;
  lRTFStream.Read(lBuf^, lRTFStream.Size);

  {add a null terminator}
  lBuf[lRTFStream.Size] := #0;

  {copy to a delphi string}
  Result := String(lBuf);


  {free the buffer memory}
  StrDispose(lBuf);

  {free the memory stream}
  lRTFStream.Free;

end;