Add Header to PDF using C#

Add Header to PDF File using C#.

Add Headers to PDF Document Using C#

In order to add Header in PDF, we’ll use Aspose.PDF for .NET API which is a feature-rich, powerful and easy to use document manipulation API for net platform. Open NuGet package manager, search for Aspose.PDF and install. You may also use the following command from the Package Manager Console.

Package Manager Console

PM > Install-Package Aspose.PDF

Add Header to PDF with C#


You need Aspose.PDF for .NET to try the code in your environment.

  1. Open a PDF document using Document object.
  2. Create a Stamp and define its properties.
  3. Add the Stamp to Page using AddStamp method.
  4. Save the PDF file.

Add a Header to PDF Document - C#

This sample code shows how to add Header to PDF

var inputFile = Path.Combine(dataDir, "TextinHeader.pdf");
var outputFile = Path.Combine(dataDir, "TextinHeader_out.pdf");
var pdfDocument = new Aspose.Pdf.Document(inputFile);

// Create header
var textStamp = new Aspose.Pdf.TextStamp("Header Text");
// Set properties of the stamp
textStamp.TopMargin = 10;
textStamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
textStamp.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Top;
// Add header on all pages
foreach (var page in pdfDocument.Pages)
{
    page.AddStamp(textStamp);
}
pdfDocument.Save(outputFile);