Articles in this section
Category / Section

How to add different header and footer in a PDF document

10 mins read

The Syncfusion Essential® PDF is a comprehensive, high-performance .NET PDF library that enables you to create, read, and edit PDF documents. This article explains how to add different headers and footers to individual pages of a PDF document.

Steps to add different header and footer in a PDF programmatically

  1. Create a new Window Forms application project
    wf-sample-creation

  2.  Install the Syncfusion.Pdf.WinForms NuGet package as a reference in your console application from Nuget.org.
    NuGet-Package

  3.  Include the following namespaces in the Form1.cs file.
    using Syncfusion.Pdf;
    using Syncfusion.Pdf.Graphics;
  4. Add a new button in Form1.Designer.cs to create a PDF document with custom headers and footers as follows.
    private Button btnCreate;
    private Label label;
      
    private void InitializeComponent()
    {
      btnCreate = new Button();
      label = new Label();
      
      //Label
      label.Location = new System.Drawing.Point(0, 40);
      label.Size = new System.Drawing.Size(426, 35);
      label.Text = "Click the button to generate PDF file by Essential PDF";
      label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
      
      //Button
      btnCreate.Location = new System.Drawing.Point(180, 110);
      btnCreate.Size = new System.Drawing.Size(85, 26);
      btnCreate.Text = "Create PDF";
      btnCreate.Click += new EventHandler(btnCreate_Click); 
                                   
      //Create PDF
      ClientSize = new System.Drawing.Size(450, 150);
      Controls.Add(label);
      Controls.Add(btnCreate);
      Text = "Create PDF";
    }
    
  5. Create the btnCreate_Click event handler and incorporate the following code to generate a PDF document with custom headers and footers.
                 // Create a new PDF document
                 PdfDocument document = new PdfDocument();
    
                // Paragraph content to display on each page
                string paragraph = @"The page tree serves as the root of the document. In the simplest case, it is just a list of the pages in the document. Each page is defined as an independent entity with metadata (e.g., page dimensions) and a reference to its resources and content, which are defined separately. Together, the page tree and page objects create the “paper” that composes the document.
    Resources are objects that are required to render a page. For example, a single font is typically used across several pages, so storing the font information in an external resource is much more efficient. A content object defines the text and graphics that actually show up on the page. Together, content objects and resources define the appearance of an individual page.
    Finally, the document’s catalog tells applications where to start reading the document. Often, this is just a pointer to the root page tree.";
    
                // ===== Section 1 =====
                PdfSection section1 = document.Sections.Add();
                // Set section margin if needed
                section1.PageSettings.Margins.Bottom = 30;
                AddSectionWithHeaderFooter(section1, "Header - Section 1", "Footer - Section 1", 1, paragraph);
    
                // ===== Section 2 =====
                PdfSection section2 = document.Sections.Add();
                // Set section margin if needed
                section2.PageSettings.Margins.Bottom = 20;
                AddSectionWithHeaderFooter(section2, "Header - Section 2", "Footer - Section 2", 1, paragraph);
    
                // Save and close the document
                document.Save("Output.pdf");
                // Close and dispose of the PDF document
                document.Close(true);
            }
    
            static void AddSectionWithHeaderFooter(PdfSection section, string headerText, string footerText, int numberOfPages, string pageContent)
            {
                // Define fonts and brush
                PdfFont headerFooterFont = new PdfStandardFont(PdfFontFamily.Helvetica, 10, PdfFontStyle.Bold);
                PdfFont bodyFont = new PdfStandardFont(PdfFontFamily.Helvetica, 11);
                PdfBrush brush = PdfBrushes.Black;
    
                for (int i = 0; i < numberOfPages; i++)
                {
                    // Add a new page
                    PdfPage page = section.Pages.Add();
    
                    // Draw Header
                    page.Graphics.DrawString(headerText, headerFooterFont, brush, new PointF(10, 10));
    
                    // Draw Footer
                    float footerY = page.GetClientSize().Height - 20;
                    page.Graphics.DrawString(footerText, headerFooterFont, brush, new PointF(10, footerY));
    
                    // Draw the paragraph content using PdfTextElement for proper layout and wrapping
                    RectangleF contentBounds = new RectangleF(20, 40, page.GetClientSize().Width - 40, page.GetClientSize().Height - 80);
                    PdfTextElement textElement = new PdfTextElement(pageContent, bodyFont, brush)
                    {
                        StringFormat = new PdfStringFormat()
                        {
                            Alignment = PdfTextAlignment.Left,
                            LineSpacing = 5f
                        }
                    };
                    // Draw the paragraph text within the defined rectangle area on the page
                    textElement.Draw(page, contentBounds);
                }
            }

    A complete working sample is available for download from GitHub.

    By executing the program, you will generate the following PDF document.


    Conclusion

    I hope you enjoyed learning about how to add different header and footer in a PDF document.

    You can refer to our Winforms PDF feature tour page to know about its other groundbreaking feature representations. You can also explore our documentation 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 check out 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 forumsDirect-Trac, 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
Please  to leave a comment
Access denied
Access denied