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.

<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</artifactId>
        <version>3.11.6</version>
    </dependency>
</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.

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 AddImageStamp {
    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\\secure.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 - 10),
                                                 (float)(page.getActualSize().getHeight() - height - 20),
                                                 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();
    }
}

Output

Output

Example 2. Add a text stamp to the PDF.

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.PdfLinearGradientBrush;
import com.spire.pdf.graphics.PdfPath;
import com.spire.pdf.graphics.PdfTemplate;
import com.spire.pdf.graphics.PdfTrueTypeFont;
import com.spire.pdf.graphics.PdfSolidBrush;
import com.spire.pdf.graphics.PdfRGBColor;
import com.spire.pdf.graphics.PdfPens;

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

public class AddTextStamp {
    public static void main(String[] args) {
        // Create a PdfDocument object
        PdfDocument document = new PdfDocument();

        // Load a PDF file
        document.loadFromFile("C:\\Users\\Administrator\\Desktop\\quotation.pdf");

        // Get the last page
        PdfPageBase page = document.getPages().get(document.getPages().getCount() - 1);

        // 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), PdfLinearGradientBrush.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";
        String s2 = "By Sales Manager " + dateToString(new java.util.Date(), "yyyy-MM-ddHH:mm:ss");
        template.getGraphics().drawString(s1, font1, solidBrush, new Point2D.Float(5, 5));
        template.getGraphics().drawString(s2, 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() - 17),
                (float) (page.getActualSize().getHeight() - template.getHeight() - 50)), 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("AddTextStamp.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);
    }
}

Output

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.


Similar Articles