Document .Net - How to add Headers and Footers in PDF using C# or VB.Net

How to add Header and Footer in PDF file using C# or VB.Net

Often, we have documents in which it is necessary to make any edits. For example, change a header or a footer in PDF file!

This sample code will help you to add Header and Footer in an existing PDF file.

For example : We have the PDF file and we need to add some text in Header and Footer.

Header: "Shrek and Donkey"

// Create new header with formatted text. 
  HeaderFooter header = new HeaderFooter(dc, HeaderFooterType.HeaderDefault); 
  header.Content.Start.Insert("Shrek and Donkey", new CharacterFormat() { Size = 14.0, FontColor = Color.Brown });


Footer: "Fiona"

// Create new footer with formatted text. 
   HeaderFooter footer = new HeaderFooter(dc, HeaderFooterType.FooterDefault); 
   footer.Content.Start.Insert("Fiona.", new CharacterFormat() { Size = 14.0, FontColor = Color.Blue });

Here you may download the input PDF file and the output result.

Complete code

using System.IO;
using SautinSoft.Document;

namespace Sample
{
    class Sample
    {
        static void Main(string[] args)
        {
            AddHeaderFooter();
        }

        /// <summary>
        /// How to add a header and footer into PDF document.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/examples/add-header-and-footer-in-pdf-net-csharp-vb.php
        /// </remarks>
        static void AddHeaderFooter()
        {
            string inpFile = @"..\..\shrek.pdf";
            string outFile = "Shrek with header and footer.pdf";
            DocumentCore dc = DocumentCore.Load(inpFile);

            // Create new header with formatted text.
            HeaderFooter header = new HeaderFooter(dc, HeaderFooterType.HeaderDefault);
            header.Content.Start.Insert("Shrek and Donkey", new CharacterFormat() { Size = 14.0, FontColor = Color.Brown });
            foreach (Section s in dc.Sections)
            {
                s.HeadersFooters.Add(header.Clone(true));
            }

            // Create new footer with formatted text.
            HeaderFooter footer = new HeaderFooter(dc, HeaderFooterType.FooterDefault);
            footer.Content.Start.Insert("Fiona.", new CharacterFormat() { Size = 14.0, FontColor = Color.Blue });
            foreach (Section s in dc.Sections)
            {
                s.HeadersFooters.Add(footer.Clone(true));
            }

            dc.Save(outFile);

            // Open the PDF documents for demonstation purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(inpFile) { UseShellExecute = true });
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true });
        }
    }
}

Download.

        
            Imports System
Imports System.IO
Imports SautinSoft.Document

Module Sample
    Sub Main()
        AddHeaderFooter()
    End Sub

    ''' <summary>
    ''' How to add a header and footer into PDF document.
    ''' </summary>
    ''' <remarks>
    ''' Details: https://sautinsoft.com/products/document/examples/add-header-and-footer-in-pdf-net-csharp-vb.php
    ''' </remarks>
    Sub AddHeaderFooter()
        Dim inpFile As String = "..\shrek.pdf"
        Dim outFile As String = "Shrek with header and footer.pdf"
        Dim dc As DocumentCore = DocumentCore.Load(inpFile)

        ' Create new header with formatted text.
        Dim header As New HeaderFooter(dc, HeaderFooterType.HeaderDefault)
        header.Content.Start.Insert("Shrek and Donkey", New CharacterFormat() With {
                .Size = 14.0,
                .FontColor = Color.Brown
            })
        For Each s As Section In dc.Sections
            s.HeadersFooters.Add(header.Clone(True))
        Next s

        ' Create new footer with formatted text.
        Dim footer As New HeaderFooter(dc, HeaderFooterType.FooterDefault)
        footer.Content.Start.Insert("Fiona.", New CharacterFormat() With {
                .Size = 14.0,
                .FontColor = Color.Blue
            })
        For Each s As Section In dc.Sections
            s.HeadersFooters.Add(footer.Clone(True))
        Next s

        dc.Save(outFile)

        ' Open the PDF documents for demonstation purposes.
        System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(inpFile) With {.UseShellExecute = True})
        System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outFile) With {.UseShellExecute = True})
    End Sub
End Module

Download.

© SautinSoft 2002 - 2019