Is there a way to display a table always at the bottom of the page?


    Yes, place the table in the document footer. Lets create 5-pages PDF document. And place five unique tables, each at the bottom of own page.

Actually, you can change the format (in the Save method) and save the document as DOCX, RTF or HTML.

Each table will have random number of rows from 1 to 20.

Complete code

using SautinSoft.Document;
using SautinSoft.Document.Drawing;
using SautinSoft.Document.Tables;
using System;

// Get your free 30-day key here:
// https://sautinsoft.com/start-for-free/
DisplayTable();

/// <summary>
/// How to display a table always at the bottom of the page.
/// </summary>
/// <remarks>
/// Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/from-customers-display-table-always-at-the-bottom-of-the-page-in-pdf-net-csharp-vb.php
/// </remarks>
static void DisplayTable()
{
    // Is there a way to display a table always at the bottom of the page?
    // Yes, place the table in the document footer.

    // Create 5-pages document and place an unique table
    // at the bottom of each page.
    var dc = new DocumentCore();

    string resultPath = "Result.pdf";
    string[] pagesText = { "March", "April", "May", "June", "July" };

    for (int page = 0; page < pagesText.Length; page++)
    {
        var s = new Section(dc);
        dc.Sections.Add(s);
        // Write some text content
        var p = new Paragraph(dc);
        p.ParagraphFormat.Alignment = HorizontalAlignment.Center;
        var cf = new CharacterFormat() { Size = 100f };
        p.Inlines.Add(new Run(dc, pagesText[page], cf));
        s.Blocks.Add(p);

        // Place the table in the document footer.                
        AddTableToFooter(dc, s);
    }

    dc.Save(resultPath);
    // Open the result for demonstration purposes.
    System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(resultPath) { UseShellExecute = true });
}
static void AddTableToFooter(DocumentCore dc, Section s)
{
    Random rand = new Random();
    Paragraph p;

    string[] tableText = { "Item", "Quantity", "Price" };
    Table t = new Table(dc);
    t.TableFormat.PreferredWidth = new TableWidth(100, TableWidthUnit.Percentage);
    t.TableFormat.Alignment = HorizontalAlignment.Center;

    // Table header
    var rowHdr = new TableRow(dc);
    foreach (var cellText in tableText)
    {
        var cellHdr = new TableCell(dc);
        cellHdr.CellFormat.Borders.SetBorders(MultipleBorderTypes.Outside, BorderStyle.Single, Color.Black, 0.5f);
        cellHdr.CellFormat.BackgroundColor = new Color(rand.Next(Int32.MaxValue));
        cellHdr.CellFormat.PreferredWidth = new TableWidth(100.0f / 3.0f, TableWidthUnit.Percentage);
        p = new Paragraph(dc, cellText);
        p.ParagraphFormat.Alignment = HorizontalAlignment.Center;
        cellHdr.Blocks.Add(p);
        rowHdr.Cells.Add(cellHdr);
    }
    t.Rows.Add(rowHdr);
    // Table body
    int rowCount = rand.Next(1, 20);
    for (int r = 0; r < rowCount; r++)
    {
        var row = new TableRow(dc);
        foreach (var cellText in tableText)
        {
            var cell = new TableCell(dc);
            cell.CellFormat.Borders.SetBorders(MultipleBorderTypes.Outside, BorderStyle.Single, Color.Black, 0.5f);
            cell.CellFormat.BackgroundColor = Color.White;
            cell.CellFormat.PreferredWidth = new TableWidth(100.0f / 3.0f, TableWidthUnit.Percentage);
            p = new Paragraph(dc, $"{rand.Next(100)}");
            p.ParagraphFormat.Alignment = HorizontalAlignment.Center;
            cell.Blocks.Add(p);
            row.Cells.Add(cell);
        }
        t.Rows.Add(row);
    }
    // Move table to page footer
    HeaderFooter footer = new HeaderFooter(dc, HeaderFooterType.FooterDefault);
    s.HeadersFooters.Add(footer);
    footer.Blocks.Add(t);
}

