If you need to add Watermark to PDF in Java, a visual overlay can protect your documents from unauthorized use. Conholdate.Total for Java provides a powerful SDK that simplifies PDF manipulation on the server side. In this guide you will see a complete, ready‑to‑run example, learn how each line works, set up the Maven dependency, and discover options to fine‑tune opacity, rotation, and alignment for professional results.
Complete Code Example: Adding a Text Watermark to PDF in Java
The following example demonstrates how to add a text watermark to a PDF using Conholdate.Total for Java.
import com.groupdocs.watermark.Watermark;
import com.groupdocs.watermark.options.PdfLoadOptions;
import com.groupdocs.watermark.options.PdfSaveOptions;
import com.groupdocs.watermark.watermarks.TextWatermark;
import com.groupdocs.watermark.watermarks.HorizontalAlignment;
import com.groupdocs.watermark.watermarks.VerticalAlignment;
import java.awt.Color;
import java.awt.Font;
public class AddPdfWatermark {
public static void main(String[] args) {
String inputFile = "input.pdf";
String outputFile = "output.pdf";
// Create a text watermark
Font font = new Font("Arial", Font.BOLD, 48);
TextWatermark textWatermark = new TextWatermark("CONFIDENTIAL", font);
textWatermark.setColor(Color.RED);
textWatermark.setOpacity(0.3);
textWatermark.setRotationAngle(45);
textWatermark.setHorizontalAlignment(HorizontalAlignment.Center);
textWatermark.setVerticalAlignment(VerticalAlignment.Center);
// Load options for the source PDF (enable caching for large files)
PdfLoadOptions loadOptions = new PdfLoadOptions(inputFile);
loadOptions.setCacheSize(100 * 1024 * 1024); // 100 MB cache
// Initialize the Watermark processor
Watermark watermark = new Watermark();
try {
// Apply the watermark to every page of the PDF
watermark.add(textWatermark, loadOptions);
// Save the watermarked PDF
PdfSaveOptions saveOptions = new PdfSaveOptions();
watermark.save(outputFile, saveOptions);
} catch (Exception e) {
System.err.println("Failed to add watermark: " + e.getMessage());
e.printStackTrace();
}
}
}
Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update the file paths (
input.pdf,output.pdf, etc.) to match your actual file locations, verify that all required dependencies are properly installed, and test thoroughly in your development environment. If you encounter any issues, please refer to the official documentation or reach out to the support team for assistance.
Understanding the Add Watermark to PDF in Java Code
The add Watermark to PDF in Java code follows a clear sequence that you can adapt for any PDF document.
- Initialize the Watermark Processor -
Watermark watermark = new Watermark();creates the engine that will apply watermarks. - Create a TextWatermark - The
TextWatermarkobject holds the text, font, color, opacity, and rotation.Font font = new Font("Arial", Font.BOLD, 48); TextWatermark textWatermark = new TextWatermark("CONFIDENTIAL", font); - Configure Appearance - Methods such as
setColor,setOpacity, andsetRotationAngledefine how the watermark looks on the page. - Load the Source PDF -
PdfLoadOptionsloads the input file and enables caching for large documents, which reduces memory pressure. - Apply and Save -
watermark.add(textWatermark, loadOptions);applies the watermark to every page, andwatermark.save(outputFile, saveOptions);writes the result.
Loading the Source PDF with Caching
When you work with multi‑megabyte PDFs, enabling a cache prevents the SDK from loading the entire document into memory. The line loadOptions.setCacheSize(100 * 1024 * 1024); reserves 100 MB for temporary storage, allowing the processor to stream pages as needed. This approach is especially useful in server environments where many concurrent watermarking jobs may run.
For detailed API information, see the Java API reference.
Getting the Environment Ready for PDF Watermarking
The add Watermark to PDF in Java workflow starts with adding the Maven dependency. After that, you need a compatible JDK and a small amount of configuration.
Prerequisites and Java Version
- Java 8 or higher is required; the SDK uses modern language features and streams.
- Maven 3.5+ simplifies dependency management.
- Ensure that your project’s
pom.xmlincludes the Conholdate repository so Maven can resolve the artifact.
<repositories>
<repository>
<id>conholdate-repo</id>
<name>Conholdate Maven Repository</name>
<url>https://repository.conholdate.com/repo/</url>
</repository>
</repositories>
<dependency>
<groupId>com.conholdate</groupId>
<artifactId>conholdate-total</artifactId>
<version>24.9</version>
<type>pom</type>
</dependency>
You can also obtain the JAR files directly from the download page. After adding the dependency, run mvn clean install to verify that the SDK is on your classpath.
Using the REST API (Optional)
If you prefer a language‑agnostic approach, Conholdate.Total also offers a REST endpoint that can add watermarks without any Java code. A simple curl command looks like this:
curl -X POST "https://api.conholdate.com/v1/pdf/watermark" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-F "file=@input.pdf" \
-F "text=CONFIDENTIAL" \
-F "font=Arial" \
-F "fontSize=48" \
-F "color=FF0000" \
-F "opacity=0.3" \
-F "angle=45" \
-F "horizontalAlignment=center" \
-F "verticalAlignment=center" \
-o output.pdf
The REST version follows the same logical steps load, configure, apply, and save so you can choose the approach that best fits your architecture.
Configuring Watermark Appearance and Position
The SDK exposes several properties that let you fine‑tune the watermark. Below are the most frequently adjusted settings.
Customizing Font and Color
You can replace the default Arial font with any TrueType font available on the server. Changing the color is as simple as passing a different java.awt.Color constant or creating a custom RGB value.
Font customFont = new Font("Times New Roman", Font.ITALIC, 36);
textWatermark.setColor(new Color(0, 102, 204)); // Deep blue
textWatermark.setFont(customFont);
Adjusting Opacity and Rotation
Opacity controls how transparent the watermark appears. Values range from 0.0 (completely invisible) to 1.0 (fully opaque). Rotation lets you tilt the text to make it harder to remove.
textWatermark.setOpacity(0.45); // 45% opacity
textWatermark.setRotationAngle(30); // 30 degrees clockwise
Aligning the Watermark on the Page
Horizontal and vertical alignment properties accept Left, Center, Right and Top, Center, Bottom. Centering is common for confidential stamps, but you can place the watermark in a corner for branding purposes.
textWatermark.setHorizontalAlignment(HorizontalAlignment.Right);
textWatermark.setVerticalAlignment(VerticalAlignment.Bottom);
If you want to add text Watermark to PDF in Java with a different layout on each page, you can modify these properties inside a loop that processes pages individually. The SDK also supports image watermarks, gradient fills, and multi‑line text if your use case requires more visual complexity.
Conclusion
Adding a watermark to PDF in Java is straightforward with Conholdate.Total for Java. The SDK handles loading, processing, and saving PDFs efficiently, even for large files thanks to built‑in caching. By customizing opacity, rotation, font, color, and alignment you can create professional‑looking watermarks that deter unauthorized distribution. Remember to acquire a proper license for production use; you can request a temporary license from the temporary license page or review pricing options on the pricing page. With the code and configuration steps covered, you are ready to integrate PDF watermarking into your Java applications.
FAQs
How can I add Watermark to PDF in Java without writing low‑level PDF code?
Use the Watermark class from Conholdate.Total for Java. It abstracts the PDF structure, letting you focus on watermark properties such as text, font, and placement.
What options are available to style the text watermark?
You can set the font (new Font(...)), size, style (bold, italic), color (setColor), opacity (setOpacity), rotation (setRotationAngle), and alignment (setHorizontalAlignment, setVerticalAlignment). These options are demonstrated in the complete code example.
Is the SDK able to process large PDFs efficiently?
Yes. By configuring PdfLoadOptions.setCacheSize, you enable streaming and caching, which reduces memory consumption for multi‑megabyte PDFs. This makes the add Watermark to PDF in Java workflow suitable for batch processing on servers.
Do I need to purchase a license to run this example?
A license is required for production deployments. Obtain a temporary license from the temporary license page or explore the full pricing details at the pricing page.
