Skip to content

Instantly share code, notes, and snippets.

@conholdate-gists
Last active December 15, 2021 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conholdate-gists/85433c042f80d481575ee80989790c27 to your computer and use it in GitHub Desktop.
Save conholdate-gists/85433c042f80d481575ee80989790c27 to your computer and use it in GitHub Desktop.
Add Headers and Footers in PDF Documents using C#

Learn how to add headers and footers in PDF documents using C#: https://blog.conholdate.com/2021/12/15/add-headers-and-footers-in-pdf-using-csharp/

The following topics are discussed/covered in this article:

  1. C# API to Add Headers and Footers in PDF Documents
  2. Add Text in Header of PDF using C#
  3. Add Text in Footer of PDF using C#
  4. Insert Image in Header of PDF using C#
  5. Insert Image in Footer of PDF using C#
  6. Add Multiple Headers & Footers in Single PDF Document
  7. Add Page Numbers in Footer of PDF using C#
// This code example demonstrates how to add page number in the footer of each page of a PDF document.
// Load the PDF document
Document pdfDocument = new Document(@"C:\Files\sample.pdf");
// Add footer on all pages
foreach (Page page in pdfDocument.Pages)
{
// Create footer
TextStamp textStamp = new TextStamp("Page " + page.Number + " of " + pdfDocument.Pages.Count + " pages.");
// Set properties of the stamp
textStamp.BottomMargin = 10;
textStamp.HorizontalAlignment = HorizontalAlignment.Center;
textStamp.VerticalAlignment = VerticalAlignment.Bottom;
// Add stamp
page.AddStamp(textStamp);
}
// Save the updated document
pdfDocument.Save(@"C:\Files\PDF\output.pdf");
// This code example demonstrates how to add an image in the footer of an existing PDF document.
// Load the PDF document
Document pdfDocument = new Document(@"C:\Files\sample.pdf");
// Create footer
ImageStamp imageStamp = new ImageStamp(@"C:\Files\conholdate-logo.jpg");
// Set properties of the stamp
imageStamp.BottomMargin = 10;
imageStamp.HorizontalAlignment = HorizontalAlignment.Center;
imageStamp.VerticalAlignment = VerticalAlignment.Bottom;
// Add footer on all pages
foreach (Page page in pdfDocument.Pages)
{
page.AddStamp(imageStamp);
}
// Save the updated document
pdfDocument.Save(@"C:\Files\output.pdf");
// This code example demonstrates how to add an image in the header of an existing PDF document.
// Load the PDF document
Document pdfDocument = new Document(@"C:\Files\sample.pdf");
// Create header
ImageStamp imageStamp = new ImageStamp(@"C:\Files\conholdate-logo.jpg");
// Set properties of the stamp
imageStamp.TopMargin = 10;
imageStamp.HorizontalAlignment = HorizontalAlignment.Center;
imageStamp.VerticalAlignment = VerticalAlignment.Top;
// Add header on all pages
foreach (Page page in pdfDocument.Pages)
{
page.AddStamp(imageStamp);
}
// Save the updated document
pdfDocument.Save(@"C:\Files\output.pdf");
// This code example demonstrates how to add different headers for different pages in a single PDF document.
// Load the PDF document
Document pdfDocument = new Document(@"C:\Files\sample.pdf");
// Create three stamps
ImageStamp stamp1 = new ImageStamp(@"C:\Files\PDF\conholdate-logo.jpg");
TextStamp stamp2 = new TextStamp("Header Stamp 2");
TextStamp stamp3 = new TextStamp("Header Stamp 3");
// Set stamp alignment for stamp1
stamp1.VerticalAlignment = VerticalAlignment.Top;
stamp1.HorizontalAlignment = HorizontalAlignment.Center;
// Set stamp alignment for stamp2
stamp2.VerticalAlignment = VerticalAlignment.Top;
// Set Horizontal alignment information for stamp as Center aligned
stamp2.HorizontalAlignment = HorizontalAlignment.Center;
// Set the zooming factor for stamp object
stamp2.Zoom = 10;
// Set stamp alignment for stamp3
stamp3.VerticalAlignment = VerticalAlignment.Top;
// Set the Horizontal alignment inforamtion for stamp object as Center aligned
stamp3.HorizontalAlignment = HorizontalAlignment.Center;
// Set the rotation angle for stamp object
stamp3.RotateAngle = 35;
// Add first stamp on first page;
pdfDocument.Pages[1].AddStamp(stamp1);
// Add second stamp on second page;
pdfDocument.Pages[2].AddStamp(stamp2);
// Add third stamp on third page.
pdfDocument.Pages[3].AddStamp(stamp3);
// Save the updated document
pdfDocument.Save(@"C:\Files\output.pdf");
// This code example demonstrates how to add text in the footer of an existing PDF document.
// Load the PDF document
Document pdfDocument = new Document(@"C:\Files\sample.pdf");
// Create footer
TextStamp textStamp = new TextStamp("Footer Text");
// Set properties of the stamp
textStamp.BottomMargin = 10;
textStamp.HorizontalAlignment = HorizontalAlignment.Center;
textStamp.VerticalAlignment = VerticalAlignment.Bottom;
// Add footer on all pages
foreach (Page page in pdfDocument.Pages)
{
page.AddStamp(textStamp);
}
// Save the updated document
pdfDocument.Save(@"C:\Files\output.pdf");
// This code example demonstrates how to add text in the header of an existing PDF document.
// Load the PDF document
Document pdfDocument = new Document(@"C:\Files\sample.pdf");
// Create header
TextStamp textStamp = new TextStamp("Header Text");
// Set properties of the stamp
textStamp.TopMargin = 10;
textStamp.HorizontalAlignment = HorizontalAlignment.Center;
textStamp.VerticalAlignment = VerticalAlignment.Top;
// Specify the font style
textStamp.TextState.FontStyle = FontStyles.Bold;
textStamp.TextState.ForegroundColor = Color.Red;
textStamp.TextState.FontSize = 14;
textStamp.TextState.BackgroundColor = Color.Pink;
textStamp.TextState.Font = FontRepository.FindFont("Verdana");
// Add header on all pages
foreach (Page page in pdfDocument.Pages)
{
page.AddStamp(textStamp);
}
// Save the updated document
pdfDocument.Save(@"C:\Files\output.pdf");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment