Working with RTF Data

Description

Using Xbasic, you can manipulate data in variables and in RTF memo fields. In order to manipulate a RTF memo field, you need to create a temporary RTF object, manipulate its contents, and then put the contents of the temporary RTF object back into the RTF memo field. The RTF.CREATE() method is used to create a RTF object. Refer to RTF Functions and Methods for more information.

The following example shows how you can add text to a RTF memo field.

t = table.open("customer")
'Get a pointer to the RTF memo field
f = t.field_get("notes")
'Create an RTF object, that is initially a NULL value
r = rtf.create("")

Get the binary data that is currently in the Notes field and assign it to the binary_text property of the rtf object. The RTF object now contains the same value as the memo field.

r.Binary_Text = f.value_get()
'Set the font (name and point size) and color for next
'insertion into the RTF object
r.set_font("courier,15","blue")

Insert a CR-LF, and then the text "Xbasic Example" into the RTF object. The position argument is set to 0 to indicate that the insertion is after the existing text in the RTF object.

r.insert(crlf()+ "Xbasic example",0)
'Now change the font for the next insertion"
r.set_font("arial,10","black")
r.insert(crlf()+ "Alpha Software" + crlf(), 0)
'Put the table into change mode
t.change_begin()
'Put the value in the rtf object's "binary_text" property into the RTF memo field.
f.value_put(r.Binary_Text)
'Save the changes
t.change_end(.T.)
'Close the table
t.close()

The following example shows how you can import text that is saved in a RTF format file into a RTF memo field. Assume that this script is attached to a button on a form and that the RTF memo field is called rtfmemo. First, get a pointer to the object on the form that contains the rtfmemo.

obj = obj("rtfmemo")
'register the rtf clipboard type
clip_type = ClipBoard.Register_Type("Rich Text Format")
fn = "c:\mydata\rtfdata.rtf"
'put the rtf data onto the clipboard
'the file.to_blob() method reads the file containing
'the rtf data into a blob variable.
ClipBoard.Set_Data(file.to_blob(fn),clip_type)
'obj.rtf is a pointer to the rtf object on the form.
obj.rtf.paste()
'now reset the clipboard to NULL
ClipBoard.Set_Data("",clip_type)

See Also