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

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

Often, we have documents in which it is necessary to make any edits. For example, remove old header and insert 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 remove 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 remove old 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(0);
         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 Sample
{
    class Sample
    {
        static void Main(string[] args)
        {
            ReplaceHeader();
        }

        /// <summary>
        /// Removes the old header/footer and inserts a new one into an existing PDF document.
        /// </summary>
        /// <remarks>
        /// Details: https://www.sautinsoft.com/products/document/examples/remove-header-and-footer-in-pdf-net-csharp-vb.php
        /// </remarks>
        static void ReplaceHeader()
        {
            string inpFile = @"..\..\somebody.pdf";
            string outFile = "With new 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 });
            // Add 10 mm from Top before new header.
            (header.Blocks[0] as Paragraph).ParagraphFormat.SpaceBefore = LengthUnitConverter.Convert(10, LengthUnit.Millimeter, LengthUnit.Point);


            foreach (Section s in dc.Sections)
            {
                // Find the first paragraph (Let's assume that it's the header) and remove it.
                if (s.Blocks.Count > 0)
                    s.Blocks.RemoveAt(0);
                // Insert the new header into the each section.
                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()
        ReplaceHeader()
    End Sub

    ''' <summary>
    ''' Removes the old header/footer and inserts a new one into an existing PDF document.
    ''' </summary>
    ''' <remarks>
    ''' Details: https://www.sautinsoft.com/products/document/examples/remove-header-and-footer-in-pdf-net-csharp-vb.php
    ''' </remarks>
    Sub ReplaceHeader()
        Dim inpFile As String = "..\somebody.pdf"
        Dim outFile As String = "With new 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
            })
        ' Add 10 mm from Top before new header.
        TryCast(header.Blocks(0), Paragraph).ParagraphFormat.SpaceBefore = LengthUnitConverter.Convert(10, LengthUnit.Millimeter, LengthUnit.Point)


        For Each s As Section In dc.Sections
            ' Find the first paragraph (Let's assume that it's the header) and remove it.
            If s.Blocks.Count > 0 Then
                s.Blocks.RemoveAt(0)
            End If
            ' Insert the new header into the each section.
            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.


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.

© SautinSoft 2002 - 2019