Thursday, 3 June 2021

Add Text Watermarks or Image Watermarks to PDF documents in Java

A watermark is text or an image which appears behind the existing content of a document, and it is usually used to indicate certain information about the owner of the document, such as name of a company, an organization logo, which can help to prevent the document from being copied or allow others to know where the document is copied from and who owns the right.

In this article, I’ll introduce how to add multiple text watermarks or an image watermark to the specified page of a PDF document using Java codes, and the operation don’t need Adobe Acrobat installed on your project.

Add Spire.Pdf.jar to Intellij IDEA

It’s worth mentioning that I used a free third-party library called Free Spire.PDF for Java in this tutorial and before running codes, we need a Jar file called Spire.Pdf.jar located in the library to Intellij IDEA. There are two methods to add it. One is that download a package from the link, unzip it and find the Jar file in the “lib” folder, finally manually add it to IDEA. The other is that directly use the following Maven configurations in Intellij IDEA to reference it.

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

Add multiple text watermarks 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\\Test1\\Desktop\\Sample.pdf");
//get the first page of the PDF
PdfPageBase page = pdf.getPages().get(0);

//use insertWatermark()to insert the watermark
insertWatermark(page, "CONFIDENTIAL");
//save the resulting document
pdf.saveToFile("output/AddTextWaterMark.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(0);
Rectangle2D loRect = new Rectangle2D.Float();
loRect.setFrame(new Point2D.Float(0, 0), page.getCanvas().getClientSize());
page.getCanvas().drawRectangle(brush, loRect);
}
}

Output


Add an image watermark to PDF

import com.spire.pdf.*;
import java.awt.geom.Rectangle2D;
public class ImageWatermark {
public static void main(String[] args) {
//Create a pdf document and load the sample document
PdfDocument doc = new PdfDocument();
doc.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pdf");

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

//Load the image and set it as background image
page.setBackgroundImage("C:\\Users\\Test1\\Desktop\\logo.png");

//Set the background region
Rectangle2D.Float rect = new Rectangle2D.Float();
rect.setRect(240, 280, 150, 150);
page.setBackgroundRegion(rect);

//Save the resulting file
doc.saveToFile("output/addImageWaterMark.pdf");
doc.close();
}
}

Output




No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...