Add Image stamps in PDF programmatically

Add Image Stamp in PDF File

You can use ImageStamp class to add an image as a stamp in PDF document. The ImageStamp class provides methods to specify height, width, and opacity etc.

To add an image stamp:

  1. Create a Document object and an ImageStamp object using required properties.
  2. Call the Page class addStamp(..) method of the Page class to add the stamp to the PDF.

The following code snippet shows how to add image stamp in the PDF file.

public static void AddImageStampInPDFFile() {
        // Open document
        Document pdfDocument = new Document(_dataDir + "AddImageStamp.pdf");

        // Create image stamp
        ImageStamp imageStamp = new ImageStamp(_dataDir + "aspose-logo.png");

        // Add stamp to particular page

        // Save output document + "AddImageStamp_out.pdf");


Control Image Quality when Adding Stamp

The ImageStamp class lets you add an image as a stamp in a PDF document. It also allows you to control the image quality when adding an image as a watermark in a PDF file. To allow this, a method named setQuality(…) has been added to the ImageStamp class. A similar method can also be found in the Stamp class of the com.aspose.pdf.facades package.

The following code snippet shows you how to control image quality when adding as stamp in the PDF file.

 public static void ControlImageQualityWhenAddingStamp() {
        // Open document
        Document pdfDocument = new Document(_dataDir + "AddImageStamp.pdf");

        // Create image stamp
        ImageStamp imageStamp = new ImageStamp(_dataDir + "aspose-logo.png");
        pdfDocument.getPages().get_Item(1).addStamp(imageStamp); + "ControlImageQuality_out.pdf");

Image Stamp as Background in Floating Box

Aspose.PDF API lets you add image stamp as background in a floating box. The BackgroundImage property of FloatingBox class can be used to set the background image stamp for a floating box as shown in following code sample.

public static void ImageStampAsBackgroundInFloatingBox() {
        // Instantiate Document object
        Document doc = new Document();
        // Add page to PDF document
        Page page = doc.getPages().add();

        // Create FloatingBox object
        FloatingBox aBox = new FloatingBox(200, 100);

        // Set left position for FloatingBox
        // Set Top position for FloatingBox
        // Set the Horizontal alignment for FloatingBox
        // Add text fragment to paragraphs collection of FloatingBox
        aBox.getParagraphs().add(new TextFragment("main text"));
        // Set border for FloatingBox
        aBox.setBorder(new BorderInfo(BorderSide.All, Color.getRed()));

        // Add background image
        Image img = new Image();
        img.setFile(_dataDir + "aspose-logo.png");

        // Set background color for FloatingBox

        // Add FloatingBox to paragraphs collection of page object
        // Save the PDF document + "AddImageStampAsBackgroundInFloatingBox_out.pdf");