1. Home
  2. Foxit PDF SDK for Windows
  3. How to create Watermarks using Foxit PDF SDK (Java)
  1. Home
  2. Foxit PDF SDK for Mac
  3. How to create Watermarks using Foxit PDF SDK (Java)
  1. Home
  2. Foxit PDF SDK for Linux
  3. How to create Watermarks using Foxit PDF SDK (Java)

How to create Watermarks using Foxit PDF SDK (Java)

A watermark is a type of PDF annotation and is widely used in PDF documents. A watermark is a visible embedded overlay on a document consisting of text, a logo, or a copyright notice. The purpose of a watermark is to brand your work and discourage its unauthorized use. Foxit PDF SDK provides APIs to work with watermarks, allowing applications to create, insert, release and remove watermarks.

Example:

How to create a text watermark and insert it into the first page

import com.foxit.sdk.common.Font;
import com.foxit.sdk.pdf.*;
import com.foxit.sdk.pdf.Watermark;
import com.foxit.sdk.pdf.WatermarkSettings;
import com.foxit.sdk.pdf.WatermarkTextProperties;
import com.foxit.sdk.common.Constants;
import static com.foxit.sdk.common.Constants.e_AlignmentCenter;
import static com.foxit.sdk.common.Font.e_StdIDTimesB;
import static com.foxit.sdk.pdf.WatermarkSettings.*;
import static com.foxit.sdk.pdf.WatermarkTextProperties.e_FontStyleNormal;
...
// Assuming PDFDoc doc has been loaded.
WatermarkSettings settings = new WatermarkSettings();
settings.setFlags(e_FlagASPageContents | e_FlagOnTop);
settings.setOffset_x(0);
settings.setOffset_y(0);
settings.setOpacity(90);
settings.setPosition(Constants.e_PosTopRight);
settings.setRotation(-45.f);
settings.setScale_x(1.f);
settings.setScale_y(1.f);
WatermarkTextProperties text_properties = new WatermarkTextProperties();
text_properties.setAlignment(e_AlignmentCenter);
text_properties.setColor(0xF68C21);
text_properties.setFont_size(e_FontStyleNormal);
text_properties.setLine_space(1);
text_properties.setFont_size(12.f);
text_properties.setFont(new Font(e_StdIDTimesB));
Watermark watermark = new Watermark(doc, "Foxit PDF SDK\nwww.foxitsoftware.com", text_properties, settings);
watermark.insertToPage(page);
// Save document to file
...

How to create an image watermark and insert it into the first page

import com.foxit.sdk.common.Bitmap;
import com.foxit.sdk.common.Image;
import com.foxit.sdk.pdf.*;
import com.foxit.sdk.pdf.Watermark;
import com.foxit.sdk.pdf.WatermarkSettings;
import com.foxit.sdk.common.Constants;
import static com.foxit.sdk.common.Constants.e_AlignmentCenter;
import static com.foxit.sdk.common.Font.e_StdIDTimesB;
import static com.foxit.sdk.pdf.WatermarkSettings.*;
import static com.foxit.sdk.pdf.WatermarkTextProperties.e_FontStyleNormal;
...
// Assuming PDFDoc doc has been loaded.
WatermarkSettings settings = new WatermarkSettings();
settings.setFlags(e_FlagASPageContents | e_FlagOnTop);
settings.setOffset_x(0.f);
settings.setOffset_y(0.f);
settings.setOpacity(20);
settings.setPosition(Constants.e_PosCenter);
settings.setRotation(0.0f);
Image image = new Image(image_file);
Bitmap bitmap = image.getFrameBitmap(0);
settings.setScale_x(page.getWidth() * 0.618f / bitmap.getWidth());
settings.setScale_y(settings.getScale_x());
Watermark watermark = new Watermark(doc, image, 0, settings);
watermark.insertToPage(page);
// Save document to file.
...

How to remove all watermarks from a page

import com.foxit.sdk.pdf.PDFPage;
...
// Assuming PDFPage page has been loaded and parsed.
...
page.removeAllWatermarks();
...
// Save document to file
...
Updated on November 13, 2019

Was this article helpful?

Related Articles