How to add a printable or non-printable bitmap stamp to a PDF?

Tags: annotationsstamp annotationimagesprint flagiText 7

I would like to add a bitmap stamp to a PDF file, that would be either printable or non-printable depending on the actual Acrobat Reader print settings. I.e.

  • when a user selects the option "Document" in Adobe Reader's Print Dialog box, it would not be printed, but
  • when "Document and stamps" is selected, then the bitmap would print

Right now I can create either printable or non-printable bitmap, but I am unable to create a bitmap that would be both printable and non-printable depending on users choice.

Posted on StackOverflow on Nov 19, 2012 by Vojtěch Dohnal

Creating stamp annotations is described in Chapter 7 of iText in Action - Second Edition, more specifically in the TimeTableAnnotations3 example:

annotation = new PdfStampAnnotation(rect)
         .setStampName(new PdfName("NotForPublicRelease"))
         .setContents("Press only")

If you look at the print preview, you can see that these annotations don't show up if you print the Document without stamps:

Print preview
Print preview

Note that a PDF viewer should have predefined icons for at least the following names:

  • Approved,
  • Experimental,
  • NotApproved,
  • AsIs,
  • Expired,
  • NotForPublicRelease,
  • Confidential,
  • Final,
  • Sold,
  • Departmental,
  • ForComment,
  • TopSecret,
  • Draft,
  • ForPublicRelease.

What these icons look like will depend from viewer to viewer.

You want to use an image instead of one of these predefined stamps. This is shown in the AddStamp example. We need to create an appearance for the stamp annotation and add it like this:

public void manipulatePdf(String src, String dest, String imgSrc) throws Exception {
    PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest));
    ImageData img = ImageDataFactory.create(imgSrc);
    float w = img.getWidth();
    float h = img.getHeight();
    Rectangle location = new Rectangle(36, 770 - h, w, h);
    PdfStampAnnotation stamp = new PdfStampAnnotation(location)
                .setStampName(new PdfName("ITEXT"));
    PdfFormXObject xObj = new PdfFormXObject(new Rectangle(w, h));
    PdfCanvas canvas = new PdfCanvas(xObj, pdfDoc);
    canvas.addImage(img, 0, 0, false);

Click this link if you want to see how to answer this question in iText 5.