-1
import java.io.FileOutputStream;

import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

public class AddWatermarkImageToAnExistingPDFFile {
    public static void main(String[] args) {
        try {
            PdfReader reader = new PdfReader("7189D0930.pdf");
            int n = reader.getNumberOfPages();
            PdfStamper stamp = new PdfStamper(reader, new FileOutputStream("NewPDFWithWatermarkImage.pdf"));
            int i = 0;
            PdfContentByte under;
            Image img = Image.getInstance("logo.png");
            img.setAbsolutePosition(200, 400);
            while (i < n) {
              i++;
              under = stamp.getUnderContent(i);
              under.addImage(img);
            }
            stamp.close();
        }
        catch (Exception de) {
            de.printStackTrace();
        }
    }
}

I've tried tracing but I don't have enough knowledge to fully understand whats going on in the Itext classes. I simply need to be able to watermark a pdf. I'm using itext since I will have to watermark 500-600 and will modify it so it can read in a notepad file of all of the names. Simply I run the program it produces the pdf but no evidence of it watermarking it is able to open the .png file but for whatever reason is not on the final pdf.

user3055889
  • 95
  • 1
  • 10
  • You do understand that I'm referring to one specific command that has a binary operation. It does what its suppose to or it doesn't. If I understood why it was messing up I wouldn't be on here. – user3055889 Feb 25 '14 at 15:25
  • is the content of the original pdf an image, or texts ? i had problem with some images with white zone 'overwriting' the watermark – PATRY Guillaume Feb 25 '14 at 16:19
  • @PATRY it looks to be cad that was imported into pdf. I can highlight the text that is there. Also I noticed that if I zoom out further for a split second I can see the watermark against a blank background. – user3055889 Feb 25 '14 at 17:37
  • If you put the logo in the overContent instead of the underContent, can you see it ? if so, i described a solution under. – PATRY Guillaume Feb 25 '14 at 17:42

2 Answers2

0

You are adding the watermark under the existing content. If the existing content is opaque, it is very normal that you don't see it (although it's there).

You are adding the watermark at position 200, 400. Maybe you're adding the watermark outside the visible area of the page. For instance: the page could be defined as a rectangle: [595, 0, 1190, 842]. That's an A4 page in portrait, but if you add a watermark at position 200, 400, you're adding it to the left of the visible area. It's there, but as it's outside the MediaBox, you don't see it.

As for your comment about "it doesn't work", please read http://lowagie.com/doesntwork

It isn't very respectful to phrase a question using those words. In this case, it probably does work: you see that the file size increases when you add the watermark. The main problem is that you're not adding it at the correct coordinates.

Read also:

You'll understand that your question may be closed as a duplicate.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
0

Your code by itself is providing the expected result on my computer, which mean that the problem may be in your original pdf.

If the content is opaque, you could have a working solution by putting a logo with alpha in the *over*Content (by logo with alpha, i mean the whole logo is 75% transparent, thus allowing to see what's underneath).

PATRY Guillaume
  • 4,287
  • 1
  • 32
  • 41