Articles in this section
Category / Section

How to create multi page report with sum in each page footer of PDF document using C#?

9 mins read

The Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can create a multi-page report with a sum in each page's footer of a PDF document using C#.

Steps to create a multi-page report with a sum in each page’s footer of a PDF document

  1. Create a new Windows Forms application project.

Create Windows Forms application project

 

  1. Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from NuGet.org.

NuGet package reference

  1. Include the following namespaces in the Form1.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Grid;
using System.Drawing;

 

  1. Add the following code in the button1_Click to create a multi-page report with a sum in each page’s footer of a PDF document.

C#

private float sumOfCell2 = 0;
private float sumOfCell3 = 0;
private float sumOfCell4 = 0;
private float sumOfCell5 = 0;
private float sumOfCell6 = 0;
private float sumOfCell7 = 0;
private float sumOfCell8 = 0;
private float sumOfCell9 = 0;
 
//Create a PDF grid. 
PdfGrid pdfGrid;
 
//Create a PDF page. 
PdfPage previousPage;
 
 
private void button1_Click(object sender, EventArgs e)
{
    //Create a new PDF document.
    PdfDocument pdfDocument = new PdfDocument();
 
    //Add a page. 
    PdfPage pdfPage = pdfDocument.Pages.Add();
    previousPage = pdfPage;
 
    //Create a page added event to get the paginated page. 
    pdfDocument.Pages.PageAdded += Pages_PageAdded; ;
 
    //Create a new PdfGrid.
    pdfGrid = new PdfGrid();
 
    //Get a data table. 
    DataTable dataTable = CreateDataTable();
 
    //Assign a data source.
    pdfGrid.DataSource = dataTable;
 
    //Set a string format to align the header content in the center. 
    for (int i = 0; i < pdfGrid.Columns.Count; i++)
    {
        pdfGrid.Columns[i].Format = new PdfStringFormat(PdfTextAlignment.Center);
    }
 
    //Set the column width. 
    pdfGrid.Columns[1].Width = 100;
 
    //Call the BeginCellLayout event handler to apply the style and calculate the total. 
    pdfGrid.BeginCellLayout += PdfGrid_BeginCellLayout; ;
 
    //Create a grid style to apply to pad for the entire grid. 
    PdfGridStyle gridStyle = new PdfGridStyle();
    gridStyle.CellPadding = new PdfPaddings(3, 3, 3, 3);
 
    //Apply the style to the grid. 
    pdfGrid.Style = gridStyle;
 
    //Draw the PdfGrid.
    PdfLayoutResult result = pdfGrid.Draw(pdfPage, new RectangleF(0, 0, pdfPage.GetClientSize().Width, pdfPage.GetClientSize().Height - 50));
 
    List<float> sumOfCells = new List<float>();
    sumOfCells.Add(sumOfCell2);
    sumOfCells.Add(sumOfCell3);
    sumOfCells.Add(sumOfCell4);
    sumOfCells.Add(sumOfCell5);
    sumOfCells.Add(sumOfCell6);
    sumOfCells.Add(sumOfCell7);
    sumOfCells.Add(sumOfCell8);
    sumOfCells.Add(sumOfCell9);
 
    //Draw the footer table. 
    DrawFooter(previousPage, pdfGrid, sumOfCells);
 
    //Save the document.
    pdfDocument.Save("Output.pdf");
 
    //Close the document.
    pdfDocument.Close(true);
 
    Process.Start("Output.pdf");
}

 

Helper methods and evet handers:

