Hi there,
In this blog we will see how to add watermark images to our PDF files using Java. Watermarks have been used to discourage counterfeiting. To generate watermarked PDFs we need an additional itext.jar file to be included in our class path which can be downloaded from here.
Java code for add watermark to our PDF
import java.io.FileOutputStream;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
public class AddWatermarkImageToPDFFile {
public static void main(String[] args) {
try {
PdfReader reader = new PdfReader("input.pdf");
int n = reader.getNumberOfPages();
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(
"PDFWithWatermarkImage.pdf"));
int i = 0;
PdfContentByte under;
Image img = Image.getInstance("images.png");
img.setAbsolutePosition(50, 300);
while (i < n) {
i++;
under = stamp.getUnderContent(i);
under.addImage(img);
}
stamp.close();
} catch (Exception de) {
de.printStackTrace();
}
}
}
Our PDF before adding watermark
PDF after adding watermark
Thanks :)
0 Comment(s)