Adding an image stamp (such as Paid, Reviewed, and Approved) to a PDF document can give readers a simple meaning to indicate the status of the file. Besides, you can add a dynamic stamp which usually consists of some dynamic information, such as company name, “Approved By” and system date. This tutorial will introduce how to add an image stamp or a dynamic stamp to a PDF document using Java codes.
Dependency
Before running codes, we need to
insert a Jar file into IDEA. You can download it from the website or directly
reference it using the following Maven configurations.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
Using the code
Add an image stamp
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.annotations.PdfRubberStampAnnotation;
import com.spire.pdf.annotations.appearance.PdfAppearance;
import com.spire.pdf.graphics.PdfImage;
import com.spire.pdf.graphics.PdfTemplate;
import java.awt.geom.Rectangle2D;
public class ImageStamp {
public static void main(String[] args) {
//create a PdfDocument object
PdfDocument doc = new PdfDocument();
//load a PDF document
doc.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pdf");
//get the first page
PdfPageBase page = doc.getPages().get(0);
//load an image file
PdfImage image = PdfImage.fromFile("C:\\Users\\Test1\\Desktop\\Image.png");
//get the width and height of the image
int width = image.getWidth();
int height = image.getHeight();
//create a PdfTemplate object based on the size of the image
PdfTemplate template = new PdfTemplate(width, height);
//draw image on the template
template.getGraphics().drawImage(image, 0, 0, width, height);
//create a rubber stamp annotation, specifying its location and position
Rectangle2D rect = new Rectangle2D.Float((float) (page.getActualSize().getWidth() - width - 50), (float) (page.getActualSize().getHeight() - height - 30), width, height);
PdfRubberStampAnnotation stamp = new PdfRubberStampAnnotation(rect);
//create a PdfAppearance object
PdfAppearance pdfAppearance = new PdfAppearance(stamp);
//set the template as the normal state of the appearance
pdfAppearance.setNormal(template);
//apply the appearance to the stamp
stamp.setAppearance(pdfAppearance);
//add the stamp annotation to PDF
page.getAnnotationsWidget().add(stamp);
//save the file
doc.saveToFile("output/ImageStamp.pdf");
doc.close();
}
}Output
Add a dynamic stampimport com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.annotations.PdfRubberStampAnnotation;
import com.spire.pdf.annotations.appearance.PdfAppearance;
import com.spire.pdf.graphics.*;
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
document.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pdf");
//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(Color.blue));
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(Color.orange),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 = "APPROVED\n";
String s2 = "By Tony 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);
appearance.setNormal(template);
//apply the appearance to stamp
stamp.setAppearance(appearance);
//add the stamp annotation to annotation collection
page.getAnnotationsWidget().add(stamp);
//save the file
document.saveToFile("output/DynamicStamp.pdf");
document.close();
}
//convert date to string
public static String dateToString(java.util.Date date,String dateFormat) {
SimpleDateFormat format = new SimpleDateFormat(dateFormat);
return format.format(date);
}
}Output