Add Header to PDF via Python

Add Header to PDF File using Python.

Add Headers to PDF Document Using Python

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 python-net platform. Open NuGet package manager, search for Aspose.PDF and install. You may also use the following command from the Package Manager Console.

Python Package Manager Console

pip install aspose-pdf

Add Header to PDF with Python


You need Aspose.PDF for Python via .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 - Python

This sample code shows how to add Header to PDF

    # Open document
    pdfDocument = Document(dataDir+ "TextinHeader.pdf");

    # Create header
    textStamp = TextStamp("Header Text")
    # Set properties of the stamp
    textStamp.TopMargin = 10
    textStamp.HorizontalAlignment = HorizontalAlignment.Center
    textStamp.VerticalAlignment = VerticalAlignment.Top

    # Add header on all pages
    for page in pdfDocument.Pages:
        page.AddStamp(textStamp)

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