PlainTable.vb
C# VB
'''' This code is part of Document Solutions for Imaging demos.'' Copyright (c) MESCIUS inc. All rights reserved.''Imports System.DrawingImports System.IOImports System.Collections.GenericImports GrapeCity.Documents.DrawingImports GrapeCity.Documents.TextImports GrapeCity.Documents.ImagingImports GrapeCity.Documents.Layout.CompositionImports GCTEXT = GrapeCity.Documents.Text
'' This example shows how to draw a plain table with random data,'' using the GrapeCity.Documents.Drawing.TableRenderer and related classes.Public Class PlainTable Public Function GenerateImage( ByVal pixelSize As Size, ByVal dpi As Single, ByVal opaque As Boolean, Optional ByVal sampleParams As String() = Nothing) As GcBitmap
Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi) Using g = bmp.CreateGraphics(Color.White) DrawTable(g, pixelSize.Width, pixelSize.Height) End Using Return bmp End Function
Private Class Record Public ReadOnly Property Name As String Public ReadOnly Property Take1 As Integer Public ReadOnly Property Take2 As Integer Public ReadOnly Property Take3 As Integer
Public Sub New(name As String, take1 As Integer, take2 As Integer, take3 As Integer) Me.Name = name Me.Take1 = take1 Me.Take2 = take2 Me.Take3 = take3 End Sub End Class
Private Shared Sub DrawTable(g As GcGraphics, pageWidth As Single, pageHeight As Single) Dim rnd = Util.NewRandom() Dim records = New List(Of Record)() For i = 0 To 9 records.Add(New Record(Util.LoremIpsum(1, 1, 1, 1, 4), rnd.Next(100), rnd.Next(100), rnd.Next(100))) Next
'' The Surface object helps with calculating layout '' of the table and drawing the table on a graphics. Dim surf = New Surface() Dim view = surf.CreateView(pageWidth, pageHeight) Dim visual = view.CreateVisual()
'' The table is always contained within a LayoutRect which is passed to the TableRenderer constructor. Dim rt = visual.LayoutRect rt.AnchorTopLeft(Nothing, 36, 36)
Dim ta = New TableRenderer(g, rt, FixedTableSides.TopLeft, rowCount:=records.Count + 1, columnCount:=4, gridLineColor:=Color.DarkGray, gridLineWidth:=1, rowMinHeight:=10)
Dim cs = New CellStyle() With { .PaddingTopBottom = 3, .PaddingLeftRight = 4, .FixedWidth = False, .TextFormat = New TextFormat() With { .Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FreeSans.ttf")), .FontSize = 21, .FontSizeInGraphicUnits = True } } Dim csHeader = New CellStyle(cs) With { .PaddingRight = 9, .ParagraphAlignment = ParagraphAlignment.Center, .TextAlignment = TextAlignment.Center } Dim csName = New CellStyle(cs) With { .MaxWidth = pageWidth / 2 } Dim csNumber = New CellStyle(cs) With { .TextAlignment = TextAlignment.Trailing }
ta.DefaultCellStyle = csHeader ta.AddCell(0, 0, "Name") ta.AddCell(0, 1, "Take 1" & vbLf & "The Good") ta.AddCell(0, 2, "Take 2" & vbLf & "The Bad") ta.AddCell(0, 3, "Take 3" & vbLf & "The Ugly")
ta.DefaultCellStyle = csNumber For i = 0 To records.Count - 1 Dim p = records(i) Dim r = i + 1 ta.AddCell(csName, r, 0, p.Name) ta.AddCell(r, 1, p.Take1.ToString()) ta.AddCell(r, 2, p.Take2.ToString()) ta.AddCell(r, 3, p.Take3.ToString()) Next
'' Applying layout to the table cells must precede drawing the Surface. ta.ApplyCellConstraints()
visual.Draw = Sub(gr As GcGraphics, vis As Visual) '' Move from Visual transform to Surface transform before drawing the table. gr.Transform = vis.Layer.Surface.BaseTransform ta.Render() End Sub
surf.Render(g) End SubEnd Class