Struggling to convert RTF format into RVE

General TRichView support forum. Please post your questions here
Post Reply
BennieC
Posts: 28
Joined: Sat Jun 18, 2011 7:00 pm

Struggling to convert RTF format into RVE

Post by BennieC »

Hi
I have a lot of data in a db, stored in rtf memos. I wish to convert this to rve so that i can use a RV Editor in future. However I battle to do this conversion. The old data is stored in a Paradox Table

Code: Select all

              with GenSQLQry do 
              begin
                SQL.Clear;
                SQL.Add('UPDATE MEMOS SET MEMODATA = :MEMO' +
                  ' WHERE SUBSPECIES_ID = ' + '''' + inttostr(SubSpeciesID) + '''' +
                  ' AND MEMOTYPE = ' + '''' + MemoType + '''');

// I need to convert it to RVE here
                RTStream := TMemoryStream.Create;
                aRVEObject := TRichViewEdit.Create(AOwner);
                TBlobField(imgtable.FieldByName('Memo' + inttostr(i))).SavetoStream(RTStream);
                RTStream.Position := 0;
                aRVEObject.LoadRTFFromStream(RTStream);
                aRVEObject.Format;

                RTStream.Position := 0;
                aRVEObject.SaveRVFToStream(RTStream,false);

                Params[0].LoadFromStream(RTStream,ftBlob);

                RTStream.Free;
                aRVEObject.Free;
                try
                  GenSQLQry.ExecSQL;
                except
                  on E: Exception do
                    MessageDlg('DataInsert Error (SubSpecies - Memos)',
                      mtWarning, [mbOK], 0);
                end;
              end;
The above works but it seems to leave the data in RTF format as I can only read it with a

Code: Select all

      if aRVComponent.LoadRTF('aRVEMemo_' + inttostr(i) + '.rvf') then
        aRVComponent.Format;
command. LoadRVF simply returns a blank screen. This would have been OK but after editing the data in the RV Editor it now seems to be in RV format as the LoadRTF now doesn't work but LoadRVF does.

I need to fix the original conversion but cannot get it right. Some assistance would be useful, please
Sergey Tkachenko
Site Admin
Posts: 17572
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

I can see several problems:
- not assigning TRVStyle;
- not changing initial values of some properties (necessary for the proper RTF reading and RVF saving)
- not clearing RTStream before saving to it.
And one more: Paradox MEMO field type is not appropriate for storing RVF; if you use a MEMO field, it's better to copy the content to a new BINARY field (and then delete MEMO field).

But I still cannot explain why data are still loaded as RTF. I have a very little experience with SQL, so I created an example using TTable:

Code: Select all

var rv: TRichView;
    RTStream: TMemoryStream;
begin
  rv := TRichView.Create(nil);
  rv.Style := TRVStyle.Create(rv);
  rv.RTFReadProperties.TextStyleMode := rvrsAddIfNeeded;
  rv.RTFReadProperties.ParaStyleMode := rvrsAddIfNeeded;
  rv.RVFOptions := rv.RVFOptions + [rvfoSaveTextStyles, rvfoSaveParaStyles];

  RTStream := TMemoryStream.Create;

  Table1.First;
  while not Table1.Eof do begin
    RTStream.Clear;
    TBlobField(Table1.FieldByName('Memo')).SaveToStream(RTStream);
    rv.Clear;
    rv.LoadRTFFromStream(RTStream);
    rv.DeleteUnusedStyles();
    RTStream.Clear;
    rv.SaveRVFToStream(RTStream, False);
    Table1.Edit;
    TBlobField(Table1.FieldByName('Memo')).LoadFromStream(RTStream);
    Table1.Post;
    Table1.Next;
  end;

  RTStream.Free;
  rv.Free;
end;
If you have hyperlinks in RTF, you also need to process rv.OnReadHyperlink.
BennieC
Posts: 28
Joined: Sat Jun 18, 2011 7:00 pm

Conversion

Post by BennieC »

Hi,
I have tried the suggestions (as shown)

Code: Select all

                SQL.Clear;
                SQL.Add('UPDATE MEMOS SET MEMODATA = :MEMO' +
                  ' WHERE SUBSPECIES_ID = ' + '''' + inttostr(SubSpeciesID) + '''' +
                  ' AND MEMOTYPE = ' + '''' + MemoType + '''');

// I need to convert it to RVE here
                aRVEObject := TRichView.Create(AOwner);
                aRVEObject.Style := TRVStyle.create(aRVEObject);
                aRVEObject.RTFReadProperties.TextStyleMode := rvrsAddIfNeeded;
                aRVEObject.RTFReadProperties.ParaStyleMode := rvrsAddIfNeeded;
                aRVEObject.RVFOptions := aRVEObject.RVFOptions +
                                       [rvfoSaveTextStyles, rvfoSaveParaStyles];

                RTStream := TMemoryStream.Create;
                RTStream.Clear;
                TBlobField(imgtable.FieldByName('Memo' + inttostr(i))).SavetoStream(RTStream);
                aRVEObject.Clear;
                aRVEObject.LoadRTFFromStream(RTStream);
                begin
                  aRVEObject.DeleteUnusedStyles(True, True, True);

//                  RTStream.Clear;
                  aRVEObject.SaveRVFToStream(RTStream,false);
                  Params[0].LoadFromStream(RTStream,ftBlob);

                  try
                    GenSQLQry.ExecSQL;
                  except
                    on E: Exception do
                      MessageDlg('DataInsert Error (SubSpecies - Memos)',
                        mtWarning, [mbOK], 0);
                  end;
                end;

                RTStream.Free;
                aRVEObject.Free;
You will notice that my DeleteUnusedStyles required parameters.
Also the RTStream.clear caused the Params field to produce an error in the SQL statement (Variant error, according to google caused by an empty blobfield)

After all this I still have the same problem of the data being stored in RTF format and cannot read it with a LoadRVF command, but I can with LoadRTF

Regards
Bennie
Sergey Tkachenko
Site Admin
Posts: 17572
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Try assigning RTStream.Position := 0 before Params[0].LoadFromStream
BennieC
Posts: 28
Joined: Sat Jun 18, 2011 7:00 pm

Post by BennieC »

Hi, I finally managed to solve the problem although the solution does not explain the problem. I simply used a file instead of a stream to save the temporary data before passing it to the DB.
Since this is a once off operation while moving the data to its new DB the penalty is not severe.
Regards
Post Reply