Java adds watermarks to PDF -- Text / image watermarks

Keywords: Java

Watermark is a very commonly used anti-counterfeiting means, commonly used in various documents, materials, etc. Common watermarks include text type watermarks, image or logo type watermarks. The following Java example will use the insertTextWatermark(PdfPageBase page, String watermark) method and page.SetBackgroundImage(String arg0) method to set the text watermark and picture watermark, respectively. The following will demonstrate the specific code operation, the content for reference.

Tools: Free spirit.pdf for Java v2.0.0 (free version)

Jar file import:

Step 1: first, create a new folder in the Java program to be named lib. Download the package of Spire.PDF for Java, extract it, and copy the two files Spire.Pdf.jar and Spire.Common.jar in the subfolder lib under the extracted folder to the new folder lib, as shown below:

Step 2: after creating the folder, reference two files: select the two files, right-click, and select "Build Path" – "Add to Build Path".

Java code example (for reference)

[example 1] 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) {
        //Establish PdfDocument Class objects
        PdfDocument pdf = new PdfDocument();
        //Load test document
        pdf.loadFromFile("test.pdf");
        
        //Get the first page in the test document
        PdfPageBase page = pdf.getPages().get(0);

        //call insertWatermark()Method to add a text watermark
        insertWatermark(page, "TOP SECRET");
        //Save document
        pdf.saveToFile("out/textWaterMark.pdf");
    }

    static void insertWatermark(PdfPageBase page, String watermark) {
        Dimension2D dimension2D = new Dimension();
        dimension2D.setSize(page.getCanvas().getClientSize().getWidth() / 5, page.getCanvas().getClientSize().getHeight() / 5);
        PdfTilingBrush brush = new PdfTilingBrush(dimension2D);
        brush.getGraphics().setTransparency(0.4F);
        brush.getGraphics().save();
        brush.getGraphics().translateTransform((float) brush.getSize().getWidth() / 4, (float) brush.getSize().getHeight() / 5);    
        brush.getGraphics().rotateTransform(-45);
        brush.getGraphics().drawString(watermark, new PdfFont(PdfFontFamily.Helvetica, 15), 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);
    }
}

Text watermark adding effect:

[example 2] add image watermark

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

public class watermark {

public static void main(String[] args) {

//instantiation PdfDocument Class and load the test document
PdfDocument doc = new PdfDocument();
doc.loadFromFile("test.pdf");

//Get document page 1
PdfPageBase page = doc.getPages().get(0);

//Load picture, set as background watermark
page.setBackgroundImage("logo.png");

//Specify the location and image size of the watermark in the document
Rectangle2D.Float rect = new Rectangle2D.Float();
rect.setRect(150, 150, 300, 150);
page.setBackgroundRegion(rect);

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

Image watermark adding effect:

(end of text)

Reprint please indicate the source!

Posted by alsouno on Sat, 07 Dec 2019 01:04:58 -0800