DEV Community

E-iceblue Product Family
E-iceblue Product Family

Posted on

Add Text Watermark and Image Watermark to PDF in Java

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 library.

Installation

Before coding, please download Free Spire.PDF for Java package through this link, unzip the package and then add Spire.Pdf.jar in lib folder into your application as dependency.

Using the code

Add Text Watermark

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

Add Image Watermark

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

Top comments (0)