Adding stamp annotations to a PDF document
Adding stamp annotations to PDFs programmatically enables teams to automate document status marking, build approval workflow systems, and implement review state tracking. Whether you’re creating automated approval stamps, marking documents as draft or final, implementing confidential document workflows, building review status indicators, or creating custom stamp systems for quality control, stamp annotations provide visual status markers with predefined styles. Stamp annotations display predefined text and icons representing document states, with customizable colors and positioning for integration into automated document processing pipelines.
How Nutrient helps you achieve this
Nutrient Java SDK handles PDF stamp annotation structures and appearance generation. With the SDK, you don’t need to worry about:
- Parsing stamp annotation dictionaries and rubber stamp icon rendering
- Managing predefined stamp appearance streams and text positioning
- Handling stamp style enumeration and icon resource loading
- Complex annotation appearance states and color transformations
Instead, Nutrient provides an API that handles all the complexity behind the scenes, letting you focus on your business logic.
Complete implementation
Below is a complete working example that demonstrates adding various stamp annotations to a PDF. The following lines set up the Java application. The package declaration and import statements bring in all necessary classes from the Nutrient SDK:
package io.nutrient.Sample;
import io.nutrient.sdk.Document;import io.nutrient.sdk.types.Color;import io.nutrient.sdk.editors.PdfEditor;import io.nutrient.sdk.editors.pdf.pages.PdfPageCollection;import io.nutrient.sdk.editors.pdf.pages.PdfPage;import io.nutrient.sdk.editors.pdf.annotations.PdfAnnotationCollection;import io.nutrient.sdk.editors.pdf.annotations.PdfStampAnnotation;import io.nutrient.sdk.enums.PdfRubberStampIcon;
public class StampAnnotations {The main method defines the entry point that will contain the stamp annotation creation logic:
public static void main(String[] args) {Working with stamp annotations
The Document.open() call opens the PDF document. The try-with-resources statement ensures the document is automatically closed when you’re done, preventing resource leaks. The following code creates a PDF editor, accesses the page collection, ensures at least one page exists by adding a letter-size page (612×792 points) if the document is empty, and retrieves the annotation collection from the first page:
try (Document document = Document.open("input.pdf")) { PdfEditor editor = PdfEditor.edit(document); PdfPageCollection pages = editor.getPageCollection();
if (pages.getCount() == 0) { pages.add(612.0f, 792.0f); }
PdfPage page = pages.getFirst(); PdfAnnotationCollection annotations = page.getAnnotationCollection();Adding a basic stamp
The following code adds a basic stamp annotation at coordinates (400, 700) with dimensions 150×50 points. The addStamp() method creates a stamp annotation with position (x, y), size (width, height), author name, and contents text. Stamps are created with a default Draft style (displaying DRAFT text with an icon) and red color (ARGB: 255, 255, 0, 0). The default Draft stamp is commonly used for marking documents that are still in development or awaiting review:
PdfStampAnnotation draftStamp = annotations.addStamp( 400.0f, 700.0f, 150.0f, 50.0f, // x, y, width, height "Author", "Work in progress" );Adding an approved stamp
The following code adds an approval stamp at coordinates (400, 600) with dimensions 150×50 points, positioned below the draft stamp. After creation, the stamp is customized using setter methods: setStampStyle() changes the icon to the Approved style (displaying APPROVED text with a checkmark icon), and setColor() applies a green color using ARGB values (255, 0, 128, 0). The Approved stamp style is commonly used in document approval workflows to visually indicate that a document has passed review and is authorized for use:
PdfStampAnnotation approvedStamp = annotations.addStamp( 400.0f, 600.0f, 150.0f, 50.0f, // x, y, width, height "Approver Name", "Document approved on review" ); // Customize the stamp appearance approvedStamp.setStampStyle(PdfRubberStampIcon.Approved); approvedStamp.setColor(Color.fromArgb(255, 0, 128, 0));Adding a confidential stamp
The following code adds a confidentiality stamp at coordinates (400, 500) with dimensions 150×50 points. The stamp is configured with the Confidential style using setStampStyle(), which displays CONFIDENTIAL text with an appropriate icon. The Confidential stamp retains the default red color inherited from the base stamp, making it visually prominent. This stamp style is commonly used to mark documents containing sensitive information, proprietary data, or content requiring restricted access:
PdfStampAnnotation confidentialStamp = annotations.addStamp( 400.0f, 500.0f, 150.0f, 50.0f, // x, y, width, height "Security Officer", "Contains sensitive information" ); // Customize the stamp appearance confidentialStamp.setStampStyle(PdfRubberStampIcon.Confidential);Other available stamp types
The following code demonstrates additional predefined stamp styles available through the PdfRubberStampIcon enumeration. The first stamp uses the NotApproved style at coordinates (50, 700), which displays NOT APPROVED text with a rejection indicator, retaining the default red color. The NotApproved style is used in review workflows to mark documents that failed approval criteria. The second stamp uses the Final style at coordinates (50, 600), which displays FINAL text indicating the document has reached its completed state. The Final stamp is customized with a blue color using ARGB values (255, 0, 0, 255) to distinguish it from approval/rejection stamps. Additional available stamp styles include Draft, Approved, Confidential, NotApproved, Final, and others defined in the PdfRubberStampIcon enumeration:
// Not Approved stamp PdfStampAnnotation notApprovedStamp = annotations.addStamp( 50.0f, 700.0f, 150.0f, 50.0f, // x, y, width, height "Reviewer", "Rejected" ); notApprovedStamp.setStampStyle(PdfRubberStampIcon.NotApproved);
// Final stamp PdfStampAnnotation finalStamp = annotations.addStamp( 50.0f, 600.0f, 150.0f, 50.0f, // x, y, width, height "Legal", "Final version" ); finalStamp.setStampStyle(PdfRubberStampIcon.Final); finalStamp.setColor(Color.fromArgb(255, 0, 0, 255));Saving the document
The final code block saves the document with all stamp annotations and closes the editor:
editor.saveAs("output.pdf"); editor.close(); } catch (Exception e) { System.err.println("Error: " + e.getMessage()); e.printStackTrace(); } }}Conclusion
The stamp annotation workflow consists of several key operations:
- Open the document and create an editor.
- Access the page collection and ensure at least one page exists.
- Retrieve the annotation collection for the target page.
- Add basic stamp annotations with the default
Draftstyle and red color. - Customize stamp styles using
setStampStyle()withPdfRubberStampIconenumeration values. - Customize stamp colors using
setColor()with ARGB color values. - Add
Approvedstamps with green color for approval workflows. - Add
Confidentialstamps for marking sensitive documents. - Add
NotApprovedstamps for rejection indicators. - Add
Finalstamps to mark completed documents. - Save and close the editor.
Nutrient handles stamp annotation dictionary structures, rubber stamp icon rendering, appearance stream generation, and text positioning so you don’t need to understand PDF stamp annotation specifications or manage icon resource loading manually. The stamp annotation system provides predefined visual status markers for document workflow automation, approval systems, confidentiality marking, and review state tracking.