rvico TRichView Reference | TRichView

TCustomRichView.OnSaveItemToFile

Top  Previous  Next

Allows you to change how document items are saved in text, RTF, DocX or HTML file or stream.

type

  TRVSaveItemToFileEvent = procedure (Sender: TCustomRichView;

    const Path: String; RVData: TCustomRVData; ItemNo: Integer;

    SaveFormat: TRVSaveFormat; Unicode: Boolean; var OutStr:TRVRawByteString;

    var DoDefault: Boolean) of object;

(introduced  in v1.8)

This is a "low level" event allowing to change saving of items completely.

There are events offering a subset of functionality of this event:

OnHTMLSaveImage,

OnSaveImage2,

OnSaveComponentToFile.

These events are easier to use than this event.

 

Input parameters:

Path – path to output file;

RVData, ItemNo – item to save. RVData document containing item that is being saved; it can be Sender.RVData, table cell, or RVData of cell inplace-editor. ItemNo index of this item inside RVData.

SaveFormat identifies file format, one of rvsfText, rvsfHTML, rvsfRTF, rvsfDocX.

UnicodeTrue when saving Unicode text files, or UTF-8 HTML files, or DocX.

OutStr:

for non-text items: empty string;

for text items: text that needs to be saved (it can be a part of the item's text when saving selected fragment). For Unicode text items, this is a "raw Unicode" string..

DoDefaultTrue.

 

Output parameters:

Set values of DoDefault and OutStr according to the table below. Mismatched values will cause corrupted files.

Unicode parameter

Output value of DoDefault

For ANSI text items

For Unicode text items

For non-text items

Any

True

For text files: default saving, value of OutStr is ignored.

For HTML, RTF, DocX: value of OutStr must be set to ANSI string. This string will be processed afterwards (for example, '<' will be  changed to '&lt;' for HTML).

For text files: default saving, value of OutStr is ignored.

For HTML, RTF, DocX: value of OutStr must be set to "raw Unicode" string. This string will be processed afterwards (for example, '<' will be  changed to '&lt;' for HTML).

Default saving, value of OutStr is ignored.

False

False

OutStr must be set to ANSI string, this string will be inserted in the file as it is (no control codes will be replaced).

True

False

OutStr must be set to "raw Unicode" string, this string will be inserted in the file without further processing (no control codes will be replaced).

If you do not want to change saving of some item, leave DoDefault to True.

You cannot override some special processing for paragraph markers when saving to HTML and RTF.

Notes about DocX:

this event is not called for tables

this event allows changing saving of numbering sequences only partially

 

Example 1: saving text items in UTF-8 encoding in HTML and text files.

In this example, we change default saving only if Unicode parameter is False (it is equal to False when saving ANSI text or HTML files).

uses RVUni, CRVData;

 

procedure TMyForm.MyRichViewSaveItemToFile(

  Sender: TCustomRichView; const Path: String;

  RVData: TCustomRVData; ItemNo: Integer;

  SaveFormat: TRVSaveFormat; Unicode: Boolean;

  var OutStr: TRVRawByteStringvar DoDefault: Boolean);

begin

  if (SaveFormat in [rvsfText, rvsfHTML]) and

     (RVData.GetItemStyle(ItemNo)>=0and

     not Unicode then

  begin

    if Sender.Style.TextStyles[RVData.GetItemStyle(ItemNo)].Unicode then

      OutStr := UTF8Encode(RVU_RawUnicodeToWideString(OutStr))

    else

      OutStr := AnsiToUtf8(OutStr);

    DoDefault := False;

  end;

end;

Note 1: For HTML files, there is an option for saving as UTF-8, so this code is given only as an example. This example is imperfect, because:

this code does not include replacement of <, >, &,  multiple spaces when saving to HTML; we cannot set DoDefault to True for HTML if the source item is Unicode, because the default RichView procedure expects "raw Unicode" in this case, but we output UTF-8;

non-text items are not converted to UTF-8 (so the file will be corrupted if their text representation contains non-English characters);

HTML encoding is not changed to UTF-8 (so the HTML will be correct only if no encoding is saved at all; it happens in RVStyle.TextStyles[0].Charset=DEFAULT_CHARSET).

Note 2: One more simplification: AnsiToUtf8 uses a system default code page for converting ANSI to Unicode (for better results, languages (Charsets) of text items must be  taken into account for the conversion).

Example 2: saving pictures as '<PIC>' (in text files, HTML and RTF)

procedure TMyForm.MyRichViewSaveItemToFile(

  Sender: TCustomRichView; const Path: String;

  RVData: TCustomRVData; ItemNo: Integer;

  SaveFormat: TRVSaveFormat; Unicode: Boolean;

  var OutStr: TRVRawByteStringvar DoDefault: Boolean);

begin

  if (RVData.GetItemStyle(ItemNo)=rvsPicturethen

  begin

    if SaveFormat=rvsfHTML then

      OutStr := '&lt;PIC&gt;'

    else

      OutStr := '<PIC>';

    if Unicode then

      OutStr := RVU_GetRawUnicode(OutStr);

    DoDefault := False;        

  end;

end;

Note: for non-text items, OutStr is saved as it is; so we need to replace '<' with '&lt;' and '>' with '&gt;' for HTML.


TRichView © trichview.com