How to: Format Data

  • 3 minutes to read

The following example demonstrates how to format data using the Snap Report API.

To start formatting document content, you need to prepare the document for modification by doing the following.

  1. Load the document template using the RichEditDocumentServer.LoadDocumentTemplate method.
  2. Call the ISnapFieldOwner.CreateSnList method to create a new Snap list or retrieve one of the existing lists by calling the ISnapFieldOwner.FindListByName method.
  3. Convert the document fields to Snap Fields. To do that, call the ISnapFieldOwner.ParseField method. Note that without parsing, you won’t be able to modify the document.
  4. Enable the modifying of the list by calling the SnapEntity.BeginUpdate method.

Every part of a snap document, whether it’s a list header or a row template, is represented by a table of document fields. Tables of the list header can be retrieved through the SnapList.ListHeader property, and the tables in the row template - through the SnapList.RowTemplate property.

To apply text formatting to cell content, call the SubDocument.BeginUpdateCharacters method for the specified range, use the returned CharacterProperties instance to modify the required properties and call the SubDocument.EndUpdateCharacters method to finalize the modification.

You can change table appearance by changing the color of different table elements. To achieve the required result, the following properties can be used.

You can also apply one of the predefined themes to the whole document. To do that, call the SnapDocument.ApplyTheme method. Pass one of the Themes enumeration values as a method parameter.

Call the SnapEntity.EndUpdate method to finalize the list modification. Then, update the list fields by calling the FieldCollection.Update method.

server.LoadDocumentTemplate("Template.snx")
Dim list As SnapList = server.Document.FindListByName("Data Source 11")
server.Document.ParseField(list.Field)
list.BeginUpdate()
list.EditorRowLimit = 100500

Dim header As SnapDocument = list.ListHeader
Dim headerTable As Table = header.Tables(0)
headerTable.SetPreferredWidth(50 * 100, WidthType.FiftiethsOfPercent)

For Each row As TableRow In headerTable.Rows
    For Each cell As TableCell In row.Cells
        ' Apply cell formatting.
        cell.Borders.Left.LineColor = System.Drawing.Color.White
        cell.Borders.Right.LineColor = System.Drawing.Color.White
        cell.Borders.Top.LineColor = System.Drawing.Color.White
        cell.Borders.Bottom.LineColor = System.Drawing.Color.White
        cell.BackgroundColor = System.Drawing.Color.SteelBlue

        ' Apply text formatting.
        Dim formatting As CharacterProperties = header.BeginUpdateCharacters(cell.ContentRange)
        formatting.Bold = True
        formatting.ForeColor = System.Drawing.Color.White
        header.EndUpdateCharacters(formatting)
    Next cell
Next row

' Customize the row template.
Dim rowTemplate As SnapDocument = list.RowTemplate
Dim rowTable As Table = rowTemplate.Tables(0)
rowTable.SetPreferredWidth(50 * 100, WidthType.FiftiethsOfPercent)
For Each row As TableRow In rowTable.Rows
    For Each cell As TableCell In row.Cells
        cell.Borders.Left.LineColor = System.Drawing.Color.Transparent
        cell.Borders.Right.LineColor = System.Drawing.Color.Transparent
        cell.Borders.Top.LineColor = System.Drawing.Color.Transparent
        cell.Borders.Bottom.LineColor = System.Drawing.Color.LightGray
    Next cell
Next row

list.EndUpdate()
list.Field.Update()

The following image illustrates the result of document formatting.

ServerExamples_FormatText