JAVA insert text watermark to PDF

This article will demonstrate how to use Spire.PDF for Java to 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("C:\\Users\\Administrator\\Desktop\\Sample.pdf");
        //get the first page of the PDF
        PdfPageBase page = pdf.getPages().get(0);

        //use insertWatermark()to insert the watermark 
        insertWatermark(page, "E-ICEBLUE");
      //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.Helvetica, 24), 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);
    }
}

JAVA insert text watermark to PDF