3

I would like to know if it's possible to watermark PDF file without any library.

I managed to do that with iText, but I would like to do watermarks in pure JAVA.
If someone knows if and how it's possible, please tell me.

informatik01
  • 16,038
  • 10
  • 74
  • 104
Bouny
  • 43
  • 1
  • 1
  • 3

2 Answers2

3

Watermarking to PDF can be added using the Java Library iText.

Here's an example of how to use it:

PdfReader reader = new PdfReader("HelloWorld.pdf");
PdfStamper pdfStamper = new PdfStamper(reader,
    new FileOutputStream("NewHelloWorld.pdf"));
Image image = Image.getInstance("MyWatermark.png");

for (int i=1; i<= reader.getNumberOfPages(); i++){
    PdfContentByte content = pdfStamper.getUnderContent(i);
    image.setAbsolutePosition(150f, 750f);
    content.addImage(image);
}

pdfStamper.close();

Here is another related example: https://web.archive.org/web/20151023054638/http://itextpdf.com/sandbox/events/Watermarking

informatik01
  • 16,038
  • 10
  • 74
  • 104
theJango
  • 1,100
  • 10
  • 22
0

In theory? Yes, it is. Most Java libraries that can produce watermarks are probably written in pure Java, so you could write that functionality yourself.

Practically? There aren't (to my knowledge; please correct me if I'm wrong) any core libraries that would allow you to manipulate PDFs in such a way, so you could either put a lot of work into basically copying part of iTexts functionality or just use a library.

blalasaadri
  • 5,990
  • 5
  • 38
  • 58
  • Ok thanks. So I'll check source code of itext to see how it works. I'll give you my impressions when i found. Thx for your reactivity. – Bouny Jan 06 '15 at 09:30
  • You're welcome. Though honestly I don't see why you wouldn't want to use a library; why reinvent the wheel if it already exists? – blalasaadri Jan 06 '15 at 09:32
  • 1
    For 2 reasons : first i want to know how it works for my personal knowledge. Second, it's about commercial licence. I want to use this in a commercial software, publishing source code of this soft is impossible. So, I wanted to know to avoid the commercial licence of itext for my client. But after having look at the code of iText, i think that the price for produce this function will be very higher that the commercial licence. So thank you for your helpful answer. – Bouny Jan 06 '15 at 09:43