Page 1 of 1

Struggling to convert RTF format into RVE

Posted: Tue Dec 27, 2011 6:40 am
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

Posted: Tue Dec 27, 2011 1:36 pm
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.

Conversion

Posted: Wed Dec 28, 2011 10:11 am
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

Posted: Wed Dec 28, 2011 2:17 pm
by Sergey Tkachenko
Try assigning RTStream.Position := 0 before Params[0].LoadFromStream

Posted: Thu Dec 29, 2011 12:16 pm
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