Alan,
The function below was written to strip RTF code; it works as a state
machine: the beginning of a tag is '{', and that character and all
including the closing '}' may be removed. Though not necessarily part
of RTF, this code also removes control characters (e.g. CRLF, ASCII
13 + ASCII 8); you might omit those lines.
This has been tested on only one type of well-formed RTF; it might
not work in all applications, so test it in context.
Bart
____
function StripRtf(RTF: String): String;
var
bsl: Boolean;
i, cntb: Integer;
begin
Result := RTF;
if not AnsiContainsText(RTF, 'rtf') then
Exit;
cntb :=
0;
bsl := False;
for i := 1 to Length(Result) do
begin // RTF tag characters shown as literals for clarity
case Result[i] of
'{': Inc(cntb);
'}': Dec(cntb);
'\': bsl := True;
' ': bsl := False;
end; // case
if (cntb > 1) or bsl then
Result[i] := SPACE;
end; // for
Delete(Result, 1, Pos('}', Result)); // delete initial RTF tag
for i := Length(Result) downto 1 do
if Ord(Result[i]) < 32 then // substitute Space for ctrl
characters
Result[i] := SPACE;
Result := Trim(Result);
end; // StripRtf
____
> Date: Sun, 14 Oct 2007 16:45:17 -0700
> From: Alan Colburn <[EMAIL PROTECTED]>
> Subject: Extracting Text from RTF-Containing Field
>
> Suppose you've got a dataset field storing rich text. Presumably
this
> would be a BLOB-type field, right?
>
> I'd like to create a method that would let me extract the info from
a
> record, including the above field, as a string. (I want to insert
the
> record's info into a plain text e-mail message.) Any idea how to do
this?
> If you just use the field;s .AsString property you end up with text
> containing the "hidden" RTF codes.
>
> As always, thanks -- Al
>
Fussy? Opinionated? Impossible to please? Perfect. Join Yahoo!'s user
panel and lay it on us.
Alan,
The function below was written to strip RTF code; it works as a state
machine: the beginning of a tag is '{', and that character and all
including the closing '}' may be removed. Though not necessarily part
of RTF, this code also removes control characters (e.g. CRLF, ASCII
13 + ASCII 8); you might omit those lines.
This has been tested on only one type of well-formed RTF; it might
not work in all applications, so test it in context.
Bart
____
function StripRtf(RTF: String): String;
var
bsl: Boolean;
i, cntb: Integer;
begin
Result := RTF;
if not AnsiContainsText(RTF, 'rtf') then
Exit;
cntb :=
0;
bsl := False;
for i := 1 to Length(Result) do
begin // RTF tag characters shown as literals for clarity
case Result[i] of
'{': Inc(cntb);
'}': Dec(cntb);
'\': bsl := True;
' ': bsl := False;
end; // case
if (cntb > 1) or bsl then
Result[i] := SPACE;
end; // for
Delete(Result, 1, Pos('}', Result)); // delete initial RTF tag
for i := Length(Result) downto 1 do
if Ord(Result[i]) < 32 then // substitute Space for ctrl
characters
Result[i] := SPACE;
Result := Trim(Result);
end; // StripRtf
____
> Date: Sun, 14 Oct 2007 16:45:17 -0700
> From: Alan Colburn <[EMAIL PROTECTED]>
> Subject: Extracting Text from RTF-Containing Field
>
> Suppose you've got a dataset field storing rich text. Presumably
this
> would be a BLOB-type field, right?
>
> I'd like to create a method that would let me extract the info from
a
> record, including the above field, as a string. (I want to insert
the
> record's info into a plain text e-mail message.) Any idea how to do
this?
> If you just use the field;s .AsString property you end up with text
> containing the "hidden" RTF codes.
>
> As always, thanks -- Al
>
Fussy? Opinionated? Impossible to please? Perfect. Join Yahoo!'s user
panel and lay it on us.
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
_______________________________________________
Delphi-DB mailing list
[email protected]
http://www.elists.org/mailman/listinfo/delphi-db