Add Dynamic Stamps to PDF Using Java

A dynamic PDF stamp usually consists of some dynamic information, such as system date, time, and company information. This article will introduce how to add a dynamic rubber stamp to PDF by using Free Spire.PDF for Java.

Import jar dependency (2 Method)
● Download the Free Spire.PDF for Java and unzip it. Then add the Spire.Pdf.jar file to your project as dependency.

● Directly add the jar dependency to maven project by adding the following configurations to the pom.xml.


Sample Code

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.annotations.PdfRubberStampAnnotation;
import com.spire.pdf.annotations.appearance.PdfAppearance;

import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.text.SimpleDateFormat;

public class DynamicStamp {

public static void main(String[] args) {

//create a PdfDocument object
PdfDocument document = new PdfDocument();

//load a PDF file

//get the first page
PdfPageBase page = document.getPages().get(0);

//create a pdf template
PdfTemplate template = new PdfTemplate(185, 50);

//create two fonts
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Elephant", Font.ITALIC,16), true);
PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", Font.ITALIC ,10), true);

//create a solid brush and a gradient brush
PdfSolidBrush solidBrush = new PdfSolidBrush(new PdfRGBColor(;
Rectangle2D rect1 = new Rectangle2D.Float();
rect1.setFrame(new Point2D.Float(0,0),template.getSize());
PdfLinearGradientBrush linearGradientBrush = new PdfLinearGradientBrush(rect1,new PdfRGBColor(Color.white),new PdfRGBColor(,PdfLinearGradientMode.Horizontal);

//create rounded rectangle path
int CornerRadius = 20;
PdfPath path = new PdfPath();
path.addArc(template.getBounds().getX(), template.getBounds().getY(), CornerRadius, CornerRadius, 180, 90);
path.addArc(template.getBounds().getX() + template.getWidth() - CornerRadius,template.getBounds().getY(), CornerRadius, CornerRadius, 270, 90);
path.addArc(template.getBounds().getX() + template.getWidth() - CornerRadius, template.getBounds().getY()+ template.getHeight() - CornerRadius, CornerRadius, CornerRadius, 0, 90);
path.addArc(template.getBounds().getX(), template.getBounds().getY() + template.getHeight() - CornerRadius, CornerRadius, CornerRadius, 90, 90);
path.addLine( template.getBounds().getX(), template.getBounds().getY() + template.getHeight() - CornerRadius, template.getBounds().getX(), template.getBounds().getY() + CornerRadius / 2);

//draw path on the template
template.getGraphics().drawPath(linearGradientBrush, path);
template.getGraphics().drawPath(PdfPens.getRed(), path);

//draw dynamic text on the template
String s1 = "REVISED\n";
String s2 = "By XX Company at " + dateToString(new java.util.Date(),"yyyy-MM-dd HH:mm:ss");
template.getGraphics().drawString(s1, font1, solidBrush, new Point2D.Float(5, 5));
template.getGraphics().drawString(s2, font2, solidBrush, new Point2D.Float(2, 28));

//create a rubber stamp, specifying its size and location
Rectangle2D rect2= new Rectangle2D.Float();
rect2.setFrame(new Point2D.Float((float)(page.getActualSize().getWidth()-250),(float)(page.getActualSize().getHeight()-120)), template.getSize());
PdfRubberStampAnnotation stamp = new PdfRubberStampAnnotation(rect2);

//create a PdfAppearance object and apply the template as its normal state
PdfAppearance appearance = new PdfAppearance(stamp);

//apply the appearance to stamp

//add the stamp annotation to annotation collection

//save the file

//convert date to string
public static String dateToString(java.util.Date date,String dateFormat) {
SimpleDateFormat format = new SimpleDateFormat(dateFormat);
return format.format(date);

Sharing Java Code