Java: Insert Text Watermarks to PDF

Text watermarks are digital marks consisting of formatted text displayed semi-transparently on PDF pages. They are often used to show copyright, author, company, or other document attributes. Unlike comments or stamps, text watermarks are integrated into PDF documents and can’t be removed easily. Therefore, they are also used to protect copyright or ownership of documents. This article will show how to use Spire.PDF for Java to insert text watermarks into PDF documents.

Install Spire.PDF for Java

First of all, you're required to add the Spire.Pdf.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf</artifactId>
        <version>10.11.2</version>
    </dependency>
</dependencies>
    

Insert a Single Text Watermark to a PDF Document

A single text watermark is a text watermark that is displayed only once in the middle of a PDF page. The steps for inserting a single text watermark into a PDF document using Spire.PDF for Java are as follows.

  • Create a PdfDocument class instance.
  • Load a PDF document using PdfDocument.loadFromFile() method.
  • Loop through the pages to insert watermarks.
  • Get a specific page using PdfDocument.getPages().get() method.
  • Set the transparency of the watermark using PdfPageBase.getCanvas().setTransparency() method.
  • Set the translation amount of the coordinate using PdfPageBase.getCanvas().translateTransform() method.
  • Set the rotation angle of the watermark using PdfPageBase.getCanvas().rotateTransform() method.
  • Draw the watermark on the page using PdfPageBase.getCanvas().drawString() method.
  • Save the document using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.*;
import com.spire.pdf.graphics.*;

import java.awt.*;

public class insertSingleTextWaterMark {
    public static void main(String[] args) {

        //Create a PdfDocument class instance
        PdfDocument pdf = new PdfDocument();

        //Load a PDF document
        pdf.loadFromFile("We’ve Always Been Distracted.pdf");

        //Loop through all pages in the PDF document to insert watermarks
        String text = "DRAFT";
        PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", Font.BOLD, 50));
        float set1 = (float) (font.measureString(text).getWidth() * Math.sqrt(2)/4);
        float set2 = (float) (font.measureString(text).getHeight() * Math.sqrt(2)/4);
        for (int i = 0; i < pdf.getPages().getCount(); i++){
            //Get a page
            PdfPageBase page = pdf.getPages().get(i);
            //Set the transparency of the watermark
            page.getCanvas().setTransparency(0.8f);
            //Set translation amount of the coordinate
            page.getCanvas().translateTransform(page.getCanvas().getSize().getWidth()/2 - set1 - set2, page.getCanvas().getSize().getHeight()/2 + set1 - set2);
            //Set the rotation angle
            page.getCanvas().rotateTransform(-45);
            //Draw the watermark text on the page
            page.getCanvas().drawString(text, font, PdfBrushes.getDarkGray(), 0, 0);
        }

        //Save the document
        pdf.saveToFile("SingleTextWatermark.pdf");
    }
}

Java: Insert Text Watermarks to PDF

Insert a Tiled Text Watermark to a PDF Document

Tiled watermarks are displayed multiple times on one page. They are used more often to protect the copyright and ownership of documents. We can create a custom method insertTextWatermark() based on classes and methods of Spire.PDF for Java to insert tiled text watermarks into a PDF document. The detailed steps are as follows.

  • Create a PdfDocument class instance.
  • Load a PDF document using PdfDocument.loadFromFile() method.
  • Loop through the pages in the document to insert watermarks.
  • Get a page in the document using PdfDocument.getPages().get() method.
  • Insert a text watermark using the custom method insertTextWatermark().
  • Save the document using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import com.spire.pdf.htmlconverter.qt.Size;

import java.awt.*;
import java.awt.geom.*;


public class insertTiledTextWatermark {
    public static void main(String[] args) {

        //Create a PdfDocument class instance
        PdfDocument pdf = new PdfDocument();

        //Load a PDF file
        pdf.loadFromFile("We’ve Always Been Distracted.pdf");

        //Loop through the pages to insert watermarks
        PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", Font.BOLD, 20));
        for (int i = 0; i < pdf.getPages().getCount(); i++){
            PdfPageBase pageBase = pdf.getPages().get(i);
            insertTextWatermark(pageBase, font, "CONFIDENTIAL", 3, 3);
        }

        //Save the document
        pdf.saveToFile("TiledTextWatermark.pdf");
    }

    static void insertTextWatermark(PdfPageBase page, PdfTrueTypeFont font, String watermark, int row, int column) {

        //Calculate the values of two offset variables to be used to calculate the translation amount of the coordinate
        float set1 = (float)(font.measureString(watermark).getWidth() * Math.sqrt(2)/4);
        float set2 = (float)(font.measureString(watermark).getHeight() * Math.sqrt(2)/4);

        //Create a tile brush
        PdfTilingBrush brush = new PdfTilingBrush(new Dimension((int) (page.getActualSize().getWidth()/column), (int) (page.getActualSize().getHeight()/row)));
        brush.getGraphics().setTransparency(0.3f);
        brush.getGraphics().save();
        brush.getGraphics().translateTransform(brush.getSize().getWidth()/2 - set1 - set2, brush.getSize().getHeight()/2 + set1 - set2);
        brush.getGraphics().rotateTransform(-45);

        //Draw watermark text on the tile brush
        brush.getGraphics().drawString(watermark, font, PdfBrushes.getViolet(), 0, 0);
        brush.getGraphics().restore();

        //Draw watermark with the tile brush
        page.getCanvas().drawRectangle(brush, new Rectangle(new Point(0, 0), new Dimension((int)(page.getActualSize().getWidth()), (int)(page.getActualSize().getHeight()))));
    }
}

Java: Insert Text Watermarks to PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.