When it comes to PDF documents, stamps offer an indispensable feature for adding contextual information and visual elements. These stamps can range from simple notes to complex graphics, enabling users to annotate, emphasize, or personalize their PDF content. With the ability to place stamps strategically, businesses can streamline workflows, indicate approvals, or provide concise feedback.
In this article, you will learn how to programmatically add dynamic stamps and image stamps to a PDF document using Spire.PDF for Java.
Install Spire.PDF for Java
To start, make sure to add the Spire.Pdf.jar file as a dependency in your Java program. You can download Spire.PDF for Java from our website and manually import the JAR file into your application. If you are using Maven, simply add the following code to your project's pom.xml file to include the JAR file effortlessly.
<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>10.11.2</version> </dependency> </dependencies>
Prior Knowledge
In PDF, a stamp refers to an annotation or graphical element that can be added to a document to provide additional information. Spire.PDF for Java introduces the PdfRubberStampAnnotation class, which serves as a representation of a rubber stamp. To create the visual representation of a rubber stamp, the PdfTemplate class is employed. This class acts as a canvas allowing you to draw various types of information, including text, images, shapes, and date/time elements.
Add a Dynamic Stamp to PDF in Java
A dynamic stamp refers to a customizable annotation that can be added to a PDF document to indicate a specific status, approval, or other information. Unlike static stamps, dynamic stamps contain variables or fields that can be dynamically updated, such as the current date, time, username, or any other custom data.
The following are the steps to add a dynamic stamp to PDF using Spire.PDF for Java.
- Create a PdfDocument object.
- Load a PDF file using PdfDocument.loadFromFile() method.
- Create a PdfTemplate object with desired size.
- Draw strings (including dynamic information such as date and time) on the template using PdfTemplate.getGraphics().drawString() method.
- Create a PdfRubberStampAnnotation object, and set the template as its appearance.
- Add the stamp to a specific PDF page using PdfPageBase.getAnnotationsWidget().add() method.
- Save the document to a different PDF file using PdfDocument.saveToFile() method.
- Java
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; public class AddDynamicStampToPdf { public static void main(String[] args) { // Create a PdfDocument object PdfDocument document = new PdfDocument(); // Load a PDF file document.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf"); // Get the last page PdfPageBase page = document.getPages().get(document.getPages().getCount() - 1); // Create a PdfTemplate object PdfTemplate template = new PdfTemplate(195, 50); // Create two fonts PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Elephant", Font.ITALIC, 15), 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.black)); 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 = 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.getRed(), path); // Draw dynamic text on the template String s1 = "APPROVED\n"; String s2 = "By Manager 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(50, (float) (page.getActualSize().getHeight() - 300)), 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); } }
Add an Image Stamp to PDF in Java
An image stamp in PDF refers to a graphical element that is added to a document as an annotation or overlay. It involves placing an image onto a specific location within a PDF page.
The steps to add an image stamp to PDF using Spire.PDF for Java are as follows.
- Create a PdfDocument object.
- Load a PDF file using PdfDocument.loadFromFile() method.
- Load an image that you want to stamp on PDF using PdfImage.fromFile() method.
- Create a PdfTemplate object based on the size of the image.
- Draw the image on the template using PdfTemplate.getGraphics().drawImage() method.
- Create a PdfRubberStampAnnotation object, and set the template as its appearance.
- Add the stamp to a specific PDF page using PdfPageBase.getAnnotationsWidget().add() method.
- Save the document to a different PDF file using PdfDocument.saveToFile() method.
- Java
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 AddImageStampToPdf { public static void main(String[] args) { // Create a PdfDocument object PdfDocument document = new PdfDocument(); // Load a PDF document document.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf"); // Get the last page PdfPageBase page = document.getPages().get(document.getPages().getCount() - 1); // Load an image file PdfImage image = PdfImage.fromFile("C:\\Users\\Administrator\\Desktop\\stamp-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() - 400), 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 document.saveToFile("output/ImageStamp.pdf"); document.close(); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.