Java - Add An Image Or Text Stamp To PDF Documents

Introduction

 
A PDF stamp is a custom (user-defined) graphic that can be applied to a document. PDF stamps are often used in internal documents to prove that the document has been reviewed and marked as "declined", "qualified", or "approved". This article will introduce how to programmatically stamp a PDF document with text or images by using Spire.PDF for Java.
 

Install Spire.Pdf.jar

 
If you create a Maven project, you can easily import the jar in your application using the following configurations. For non-Maven projects, download the jar file from this link and add it as a dependency in your application. 
  1. <repositories>  
  2.     <repository>  
  3.         <id>com.e-iceblue</id>  
  4.         <name>e-iceblue</name>  
  5.         <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>  
  6.     </repository>  
  7. </repositories>  
  8. <dependencies>  
  9.     <dependency>  
  10.         <groupId> e-iceblue</groupId>  
  11.         <artifactId>spire.pdf</artifactId>  
  12.         <version>3.11.6</version>  
  13.     </dependency>  
  14. </dependencies>  

Previous knowledge

 
To help you understand the following code examples, you’d better learn about the important classes and theories involved. 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.
 
After a template is created, you can place it anywhere on a PDF page or on a rubber stamp. The difference between them is that if you put it directly on a PDF page, it can't be moved. So in this case, the PdfTamplate is used to create the appearance of a rubber stamp.
 
Example 1. Add an image stamp to PDF
  1. import com.spire.pdf.annotations.PdfRubberStampAnnotation;  
  2. import com.spire.pdf.annotations.appearance.PdfAppearance;  
  3. import com.spire.pdf.graphics.PdfImage;  
  4. import com.spire.pdf.graphics.PdfTemplate;  
  5. import java.awt.geom.Rectangle2D;  
  6. public class AddImageStamp {  
  7.     public static void main(String[] args) {  
  8.         //Create a PdfDocument object  
  9.         PdfDocument doc = new PdfDocument();  
  10.         //Load a PDF document  
  11.         doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");  
  12.         //Get the last page  
  13.         PdfPageBase page = doc.getPages().get(doc.getPages().getCount() - 1);  
  14.         //Load an image file  
  15.         PdfImage image = PdfImage.fromFile("C:\\Users\\Administrator\\Desktop\\secure.png");  
  16.         //Get the width and height of the image  
  17.         intwidth = image.getWidth();  
  18.         intheight = image.getHeight();  
  19.         //Create a PdfTemplate object based on the size of the image  
  20.         PdfTemplate template = new PdfTemplate(width, height);  
  21.         //Draw image on the template  
  22.         template.getGraphics().drawImage(image, 0, 0, width, height);  
  23.         //Create a rubber stamp annotation, specifying its location and position  
  24.         Rectangle2D rect = new Rectangle2D.Float((float)(page.getActualSize().getWidth() - width - 10), (float)(page.getActualSize().getHeight() - height - 20), width, height);  
  25.         PdfRubberStampAnnotation stamp = new PdfRubberStampAnnotation(rect);  
  26.         //Create a PdfAppearance object  
  27.         PdfAppearancepdfAppearance = new PdfAppearance(stamp);  
  28.         //Set the template as the normal state of the appearance  
  29.         pdfAppearance.setNormal(template);  
  30.         //Apply the appearance to the stamp  
  31.         stamp.setAppearance(pdfAppearance);  
  32.         //Add the stamp annotation to PDF  
  33.         page.getAnnotationsWidget().add(stamp);  
  34.         //Save the file  
  35.         doc.saveToFile("AddImageStamp.pdf");  
  36.         doc.close();  
  37.     }  
  38. }  
Output
 
Java - Add an Image or Text Stamp to PDF Documents
 
