PDF watermarks serve as a means of identifying ownership, indicating confidentiality, or showing status of the PDF file by placing text or images under or in front of its content. This article will show you how to add a one-line or multi-line text watermark to PDF in Java, and how to set the position, opacity, size, and angle of the watermarks, by using Spire.PDF for Java.
Add Spire.Pdf.jar as dependency
If you are working on a maven project, you can include the dependency in pom.xml file using this:
<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>8.8.3</version> </dependency> </dependencies>
If you are not using maven, then you can find the required jar files from the zip file available in this location. Include all the jar files into the application lib folder to run the sample code given in this tutorial.
Add One-Line Text Watermark to PDF in Java
Below are the steps to add a one-line text watermark to PDF using Spire.PDF for Java.
- Create a PdfDocument object.
- Load a PDF file using PdfDocument.loadFromFile() method.
- Specify the watermark text and font.
- Move the coordinate system of a page to an appropriate position.
- Draw the text watermark at the specified location.
- Save the document to another PDF file using PdfDocument.saveToFile() method.
import com.spire.pdf.PdfDocument; import com.spire.pdf.PdfPageBase; import com.spire.pdf.graphics.PdfBrushes; import com.spire.pdf.graphics.PdfTrueTypeFont; import java.awt.*; import java.awt.geom.Dimension2D; public class AddOneLineTextWatermark { public static void main(String[] args) { //Create a PdfDocument object PdfDocument pdf = new PdfDocument(); //Load a sample PDF document pdf.loadFromFile("C:/Users/Administrator/Desktop/Terms of Service.pdf"); //Create a PdfTrueTypeFont object PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", Font.PLAIN,50), true); //Set the watermark text String text = "CONFIDENTIAL"; //Measure the text size Dimension2D textSize = font.measureString(text); //Calculate the values of two offset variables, //which will be used to calculate the translation amount of the coordinate system float offset1 = (float)(textSize.getWidth() * Math.sqrt(2) / 4); float offset2 = (float)(textSize.getHeight() * Math.sqrt(2) / 4); //Traverse all the pages in the document for (int i = 0; i < pdf.getPages().getCount(); i++) { PdfPageBase page = pdf.getPages().get(i); //Set the page transparency page.getCanvas().setTransparency(0.8f); //Translate the coordinate system by specified coordinates page.getCanvas().translateTransform(page.getCanvas().getSize().getWidth() / 2 - offset1 - offset2, page.getCanvas().getSize().getHeight() / 2 + offset1 - offset2); //Rotate the coordinate system 45 degrees counterclockwise page.getCanvas().rotateTransform(-45); //Draw watermark text on the page page.getCanvas().drawString(text, font, PdfBrushes.getDarkGray(), 0, 0); } //Save the changes to another file pdf.saveToFile("output/OneLineTextWatermark.pdf"); } }
Add Multi-Line Text Watermark to PDF in Java
The following are the steps add a muti-line text watermark to PDF.
- Create a PdfDocument object
- Load a sample PDF document using PdfDocument.loadFromFile() method.
- Create a custom method insertMultiLineTextWatermark(PdfPageBase page, String watermarkText, PdfTrueTypeFont font, int rowNum, int columnNum) to add multi-line text watermarks to a PDF page. The parameters rowNum and columnNum specify the row and column number of the tiled watermarks.
- Traverse all pages in the document, and call the custom method insertMultiLineTextWatermark() to apply watermarks to each page.
- Save the document to another file using PdfDocument.saveToFile() method.
import com.spire.pdf.PdfDocument; import com.spire.pdf.PdfPageBase; import com.spire.pdf.graphics.PdfBrushes; import com.spire.pdf.graphics.PdfTilingBrush; import com.spire.pdf.graphics.PdfTrueTypeFont; import java.awt.*; import java.awt.geom.Dimension2D; public class AddMultiLineWatermarkToPDF { public static void main(String[] args) { //Create a PdfDocument instance PdfDocument pdf = new PdfDocument(); //Load a sample PDF document pdf.loadFromFile("C:/Users/Administrator/Desktop/Terms of Service.pdf"); //Create a PdfTrueTypeFont object PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", Font.PLAIN, 20), true); //Traverse all the pages for (int i = 0; i < pdf.getPages().getCount(); i++) { //Call insertMultiLineTextWatermark() method to add text watermarks to the specified page insertMultiLineTextWatermark(pdf.getPages().get(i), "Internal Use Only", font, 3, 3); } //Save the document to another file pdf.saveToFile("output/MultiLineTextWatermark.pdf"); } //Create a custom method to insert multi-line text watermarks to a page static void insertMultiLineTextWatermark(PdfPageBase page, String watermarkText, PdfTrueTypeFont font, int rowNum, int columnNum) { //Measure the text size Dimension2D textSize = font.measureString(watermarkText); //Calculate the values of two offset variables, which will be used to calculate the translation amount of coordinate system double offset1 = textSize.getWidth() * Math.sqrt(2) / 4; double offset2 = textSize.getHeight() * Math.sqrt(2) / 4; //Create a tile brush PdfTilingBrush brush = new PdfTilingBrush(new Dimension((int) page.getActualSize().getWidth() / columnNum, (int) page.getActualSize().getHeight() / rowNum)); brush.getGraphics().setTransparency(0.3f); brush.getGraphics().save(); brush.getGraphics().translateTransform(brush.getSize().getWidth() / 2 - offset1 - offset2, brush.getSize().getHeight() / 2 + offset1 - offset2); brush.getGraphics().rotateTransform(-45); //Draw watermark text on the tile brush brush.getGraphics().drawString(watermarkText, font, PdfBrushes.getDarkGray(), 0, 0); brush.getGraphics().restore(); //Draw a rectangle (that covers the whole page) using the tile brush page.getCanvas().drawRectangle(brush, 0, 0, page.getActualSize().getWidth(), page.getActualSize().getHeight()); } }
Top comments (3)
Nice article. Is JDK 8 enough to write this app ?
Yes, it is.
Thanks. I will try the pdf library you use here.