Add watermark to Java to PDF document

Watermarks are statements that are always used in various documents, are a means of preventing counterfeiting, and set watermarks on letters. You can do it or import photos. The following content is converted into a PDF document through Java How to add a watermark effect, that is, share a text watermark with an image watermark.

Added 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) {
        
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromFile("Sample.pdf");
        PdfPageBase page = pdf.getPages().get(0);

        //use insertWatermark()to insert the watermark 
        insertWatermark(page, "E-ICEBLUE");
        pdf.saveToFile("out/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);
    }
}

JAVA-insert-text-watermark-to-PDF.png

Add image watermark:

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

public class watermark {

    public static void main(String[] args) {

        PdfDocument doc = new PdfDocument();
        doc.loadFromFile("Sample.pdf");
        PdfPageBase page = doc.getPages().get(0);  

        page.setBackgroundImage("logo.png ");

        Rectangle2D.Float rect = new Rectangle2D.Float();
        rect.setRect(280, 300, 150, 150);
        page.setBackgroundRegion(rect);

        doc.saveToFile("output/imageWaterMark.pdf");
        doc.close();

    }
}

JAVA-add-image-watermark-to-PDF.png

Recommended Posts

Add watermark to Java to PDF document
Add multi-letter watermark to Java Word
[Java] Add WordArt to Word document
Add / remove watermark to Java PowerPoint
Add Document to Azure Search Service (Java)
Java compressed PDF document
Java adds table to PDF
Java to extract PDF text content
Add, modify, delete JAVA PDF bookmarks
Java (add2)
Java (add)
Add, replace, delete Java PDF images
Java adds form fields to PDF
[Java] How to add data to List (add, addAll)
How to print a Java Word document
Add footnotes to Word documents in Java
Add SameSite attribute to cookie in Java
[Java] Introduction to Java
Introduction to java
Java 11 document summary
[Java] Article to add validation with Spring Boot 2.3.1.
Java adds page numbers to existing PDF documents
Launch Docker from Java to convert Office documents to PDF
Desktop: OpenCV Add WaterMark
[Java] Convert PDF version
Changes from Java 8 to Java 11
Sum from Java_1 to 100
[Java] Connect to MySQL
PKIX path building failed: Add SSL certificate to java
Use Maven to add your favorite Java library to your environment.
Kotlin's improvements to Java
[Java] PDF viewing settings
Java applications convert Word (DOC / DOCX) documents to PDF
From Java to Ruby !!
[Java] [XML signature] Add prefix and ID to Signature tag
Introduction to java command
java: Add date [Note]
[Java] How to use Map
How to lower java version
Migration from Cobol to JAVA
[Java] How to use Map
Convert Java Powerpoint to XPS
How to uninstall Java 8 (Mac)
Java to play with Function
Save Java PDF in Excel
How to add ActionText function
How to use java Optional
Java encryption and decryption PDF
New features from Java7 to Java8
How to minimize Java images
How to write java comments
How to use java class
Paging PDF with Java + PDFBox.jar
[Java] How to use Optional ②
Connect from Java to PostgreSQL
[Java] How to use removeAll ()
[Java] How to display Wingdings
Java turns Excel into PDF
Save Java HTML as PDF
[Java] Introduction to lambda expressions
4 Add println to the interpreter