Add Text/Image Watermark to PDF Using Java

Watermarks are lightly shaded words or pictures that are often used to indicate the importance of a document or to protect it from copying. This article will demonstrate how to add text and image watermarks to PDF document using a free library (Free Spire.PDF for Java).

Import jar dependency (2 Methods)
● Download the free library and unzip it.Then add the Spire.Pdf.jar file to your project as dependency.

● Directly add the jar dependency to maven project by adding the following configurations to the pom.xml.

<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.free</artifactId>
<version>4.3.0</version>
</dependency>
</dependencies>

Add text watermark to PDF:

import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.*;


public class Textwatermark {
public static void main(String[] args) {
//create a PdfDocument instance
PdfDocument pdf = new PdfDocument();

//load the sample document
pdf.loadFromFile("Walt.pdf");
//get the first page of the PDF
PdfPageBase page = pdf.getPages().get(0);

//use insertWatermark()to insert the watermark
insertWatermark
(page, "INTERNAL USE");
//save the document to file
pdf.saveToFile("out/TextWatermark.pdf");
}

static void insertWatermark(PdfPageBase page, String watermark) {
Dimension2D dimension2D = new Dimension();
dimension2D.setSize(page.getCanvas().getClientSize().getWidth() / 2, page.getCanvas().getClientSize().getHeight() / 3);
PdfTilingBrush brush = new PdfTilingBrush(dimension2D);
brush.getGraphics().setTransparency(0.3F);
brush.getGraphics().save();
brush.getGraphics().translateTransform((float) brush.getSize().getWidth() / 2, (float) brush.getSize().getHeight() / 2);
brush.getGraphics().rotateTransform(-45);
brush.getGraphics().drawString(watermark, new PdfFont(PdfFontFamily.Times_Roman, 28), PdfBrushes.getViolet(), 0, 0, new PdfStringFormat(PdfTextAlignment.Center));
brush.getGraphics().restore();
brush.getGraphics().setTransparency(1);
Rectangle2D loRect = new Rectangle2D.Float();
loRect.setFrame(new Point2D.Float(0, 0), page.getCanvas().getClientSize());
page.getCanvas().drawRectangle(brush, loRect);
}
}

Add image watermark to PDF:

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("Walt.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.jpg");

//Set the background region
Rectangle2D.Float rect = new Rectangle2D.Float();
rect.setRect(280, 280, 150, 150);
page.setBackgroundRegion(rect);

//Save pdf file
doc.saveToFile("out/ImageWatermark.pdf");
doc.close();

}
}

Sharing Java Code