Changing the fontname of text in a document

General TRichView support forum. Please post your questions here
Post Reply
bglboss
Posts: 14
Joined: Mon Aug 20, 2007 5:33 am

Changing the fontname of text in a document

Post by bglboss »

Hello

This should be simple but seems to be so difficult.

I have a document in a stream.


I load the document into a TRichview
VRichView := TRichView.Create( nil );
VStyles := TRVStyle.Create( nil );
VRichView.Style := VStyles;
VRichView.RVFParaStylesReadMode := {rvf_sIgnore;//}rvf_sInsertMerge;
VRichView.RVFTextStylesReadMode := {rvf_sIgnore;//}rvf_sInsertMerge;

VRichView.LoadRVFFromStream( Document );

I now want to change the font for all the text in the document
VRichView.Style.TextStyles[0].FontName := 'Arial Unicode MS';

This does not seem to work.

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

Post by Sergey Tkachenko »

Code: Select all

for i :=0 to VStyles.TextStyles.Count-1 do
  VRichView.Style.TextStyles[i].FontName := 'Arial Unicode MS'; 
VRichView.Format;
PS: currently, TRichView cannot work without Parent assigned.

So add:

Code: Select all

VRichView.Visible := False;
VRichView.Parent := <some form>
bglboss
Posts: 14
Joined: Mon Aug 20, 2007 5:33 am

Post by bglboss »

Sergey

Thanks for your prompt reply but this did not seem to fix the problem

My code as it stands now:

VRichView := TRichView.Create( nil );
VStyles := TRVStyle.Create( nil );

VRichView.Visible := False;
VRichView.Parent := Form6;

VRichView.Style := VStyles;
VRichView.RVFParaStylesReadMode := rvf_sInsertMerge;
VRichView.RVFTextStylesReadMode := rvf_sInsertMerge;

VRichView.LoadRVFFromStream( Document );

for i :=0 to VStyles.TextStyles.Count-1 do
VRichView.Style.TextStyles.FontName := 'Arial Unicode MS';

VRichView.Format;


VRichView.SaveRVFToStream( Document, True );

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

Post by Sergey Tkachenko »

This code must work.
But if you want to replace content of Document, call:

Code: Select all

Document.Clear;
VRichView.SaveRVFToStream( Document, False ); 
instead of

Code: Select all

VRichView.SaveRVFToStream( Document, True ); 
Without clearing the stream, new information will be written to the end of the existing content. It may produce incorrect RVF. Besides, you call SaveRVFToStream( Document, True ), so only selection is saved. In your code, the selection is empty.

In addition, add the following line before saving:

Code: Select all

VRichView.RVFOptions := VRichView.RVFOptions + [rvfoSaveTextStyles, rvfoSaveParaStyles]
In addition, calling Format is not necessary if you do not need to display this document. Removing VRichView.Format will speed up this code.
Last edited by Sergey Tkachenko on Tue Aug 21, 2007 4:45 am, edited 1 time in total.
bglboss
Posts: 14
Joined: Mon Aug 20, 2007 5:33 am

Post by bglboss »

Thanks Sergey , now works beautifully
Post Reply