Java PDF Stamper (Beginner Tutorial)

PDF document is a digital format used to send data over the internet. It preserves data formatting and allows its user control over the document. As most of the businesses happen online these days, data sharing is mostly done using PDF files. Adding new data to a PDF file attachment is sometimes required by businesses. However, editing a PDF document can be difficult especially when trying to add new content. Adding or modifying all the interactive elements in existing PDF documents in Java is sometimes also difficult for developers and to build a PDF Stamper from scratch can be quite tedious and time-consuming. With improving technologies, and emergence of numerous libraries, creating a Java PDF Stamper is now easy.

In this article, we will create a PDF Stamper in Java using IronPDF Library.

IronPDF - Java PDF Library

IronPDF is a Java PDF Library for generating, reading, and editing PDF documents. It allows users to work with PDF documents with ease and accuracy. IronPDF for Java is built on the success of IronPDF for .NET and provides efficiency across different platforms. IronPDF for Java uses IronPdfEngine, which is fast and optimized for performance.

IronPDF helps extract text, images and other objects from PDF files. It helps create PDFs from HTML String or file, URL and images. It also allows conversion between different file formats. You can easily add new content and stamp signatures along with metadata to existing PDF documents. It is designed especially for Java 8+, Scala and Kotlin, on any Windows, Linux and Cloud platforms.

Steps to Create PDF Stamper using IronPDF in Java Application

Prerequisites

To make a PDF Stamper, you will need the following prerequisites:

1. Java IDE

You can use any Java supported IDE. There are a bunch of IDEs available for Java development. Here we will be using IntelliJ IDE. You can use NetBeans, Eclipse, etc.

2. Maven Project

Maven is a dependency manager and allows control over the Java project. Maven for Java can be downloaded from here. IntelliJ IDE for Java contains the Maven support.

3. IronPDF

You can download and install IronPDF for Java in multiple ways.

  • Adding IronPDF dependency in the pom.xml file in Maven project.
<dependency>
   <groupId>com.ironsoftware</groupId>
   <artifactId>com.ironsoftware</artifactId>
   <version>2024.3.1</version>
</dependency>
XML
  • Visit Maven website and download the latest IronPDF package for Java. Download from the link here.
  • Direct download from IronPDF website through this link.
  • Manually install IronPDF using the JAR file in your Java Application.

4. Slf4j

This dependency is also required to stamp content to an existing PDF document. It can be added using the Maven dependencies manager in IntelliJ or directly downloaded from the Maven website. Add the following dependency to pom.xml file:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>2.0.5</version>
</dependency>
XML

Adding Necessary Imports

Once all the prerequisites are installed, we need to import the necessary IronPDF packages to work with PDF documents. Add the following code on top of the Main.java file:

import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.stamp.*;
import java.io.IOException;
import java.nio.file.Paths;
JAVA

License Key

Some methods available in IronPDF require a license to be used. You can purchase a license or try IronPDF for free. You can set the key as follows:

License.setLicenseKey("YOUR-KEY");
JAVA

Open an Existing PDF document

To import an existing document for stamping new content, PdfDocument class is used. Its static fromFile method is used to load a file from a specific path with the actual file name. The code goes as follows:

PdfDocument pd = PdfDocument.fromFile(Paths.get("sample.pdf"));
JAVA

Loaded original document:

Java PDF Stamper (Beginner Tutorial): Figure 1

Add New HTML Content

IronPDF provides a stamp package. It allows a bunch of useful stamping options like BarcodeStampter, HtmlStampter, ImageStamper, TextStamper and other for content alignment. To add new HTML content to our PDF document we will use HtmlStamper class. We opened the file in previous section, let's now add some content to it. The following code helps to achieve this task:

HtmlStamper htmlStamper = new HtmlStamper();
htmlStamper.setHtml("New content added!");
JAVA

First we created a HtmlStamper object and then used its setHtml method to attach new HTML code. Now we have to apply it to the existing PDF document to create a new PDF version.

Adding all the Interactive Elements to PDF Document

Using the PdfDocument object created earlier we will add the HTML code to the existing document. It provides applyStamp with two overloads, one that accepts only the content as a Stamper object and the other with page selection as well.

 pd.applyStamp(htmlStamper);
JAVA

This will add the string description to the existing document.

Saving the Changes to the PDF

To save the file, use saveAs method of the PdfDocument object.

pd.saveAs("stamped.pdf");
JAVA

Java PDF Stamper (Beginner Tutorial): Figure 2

The HTML string is added to every page of the PDF document and at the middle of every page.

Stamp to Specific Pages

You can use another overload of the applyStamp method to add the content to a specific page.

pd.applyStamp(htmlStamper, PageSelection.singlePage(1));
JAVA

PageSelection class provides different methods to control the page number. firstPage, lastPage, allPages, and pageRange are some methods available to add the content appropriately.

Aligning the content

You can use setVerticalAlignment, setHorizontalAlignment, setWidth, setHeight methods in each Stamper class to adjust the position of the content added to the existing PDF document. The following example code will help to place the text at bottom left of the page:

htmlStamper.setHorizontalAlignment(HorizontalAlignment.LEFT);
htmlStamper.setVerticalAlignment(VerticalAlignment.BOTTOM);
JAVA

Java PDF Stamper (Beginner Tutorial): Figure 3

You can use setVerticalOffset, setHorizontalOffset method to further adjust the positioning of the content.

For more information on working with PDF files using IronPDF visit this code example page.

Stamp Metadata, Signature, and Security Options to Existing Document

IronPDF for Java is a versatile library and provides the facility to add file description in form of metadata, user permissions, user password, add digital signature and create signed document to same the version of the PDF document.

Metadata

// Edit file metadata
MetadataManager metadata = pd.getMetadata();
metadata.setAuthor("Satoshi Nakamoto");
metadata.setKeywords("SEO, Friendly");
metadata.setModifiedDate(new Date().toString());
JAVA

Security Options

You can also control the security options of your PDF file so that data is saved from unauthorized use. It provides options to set allowPrinting, allowModifyContents, allowCopy, allowModifyAnnotations parameters to true or false. The following sample code will help to set all the objects properties mentioned above.

// Edit file security settings
SecurityOptions securityOptions = new SecurityOptions();
securityOptions.setAllowUserCopyPastContent(false);
securityOptions.setAllowUserAnnotations(false);
securityOptions.setAllowUserPrinting(PdfPrintSecurity.FULL_PRINT_RIGHTS);
securityOptions.setAllowUserFormData(false);
securityOptions.setOwnerPassword("top-secret");
securityOptions.setUserPassword("sharable");
JAVA

User Password

// Change or set the document encryption password
SecurityManager securityManager = pd.getSecurity();
securityManager.removePasswordsAndEncryption();
securityManager.makePdfDocumentReadOnly("secret-key");

securityManager.setSecurityOptions(securityOptions);
pd.saveAs(Paths.get("assets/secured.pdf"));
JAVA

For stamping digital signatures on the actual file, you can see this code examples page.

Summary

In this article, we discussed how to stamp content to an existing PDF document in Java. There is a stamp package with a bunch of useful stamping classes, which can be used to add the multiple format content to PDF at any desired location in the original document.

IronPDF is a versatile library as it can be seen from above code examples as well. It is quite simple but yet very powerful PDF generation and manipulation tool. This helps developers to easily integrate all PDF functionalities in a single Java application program.

IronPDF is free for single development and provides a free trial to test out its complete functionality without watermark. However, for commercial use it should be licensed.

Lastly, IronPDF offers a special promotion in which developers can buy all five Iron Software products for the price of two licenses.