Java: Add Watermark to PDF
In daily work and study, the popularization of PDF has brought great convenience for people, but at the same time, a lot of problems have occurred. It is easy to copy and spread the PDF documents, which makes the copyrights difficult to protect. It is also hard to distinguish information in the mass of files, for example, have you ever opened a folder with so many PDF documents used the same name? Even you cannot tell which one is the final modified version. Using watermarks can solve these problems for you now. This article will be divided two parts to demonstrate how to add watermark to PDF in Java applications.
- Add Text Watermark
- Add Image Watermark
Programming Environment
First of all, you're required to add the Free 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.free</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
Add Text Watermark
The following are the steps to add text watermark to PDF.
- Create a PdfDocument instance.
- Load the sample document using PdfDocument.loadFromFile() method.
- Get the first page of the PDF using PdfPageBase.getPages() method.
- Insert the watermark using insertWatermark() method.
- Save the document to file using PdfDocument.saveToFile () method.
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("Yellowstone National Park 1.pdf");
//get the first page of the PDF
PdfPageBase page = pdf.getPages().get(0);
//use insertWatermark()to insert the watermark
insertWatermark(page, "INTERNAL USE");
//save the document to file
pdf.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);
}
}
Add Image Watermark
The following are the steps to add image watermark to PDF.
- Create a PDF document and load the sample document from file using PdfDocument.loadFromFile() method.
- Get the first page using PdfPageBase.getPages()method.
- Load the image and set it as background image using PdfPageBase.setBackgroundImage() method.
- Set the background region using Rectangle2D.Float.setRect() method.
- Save PDF file using PdfDocument.saveToFile() method.
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 from filePdfDocument doc = new PdfDocument();doc.loadFromFile("Yellowstone National Park 1.pdf");//Get the first pagePdfPageBase page = doc.getPages().get(0);//Load the image and set it as background imagepage.setBackgroundImage("logo.jpg");//Set the background region
Rectangle2D.Float rect = new Rectangle2D.Float();rect.setRect(280, 300, 150, 150);page.setBackgroundRegion(rect);//Save pdf filedoc.saveToFile("imageWaterMark.pdf");doc.close();}}
Conclusion:
In this post, you have learned how to add watermarks to PDF in Java. Not only that, we also have other functions, such as, Java: Extract Images from a PDF Document, Java: Find and Replace Text in PDF and so on. Apart from that, if you'd like to learn more, you can visit this link to explore more about for Spire.PDF for Java.
Comments
Post a Comment