Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import external RTF file for TRichEditViewer?

Tags:

inno-setup

rtf

I'm using TRichEditViewer on a custom page in an Inno Setup script. Is it possible to read an external RTF file into a variable, and use that variable as the content of the viewer? Or do I have to keep the actual RTF code in the script?

Any help gratefully received.

like image 714
emendelson Avatar asked Oct 23 '25 19:10

emendelson


2 Answers

You should be able to use LoadStringFromFile to read an RTF file into a string. From the Inno documentation:

Prototype:

function LoadStringFromFile(const FileName: String; var S: AnsiString): Boolean;

Description:

Loads the specified binary or non Unicode text file into the specified string. Returns True if successful, False otherwise.

You should be able to define a string type variable for ANSI Inno Setup or AnsiString type variable for Unicode Inno Setup using something like:

var
#ifndef UNICODE
  rtfstr: string;
#else
  rtfstr: AnsiString;
#endif

Then in the code:

LoadStringFromFile('filenamehere.rtf', rtfstr);

And then use code similar to what is below. In this example assume oRichViewer is a TRichEditViewer object:

oRichViewer.UseRichEdit := True;
oRichViewer.RTFText := rtfstr;

This should have the effect of putting rtfstr which we loaded from the file earlier into the TRichEditViewer .

like image 57
Michael Petch Avatar answered Oct 26 '25 19:10

Michael Petch


If you don't want to copy your .rtf file to the temporary folder before displaying it, and want to fill variable with RTF content at compile time - you can use Inno Setup Preprocessor.

First, create a macros ReadFileAsStr:

#pragma parseroption -p- 

#define FileHandle
#define FileLine
#define FileName
#define Result
#sub ProcessFileLine
  #define FileLine = FileRead(FileHandle) 
  #if Len(Result) > 0 && !FileEof(FileHandle)
    #expr Result = Result + "#10#13 +  \n"
  #endif
  #if FileLine != '\0'
    #expr Result = Result + "'" + FileLine + "'"
  #endif
#endsub
#sub ProcessFile
  #for {FileHandle = FileOpen(FileName); \
    FileHandle && !FileEof(FileHandle); ""} \
    ProcessFileLine
  #if FileHandle
    #expr FileClose(FileHandle)
  #endif   
#endsub

#define ReadFileAsStr(str AFileName) \
  Result = '', FileName = AFileName, ProcessFile, Result

And use it:

var
  rtfstr := {#emit ReadFileAsStr("path_to.rtf")};

This works for most RTF files, but some characters inside RTF can broke this code. To fix this you need to escape ' and may be some other characters inside ProcessFileLine sub.

like image 45
Eugene Mala Avatar answered Oct 26 '25 19:10

Eugene Mala



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!