Add Header to PDF via Java

Add Header to PDF File using Java Library.

Add Headers to PDF Document Using Java

In order to add Header in PDF, we’ll use Aspose.PDF for Java API which is a feature-rich, powerful, and easy-to-use conversion API for the Java platform. You can download its latest version directly from Maven and install it within your Maven-based project by adding the following configurations to the pom.xml.

Repository

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java AP</name>
    <url>https://releases.aspose.com/java/repo/</url>
</repository>

Dependency

<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>version of aspose-pdf API</version>
</dependency>

Add Header to PDF with Java


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

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");