PDFOne (for Java™)
Powerful all-in-one PDF library for Java
Compatibility
J2SE J2EE

PDF Stamping Using PDFOne Java Stamp

How to add rubber-stamp annotations and text/image watermarks to PDF documents.
By Santhanam L.
Important

PDFOne Java Stamp has been discontinued. It was a function-based subset edition of the bigger and currently available PDFOne Java Professional.

In this article, I will show you how to add rubberstamp annotations, and text and image watermarks to PDF documents using PDFOne Java. A rubber stamp has to look as it were stamped with a rubber stamp.

The example in this article uses a PDF document DocumentToStamp.pdf to which two watermarks and one annotation are added.

The following code snippet shows the input PDF document being loaded. The output document is to be saved as StampedDocument.pdf.

PdfReader r = PdfReader.fileReader("DocumentToStamp.pdf");
r.setOutFilePath("StampedDocument.pdf");
PdfDocument d = new PdfDocument(r);

Adding A Rubber-Stamp Annotation

In this code snippet, we create a rubber-stamp annotation that says, "CONFIDENTIAL" and add it to the first page of the loaded document. First, a PDFAnnotation object is created by specifying its rectangle and color. Then, you specify the type of stamp annotation. Next, you specify the annotation's subject and contents, which are displayed when the user interacts with the annotation on the output PDF. Finally, the stamp annotation is added to the page.

// Gets the first page of the document
PdfPage p = d.getPage(1);

// Creates a rubber-stamp annotation and sets the name of 
// an icon to be used in displaying the annotation.
PdfStampAnnot stampAnnot 
   = new PdfStampAnnot(
        // anotation rectangle
        new PdfRect(((p.getWidth() - 100)/2), 5, 100, 50),  
        // annotation color
        Color.orange);                                      

stampAnnot.setStamp(PdfStampAnnot.CONFIDENTIAL);
stampAnnot.setSubject("Confidential Information");
stampAnnot.setContents(
  "This information in this document is Confidential");
stampAnnot.setTitle("Confidential");

// Adds the above rubber stamp annotation to Page 1
p.addAnnotation(stampAnnot);

Adding a Text Watermark

In this code snippet, a text watermark is added to the document object so that the watermark can be added to all pages in one stroke. As this is a text watermark, a font object is created and then that font is used to render the text in the watermark. The watermark is positioned in the dead center of the page (taking into consideration the page margins) at an angle of 55 degrees. The underlay property of the watermark is set to true and the watermark text appears below all other content in the page.

// Creates a font object
PdfFont waterMarkFont 
  = PdfFont.create(
       "Arial",                 // font family
       40,                      // font size                
       PdfEncodings.WINANSI);   // font character encoding
waterMarkFont.setColor(Color.lightGray);

// Writes text as watermark on all the pages
d.addWatermarkText(
     "Watermark Text",        
     waterMarkFont,
     PdfPage.HP_MIDDLE | PdfPage.VP_CENTRE,  // position
     true,    // applyPageMargins             
     55,      // rotation
     true,    // underlay
     "-");    // page range (all pages)

Adding an Image Watermark

In this code snippet, an image object is created and then the image is added as a watermark to the first page in the PDF. The underlay property of the watermark is set to true and the watermark image appears below all content in the page.

// Gets first page from the document
PdfPage p = d.getPage(1);

// Creates an image object
PdfImage img = PdfImage.create("bubbles.bmp");

// Adds image watermark
p.addWatermarkImage(
     img,   
     PdfPage.HP_LEFT | PdfPage.VP_TOP,  // position
     0,                                 // rotation
     true);                             // underlay

One thing that needs to be taken into consideration when using the underlay property is that it operates on a Last-In-First-Out (LIFO) basis. For example, if you set the underlay as true to several watermarks and add them to a page, then the watermark that appears below all content (including other watermarks) would be the one that was added last. The watermark that was added before the last one will appear above it.

Original PDF Document - DocumentToStamp.pdf

Image Used In Watermark - bubbles.bmp

Output PDF Document - StampedDocument.pdf

---oO0Oo---

Downloads:

---o0O0o---

Our Developer Tools
eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

XtremePDFConverter VCL

A Delphi/C++Builder component to intelligently convert PDF to user-friendly Word RTF documents.

PDFOne .NET

A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

XtremeDocumentStudio .NET

Multi-format document-processing component suite for .NET developers

PDFOne (for Java™)

A Java™ PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java™ applications.

XtremeFontEngine (for Java)

Java font engine to render glyphs from Type 1, Type 2 (CFF), and TrueType fonts

Our Office Productivity Applications
Free PDF Reader

A free, fast, and portable application for viewing, printing and converting PDF documents.

Privacy | Legal | Feedback | Newsletter | Resellers © 2002-2013 Gnostice Information Technologies Private Limited. All rights reserved.

This site is best viewed on a screen with minimum resolution of 1152 x 864 pixels. Windows XP users are advised to use Microsoft ClearType Tuning for optimal experience. Also, please use the latest version of a standards-compliant browser such as Firefox, Opera, or Dragon (Chromium).