private void PdfGrid_BeginCellLayout(object sender, PdfGridBeginCellLayoutEventArgs args)
{
    if (!args.IsHeaderRow)
    {
        if (args.CellIndex == 2)
            sumOfCell2 += float.Parse(args.Value);
        else if (args.CellIndex == 3)
            sumOfCell3 += float.Parse(args.Value);
        else if (args.CellIndex == 4)
            sumOfCell4 += float.Parse(args.Value);
        else if (args.CellIndex == 5)
            sumOfCell5 += float.Parse(args.Value);
        else if (args.CellIndex == 6)
            sumOfCell6 += float.Parse(args.Value);
        else if (args.CellIndex == 7)
            sumOfCell7 += float.Parse(args.Value);
        else if (args.CellIndex == 8)
            sumOfCell8 += float.Parse(args.Value);
        else if (args.CellIndex == 9)
            sumOfCell9 += float.Parse(args.Value);
    }
    //Create a PDF grid. 
    PdfGrid grid = sender as PdfGrid;
 
    //If it is not a header row, then apply the string format and grid style. 
    if (!args.IsHeaderRow)
    {
        if (args.RowIndex != grid.Rows.Count - 1)
        {
            grid.Rows[args.RowIndex].Cells[args.CellIndex].Style.Borders.Top = PdfPens.Transparent;
            grid.Rows[args.RowIndex].Cells[args.CellIndex].Style.Borders.Bottom = PdfPens.Transparent;
        }
 
        else if (args.RowIndex == grid.Rows.Count - 1)
        {
            grid.Rows[args.RowIndex].Cells[args.CellIndex].Style.Borders.Top = PdfPens.Transparent;
        }
    }
 
    if (!args.IsHeaderRow)
    {
        if (args.CellIndex == 0 || args.CellIndex == 1)
        {
            grid.Rows[args.RowIndex].Cells[args.CellIndex].StringFormat = new PdfStringFormat(PdfTextAlignment.Left);
        }
        else if (args.CellIndex != 0 && args.CellIndex != 1)
        {
            grid.Rows[args.RowIndex].Cells[args.CellIndex].StringFormat = new PdfStringFormat(PdfTextAlignment.Right);
        }
    }
}
 
private void Pages_PageAdded(object sender, PageAddedEventArgs args)
{
    //Add the calculated cell values to the list. 
    List<float> sumOfCells = new List<float>();
    sumOfCells.Add(sumOfCell2);
    sumOfCells.Add(sumOfCell3);
    sumOfCells.Add(sumOfCell4);
    sumOfCells.Add(sumOfCell5);
    sumOfCells.Add(sumOfCell6);
    sumOfCells.Add(sumOfCell7);
    sumOfCells.Add(sumOfCell8);
    sumOfCells.Add(sumOfCell9);
 
    //Draw the footer table. 
    DrawFooter(previousPage, pdfGrid, sumOfCells);
 
    //Assign the current page.
    previousPage = args.Page;
}
 
/// <summary>
/// Draw a table in the document footer. 
/// </summary>
/// <param name="page"></param>
/// <param name="pdfGrid"></param>
/// <param name="sumOfCells"></param>
private void DrawFooter(PdfPage page, PdfGrid pdfGrid, List<float> sumOfCells)
{
    //Create a PDF template.
    PdfTemplate template = new PdfTemplate(page.GetClientSize().Width, 80);
 
    //Create a table in the footer. 
    PdfGrid footerGrid = new PdfGrid();
 
    //Get a DataTable.
    DataTable dataTable = CreateFooterDataTable(sumOfCells);
 
    //Assign a data source.
    footerGrid.DataSource = dataTable;
 
    //Assign a column with the same existing grid width. 
    footerGrid.Columns[0].Width = pdfGrid.Columns[0].Width + pdfGrid.Columns[1].Width;
 
    ////Create a grid style to apply to pad for the entire grid. 
    PdfGridStyle gridStyle = new PdfGridStyle();
    gridStyle.CellPadding = new PdfPaddings(3, 3, 3, 3);
 
    //Apply the style to the grid. 
    footerGrid.Style = gridStyle;
 
    //Call the BeginCellLayout event handler to apply the style and string format. 
    footerGrid.BeginCellLayout += FooterGrid_BeginCellLayout; ;
 
    //Draw a table in the template graphics. 
    footerGrid.Draw(template.Graphics, new RectangleF(0, 0, page.GetClientSize().Width, 80));
 
    //Draw the template on the page graphics of the document.
    page.Graphics.DrawPdfTemplate(template, new PointF(0, page.GetClientSize().Height - 80), new SizeF(page.GetClientSize().Width, 80));
 
}
 
