Examples in this group show how to draw tables on a GrapeCity.Documents.Drawing.GcGraphics (such as GcBitmapGraphics, GcSvgGraphics, etc.) with the GrapeCity.Documents.Drawing.TableRenderer class. TableRenderer can be used together with classes from the GrapeCity.Documents.Layout.Composition namespace to draw complex layouts including tables, text and graphics. Drawing a table typically involves the following steps:
  1. Create a rectangle (LayoutRect) for the table. There are two possible approaches. You can either use helper classes from the GrapeCity.Documents.Layout.Composition namespace, or classes from the GrapeCity.Documents.Layout namespace directly:
    • If using helper composition classes: create a Surface object, then create a View (e.g. with the size of the target page). Use the View object to create a Visual that will host the table. The Visual object has the associated LayoutRect object.
    • Or, if using layout classes directly, create a LayoutHost object, then create a LayoutView with the target page's width and height. Use the LayoutView object to create a LayoutRect.
  2. Set constraints for the table LayoutRect. You must fix at least one corner of the table. The position of other corners will then be calculated depending on the table content. You can also fix two corners of the table (e.g. by setting constraints for its top, left, and right sides), or all four. The value of the fixedSides parameter passed to the TableRenderer constructor must be chosen accordingly.
  3. Create a TableRenderer object, passing GcGraphics, LayoutRect, and other parameters to the constructor. The number of table rows and columns, as well as table padding settings, are passed to the constructor and cannot be changed later.
  4. If necessary, apply additional constraints to some column widths and row heights. The default minimum column width and minimum row height can be passed to the TableRenderer constructor.
  5. As needed, create CellStyle objects with text formats, padding settings, and other adjustments. Usually it is convenient to create a base cell style, then create other more specific styles based on it.
  6. Use the TableRenderer.AddCell() method to add table cells with the prepared cell styles and table data. Some cells can span multiple columns and rows. Cells using background or foreground styles might overlap multiple regular cells, for example, to display highlighted rows.
  7. If needed, call the TableRenderer.AddMissingCells() method to add empty cells to fill out any gaps in the table. Grid lines will not be drawn around empty cells otherwise.

When all constraints are specified and all cells added, if you're working with the LayoutHost object directly, just call the TableRenderer.Render() method. It draws the table on the GcGraphics object passed to the constructor.

Or, if you created the Surface and Visual objects, a few additional steps must be performed:

  1. Call the TableRenderer.ApplyCellConstraints() method to calculate the table layout.
  2. Attach the Draw delegate to the Visual object. In its code you need to assign GcGraphics.Transform to Visual.Layer.Surface.BaseTransform and then execute the TableRenderer.Render() method.
  3. Now you can draw the Surface with one or more tables and other visuals. Call the Surface.Render() method, passing the target GcGraphics object to it.
GrapeCity.Documents.Drawing.TableRenderer examples: