Unable to write multiple lines of Rich Text data to file

Hi, I’m new here but not new to programming. I got Xojo a few weeks ago and have been developing an app on OS X. Most things are working pretty well and are fairly simple to pick up, but I have one issue that I have not been able to get around. My app will allow the user to convert a textfield (title) and a textarea (body as Rich Text) as a PDF combined. I have PDF working but I need to write the contents to a Rich Text File before conversion. The problem I have ran into is with the stream writers (text and binary). If I write the title text it writes the file as expected. If I write the body it writes as expected but I cannot get them both to write.

Dim body As String  'This is retrieved from a data base where it was saved as RTFData.
Dim title As String     'Saved in database as text.
Dim sTitle As New StyledText

title = rec.Field("entryTitle").StringValue
sTitle.Text=title
body = rec.Field("entry").StringValue

Dim tmpF As FolderItem
tmpF = SpecialFolder.Documents.Child(tmpPath)

Dim stream as TextOutputStream=TextOutputStream.Create(tmpF)
sTitle.ParagraphAlignment(0) = Paragraph.AlignCenter
stream.WriteLine sTitle.RTFData
stream.Write body
stream.Close

That code, for instance, only writes the title. If I reverse title and body I get the body written but not the title. The formatting is preserved, btw. It’s driving me crazy. Is there a way to merge RTFData or write both items to the file that I have missed? I have tried StyleRun and TextStream append also, no go. I assume something in styled text is the problem but I’m not totally sure. I’ve taken out all the conditional and exception handling code for clarity.

Thanks!

How are you confirming the contents of the file?

I physically look in both the rtf and PDF. The only time I got both to write I end up with the body text written with the formatting and not just the text formatted - if that makes sense. This is why I think the problem is related to the rtfData. Plain text works as expected. The data is good, just can’t get both pieces to write to the same file if rich text. Thanks

Stupid suggestion here:

why don’t you add the title at the beginning of the RTFData ?
(put the rtfdata in n offscreen TextArea, place the cursor at 0,0, write the title there).

Then save the new rtfdata to disk…

You can’t simply write a line to the end of RTF.
There is a tag in RTF to mark end, so parser will not read what’s after it.

Sorry, it seems that i was not crystal clear:

a. write the rtfdata in a TextArea,
b. TextArea1.SelStart = 0 // Set the cursor at the beg of the TextArea
c. TextArea.SelText = Title // Place there (at the beg of text) the title
d. Save the whole TextArea contents in an rtf file.

Manipulating a styled text: http://documentation.xojo.com/index.php/StyledText

Thank you Emile, Christian, and Rick. I thought I could string these together after being retrieved from the database then write them out as one big RTF. Is there a way to insert the title, converted to rtfData, into the styled body so it can all be saved to disk? I’m not needing or using a textarea for this part of the code, just data saved to a database from a textfield and textarea. This save code is executed from a contextual menu item off of a listbox. Would it be best to create a hidden textarea using the suggested textarea code? Worse case this is how I may approach this.

I tried new styledtext and using two style runners with append to dump both pieces of data to the new styled text variable, but this also does not work. Any suggestions there?

Thanks

Use what Emile described. Put the styledtext into a TextArea and use SelText to add the title with whatever style attribute you need before saving TextArea.StyledText.RTFData.

If you do not want the user to see the textarea, make it invisible.