Add Image Stamps to PDF in Java

Aspose.PDF for Java supports image stamps as overlays and image-backed layout elements.

Add an image stamp

Use this example when a page should display an image stamp with custom placement and opacity.

  1. Open the source PDF Document.
  2. Create an ImageStamp and configure its appearance.
  3. Add the stamp to the page and save the document.
public static void addImageStamp(Path inputFile, Path imageFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        ImageStamp imageStamp = new ImageStamp(imageFile.toString());
        imageStamp.setBackground(true);
        imageStamp.setXIndent(100);
        imageStamp.setYIndent(100);
        imageStamp.setHeight(300);
        imageStamp.setWidth(300);
        imageStamp.setRotate(Rotation.on270);
        imageStamp.setOpacity(0.5);

        document.getPages().get_Item(1).addStamp(imageStamp);
        document.save(outputFile.toString());
    }
}

Add an image stamp with quality control

Use this example when you need to adjust the rendering quality of the image stamp.

  1. Open the source PDF Document.
  2. Create an ImageStamp and set the quality value.
  3. Add the stamp to the page and save the result.
public static void addImageStampWithQualityControl(Path inputFile, Path imageFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        ImageStamp imageStamp = new ImageStamp(imageFile.toString());
        imageStamp.setQuality(10);
        document.getPages().get_Item(1).addStamp(imageStamp);
        document.save(outputFile.toString());
    }
}

Use an image as a floating box background

Use this example when an image should serve as the background of a styled layout container.

  1. Open the source PDF Document and access the target page.
  2. Create a FloatingBox with text and border settings.
  3. Set the background image, add the box to the page, and save the document.
public static void addImageAsBackgroundInFloatingBox(Path inputFile, Path imageFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        Page page = document.getPages().get_Item(1);
        FloatingBox box = new FloatingBox(200.0f, 100.0f);
        box.setLeft(40);
        box.setTop(80);
        box.setHorizontalAlignment(HorizontalAlignment.Center);
        box.getParagraphs().add(new TextFragment("Text in Floating Box"));
        box.setBorder(new BorderInfo(BorderSide.All, Color.getRed()));

        Image image = new Image();
        image.setFile(imageFile.toString());
        box.setBackgroundImage(image);
        box.setBackgroundColor(Color.getYellow());
        page.getParagraphs().add(box);

        document.save(outputFile.toString());
    }
}