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

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

import aspose.pdf as apdf

from os import path

path_infile = path.join(self.data_dir, infile)
path_outfile = path.join(self.data_dir, outfile)

document = apdf.Document(path_infile)

headerText = apdf.Text.TextFragment("This is a footer")
header = apdf.HeaderFooter()
header.Paragraphs.Add(headerText)
marginInfo = apdf.MarginInfo()
marginInfo.Left = 50
marginInfo.Top = 20

for page in document.pages:
    page.header = header

document.save(path_outfile )