Monday, 3 September 2007

Rich Text fields and tables.

Oh boy are they a pain to do, but as I have just spent the best part of a day fighting them I will pass on some of the code that I have pulled together from various places on the web.

Firstly you obviously need a Richtext field and given I am assuming you all know how to set that up and grab it as a NotesItem, I'm not going to tell you how to do it.

Call bodyitem.AppendTable(dc.Count+1, 8,"",RULER_ONE_INCH, rtpsCols)
Append the table to the Richtext field, here I have a document collection and I'm making the table 1 row bigger than the collection so I can have a header. There are 8 Columns in the table, the "" is where you could add in a list of text if you are using a tabbed table. The Ruler_One_Inch denotes the left margin and the rtpsCols is the array defining the Style for each column.
Dim rtnav As NotesRichTextNavigator
Set rtnav = bodyitem.CreateNavigator
The RichTextNavigator allows you to move through the table, it's not as easy as saying put this value in the 4th row, 3rd column.
Call rtnav.FindFirstElement(RTELEM_TYPE_TABLECELL)
This sends the navigator to the first cell in the table.
Call bodyItem.AppendStyle(rtsTableHeader)
This sets the style for anything else added to the table, until you change it again.
Call bodyitem.AppendText(doc.Sortby(0))
Add some text to the table cell
Call rtnav.FindNextElement(RTELEM_TYPE_TABLECELL)
Goto the next table cell
Call bodyItem.AppendStyle(rtsTableRow)
Set the style to something else.

So there you go, the very basics on creating a table, stepping through it, adding text and changing styles.

However we have styles and that wonderfully titled rtpsCols still to go. So lets start with Styles, these are simply NotesRichTextStyles and in the code I am using the TableHeader and Row formats are as follows, and also fairly self explanatory.

Dim rtsTableHeader As NotesRichTextStyle
Set rtsTableHeader = sess.CreateRichTextStyle
rtsTableHeader.FontSize = 10
rtsTableHeader.Bold = True
Dim rtsTableRow As NotesRichTextStyle
Set rtsTableRow= sess.CreateRichTextStyle
rtsTableRow.FontSize = 7
rtsTableRow.Bold = False

I think it's always a good practice when you are building a table to have headers and potentially footers or row headings in a larger font to make them stand out, however you need to take into account screen resolutions and get the balance right between the amount of information on the screen and the readability of the font.
Finally rtpsCols, this is an array of NotesRichTextParagraphStyles, one for each column in your table, this example just uses the basics on the formatting available, Column Alignment and Column Width. Alignment is a numeric value 0 to 4, 0 being Left, 1 Right, 2 Full, 3 Center and 4 No Wrap. The Column Width is defined simply using the Left Margin and Right Margin values with the Left Margin set to 0 and the Right Margin set to the length you require.

Dim rtpsCols(8) As NotesRichTextParagraphStyle
Set rtpsCols(0) = sess.CreateRichTextParagraphStyle
This column in left aligned
rtpsCols(0).Alignment = 3
rtpsCols(0).Leftmargin = 0
rtpsCols(0).RightMargin = RULER_ONE_CENTIMETER * 0.75

Set rtpsCols(1) = sess.CreateRichTextParagraphStyle
This column is centre aligned
rtpsCols(1).Alignment =0
rtpsCols(1).Leftmargin = 0
rtpsCols(1).RightMargin = RULER_ONE_CENTIMETER * 5

Well that's it for my first attempt at RichTextTables, more soon if I uncover more gems.

No comments: