|
TCustomRichView.OnSaveItemToFile |
Top Previous Next |
|
Allows you to change how document items are saved in text, RTF 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: 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. Unicode – True when saving Unicode text files or UTF-8 HTML files. 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.. DoDefault – True.
Output parameters: Set values of DoDefault and OutStr according to the table below. Mismatched values will cause corrupted files.
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.
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: TRVRawByteString; var DoDefault: Boolean); begin if (SaveFormat in [rvsfText, rvsfHTML]) and (RVData.GetItemStyle(ItemNo)>=0) and 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: TRVRawByteString; var DoDefault: Boolean); begin if (RVData.GetItemStyle(ItemNo)=rvsPicture) then begin if SaveFormat=rvsfHTML then OutStr := '<PIC>' 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 '<' and '>' with '>' for HTML. | ||||||||||||||||||||