Free and easy to use, supports a variety of custom styles.
Support file format:
- pdf (detail: pdfbox)
- image (
BufferedImage
, e.g.png
,jpg
,bmp
, ...)
Support watermark:
- text (
String
) - image (
BufferedImage
)
Support styles:
font
: any font you installedmargin
: a box (text or image) marginstop
,bottom
,left
,right
position
: the position of watermark, e.g.top-left
,bottom-right
theta
: rotation of watermark in degree, e.g.-45
alpha
: transparency of watermark, the range is0-1
mode
: supportfit-page
(repeat over the entire page), andsingle
(only render once at the specifiedposition
)dense
andspace
: ifmode=fit-page
, it changes the density of watermarks on the page
Text watermark:
public class Example {
public static void main(String[] args) throws IOException, WatermarkException {
Watermark watermark = new PdfWatermark();
String watermarkStr = "Hello World";
Config config = Config.buildTextConfig(true, new Config.FontStyle("Roboto", 32, Color.RED), -45, 3, 1.6f, 0.2f);
watermark.make(new FileInputStream("test.pdf"), new FileOutputStream("target.pdf"), watermarkStr, config);
}
}
Image watermark:
public class Example {
public static void main(String[] args) throws IOException, WatermarkException {
Watermark watermark = new PdfWatermark();
BufferedImage watermarkIcon = ImageIO.read(new File("image.png"));
Config config = Config.buildTextConfig(true, null, -45, 3, 1.6f, 0.2f);
watermark.make(new FileInputStream("test.pdf"), new FileOutputStream("target.pdf"), watermarkIcon, config);
}
}
<dependencies>
<dependency>
<groupId>com.youxingz</groupId>
<artifactId>watermark</artifactId>
<version>1.0.2</version>
</dependency>
</dependencies>
You can implement interface Watermark
to support more files of different formats.
Example:
public class DocxWatermark implements Watermark {
@Override
public void make(InputStream src, OutputStream target, String text, Config config) throws IOException, WatermarkException {
assertConfigType(config, Config.Type.text);
// read input-stream and write result into target...
}
@Override
public void make(InputStream src, OutputStream target, BufferedImage image, Config config) throws IOException, WatermarkException {
assertConfigType(config, Config.Type.image);
// read input-stream and write result into target...
}
}