Add Text Watermark and Image Watermark to PDF in Java
Tags: java, pdf, watermark, freelibrary
Published: 2020-02-18 08:40:26

---
title: Add Text Watermark and Image Watermark to PDF in Java
published: true
description: This article shows how to add text watermark and image watermark to a PDF file in Java.
tags: java, pdf, watermark, free library
---
A watermark is text or an image that appears either in front of or behind existing document content. For example, you could apply a “Confidential” watermark to pages with sensitive information or apply an image watermark like your company’s logo to pages associated with your company. This article will show you how to add text watermark and image watermark to an existing PDF file in Java using [Free Spire.PDF for Java](https://www.e-iceblue.com/Introduce/free-pdf-for-java.html) library.

##Installation##

Before coding, please download Free Spire.PDF for Java package through [this link](https://www.e-iceblue.com/Download/pdf-for-java-free.html), unzip the package and then add Spire.Pdf.jar in lib folder into your application as dependency.

##Using the code##

**Add Text Watermark**
```java
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){

        //load a PDF file
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromFile("Sample.pdf");

        //get the first page
        PdfPageBase page = pdf.getPages().get(0);

        Dimension2D dimension2D = new Dimension();
        dimension2D.setSize(page.getCanvas().getClientSize().getWidth() / 2, page.getCanvas().getClientSize().getHeight() / 3);
        //watermark text
        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("Confidential", new PdfTrueTypeFont(new Font("Arial Unicode MS",Font.PLAIN,30),true), PdfBrushes.getRed(), 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());
        //draw watermark text to the page
        page.getCanvas().drawRectangle(brush, loRect);

        //save the resultant file
        pdf.saveToFile("TextWatermark.pdf");
        //close
        pdf.close();
    }
}
```
The following screenshot shows the output of adding text watermark to PDF file.
![Adding text watermark](https://dev-to-uploads.s3.amazonaws.com/i/usffb5s45mty9e9l5xfv.png)
 
**Add Image Watermark**
```java
import com.spire.pdf.*;
import java.awt.geom.*;

public class ImageWatermark {
    public static void main(String[] args){
        //load a PDF file
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromFile("Sample.pdf");

        //get the first page
        PdfPageBase page = pdf.getPages().get(0);
        //set background image
        page.setBackgroundImage("watermark.png");
        Rectangle2D rect = new Rectangle2D.Float();
        rect.setFrame(page.getClientSize().getWidth() / 2 - 100, page.getClientSize().getHeight() / 2 - 200, 200, 300);
        //set background region
        page.setBackgroundRegion(rect);

        //save the resultant file
        pdf.saveToFile("ImageWatermark.pdf");
        //close
        pdf.close();
    }
}
```
The following screenshot shows the output of adding image watermark to PDF file.
![Adding image watermark](https://dev-to-uploads.s3.amazonaws.com/i/cldpu2r54hytni265doq.png)