Page 1 of 1

[Example] How to store HTML in database

Posted: Tue May 12, 2009 4:29 pm
by Sergey Tkachenko
TDBRichViewEdit can store data in the following formats: RVF, RTF, text, depending on the value of FieldFormat property.
If you store data in another format, such as HTML, you need to process OnSaveCustomFormat and OnLoadCustomFormat events.

Example 1: using TrvHtmlImporter

Code: Select all

procedure TForm1.DBRichViewEdit1SaveCustomFormat(Sender: TCustomRichView;
  Stream: TStream; var DoDefault: Boolean);
begin

  Sender.SaveHTMLToStreamEx(Stream, ExtractFilePath(Application.ExeName),
    'title', 'img', '', '', '', []);
  {
  or
  Sender.SaveHTMLToStream(Stream, ExtractFilePath(Application.ExeName),
    'title', 'img', []);
  }
  DoDefault := False;
end;

procedure TForm1.DBRichViewEdit1LoadCustomFormat(Sender: TCustomRichView;
  Stream: TStream; var DoDefault: Boolean);
var s: String;
begin
  SetLength(s, Stream.Size);
  Stream.ReadBuffer(PChar(s)^, Length(s));
  RvHtmlImporter1.LoadHtml(s);
  DoDefault := False;
end;
If your documents have images, this example stores them in the application directory (with autogenerated names, so there will be many unused images). You can process OnImportPicture and OnSaveImage2 events to store them in database instead.

Example 2: using TRvHtmlViewImporter:

All the same, except for

Code: Select all

procedure TForm1.DBRichViewEdit1LoadCustomFormat(Sender: TCustomRichView;
  Stream: TStream; var DoDefault: Boolean);
var s: String;
begin
  HTMLViewer1.LoadFromStream(Stream);
  RVHTMLViewImporter1.ImportHtmlViewer(HTMLViewer1, Sender as TCustomRichViewEdit);
  DoDefault := False;
end;

Re: [Example] How to store HTML in database

Posted: Tue Feb 21, 2023 12:50 pm
by Sergey Tkachenko
This example is obsolete.

Since TRichView v21, this is a built-in feature. It does not require additional HTML importer components. Just assign DBRichViewEdit.FieldFormat = rvdbHTML.