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)
{
// Get your free 30-day key here:
// https://sautinsoft.com/start-for-free/
ChangeHeaderAndFooter();
}
/// <summary>
/// How to edit Header and Footer in PDF file
/// </summary>
/// <remarks>
/// Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/edit-header-and-footer-in-pdf-net-csharp-vb.php
/// </remarks>
static void ChangeHeaderAndFooter()
{
string inpFile = @"..\..\..\somebody.pdf";
string outFile = "With changed 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("Modified: 1 April 2020", new CharacterFormat() { Size = 14.0, FontColor = Color.DarkGreen });
// Create the footer with orange text, with font name Elephant and size of 50 pt.
HeaderFooter footer = new HeaderFooter(dc, HeaderFooterType.FooterDefault);
Paragraph p = new Paragraph(dc, new Run(dc, "Last modified: 1st June 2021",
new CharacterFormat() { Size = 50.0, FontColor = Color.Orange, FontName = "Elephant" }));
p.ParagraphFormat.Alignment = HorizontalAlignment.Left;
footer.Blocks.Add(p);
foreach (Section s in dc.Sections)
{
if (s.Blocks.Count > 0)
s.Blocks.RemoveAt(1);
s.HeadersFooters.Add(header.Clone(true));
s.HeadersFooters.Add(footer.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 });
}
}
}
Imports System
Imports System.IO
Imports SautinSoft.Document
Module Sample
Sub Main()
ChangeHeaderAndFooter()
End Sub
''' Get your free 30-day key here:
''' https://sautinsoft.com/start-for-free/
''' <summary>
''' How to edit Header and Footer in PDF file
''' </summary>
''' <remarks>
''' Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/edit-header-and-footer-in-pdf-net-csharp-vb.php
''' </remarks>
Sub ChangeHeaderAndFooter()
Dim inpFile As String = "..\..\..\somebody.pdf"
Dim outFile As String = "With changed 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("Modified : 1 April 2020", New CharacterFormat() With {
.Size = 14.0,
.FontColor = Color.DarkGreen
})
' Create the footer with orange text, with font name Elephant and size of 50 pt.
Dim footer As New HeaderFooter(dc, HeaderFooterType.FooterDefault)
Dim p As Paragraph = New Paragraph(dc, New Run(dc, "Last modified: 1st June 2021", New CharacterFormat() With {
.Size = 50.0,
.FontColor = Color.Orange,
.FontName = "Elephant"
}))
p.ParagraphFormat.Alignment = HorizontalAlignment.Left
footer.Blocks.Add(p)
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))
s.HeadersFooters.Add(footer.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
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: