Add Footer only to last page of the word document using open XML sdk with C#
<p>I am new to OPEN XML SDK 2.5 using C#. I created a doc with a table in it and the contents in the table may go more than 500 rows. So the document may have multiple pages. I have added header and footer successfully. But What I want is that I need to add footer only to the last page in the document. How to do this. Please find attached the sample code. Hope my question is clear</p>

<pre><code>    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

namespace ConsoleApplication8
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = @'C:\temp\HeaderFooterDocument.docx';
            CreateTable(fileName);
        }

        public static void ApplyHeader(WordprocessingDocument doc)
        {
            // Get the main document part.
            MainDocumentPart mainDocPart = doc.MainDocumentPart;

            HeaderPart headerPart1 = mainDocPart.AddNewPart&lt;HeaderPart&gt;('r97');



            Header header1 = new Header();

            Paragraph paragraph1 = new Paragraph() { };



            Run run1 = new Run();
            Text text1 = new Text();
            text1.Text = 'Header stuff';

            run1.Append(text1);

            Text text3 = new Text();
            text3.Text = '\rHeader stuff';

            run1.Append(text3);

            paragraph1.Append(run1);


            header1.Append(paragraph1);

            headerPart1.Header = header1;



            SectionProperties sectionProperties1 = mainDocPart.Document.Body.Descendants&lt;SectionProperties&gt;().FirstOrDefault();
            if (sectionProperties1 == null)
            {
                sectionProperties1 = new SectionProperties() { };
                mainDocPart.Document.Body.Append(sectionProperties1);
            }
            HeaderReference headerReference1 = new HeaderReference() { Type = HeaderFooterValues.Default, Id = 'r97' };


            sectionProperties1.InsertAt(headerReference1, 0);

        }
        public static void ApplyFooter(WordprocessingDocument doc)
        {
            // Get the main document part.
            MainDocumentPart mainDocPart = doc.MainDocumentPart;

            FooterPart footerPart1 = mainDocPart.AddNewPart&lt;FooterPart&gt;('r98');



            Footer footer1 = new Footer();

            Paragraph paragraph1 = new Paragraph() { };



            Run run1 = new Run();
            Text text1 = new Text();
            text1.Text = 'Footer stuff';

            run1.Append(text1);

            paragraph1.Append(run1);


            footer1.Append(paragraph1);

            footerPart1.Footer = footer1;



            SectionProperties sectionProperties1 = mainDocPart.Document.Body.Descendants&lt;SectionProperties&gt;().FirstOrDefault();
            if (sectionProperties1 == null)
            {
                sectionProperties1 = new SectionProperties() { };
                mainDocPart.Document.Body.Append(sectionProperties1);
            }
            FooterReference footerReference1 = new FooterReference() { Type = DocumentFormat.OpenXml.Wordprocessing.HeaderFooterValues.Default, Id = 'r98' };


            sectionProperties1.InsertAt(footerReference1, 0);

        }
        // Insert a table into a word processing document.
        public static void CreateTable(string fileName)
        {
            // Use the file name and path passed in as an argument 
            // to open an existing Word 2007 document.

            using (WordprocessingDocument wordDocument =
                    WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document))
            {

                // Add a main document part. 
                MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();

                // Create the document structure and add some text.
                mainPart.Document = new Document();
                Body body = mainPart.Document.AppendChild(new Body());
                Paragraph para = body.AppendChild(new Paragraph());
                Run run = para.AppendChild(new Run());
                run.AppendChild(new Text('Create text in body - CreateWordprocessingDocument'));
                ApplyHeader(wordDocument);

                ApplyFooter(wordDocument);
                // Create an empty table.
                Table table = new Table();

                // Create a TableProperties object and specify its border information.
                TableProperties tblProp = new TableProperties(
                    new TableBorders(
                        new TopBorder()
                        {
                            Val =
                            new EnumValue&lt;BorderValues&gt;(BorderValues.None),
                            Size = 24
                        },
                        new BottomBorder()
                        {
                            Val =
                            new EnumValue&lt;BorderValues&gt;(BorderValues.None),
                            Size = 24
                        },
                        new LeftBorder()
                        {
                            Val =
                            new EnumValue&lt;BorderValues&gt;(BorderValues.None),
                            Size = 24
                        },
                        new RightBorder()
                        {
                            Val =
                            new EnumValue&lt;BorderValues&gt;(BorderValues.None),
                            Size = 24
                        },
                        new InsideHorizontalBorder()
                        {
                            Val =
                            new EnumValue&lt;BorderValues&gt;(BorderValues.None),
                            Size = 24
                        },
                        new InsideVerticalBorder()
                        {
                            Val =
                            new EnumValue&lt;BorderValues&gt;(BorderValues.None),
                            Size = 24
                        }
                    )
                );

                // Append the TableProperties object to the empty table.
                table.AppendChild&lt;TableProperties&gt;(tblProp);

                // Create a row.
                TableRow tr = new TableRow();

                // Create a cell.
                TableCell tc1 = new TableCell();

                // Specify the width property of the table cell.
                tc1.Append(new TableCellProperties(
                    new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = '2400' }));

                // Specify the table cell content.
                tc1.Append(new Paragraph(new Run(new Text('some text'))));

                // Append the table cell to the table row.
                tr.Append(tc1);

                // Create a second table cell by copying the OuterXml value of the first table cell.
                TableCell tc2 = new TableCell(tc1.OuterXml);

                // Append the table cell to the table row.
                tr.Append(tc2);

                // Append the table row to the table.
                table.Append(tr);

                for (int rows = 1; rows &lt; 50; rows++)
                {
                    TableRow tr2 = new TableRow();
                    for (int index2 = 0; index2 &lt; 2; index2++)
                    {
                        TableCell newCells = new TableCell();
                        newCells.Append(new Paragraph(new Run(new Text('row' + rows + 'column' + index2))));
                        tr2.Append(newCells);
                    }
                    table.Append(tr2);
                    // Append the table to the document.

                }
                wordDocument.MainDocumentPart.Document.Body.Append(table);
            }
        }
    }
}
</code></pre>