private void FooterGrid_BeginCellLayout(object sender, PdfGridBeginCellLayoutEventArgs args)
{
    //Hide the header in the table. 
    if (args.IsHeaderRow)
    {
        args.Skip = true;
    }
 
    //Create a PDF grid 
    PdfGrid grid = sender as PdfGrid;
 
    //If it is not a header row, then apply the cell style and string format. 
    if (!args.IsHeaderRow)
    {
        //create and customize the string formats.
        grid.Rows[args.RowIndex].Cells[args.CellIndex].StringFormat = new PdfStringFormat(PdfTextAlignment.Right);
 
        if (args.CellIndex != grid.Rows[0].Cells.Count - 1)
        {
            //Set transparent to the respective borders.
            grid.Rows[0].Cells[args.CellIndex].Style.Borders.Bottom = PdfPens.Transparent;
            grid.Rows[0].Cells[args.CellIndex].Style.Borders.Right = PdfPens.Transparent;
 
            grid.Rows[1].Cells[args.CellIndex].Style.Borders.Top = PdfPens.Transparent;
            grid.Rows[1].Cells[args.CellIndex].Style.Borders.Right = PdfPens.Transparent;
        }
        else
        {
            grid.Rows[0].Cells[args.CellIndex].Style.Borders.Bottom = PdfPens.Transparent;
            grid.Rows[1].Cells[args.CellIndex].Style.Borders.Top = PdfPens.Transparent;
        }
    }
}
 
public DataTable CreateDataTable()
{
    //Create a DataTable.
    DataTable dataTable = new DataTable();
 
    //Add columns to the DataTable.
    dataTable.Columns.Add("OrderID");
    dataTable.Columns.Add("ShipName");
    dataTable.Columns.Add("Volume");
    dataTable.Columns.Add("Quantity");
    dataTable.Columns.Add("Amount");
    dataTable.Columns.Add("Amount1");
    dataTable.Columns.Add("Amount2");
    dataTable.Columns.Add("Amount3");
    dataTable.Columns.Add("Amount4");
    dataTable.Columns.Add("Amount5");
 
    //Add rows to the DataTable.
    dataTable.Rows.Add(new object[] { "204.009", "Northwind Tradersd", "0.00", "8934.00", "9056.009", "10070.01", "19357.004", "24761.03", "0.00", "5404.00" });
 
    for (int i = 0; i < 15; i++)
    {
        dataTable.Rows.Add(new object[] { "204.009", "Queso Cabrales", "100.75", "8934.00", "9056.00", "5404.00", "100.75", "24761.00", "798.04", "5404.00" });
        dataTable.Rows.Add(new object[] { "220.1", "Singaporean Hokkien Fried Mee", "00.00", "0.00", "345.00", "0.74", "0.75", "24761.00", "94.05", "98.01" });
        dataTable.Rows.Add(new object[] { "231", "Mozzarella di Giovanniu", "50.00", "534.00", "8415.00", "90.00", "94.01", "0.00", "94.00", "0.10" });
    }
 
    return dataTable;
}
 
public DataTable CreateFooterDataTable(List<float> sumOfCells)
{
    //Create a DataTable.
    DataTable dataTable = new DataTable();
 
    //Add columns to the DataTable.
    for (int i = 0; i < 9; i++)
    {
        dataTable.Columns.Add();
    }
 
    //Add rows to the DataTable.
    dataTable.Rows.Add(new object[] { "Hanari Carne", "189081.19", "10949.75", "45880.24", "50306.60", "312013.78", "189739.24", "133186.24", "10911.70" });
    dataTable.Rows.Add(new object[] { "Sum of cells", sumOfCells[0], sumOfCells[1], sumOfCells[2], sumOfCells[3], sumOfCells[4], sumOfCells[5], sumOfCells[6], sumOfCells[7] });
 
    return dataTable;
}

 

A complete working sample can be download from CreateTableSample.zip.

 

By executing the program, you will get the output document as follows,

Output document screenshot

 

Take a moment to peruse the documentation. You can find options like create a table using the PdfGrid and PdfLightTable, Paginating table, rows, column, and table customization. Also, the features like add header and footer in PDF document.

 

Refer to this link to explore a rich set of Syncfusion Essential® PDF features.

 

Note:

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.

 

Also see:

Add header and footer to PDF document

Create a table in a PDF document

Create a table in a PDF file from DataTable

 

 

Conclusion:

I hope you enjoyed learning about how to create multi page report with sum in each page footer of PDF document using C#.

 

You can refer to our Flutter PDF feature tour page to learn about its other groundbreaking features and documentation, and how to quickly get started with configuration specifications. You can also explore our Flutter PDF Flutter PDF examples to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to explore our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied