Add Header to PDF via 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


    // Open document
    Document pdfDocument = new Document(dataDir+ "TextinHeader.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;
    // Add header on all pages
    foreach (Page page in pdfDocument.Pages)
    {
        page.AddStamp(textStamp);
    }

    // Save updated document
    pdfDocument.Save(dataDir+ "TextinHeader_out.pdf");