Calculating summary of records in a table is very important from the point of view of data representation. This blog is all about summarizing data in a RenderTable of C1PrintDocument.

Consider an example of an Account Book where it is mandatory to show the total of entries printed on a page such that every page displays a subtotal followed by a grand total on the last page in the document. The requirement seems to be a bit tricky, however, the implementation is quite simple :) Before moving further to the approach for the solution, lets take a look at our output.

Result

The implementation includes adding a Dictionary which calculates the progressive total in the PageAdded event of C1PrintDocument and displaying the resultant value in the last row of the page. Here is the code snippet for accomplishing the same:


private void Generate()
{
RenderTable rt = new RenderTable();
rt.Style.FontSize = 14;
rt.CellStyle.Spacing.All = new Unit(2, C1.C1Preview.UnitTypeEnum.Mm);
rt.Style.GridLines.All = new C1.C1Preview.LineDef(new Unit(0.3, UnitTypeEnum.Mm), Color.Black, DashStyle.Solid);
rt.Cols[0].Width = new Unit(2, C1.C1Preview.UnitTypeEnum.Cm);
rt.Cols[1].Width = new Unit(5, C1.C1Preview.UnitTypeEnum.Cm);
rt.Cols[2].Width = new Unit(2, C1.C1Preview.UnitTypeEnum.Cm);
rt.Cols[3].Width = new Unit(2, C1.C1Preview.UnitTypeEnum.Cm);
rt.Cols[4].Width = new Unit(3, C1.C1Preview.UnitTypeEnum.Cm);
// Header
int row = rt.Rows.Count;
rt.Cells[row, 0].Text = "No.";
rt.Cells[row, 1].Text = "Description";
rt.Cells[row, 2].Text = "Count";
rt.Cells[row, 3].Text = "Price";
rt.Cells[row, 4].Text = "Sum";
rt.Rows[row].Style.FontBold = true;
rt.Rows[row].Style.BackColor = Color.LightGray;
rt.RowGroups[row, 1].PageHeader = true;
// Rows
int total = 0;
for (int n = 1; n < 64; n++)
{
row = rt.Rows.Count;
rt.Cells[row, 0].Text = string.Format("{0}", n);
rt.Cells[row, 1].Text = string.Format("Name {0}", n);
rt.Cells[row, 2].Text = string.Format("{0}", n * 2);
rt.Cells[row, 3].Text = string.Format("{0}", 1000);
CellData cellData;
cellData.value = 1000 * n * 2;
rt.Cells[row, 4].Text = string.Format("{0}", cellData.value);
rt.Cells[row, 4].Tag = cellData;
total += 1000 * n * 2;
}
// Page footer
row = rt.Rows.Count;
rt.Rows[row].Style.BackColor = Color.LightGray;
rt.Cells[row, 1].Text = "PAGE TOTAL";
rt.Cells[row, 4].Text = "[((Dictionary<int, int>)Page.Document.UserData)[Page.Index]]";
rt.Cells[row, 4].Style.TextColor = Color.Red;
rt.RowGroups[row, 1].PageFooter = true;
// Report footer
row = rt.Rows.Count;
rt.Rows[row].Style.BackColor = Color.Gray;
rt.Cells[row, 1].Text = "GRAND TOTAL";
rt.Cells[row, 4].Text = string.Format("{0}", total);
rt.RowGroups[row, 1].Footer = TableFooterEnum.None;
_printDocument.UserData = new Dictionary<int, int>();
_printDocument.Body.Children.Clear();
_printDocument.PageLayout.PageSettings.Landscape = true;
_printDocument.PageLayout.PageSettings.PaperKind = System.Drawing.Printing.PaperKind.A4;
_printDocument.Body.Children.Add(rt);
_printDocument.Generate();
}
private void _printDocument_PageAdded(C1PrintDocument sender, PageEventArgs e)
{
int summ = 0;
foreach (RenderFragment fragment in e.Page.Fragments)
{
foreach (object child in fragment.Children)
{
if (child.GetType() == typeof(RenderTextFragment))
{
RenderText text = ((RenderTextFragment)child).RenderObject;
if (text.TableCell.Tag != null && text.TableCell.Tag.GetType() == typeof(CellData))
{
summ += ((CellData)text.TableCell.Tag).value;
}
}
}
}
((Dictionary<int, int>)e.Page.Document.UserData).Add(e.Page.Index, summ);
}


Download Sample