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

How to edit Header in PDF file using C# or VB.Net

Often, we have documents in which it is necessary to make any edits. For example, edit old header and add new header instead!

In PDF refrence there is no concept of header / footer.
When loading a document into the DocumentCore - header / footer are written to the beginning of the block collection of partitions (Sections.Blocks)
This example shows how to edit a header from an existing PDF.

This sample code will help you to change Header in an existing PDF file.

For example : We have the PDF file and we need to add new Header: "Created : 20 January 2019" and insert new Header: " Modified : 1 April 2020"

So. Let's do it:

HeaderFooter header = new HeaderFooter(dc, HeaderFooterType.HeaderDefault);
header.Content.Start.Insert("Modified : 1 April 2020", new CharacterFormat() { Size = 14.0, FontColor = Color.DarkGreen });
      foreach (Section s in dc.Sections)
       {
         if (s.Blocks.Count > 0)
             s.Blocks.RemoveAt(1);
         s.HeadersFooters.Add(header.Clone(true));
       }

 

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

Complete code

using System.IO;
using SautinSoft.Document;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            ChangeHeader();
        }
        /// <summary>
        /// How to edit Header and Footer in PDF file
        /// </summary>
        /// <remarks>
        /// Details: https://www.sautinsoft.com/products/document/examples/edit-header-and-footer-in-pdf-net-csharp-vb.php
        /// </remarks>
        static void ChangeHeader()
        {
            string inpFile = @"..\..\somebody.pdf";
            string outFile = "With changed header.pdf";
            DocumentCore dc = DocumentCore.Load(inpFile);

            // Create new header with formatted text.
            HeaderFooter header = new HeaderFooter(dc, HeaderFooterType.HeaderDefault);
            header.Content.Start.Insert("Modified : 1 April 2020", new CharacterFormat() { Size = 14.0, FontColor = Color.DarkGreen });
            foreach (Section s in dc.Sections)
            {
                if (s.Blocks.Count > 0)
                    s.Blocks.RemoveAt(1);
                s.HeadersFooters.Add(header.Clone(true));
            }
            dc.Save(outFile);

            // Open the results 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()
        ChangeHeader()
    End Sub

    ''' <summary>
    ''' How to edit Header and Footer in PDF file
    ''' </summary>
    ''' <remarks>
    ''' Details: https://www.sautinsoft.com/products/document/examples/edit-header-and-footer-in-pdf-net-csharp-vb.php
    ''' </remarks>
    Sub ChangeHeader()
        Dim inpFile As String = "..\somebody.pdf"
        Dim outFile As String = "With changed header.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("Modified : 1 April 2020", New CharacterFormat() With {
                .Size = 14.0,
                .FontColor = Color.DarkGreen
            })
        For Each s As Section In dc.Sections
            If s.Blocks.Count > 0 Then
                s.Blocks.RemoveAt(1)
            End If
            s.HeadersFooters.Add(header.Clone(True))
        Next s
        dc.Save(outFile)

        ' Open the results 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