A PDF Stamp is a PDF annotation that acts like a rubber stamp on paper. Like a rubber stamp, a PDF stamp has a custom graphic that is created from text or an image. Usually, it will be something that will effectively communicate your intent. In this article, you will learn how to stamp a PDF document with an image or dynamic text in Java using Spire.PDF for Java.
Add Spire.Pdf.jar as dependency
If you are working on a maven project, you can include the dependency in pom.xml file using this:
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf</artifactId>
<version>9.3.11</version>
</dependency>
</dependencies>
If you are not using maven, then you can find the required jar files from the zip file available in this location. Include all the jar files into the application lib folder to run the sample code given in this tutorial.
Prerequisite Knowledge
To help you better understand the code examples provided in this tutorial, I have listed below some of the core classes, methods, and properties.
The PdfTemplate class represents a piece of canvas, on which you can draw whatever information you want, such as text, images, date, and time. The PdfRubberStampAnnotation class represents a rubber stamp annotation that displays text or graphics on a PDF page with a rubber stamp. The PdfTemplate here is used to create the appearance of a rubber stamp.
Add an Image Stamp to PDF in Java
An image stamp is a stamp created based on an existing image. The following are the steps to add an image stamp to a PDF document using Free Spire.PDF for Java.
- Create a PdfDocument object.
- Load a PDF file using PdfDocument.loadFromFile() method.
- Get a specific page using PdfDocument.getPages().get() method.
- Create a PdfTemplate object, which is used to create the appearance of stamp.
- Load an image using PdfImage.fromImage() method.
- Draw the image on the template using PdfTemplate.getGraphics().drawImage() method.
- Create a PdfRubberStampAnnotation object, and apply the template to the stamp as the appearance.
- Add the stamp to the selected page using PdfPageBase.getAnnotationsWidget().add() method.
- Save the document to a PDF file using PdfDocument.saveToFile() method.
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\\Administrator\\Desktop\\sample.pdf");
//Get the last page
PdfPageBase page = doc.getPages().get(doc.getPages().getCount() - 1);
//Load an image file
PdfImage image = PdfImage.fromFile("C:\\Users\\Administrator\\Desktop\\paid.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 - 20), (float) (page.getActualSize().getHeight() - height - 200), 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("AddImageStamp.pdf");
doc.close();
}
}
Add a Custom Dynamic Stamp to PDF in Java
A dynamic stamp takes information from your computer and identity panel, allowing you to include the user name, date and time information on the stamp. The following are the steps to add a dynamic stamp to a PDF document using Free Spire.PDF for Java.
- Create a PdfDocument object.
- Load a PDF file using PdfDocument.loadFromFile() method.
- Get a specific page using PdfDocument.getPages().get() method.
- Create a PdfTemplate object, which is used to create the appearance of stamp.
- Draw a rounded rectangle on the template using PdfTemplate.getGraphics().drawPath() method.
- Draw strings on the template using PdfTemplate.getGraphics().drawString() method.
- Create a PdfRubberStampAnnotation object, and apply the template to the stamp as the appearance.
- Add the stamp to the selected page using PdfPageBase.getAnnotationsWidget().add() method.
- Save the document to a PDF file using PdfDocument.saveToFile() method.
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.*;
import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.text.SimpleDateFormat;
import java.util.Map;
public class AddDynamicStamp {
public static void main(String[] args) {
//Create a PdfDocument object
PdfDocument document = new PdfDocument();
//Load a PDF file
document.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");
//Get the last page
PdfPageBase page = document.getPages().get(document.getPages().getCount() - 1);
//Create a pdf template
PdfTemplate template = new PdfTemplate(220, 50);
//Create two fonts
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Elephant", Font.ITALIC, 16), true);
PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Times New Roman", Font.ITALIC, 12), 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(113,191,234), PdfLinearGradientMode.Horizontal);
//Create a rounded rectangle path
int CornerRadius = 10;
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.getBlue(), path);
//Draw dynamic text on the template
String s1 = "APPROVED\n";
Map<String, String> map = System.getenv();
String userName = map.get("USERNAME");
String s2 = "By " + userName + " at ";
String s3 = 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 + s3, font2, solidBrush, new Point2D.Float(5, 28));
//Create a rubber stamp, specifying its size and location
Rectangle2D rect2 = new Rectangle2D.Float();
rect2.setFrame(new Point2D.Float((float) (page.getActualSize().getWidth() - template.getWidth() - 35), (float) (page.getActualSize().getHeight() - template.getHeight() - 220)), 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("AddDynamicStamp.pdf");
document.close();
}
//Convert date to string
public static String dateToString(java.util.Date poDate, String pcFormat) {
SimpleDateFormat loFormat = new SimpleDateFormat(pcFormat);
return loFormat.format(poDate);
}
}
Top comments (0)