Logo

dev-resources.site

for different kinds of informations.

Add Text Watermark and Image Watermark to PDF in Java

Published at
2/18/2020
Categories
java
pdf
watermark
freelibrary
Author
eiceblue
Categories
4 categories in total
java
open
pdf
open
watermark
open
freelibrary
open
Author
8 person written this
eiceblue
open
Add Text Watermark and Image Watermark to PDF in Java

A watermark is text or an image that appears either in front of or behind existing document content. For example, you could apply a “Confidential” watermark to pages with sensitive information or apply an image watermark like your company’s logo to pages associated with your company. This article will show you how to add text watermark and image watermark to an existing PDF file in Java using Free Spire.PDF for Java library.

Installation

Before coding, please download Free Spire.PDF for Java package through this link, unzip the package and then add Spire.Pdf.jar in lib folder into your application as dependency.

Using the code

Add Text Watermark

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){

        //load a PDF file
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromFile("Sample.pdf");

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

        Dimension2D dimension2D = new Dimension();
        dimension2D.setSize(page.getCanvas().getClientSize().getWidth() / 2, page.getCanvas().getClientSize().getHeight() / 3);
        //watermark text
        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("Confidential", new PdfTrueTypeFont(new Font("Arial Unicode MS",Font.PLAIN,30),true), PdfBrushes.getRed(), 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());
        //draw watermark text to the page
        page.getCanvas().drawRectangle(brush, loRect);

        //save the resultant file
        pdf.saveToFile("TextWatermark.pdf");
        //close
        pdf.close();
    }
}

The following screenshot shows the output of adding text watermark to PDF file.
Adding text watermark

Add Image Watermark

import com.spire.pdf.*;
import java.awt.geom.*;

public class ImageWatermark {
    public static void main(String[] args){
        //load a PDF file
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromFile("Sample.pdf");

        //get the first page
        PdfPageBase page = pdf.getPages().get(0);
        //set background image
        page.setBackgroundImage("watermark.png");
        Rectangle2D rect = new Rectangle2D.Float();
        rect.setFrame(page.getClientSize().getWidth() / 2 - 100, page.getClientSize().getHeight() / 2 - 200, 200, 300);
        //set background region
        page.setBackgroundRegion(rect);

        //save the resultant file
        pdf.saveToFile("ImageWatermark.pdf");
        //close
        pdf.close();
    }
}

The following screenshot shows the output of adding image watermark to PDF file.
Adding image watermark

watermark Article's
27 articles in total
Favicon
TikTokio: The Ultimate TikTok Video Downloader Without Watermark
Favicon
Dynamic watermarking on the JVM
Favicon
Java-Fähigkeiten: Einfügen von Text- oder Bild-Wasserzeichen in Word-Dokumente mit Java-Programmierung
Favicon
EinfĂĽgen eines Textwasserzeichens in PDF mit Java
Favicon
Wasserzeichen zu PDF-Dokumenten mit Python hinzufĂĽgen
Favicon
Comment supprimer les filigranes des documents Word avec Python
Favicon
HinzufĂĽgen eines Text- oder Bild-Wasserzeichens zu einem Word-Dokument mit Python
Favicon
Comment ajouter des filigranes Ă  Word avec Python
Favicon
Comment ajouter des filigranes de texte Ă  une ou plusieurs lignes dans un fichier PDF avec C# et VB.NET
Favicon
Wie man Text- und Bild-Wasserzeichen zu einem Word-Dokument in Java hinzufĂĽgt
Favicon
Comment ajouter des filigranes d'image aux fichiers PDF avec C# et VB.NET
Favicon
HinzufĂĽgen eines Text- oder Bild-Wasserzeichens zu einem Word-Dokument
Favicon
Insert Watermarks into Word Documents with C++
Favicon
Tencent Cloud COS + CI for a Comprehensive Image Solution
Favicon
Java: Add One-Line or Multi-Line Text Watermarks to PDF
Favicon
Java Add Watermark to Word document
Favicon
Add Watermark to Excel Spreadsheets in Java
Favicon
Adding text or image watermark programmatically
Favicon
awesome github profile
Favicon
Java add text watermark and image watermark to word document
Favicon
How to add image and text watermark to presentation slides
Favicon
How to make a logo watermark tool in Golang on any Unix Distribution
Favicon
Add watermark to the converted document in C# or Java
Favicon
Watermark batch of images
Favicon
Adding, Removing Text Watermark & Image Watermark in Word using Java
Favicon
Add Text Watermark and Image Watermark to PDF in Java
Favicon
How to add text and image watermark to PDF files in Java applications

Featured ones: