PDFOne (for Java)
Create, edit, view, print & enhance PDF documents and forms in Java SE/EE
Compatibility
J2SE J2EE Windows Linux Mac (OS X)

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 .NET Developer Tools
Gnostice Document Studio .NET

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

PDFOne .NET

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

Our Delphi/C++Builder developer tools
Gnostice Document Studio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

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.

Our Java developer tools
Gnostice Document Studio Java

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

PDFOne (for Java)

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

Our Platform-Agnostic Cloud and On-Premises APIs
StarDocs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

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