A lightweight Java library for adding watermarks to various file types, including PDFs and images. The library was developed to address the challenge of creating watermarks that cannot be easily removed from PDF files. Many PDF editors allow users to edit even secured files, and when a watermark is added as a separate layer, it can be easily removed.
-
DSL: Provides a user-friendly way to configure and apply watermarks with ease.
-
Types of Watermarks:
- Text-based watermarks
- Image-based watermarks
-
Customizable Watermarks: Customize various aspects of your watermark, including:
- Text
- Color
- Size
- Position
- Rotation
- Opacity
- Trademark
- DPI
-
Supported Formats:
- JPEG
- PNG
- TIFF
- BMP
-
Unremovable Watermarks: Ensures that watermarks applied to PDF files are designed to be unremovable. The library provides the
WatermarkingMethod.DRAW
method to address the issue, whereas theWatermarkingMethod.OVERLAY
one adds a separate layer that can be easily removed. -
Multithreading: Leverages a thread pool for efficient watermarking. Particularly useful for the
WatermarkingMethod.DRAW
approach and multi-page files such as PDFs, enabling parallel watermarking with a separate thread for each page. -
Page orientation support: Full support for both portrait and landscape orientations.
- Java 11 or higher
- Maven or Gradle
For Maven, add the following dependency to your pom.xml
:
<dependency>
<groupId>io.github.watermark-lab</groupId>
<artifactId>WaterMarkIt</artifactId>
<version>1.3.0</version>
</dependency>
For Gradle, add the following to your build.gradle
:
implementation 'io.github.watermark-lab:WaterMarkIt:1.3.0'
WatermarkService.create(
//using a thread pool here isn't mandatory; use it only when necessary
Executors.newFixedThreadPool(
Runtime.getRuntime().availableProcessors()
)
)
.watermarkPDF(readFileFromClasspathAsBytes("ITIL.pdf")
.withImage(readFileFromClasspathAsBytes("logo.png"))
.position(WatermarkPosition.CENTER)
.opacity(0.2f)
.and()
.withText("WaterMarkIt")
.addTrademark()
.color(Color.BLUE)
.watermark()
.size(110)
.position(WatermarkPosition.TILED)
.adjust(35, 0)
.opacity(0.1f)
.rotation(25)
.and()
.withText(LocalDateTime.now().toString())
.watermark()
.position(WatermarkPosition.TOP_RIGHT)
.adjust(0, -30)
.size(50)
.apply()
// skip the first page (the page index starts from 0)
WatermarkService.create()
.watermarkPDF(document)
.withText("Text-based Watermark")
.pageFilter(index -> index >= 1)
.apply()
// don't add a watermark for the owner of the file; the owner has access to the original file.
WatermarkService.create()
.watermarkPDF(document)
.withText("Text-based Watermark")
.when(!isOwner)
.apply()
// Apply watermark only if the document has more than 3 pages
WatermarkService.create()
.watermarkPDF(document)
.withText("Text-based Watermark")
.documentFilter(document -> document.getNumberOfPages() > 3)
.apply()
- Apache PDFBox: Apache PDFBox - A Java library for working with PDF documents.
- JAI Image I/O: JAI Image I/O - Image I/O library for Java, supporting various image formats.
- commons-logging: Apache Commons Logging - A simple logging facade for Java.