This article will demonstrate how to use Spire.PDF for Java to add image watermark to PDF.
We need to load the image by the method loadFromFile() and then, set the PDF background image to be the image watermark and then set the background region for the image.
import com.spire.pdf.*; import java.awt.geom.Rectangle2D; public class watermark { public static void main(String[] args) { //Create a pdf document and load the sample document from file PdfDocument doc = new PdfDocument(); doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.pdf"); //Get the first page PdfPageBase page = doc.getPages().get(0); //Load the image and set it as background image page.setBackgroundImage("C:\\Users\\Administrator\\Desktop\\logo.png"); //Set the background region Rectangle2D.Float rect = new Rectangle2D.Float(); rect.setRect(280, 300, 150, 150); page.setBackgroundRegion(rect); //Save pdf file doc.saveToFile("output/imageWaterMark.pdf"); doc.close(); } }