Text Conversion

  • I am attempting to convert text in the background in my application using the following procedure. Is there a better or easier way to do this, also I am getting an error on the line RTFData.FormatOptions := [wpDisableAutosizeTables]; Undeclared identifier: 'wpDisableAutosizeTables'


    Using Delphi Rad Studio 10.2 and WPTools 8


    Thanks,


    Jim



    function ConvertFormatedText(Format: Integer; RawDataStream: TMemoryStream): String;

    var

    WPRichText : TWPCustomRtfEdit;

    RTFData : TWPRTFDataCollection;

    RTFDataProps: TWPRTFProps;

    AMessage : String;

    begin

    WriteHeapStatus('ConvertFormatedText Enter');

    RTFData := TWPRTFDataCollection.Create(TWPRTFDataBlock);

    RTFDataProps := TWPRTFProps.Create;

    WPRichText := TWPCustomRtfEdit.Create(nil);

    Result := '';

    WriteHeapStatus('ConvertFormatedText Creates');

    try

    try

    //RTFData.FormatOptions := [wpDisableAutosizeTables];

    RTFData.RTFProps := RTFDataProps;


    WPRichText.Memo.SetRTFDataOrProps(RTFData, nil);


    WPRichText.Clear;

    WPRichText.Visible := False;


    WPRichText.LoadFromStream(RawDataStream);

    WriteHeapStatus('ConvertFormatedText Loaded');


    AMessage := 'ConvertFormatedText - Raw Data Size: ' + IntToStr(RawDataStream.Size);


    if Format = 1 then AMessage := AMessage + ' Conversion: ASCII'

    else if Format = 2 then AMessage := AMessage + ' Conversion: RTF '

    else if Format = 3 then AMessage := AMessage + ' Conversion: HTML ';


    EClogging.WriteDebug(AMessage, 2);


    if Format = 1 then Result := WPRichText.AsANSIString('ANSI')

    else if Format = 2 then Result := WPRichText.AsANSIString('RTF')

    else if Format = 3 then Result := WPRichText.AsANSIString('HTML');


    WPRichText.Clear;

    WriteHeapStatus('ConvertFormatedText Cleared');


    Result := TRIM(Result);

    except

    on E: Exception do

    begin

    EClogging.WriteException(E.Message);

    end;

    end;

    finally

    if WPRichText <> nil then FreeAndNil(WPRichText);

    if RTFData <> nil then FreeAndNil(RTFData);

    if RTFDataProps <> nil then FreeAndNil(RTFDataProps);

    WriteHeapStatus('ConvertFormatedText Free');

    EClogging.WriteDebug('ConvertFormatedText - Objects Free', 2);

    end;

    end;

  • You need to add the unit

    WPRTEDefsConsts

    to uses clause.


    But your code is far too complicated.


    WPRichText := TWPCustomRtfEdit.CreatDynamic;

    try

    WPRichText.LoadFromStream(RawDataStream);

    if Format = 1 then Result := WPRichText.AsANSIString('ANSI')

    else if Format = 2 then Result := WPRichText.AsANSIString('RTF')

    else if Format = 3 then Result := WPRichText.AsANSIString('HTML');

    finally

    FreeAndNil(WPRichText);

    end;


    If that is supposed to be thread save you need to create a WPToolsEnvironment object, see manual.