Skip to content

A lightweight Java library for adding watermarks to various file types, including PDFs and images

License

Notifications You must be signed in to change notification settings

OlegCheban/WaterMarkIt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build javadoc Code climate Hits-of-Code PRs Welcome License

WaterMarkIt

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.

Features

  • 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
  • Page orientation support: Full support for both portrait and landscape orientations.

  • Supported Formats:

    • PDF
    • JPEG
    • PNG
    • TIFF
    • BMP
  • Drawn Watermarks: The library provides the WatermarkingMethod.DRAW method to add watermarks to PDF files that can't be easily removed. This mode renders an image based on a PDF page and replaces all layers of the page with the image.

  • Multithreading: Leverages a thread pool for efficient watermarking. Particularly useful for the WatermarkingMethod.DRAW method and multi-page files such as PDFs, enabling parallel watermarking with a separate thread for each page.

Getting Started

Prerequisites

  • Java 11 or higher
  • Maven or Gradle

Installation

For Maven, add the following dependency to your pom.xml:

<dependency>
    <groupId>io.github.watermark-lab</groupId>
    <artifactId>WaterMarkIt</artifactId>
    <version>1.3.1</version>
</dependency>

For Gradle, add the following to your build.gradle:

implementation 'io.github.watermark-lab:WaterMarkIt:1.3.1'

Usage

WatermarkService.create(
                //using a thread pool here isn't mandatory; use it only when necessary
                Executors.newFixedThreadPool(
                        Runtime.getRuntime().availableProcessors()
                )
        )
        .watermarkPDF(new File("path/to/file.pdf"))
           .withImage(new File("path/to/watermark.png"))
           .position(WatermarkPosition.CENTER).end()
           .opacity(0.2f)
        .and()
           .withText("WaterMarkIt")
               .color(Color.BLUE)
               .addTrademark()
               .end()           
           .position(WatermarkPosition.TILED)
               .adjust(35, 0)
               .horizontalSpacing(10)
               .end()
           .opacity(0.1f)
           .rotation(25)
           .size(110)
        .and()
           .withText(LocalDateTime.now().toString()).end()
           .position(WatermarkPosition.TOP_RIGHT)
               .adjust(0, -30)
               .end()
           .size(50)
        .apply()

Screenshot

Watermarking conditions

// skip the first page (the page index starts from 0)
WatermarkService.create()
    .watermarkPDF(document)
        .withText("Text-based Watermark").end()
            .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").end()
            .when(!isOwner)
    .apply()
// Apply watermark only if the document has more than 3 pages
WatermarkService.create()
    .watermarkPDF(document)
        .withText("Text-based Watermark").end()
            .documentFilter(document -> document.getNumberOfPages() > 3)
    .apply()  

Dependencies

  • 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.