WPCubed Product Support

CONCATENATE CONTENT FROM STREAMS

Marcelo

I have a field in a database table that holds the contents of a WPRichText component with tags as Stream

I would like to concatenate the streams of a select on that field:

"Select TOP 5 STREAM_TEXT From Table"

And display these five contents on a WPRichText

Stream := TMemoryStream.Create;

TempStream := TMemoryStream.Create;

WPRichTextResult.ReadOnly := False;

WPRichTextResult.Clear;

WPRichTextResult.AsString := '';

while not dmSGA.QBusca.Eof do

begin

TBlobField(dmSGA.QBusca.FieldByName('STREAM_TEXT')).SaveToStream(TempStream);

Stream.CopyFrom(TempStream, 0);

Stream.seek(0, soEnd);

Stream.CopyFrom(TempStream, 0);

WPRichTextResult.LoadFromStream(Stream); // load stream contents into rich

dmSGA.QBusca.Next;

end;

Stream.Free;

TempStream.Free;

#1 ♥ 0
9.4k Views 2 users read 3 min
wpsupport

RTF Format does not allow concatenation but you can use LoadFromStream to append text:

TempStream := TMemoryStream.Create;
first := true;
while not dmSGA.QBusca.Eof do
begin
    TBlobField(dmSGA.QBusca.FieldByName('STREAM_TEXT')).SaveToStream(TempStream);
    TempStream.Position := 0;
    WPRichTextResult.LoadFromStream(TempStream,'AUTO',first); // appends 
    dmSGA.QBusca.Next;
   first := false;
end;
TempStream.Free;

The boolean variable first makes sure the first text is loaded completely, including page format and other elements. The other texts are just appended.

#2 ♥ 0
Marcelo

Than you very much, but it does not works, unfortunately.

I took th decision to transform STREAMS to Strings and get tet with formating tags. After that concatenate the strings appending one after other.

Wich methods is the appropriated to display that text?

#3 ♥ 0
wpsupport

Please check if you set the Stream.Position := 0 before you load from the stream.

Usually this is required.

In my code there is a small error:

instead of

WPRichTextResult.LoadFromStream(Stream,'AUTO',first);

it must be

WPRichTextResult.LoadFromStream(TempStream,'AUTO',first);

#4 ♥ 0
Marcelo

Hallo There , back to this situation again after a while, so thanks for answers.

Ok such method don't worked to me. So as I need to get the text of the stream I wasted some time working on transform the stream to String function that works pretty well!

Now that I got the String from stream how to Append each RTF formated String on WPRichText ? I tried Fast Add but it treats only pure text

prp.Text := StreamToString(Stream);

WPLaudoLabDetalhe.FastAddText(prp);

function StreamToString(aStream: TStream): string;
var
   SS: TStringStream;
begin
     if aStream <> nil then
    begin
         SS := TStringStream.Create('');
         try   
               SS.CopyFrom(aStream, 0); 
               Result := SS.DataString;
         finally
               SS.Free;
          end
    end 
    else Result := '';
end;

the function above works pretty fine !

Now Calling the method:

first := true;
while not dmSGA.QBusca.Eof do
begin
       TBlobField(dmSGA.QBusca.FieldByName('LAUD_TEXTO')).SaveToStream(Stream);
       Stream.Position := 0;
        WPLaudoLabDetalhe.LoadFromString(StreamToString(Stream),'AUTO',first); 
        dmSGA.QBusca.Next;
        first := false;
end;

It just dont works !

Should be another method to threat RTF tags?

Thank you very much1

#5 ♥ 0
wpsupport

Did you check if you really have RTF in your streams? It may be so that you have RTF code which was converted to unicode in the blob fields. It should only contain ASCII text.

When you save the contents to a file you can check the data in a file viewer

#6 ♥ 0
Marcelo

Yes I did, RTF. Any way to Append such content?

#7 ♥ 0
wpsupport

If the RTF streams are alright, LoadFromStream will append a file to the text in memory.

LoadFromString is not required, it just calls LoadFromStream internally.

Thats my suggestion:

TempStream := TMemoryStream.Create;
first := true;
while not dmSGA.QBusca.Eof do
begin
    TempStream.Clear;
    TBlobField(dmSGA.QBusca.FieldByName('STREAM_TEXT')).SaveToStream(TempStream);
    TempStream.Position := 0;
    WPRichTextResult.LoadFromStream(TempStream,'AUTO',first); // appends 
    dmSGA.QBusca.Next;
    first := false;
end;
TempStream.Free;

Note the TempStream.Clear - it was missing the the code above.

#8 ♥ 0
This category is archived. No new posts are possible.