4

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.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
emendelson
  • 416
  • 5
  • 18
  • Your follow-up question: [LoadStringFromFile into compiled script (so available on system that doesn't have the file)?](https://stackoverflow.com/q/26315825/850848) – Martin Prikryl Nov 01 '17 at 10:35

2 Answers2

4

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 .

Community
  • 1
  • 1
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
1

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.

Eugene Mala
  • 1,081
  • 12
  • 25
  • Maybe you should start the answer by explaining that it really does not do, what OP has asked for, and why you chose this solution. – Martin Prikryl Nov 01 '17 at 10:34
  • 1
    I think my answer do exactly what OP had asked for. And this is the alternative solution. I added more explanations to my answer. – Eugene Mala Nov 01 '17 at 15:54