DEV Community

loading...

How to add text and image watermark to PDF files in Java applications

eiceblue profile image E-iceblue Product Family ・3 min read

Watermark is generally used in commercial field to show the documents are authorized that others can¡¯t copy to use them directly, and sometimes, the watermark will show the basic status of the documents, such as draft status or the final version of the documents. This article will demonstrate how to add image watermark and text watermark into the PDF files using free Spire.PDF on Java applications.

Java add Text Watermark to PDF. We need to draw the text string that we want to use for the watermark. And then we need to set the text font, color, position and text format to fit the PDF page suitably. We can do it easily by calling the method: drawString(string s, PdfFont font, PdfBrushes brush, float x, float y, PdfStringFormat format). Please see the code snippet below:

import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.io.*;

public class textWatermark {
    public static void main(String[] args) throws IOException {

        //load the sample document
        PdfDocument doc = new PdfDocument();
        doc.loadFromFile("Sample.pdf");

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

        //insert text watermark
        insertWatermark(page,"Draft version");

        //save the file
        doc.saveToFile("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);
    }
}

The effective screenshot of the text watermark:
Text watermark

Java add Image Watermark to PDF. It is quite easy that we only need to load the image and set it as the background for PDF by calling page.setBackgroundImage(string filename) method to set the image as watermark for PDF.

import com.spire.pdf.*;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;

public class imageWatermark {
           public static void main(String[] args) {
                //create a pdf document and load file from disk
                PdfDocument doc = new PdfDocument();
                doc.loadFromFile("Sample.pdf");

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

                //Set background image
                page.setBackgroundImage("E-logo.png");

                //save pdf file
                doc.saveToFile("Imagewatermark.pdf", FileFormat.PDF);
            }
        }

Image background as watermark:
image watermark

From the screenshot, we could find that the result is not pretty enough. Spire.PDF for Java offers a method page.drawImage() to add an image to PDF and adjust the size and position to make the image beautiful.

import com.spire.pdf.*;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.PdfBlendMode;
import com.spire.pdf.graphics.PdfImage;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class imageWatermark {
           public static void main(String[] args) throws IOException {
                //create a pdf document and load file from disk
                PdfDocument doc = new PdfDocument();
                doc.loadFromFile("Sample.pdf");

               //load an image
               BufferedImage image = ImageIO.read(new File("E-logo.png"));

               //adjust image size
               int width = image.getWidth();
               int height = image.getHeight();
               float scale = 1f;
               BufferedImage scaleImage = new BufferedImage((int)(width * scale), (int)(height * scale), BufferedImage.TYPE_INT_ARGB );
               Graphics2D g = scaleImage.createGraphics();
               g.drawImage(image, 0, 0, (int)(width * scale), (int)(height * scale), null);
               g.dispose();

               //insert an image into the first PDF page at specific position
               PdfImage pdfImage = PdfImage.fromImage(scaleImage);
               PdfPageBase page = doc.getPages().get(0);
               page.getCanvas().save();
               page.getCanvas().setTransparency(0.5f, 0.5f, PdfBlendMode.Multiply);
               page.getCanvas().drawImage(pdfImage, new Point2D.Float(160, 260));
               page.getCanvas().restore();

               //save the Pdf document
               doc.saveToFile("ImageWatermark2.pdf", FileFormat.PDF);
            }
        }

Effective screenshot:

Image Watermar k

Conclusion:
Free Spire.PDF is a professional PDF API that enables developers to create, read, write, modify, convert and print PDF on Java applications. You could use it to operate PDF files for many rich features.

Discussion (0)

pic
Editor guide