Insert Unicode in RichText

General TRichView support forum. Please post your questions here
Post Reply
mgalindo
Posts: 4
Joined: Fri Apr 10, 2009 1:35 pm

Insert Unicode in RichText

Post by mgalindo »

Delphi 7, I have a problem where I need to replace some plain text in a Rich Text with unicode characters, the replacement is done correctly, the problem is when I save the Rich text to a File and when I reload it to the RichViewEdit, the replaced unicode is displayed as ???????, if I open the same file in Wordpad the unicode looks ok.

Code: Select all

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, RVScroll, RichView, RVStyle, StdCtrls, Buttons, RVEdit, ExtCtrls, RVTypes;

type
  TForm1 = class(TForm)
    RichViewEdit1: TRichViewEdit;
    RVStyle1: TRVStyle;
    btbtnLoadFile: TBitBtn;
    ledFileName: TLabeledEdit;
    btbtnInsertUnicode: TBitBtn;
    btbtnSearchAndReplace: TBitBtn;
    btbtnSaveFile: TBitBtn;
    ledOutputFileName: TLabeledEdit;
    btntReloadoutput: TBitBtn;
    procedure btbtnLoadFileClick(Sender: TObject);
    procedure btbtnInsertUnicodeClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure btbtnSearchAndReplaceClick(Sender: TObject);
    procedure btbtnSaveFileClick(Sender: TObject);
    procedure btntReloadoutputClick(Sender: TObject);
  private
    { Private declarations }
    function GetDummyUnicodeText: TRVUnicodeString;
    procedure LoadFile(aFileName: String);
    procedure CreateUniCodeStyle;

  public
    { Public declarations }
  end;

var
  Form1: TForm1;


implementation

{$R *.dfm}

procedure TForm1.FormShow(Sender: TObject);
begin
  RichViewEdit1.RTFReadProperties.TextStyleMode := rvrsAddIfNeeded;
  CreateUniCodeStyle;
  LoadFile(ledFileName.Text);
end;

procedure TForm1.LoadFile(aFileName: String);
begin
  RichViewEdit1.Clear;
  RichViewEdit1.RTFReadProperties.TextStyleMode := rvrsAddIfNeeded;
  RichViewEdit1.LoadRTF(aFileName);
  RichViewEdit1.Format;
end;

procedure TForm1.btbtnLoadFileClick(Sender: TObject);
begin
  LoadFile(ledFileName.Text);
end;

procedure TForm1.btbtnInsertUnicodeClick(Sender: TObject);
Var
  sText : TRVUnicodeString;
begin
  sText := GetDummyUnicodeText;
  RichViewEdit1.InsertTextW(sText);
end;


function TForm1.GetDummyUnicodeText: TRVUnicodeString;
begin
  //Thai Characters
  Result := #$0E26;
  Result := Result+#$0E27;
  Result := Result+#$0E28;
  Result := Result+#$0E29;
  Result := Result+#$0E30;
  Result := Result+#$0E32;
  Result := Result+#$0E27;
  Result := Result+#$0E28;
  Result := Result+#$0E29;
  Result := Result+#$0E30;
  Result := Result+#$0E32;
  Result := Result+#$0E27;
  Result := Result+#$0E28;
  Result := Result+#$0E29;
  Result := Result+#$0E30;
  Result := Result+#$0E32;
end;


procedure TForm1.btbtnSearchAndReplaceClick(Sender: TObject);
var
  sTextToSearch: String;
  sTextToInsert : TRVUnicodeString;
begin
  sTextToSearch := '<Name>';

  RichViewEdit1.SetSelectionBounds(0, RichViewEdit1.GetOffsBeforeItem(0), 0, RichViewEdit1.GetOffsBeforeItem(0));

  if RichViewEdit1.SearchText(sTextToSearch,  [rvseoWholeWord, rvseoDown]) then
  begin
    RichViewEdit1.ApplyTextStyle(0);

    sTextToInsert :=  GetDummyUnicodeText;

    RichViewEdit1.InsertTextW(sTextToInsert);
  end;
end;


procedure TForm1.btbtnSaveFileClick(Sender: TObject);
begin
  RichViewEdit1.SaveRTF(ledOutputFileName.Text, False);
end;

procedure TForm1.CreateUniCodeStyle;
var
 aFontInfo: TFontInfo;
begin
  RichViewEdit1.Clear;

  aFontInfo := RVStyle1.TextStyles.Add;

  aFontInfo.Unicode := True;
  aFontInfo.StyleName := 'UniCode';

  RVStyle1.DefUnicodeStyle :=  RVStyle1.TextStyles.Count-1;

end;

procedure TForm1.btntReloadoutputClick(Sender: TObject);
begin
  LoadFile(ledOutputFileName.Text);
end;

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

Post by Sergey Tkachenko »

Set RichViewEdit1.RTFReadProperties.UnicodeMode to rvruMixed (if you want to load text as ANSI + minimal Unicode using) or rvruOnlyUnicode (to load all RTF text as Unicode)
mgalindo
Posts: 4
Joined: Fri Apr 10, 2009 1:35 pm

Insert Unicode in RichText

Post by mgalindo »

Thanks Sergey that worked fine.
Now I am having another problem, I load an RTF from a file into a TRichViewEdit, the RTF was created with wordpad and contains some Unicode lines (Thai), programatically I insert more unicode lines (as shown in my example below), after that I have to output the RTF as PDF.
The Unicode entered in Wordpad is displayed correctly in the PDF but the one I inserted programatically shows as ??????.
I am using GNostice EDoc Engine to export as PDF. I was wondering what could be the difference between the Unicode entered in Wordpad and the one I insert programatically in a TRichViewEdit.

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

Post by Sergey Tkachenko »

Try converting all styles to Unicode (instead of creating one Unicode style):

Code: Select all

RichViewEdit1.Clear
for i := 0 to RVStyle1.TextStyles.Count-1 do
  RVStyle1.TextStyles[i].Unicode := True;
mgalindo
Posts: 4
Joined: Fri Apr 10, 2009 1:35 pm

Post by mgalindo »

I changed what you suggested and I am still getting the same result, ???? in the unicode that I inserted programatically.
Anyother ideas?
Sergey Tkachenko
Site Admin
Posts: 17559
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Please save this document as RVF file and send it to me.
I'll try to find the difference between text from RTF and this text.
mgalindo
Posts: 4
Joined: Fri Apr 10, 2009 1:35 pm

Post by mgalindo »

I sent you an e-mail with the requested file.
Post Reply