Private Shared Function RenderTableHeader(word As C1WordDocument, font As Font, rc As Rect, fields As String()) As Rect
' calculate cell width (same for all columns)
Dim rcCell As Rect = rc
rcCell.Width = rc.Width / fields.Length
rcCell.Height = 0
' calculate cell height (max of all columns)
For Each field As String In fields
Dim height = word.MeasureString(field, font, rcCell.Width).Height
rcCell.Height = Math.Max(rcCell.Height, height)
Next
rcCell.Height += 6
' add 6 point margin
' render header cells
Dim fmt = New StringFormat()
fmt.LineAlignment = VerticalAlignment.Center
For Each field As String In fields
word.FillRectangle(Colors.Black, rcCell)
word.DrawString(field, font, Colors.White, rcCell, fmt)
rcCell = WordUtils.Offset(rcCell, rcCell.Width, 0)
Next
' update rectangle and return it
Return WordUtils.Offset(rc, 0, rcCell.Height)
End Function
Private Shared Function RenderTableRow(word As C1WordDocument, font As Font, hdrFont As Font, rcPage As Rect, rc As Rect, fields As String(), _
dr As DataRow) As Rect
' calculate cell width (same for all columns)
Dim rcCell As Rect = rc
rcCell.Width = rc.Width / fields.Length
rcCell.Height = 0
' calculate cell height (max of all columns)
rcCell = WordUtils.Inflate(rcCell, -4, 0)
For Each field As String In fields
Dim text As String = dr(field).ToString()
Dim height = word.MeasureString(text, font, rcCell.Width).Height
rcCell.Height = Math.Max(rcCell.Height, height)
Next
rcCell = WordUtils.Inflate(rcCell, 4, 0)
' add 4 point margin
rcCell.Height += 2
' break page if we have to
If rcCell.Bottom > rcPage.Bottom Then
word.PageBreak()
rc = RenderTableHeader(word, hdrFont, rcPage, fields)
rcCell.Y = rc.Y
End If
' center vertically just to show how
Dim fmt As New StringFormat()
fmt.LineAlignment = VerticalAlignment.Center
' render data cells
For Each field As String In fields
' get content
Dim text As String = dr(field).ToString()
' set horizontal alignment
Dim d As Double
fmt.Alignment = If((Double.TryParse(text, NumberStyles.Any, CultureInfo.CurrentCulture, d)), HorizontalAlignment.Right, HorizontalAlignment.Left)
' render cell
word.DrawRectangle(Colors.LightGray, rcCell)
rcCell = WordUtils.Inflate(rcCell, -4, 0)
word.DrawString(text, font, Colors.Black, rcCell, fmt)
rcCell = WordUtils.Inflate(rcCell, 4, 0)
rcCell = WordUtils.Offset(rcCell, rcCell.Width, 0)
Next
' update rectangle and return it
Return WordUtils.Offset(rc, 0, rcCell.Height)
End Function