Example 2. Add a text stamp to PDF
  1. import com.spire.pdf.annotations.PdfRubberStampAnnotation;  
  2. import com.spire.pdf.annotations.appearance.PdfAppearance;  
  3. import com.spire.pdf.graphics.*;  
  4. import java.awt.*;  
  5. import java.awt.geom.Point2D;  
  6. import java.awt.geom.Rectangle2D;  
  7. import java.text.SimpleDateFormat;  
  8. public class AddTextStamp {  
  9.     public static void main(String[] args) {  
  10.         //Create a PdfDocument object  
  11.         PdfDocument document = new PdfDocument();  
  12.         //Load a PDF file  
  13.         document.loadFromFile("C:\\Users\\Administrator\\Desktop\\quotation.pdf");  
  14.         //Get the last page  
  15.         PdfPageBase page = document.getPages().get(document.getPages().getCount() - 1);  
  16.         //Create a pdf template  
  17.         PdfTemplate template = new PdfTemplate(185, 50);  
  18.         //Create two fonts  
  19.         PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Elephant", Font.ITALIC, 16), true);  
  20.         PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", Font.ITALIC, 10), true);  
  21.         //Create a solid brush and a gradient brush  
  22.         PdfSolidBrushsolidBrush = new PdfSolidBrush(new PdfRGBColor(Color.blue));  
  23.         Rectangle2D rect1 = new Rectangle2D.Float();  
  24.         rect1.setFrame(new Point2D.Float(0, 0), template.getSize());  
  25.         PdfLinearGradientBrushlinearGradientBrush = new PdfLinearGradientBrush(rect1, new PdfRGBColor(Color.white), new PdfRGBColor(Color.orange), PdfLinearGradientMode.Horizontal);  
  26.         //Create a rounded rectangle path  
  27.         intCornerRadius = 10;  
  28.         PdfPath path = new PdfPath();  
  29.         path.addArc(template.getBounds().getX(), template.getBounds().getY(), CornerRadius, CornerRadius, 180, 90);  
  30.         path.addArc(template.getBounds().getX() + template.getWidth() - CornerRadius, template.getBounds().getY(), CornerRadius, CornerRadius, 270, 90);  
  31.         path.addArc(template.getBounds().getX() + template.getWidth() - CornerRadius, template.getBounds().getY() + template.getHeight() - CornerRadius, CornerRadius, CornerRadius, 0, 90);  
  32.         path.addArc(template.getBounds().getX(), template.getBounds().getY() + template.getHeight() - CornerRadius, CornerRadius, CornerRadius, 90, 90);  
  33.         path.addLine(template.getBounds().getX(), template.getBounds().getY() + template.getHeight() - CornerRadius, template.getBounds().getX(), template.getBounds().getY() + CornerRadius / 2);  
  34.         //Draw path on the template  
  35.         template.getGraphics().drawPath(linearGradientBrush, path);  
  36.         template.getGraphics().drawPath(PdfPens.getBlue(), path);  
  37.         //Draw dynamic text on the template  
  38.         String s1 = "APPROVED\n";  
  39.         String s2 = "By Sales Manager " + dateToString(new java.util.Date(), "yyyy-MM-ddHH:mm:ss");  
  40.         template.getGraphics().drawString(s1, font1, solidBrush, new Point2D.Float(5, 5));  
  41.         template.getGraphics().drawString(s2, font2, solidBrush, new Point2D.Float(5, 28));  
  42.         //Create a rubber stamp, specifying its size and location  
  43.         Rectangle2D rect2 = new Rectangle2D.Float();  
  44.         rect2.setFrame(new Point2D.Float((float)(page.getActualSize().getWidth() - template.getWidth() - 17), (float)(page.getActualSize().getHeight() - template.getHeight() - 50)), template.getSize());  
  45.         PdfRubberStampAnnotation stamp = new PdfRubberStampAnnotation(rect2);  
  46.         //Create a PdfAppearance object and apply the template as its normal state  
  47.         PdfAppearance appearance = new PdfAppearance(stamp);  
  48.         appearance.setNormal(template);  
  49.         //Apply the appearance to stamp  
  50.         stamp.setAppearance(appearance);  
  51.         //Add the stamp annotation to annotation collection  
  52.         page.getAnnotationsWidget().add(stamp);  
  53.         //Save the file  
  54.         document.saveToFile("AddTextStamp.pdf");  
  55.         document.close();  
  56.     }  
  57.     //Convert date to string  
  58.     public static String dateToString(java.util.DatepoDate, String pcFormat) {  
  59.         SimpleDateFormatloFormat = new SimpleDateFormat(pcFormat);  
  60.         return loFormat.format(poDate);  
  61.     }  
  62. }  
Output
 
Java - Add an Image or Text Stamp to PDF Documents
 

Conclusion

 
If you’ve read the above code snippets line by line, you must find that the syntax and code logic are quite easy to understand. It’s not a difficult task if you would like to modify parts of the code to achieve different results. What’s more, Spire.PDF for Java is a comprehensive PDF library, which is able to do a lot more things than what is shown in this article.