Download

Option Infer On
Imports SautinSoft.Document
Imports SautinSoft.Document.Drawing
Imports SautinSoft.Document.Tables

Namespace Example
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			' Get your free 30-day key here:   
			' https://sautinsoft.com/start-for-free/
			DisplayTable()
		End Sub

		''' <summary>
		''' How to display a table always at the bottom of the page.
		''' </summary>
		''' <remarks>
		''' Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/from-customers-display-table-always-at-the-bottom-of-the-page-in-pdf-net-csharp-vb.php
		''' </remarks>
		Private Shared Sub DisplayTable()
			' Is there a way to display a table always at the bottom of the page?
			' Yes, place the table in the document footer.

			' Create 5-pages document and place an unique table
			' at the bottom of each page.
			Dim dc = New DocumentCore()

			Dim resultPath As String = "Result.pdf"
			Dim pagesText() As String = {"March", "April", "May", "June", "July"}

			For page As Integer = 0 To pagesText.Length - 1
				Dim s = New Section(dc)
				dc.Sections.Add(s)
				' Write some text content
				Dim p = New Paragraph(dc)
				p.ParagraphFormat.Alignment = HorizontalAlignment.Center
				Dim cf = New CharacterFormat() With {.Size = 100.0F
				}
				p.Inlines.Add(New Run(dc, pagesText(page), cf))
				s.Blocks.Add(p)

				' Place the table in the document footer.                
				AddTableToFooter(dc, s)
			Next page

			dc.Save(resultPath)
			' Open the result for demonstration purposes.
			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(resultPath) With {.UseShellExecute = True})
		End Sub
		Private Shared Sub AddTableToFooter(ByVal dc As DocumentCore, ByVal s As Section)
			Dim rand As New Random()
			Dim p As Paragraph

			Dim tableText() As String = {"Item", "Quantity", "Price"}
			Dim t As New Table(dc)
			t.TableFormat.PreferredWidth = New TableWidth(100, TableWidthUnit.Percentage)
			t.TableFormat.Alignment = HorizontalAlignment.Center

			' Table header
			Dim rowHdr = New TableRow(dc)
			For Each cellText In tableText
				Dim cellHdr = New TableCell(dc)
				cellHdr.CellFormat.Borders.SetBorders(MultipleBorderTypes.Outside, BorderStyle.Single, Color.Black, 0.5F)
				cellHdr.CellFormat.BackgroundColor = New Color(rand.Next(Int32.MaxValue))
				cellHdr.CellFormat.PreferredWidth = New TableWidth(100.0F / 3.0F, TableWidthUnit.Percentage)
				p = New Paragraph(dc, cellText)
				p.ParagraphFormat.Alignment = HorizontalAlignment.Center
				cellHdr.Blocks.Add(p)
				rowHdr.Cells.Add(cellHdr)
			Next cellText
			t.Rows.Add(rowHdr)
			' Table body
			Dim rowCount As Integer = rand.Next(1, 20)
			For r As Integer = 0 To rowCount - 1
				Dim row = New TableRow(dc)
				For Each cellText In tableText
					Dim cell = New TableCell(dc)
					cell.CellFormat.Borders.SetBorders(MultipleBorderTypes.Outside, BorderStyle.Single, Color.Black, 0.5F)
					cell.CellFormat.BackgroundColor = Color.White
					cell.CellFormat.PreferredWidth = New TableWidth(100.0F / 3.0F, TableWidthUnit.Percentage)
					p = New Paragraph(dc, $"{rand.Next(100)}")
					p.ParagraphFormat.Alignment = HorizontalAlignment.Center
					cell.Blocks.Add(p)
					row.Cells.Add(cell)
				Next cellText
				t.Rows.Add(row)
			Next r
			' Move table to page footer
			Dim footer As New HeaderFooter(dc, HeaderFooterType.FooterDefault)
			s.HeadersFooters.Add(footer)
			footer.Blocks.Add(t)
		End Sub
	End Class
End Namespace

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.