Modify a header in Word/DOCX document in C# and .NET

  1. Add SautinSoft.Document from Nuget.
  2. Load a PDF document.
  3. Iterate by sections and remove the first paragraph.
  4. Save the document.

The headers and footers are used to add repetitive information to each page of the document. They may contain elements such as page numbers, dates, chapter titles, logos, and contact information. This helps to improve document navigation and provides important information on each page.
In SautinSoft PDF .Net and Document.Net can easily add headers and footers to PDF or DOCX documents.

Below is an example code for adding a header and footer to a DOCX document using PDF .Net:

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)
        {
            // Get your free 30-day key here:   
            // https://sautinsoft.com/start-for-free/

            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/help/net/developer-guide/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 demonstration 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
    ''' Get your free 30-day key here:   
    ''' https://sautinsoft.com/start-for-free/
    ''' <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/help/net/developer-guide/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 demonstration 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.