# Show RTF File content with formatting on Text Area 

**URL:** <https://forum.xojo.com/t/show-rtf-file-content-with-formatting-on-text-area/37495>\
**Category:** macOS\
**Created:** [May 6, 2017, 5:30pm UTC](https://forum.xojo.com/t/show-rtf-file-content-with-formatting-on-text-area/37495 "2017-05-06T17:30:46Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aurelian\_N](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/b4bc9f/32.png) [@Aurelian\_N](https://forum.xojo.com/u/Aurelian_N)\
**Post date:** [May 6, 2017, 5:30pm UTC](https://forum.xojo.com/t/show-rtf-file-content-with-formatting-on-text-area/37495/1 "2017-05-06T17:30:46Z")

</div>

Hello Guys,

I`m trying to put the text that i have saved in a .RTF document on a text area for license purposes and it seems that i cannot make it , i guess i`m doing something wrong.

On the text Area MultiLine is enabled, Styled text as well enabled.

Code is the following :

[code]dim ti As TextInputStream  
dim RecordText As New StyledText

dim fi As FolderItem  
dim fiPath As String

fi = app.ExecutableFile.Parent.Parent.Child(“Resources”).Child(“eula\_fr.rtf”)

If fi.Exists AND fi \<\> Nil Then  
fiPath = fi.NativePath

dim tString As String

ti = TextInputStream.Open(fi)

tString = ti.ReadAll

RecordText.RTFData = tString

ti.Close

me.StyledText.RTFData = RecordText

End If[/code]

So far for test purposes i get the data on the tString , with all the RTF Encodings but nothing on the Text Area Text , it comes empty .

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [May 6, 2017, 5:32pm UTC](https://forum.xojo.com/t/show-rtf-file-content-with-formatting-on-text-area/37495/2 "2017-05-06T17:32:27Z")

</div>

Why do you go through a convoluted system of separate styled Text ?

This should work just fine :

```auto
me.StyledText.RTFData = ti.ReadAll
```

It could also work like this :

[code]dim ti As TextInputStream

dim fi As FolderItem  
dim fiPath As String

fi = app.ExecutableFile.Parent.Parent.Child(“Resources”).Child(“eula\_fr.rtf”)

If fi.Exists AND fi \<\> Nil Then  
fiPath = fi.NativePath

dim tString As String

ti = TextInputStream.Open(fi)

tString = ti.ReadAll

ti.Close

me.StyledText.RTFData = tString

End If[/code]

---

<div class="post-metadata">

**Author:** ![Aurelian\_N](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/b4bc9f/32.png) [@Aurelian\_N](https://forum.xojo.com/u/Aurelian_N)\
**Post date:** [May 6, 2017, 5:34pm UTC](https://forum.xojo.com/t/show-rtf-file-content-with-formatting-on-text-area/37495/3 "2017-05-06T17:34:55Z")

</div>

Thank you Michel but i don`t get anything from there honestly . I looked on the forum for samples and ways to read .rtf files and to show them styled on the text area as i read that it is possible but no luck, i guess if i still don`t manage to get it then i will do a .html document and an html viewer and that`s it .

Thanks again .

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [May 6, 2017, 5:37pm UTC](https://forum.xojo.com/t/show-rtf-file-content-with-formatting-on-text-area/37495/4 "2017-05-06T17:37:34Z")

</div>

Look. I have been using that technique for years. Your error is to insist on a separate styledtext variable. Just shove the RTFData string from the disk into your TextArea.StyledText.RTFData. BTW are you sure of eula\_fr.rtf ? Have you opened it with a word processor ? Does it actually contain what you think it does ?

Keep it simple.

You should really look at [http://documentation.xojo.com/index.php/StyledText.RTFData](http://documentation.xojo.com/index.php/StyledText.RTFData)

That said, an HTMLViewer is what I have been using for quite a while for my apps help.

---

<div class="post-metadata">

**Author:** ![Aurelian\_N](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/b4bc9f/32.png) [@Aurelian\_N](https://forum.xojo.com/u/Aurelian_N)\
**Post date:** [May 6, 2017, 5:44pm UTC](https://forum.xojo.com/t/show-rtf-file-content-with-formatting-on-text-area/37495/5 "2017-05-06T17:44:09Z")

</div>

Well i did and still the Text Area is empty

[code]dim ti As TextInputStream

dim fi As FolderItem  
dim fiPath As String

fi = app.ExecutableFile.Parent.Parent.Child(“Resources”).Child(“eula\_fr.rtf”)

If fi.Exists AND fi \<\> Nil Then

ti = TextInputStream.Open(fi)

me.StyledText.RTFData = ti.ReadAll

ti.Close

End If[/code]

So maybe i`m getting this wrong, if i throw the ti.readall data into a string i get the data, if i put it into the me.StyledText.RTFData it comes empty on the form.

---

<div class="post-metadata">

**Author:** ![DaveS](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@DaveS](https://forum.xojo.com/u/DaveS)\
**Post date:** [May 6, 2017, 7:14pm UTC](https://forum.xojo.com/t/show-rtf-file-content-with-formatting-on-text-area/37495/6 "2017-05-06T19:14:40Z")

</div>

```auto
me.StyledText.RTFData = ti.ReadAll
```

where is this code located?  
are you sure the reference to “ME” is the correct textarea? (because that is the first place _I’d_ look)

and otherwise, Michel is quite correct, there is no reason to jump through hoops…  
but you need to insure the textarea you are “loading” is the same textarea you expect to see  
AND there is nothing in between that UNDOES what you did (that has bitten me an unknown number of times)

---

<div class="post-metadata">

**Author:** ![Aurelian\_N](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/b4bc9f/32.png) [@Aurelian\_N](https://forum.xojo.com/u/Aurelian_N)\
**Post date:** [May 6, 2017, 7:23pm UTC](https://forum.xojo.com/t/show-rtf-file-content-with-formatting-on-text-area/37495/7 "2017-05-06T19:23:28Z")

</div>

[quote=329485:@Dave S]`
me.StyledText.RTFData = ti.ReadAll
`  
where is this code located?  
are you sure the reference to “ME” is the correct textarea? (because that is the first place _I’d_ look)

and otherwise, Michel is quite correct, there is no reason to jump through hoops…  
but you need to insure the textarea you are “loading” is the same textarea you expect to see  
AND there is nothing in between that UNDOES what you did (that has bitten me an unknown number of times)[/quote]  
Hi Dave, Into the Open Event of the TextArea

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [May 6, 2017, 7:24pm UTC](https://forum.xojo.com/t/show-rtf-file-content-with-formatting-on-text-area/37495/8 "2017-05-06T19:24:42Z")

</div>

From where does the RTF comes ?

---

<div class="post-metadata">

**Author:** ![Aurelian\_N](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/b4bc9f/32.png) [@Aurelian\_N](https://forum.xojo.com/u/Aurelian_N)\
**Post date:** [May 6, 2017, 7:26pm UTC](https://forum.xojo.com/t/show-rtf-file-content-with-formatting-on-text-area/37495/9 "2017-05-06T19:26:07Z")

</div>

> [@329489:@Michel Bujardet](#):
>
> From where does the RTF comes ?

A file located on the disk,

If i do ti.ReadAll and put all that into a String i see all the RTF Data , so the data comes and formatted.

The issue is on Displaying that on the Text Area and correctly .

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [May 6, 2017, 7:30pm UTC](https://forum.xojo.com/t/show-rtf-file-content-with-formatting-on-text-area/37495/10 "2017-05-06T19:30:25Z")

</div>

That is not what I mean. Which program generated that RTF ?

---

<div class="post-metadata">

**Author:** ![Aurelian\_N](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/b4bc9f/32.png) [@Aurelian\_N](https://forum.xojo.com/u/Aurelian_N)\
**Post date:** [May 6, 2017, 7:31pm UTC](https://forum.xojo.com/t/show-rtf-file-content-with-formatting-on-text-area/37495/11 "2017-05-06T19:31:05Z")

</div>

> [@329491:@Michel Bujardet](#):
>
> That is not what I mean. Which program generated that RTF ?

TextEdit, MAC

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [May 6, 2017, 7:37pm UTC](https://forum.xojo.com/t/show-rtf-file-content-with-formatting-on-text-area/37495/12 "2017-05-06T19:37:59Z")

</div>

Here is what I did in a small test project, with my own User license file created in TextEDit. Works flawlessly.

[code]Sub Open() Handles Open  
dim f as folderItem = SpecialFolder.Documents.child(“User\_License.rtf”)

If f \<\> Nil Then  
If f.Exists Then  
system.DebugLog “loading”  
// Be aware that TextInputStream.Open coud raise an exception  
Dim t As TextInputStream  
Try  
t = TextInputStream.Open(f)  
t.Encoding = Encodings.MacRoman  
me.styledText.RTFData = t.ReadAll  
Catch e As IOException  
t.Close  
MsgBox(“Error accessing file.”)  
End Try  
End If  
End If  
End Sub  
[/code]

All I did was to modify the first example at [http://documentation.xojo.com/index.php/Textinputstream](http://documentation.xojo.com/index.php/Textinputstream)

No complicated styledText value and intermediary variables. Plain, simple.

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [May 6, 2017, 7:44pm UTC](https://forum.xojo.com/t/show-rtf-file-content-with-formatting-on-text-area/37495/13 "2017-05-06T19:44:49Z")

</div>

BTW, you should really do yourself a favor and get TPSF from Tim Parnell, that will let you access files in the bundle in a much easier way.

[https://forum.xojo.com/15318-modules-for-you-updated-12-17/0](https://forum.xojo.com/15318-modules-for-you-updated-12-17/0)

---

<div class="post-metadata">

**Author:** ![Roger\_Clary](https://forum.xojo.com/letter_avatar_proxy/v4/letter/r/eada6e/32.png) [@Roger\_Clary](https://forum.xojo.com/u/Roger_Clary)\
**Post date:** [May 7, 2017, 1:06am UTC](https://forum.xojo.com/t/show-rtf-file-content-with-formatting-on-text-area/37495/14 "2017-05-07T01:06:54Z")

</div>

Regarding another issue in your code…

```auto
If fi.Exists AND fi <> Nil Then
```

…is incorrect. It should be

```auto
If fi <> Nil and fi.Exists Then
```

The reason is that, if fi is, in fact, nil your code will already have crashed with a NilObjectException on fi.Exists and never get to fi\<\> Nil.

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 6:45pm UTC](https://forum.xojo.com/t/show-rtf-file-content-with-formatting-on-text-area/37495/15 "2020-10-29T18:45:17Z")

</div>


