Convert TextItem with text styles to html text

General TRichView support forum. Please post your questions here
Post Reply
saeid2016
Posts: 44
Joined: Wed Mar 16, 2016 11:56 am

Convert TextItem with text styles to html text

Post by saeid2016 » Wed Aug 02, 2017 3:22 pm

Hello support,
Is there a method to convert a Text Item with styles to a html text?

SaveHTML function exports TRichView document to HTML or XHTML file, but I want to convert a text to html text.

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

Re: Convert TextItem with text styles to html text

Post by Sergey Tkachenko » Wed Aug 02, 2017 3:43 pm

Do you want to save all text properties (font, color, etc.)?

saeid2016
Posts: 44
Joined: Wed Mar 16, 2016 11:56 am

Re: Convert TextItem with text styles to html text

Post by saeid2016 » Wed Aug 02, 2017 3:47 pm

Sergey Tkachenko wrote:
Wed Aug 02, 2017 3:43 pm
Do you want to save all text properties (font, color, etc.)?
only font, color and backcolor

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

Re: Convert TextItem with text styles to html text

Post by Sergey Tkachenko » Wed Aug 02, 2017 4:50 pm

Code: Select all

uses
  RVTypes, CRVData;

type
  TCustomRVDataAccess = class (TCustomRVData)
  end;

function GetTextItemHTML(RVData: TCustomRVData; ItemNo: Integer): TRVUnicodeString;
var
  Span: TRVRawByteString;
  FontName: TRVUnicodeString;
  TextStyle: TFontInfo;
begin
  Result := UTF8ToString(TCustomRVDataAccess(RVData).GetTextForHTML('', ItemNo, True, [rvsoUTF8]));
  TextStyle := RVData.GetRVStyle.TextStyles[RVData.GetItemStyle(ItemNo)];
  FontName := UTF8ToString(StringToHTMLString3(TextStyle.FontName, True, CP_ACP));
  if (AnsiCompareText(FontName, RVFONT_SYMBOL) = 0) or
     (AnsiCompareText(FontName, RVFONT_WINGDINGS) = 0) then
    FontName := '''Arial Unicode MS'', ''Lucida Sans Unicode'', ''Arial'', ''Helvetica'', sans-serif'
  else
    FontName := '''' + FontName + '''';
  Span := '<span style="font-family : ' + Utf8Encode(FontName);
  if (TextStyle.Color <> clBlack) and (TextStyle.Color <> clWindowText) then
   Span := Span + '; color : ' + RV_GetHTMLRGBStr(TextStyle.Color, False);
  if TextStyle.BackColor <> clNone then
    Span := Span + '; background-color : ' + RV_GetCSSBkColor(TextStyle.BackColor);
  Span := Span + '">';
  Result := UTF8ToString(Span) + Result + '</span>';
end;
I had to use a hack (accessing protected method of TCustomRVData).

saeid2016
Posts: 44
Joined: Wed Mar 16, 2016 11:56 am

Re: Convert TextItem with text styles to html text

Post by saeid2016 » Wed Aug 02, 2017 8:34 pm

Thank you very much.

Post Reply