Add Digital Signature, Watermark, and Permission to PDF in Java without Effort

Casie LiuCasie Liu
6 min read

PDF is the standard format for sharing contracts, invoices, and approvals in modern businesses. But without protection, these files are vulnerable to tampering, copying, or misuse—leading to potential legal and security risks.

Adding digital signatures, watermarks, and permission to PDF helps ensure document integrity, enhance authenticity, and support secure digital workflows.

💡
This article demonstrates how to secure PDF documents using Spire.PDF for Java, a powerful library designed for processing PDF files in Java applications.

Adding a Digital Signature: Embed a Certified Visual Stamp into Your PDF

In situations like contract signing or report approval, digital signatures offer a reliable and legally recognized way to verify document authenticity. Unlike handwritten signatures or rubber stamps, digital signatures use encryption and certificate-based identity verification to detect any changes made after signing.

With Spire.PDF for Java, you can easily create a visual digital signature that combines a graphical stamp (such as a company seal or handwritten signature) with certificate-based identity verification.

(1) What You’ll Need

  • A .pfx certificate file, which you can request from a certificate authority (CA) or generate for testing purposes.

  • A signature image—ideally a transparent PNG of your company seal or handwritten signature.

(2) Key Implementation Steps

  • Load the PDF document you want to sign.

  • Import the .pfx certificate and initialize a PdfSignature object.

  • Define the position and size of the signature on the page.

  • Fill in signer details and attach the signature image.

  • (Optional) Set permissions to restrict editing or printing.

  • Save the file as a new, signed PDF.

(3) Full Code Example (Ready to Run in IntelliJ IDEA)

import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import com.spire.pdf.security.*;

import java.awt.*;
import java.awt.geom.*;

public class AddVisibleSignature {
   public static void main(String[] args) {

       // Create a PdfDocument object
       PdfDocument doc = new PdfDocument();

       // Load a PDF file
       doc.loadFromFile("/input/Booklet.pdf");

       // Load a certificate
       PdfCertificate cert = new PdfCertificate(
               "E:/intellij IDEA/Spire.Pdf_10.10.7/samples/data/gary.pfx",
               "e-iceblue");

       // Create a PdfSignature object and specify its position and size
       PdfSignature signature = new PdfSignature(
               doc, doc.getPages().get(0), cert, "My signature");
       Rectangle2D rect = new Rectangle2D.Float();
       rect.setFrame(
               new Point2D.Float(
                       (float) doc.getPages().get(0).getActualSize().getWidth() - 320,
                       (float) doc.getPages().get(0).getActualSize().getHeight() - 140),
               new Dimension(270, 100));
       signature.setBounds(rect);

       // Set the graphic mode
       signature.setGraphicMode(GraphicMode.Sign_Image_And_Sign_Detail);

       // Add signature content
       signature.setNameLabel("Signature:");
       signature.setName("Me");
       signature.setContactInfoLabel("Tel: ");
       signature.setContactInfo("02881705109");
       signature.setDateLabel("Date: ");
       signature.setDate(new java.util.Date());
       signature.setLocationInfoLabel("Address: ");
       signature.setLocationInfo("NY");
       signature.setReasonLabel("Reasion: ");
       signature.setReason("Confirm file");
       signature.setDistinguishedNameLabel("DN:");
       signature.setDistinguishedName(
               signature.getCertificate().get_IssuerName().getName());

       // Set the signature image
       signature.setSignImageSource(
               PdfImage.fromFile("/signature2.png"));

       // Set the signature font
       signature.setSignDetailsFont(
               new PdfFont(PdfFontFamily.Helvetica, 10f, PdfFontStyle.Regular));

       // Set the document permission to forbid changes
       signature.setDocumentPermissions(PdfCertificationFlags.Forbid_Changes);
       signature.setCertificated(true);

       // Save the document
       doc.saveToFile("/output/digitalsign.pdf");
       doc.close();
   }
}

(4) Result Preview

Java Adding a Digital Signature to a PDF

Notes:

  • A .pfx certificate file is required. It can be issued by a Certificate Authority (CA) or generated for testing purposes.

  • Signature images can be in common formats such as PNG or JPG.

  • The signature's position is based on coordinates starting from the bottom-left corner of the PDF page, with the unit measured in points.

Adding a Text Watermark: Visually Tag Your PDF with Identity Information

In internal reports, draft documents, and company materials, watermarks are commonly used to prevent misuse and clearly indicate the document’s purpose—such as “Confidential,” “Internal Use Only,” or “Draft.” Compared to manually inserting watermark layers, automating the process ensures consistency and efficiency across multiple files.

With Spire.PDF, you can add a centered text watermark to every page of a PDF, with full control over the font, color, transparency, position, and rotation.

(1) Overview of Key Steps

  • Load the target PDF document

  • Create a font object and calculate watermark offsetsLoop through each page and set transparency, rotation, and drawing coordinates

  • Draw the text watermark

  • Save the updated file as a new PDF

(2) Complete Code Example (Ready to Use in IntelliJ IDEA)

Below is the full working code to add a customizable text watermark to all pages of a PDF.

import com.spire.pdf.*;
import com.spire.pdf.graphics.*;

import java.awt.*;

public class insertSingleTextWaterMark {
   public static void main(String[] args) {

       // Create a PdfDocument object
       PdfDocument pdf = new PdfDocument();

       // Load a PDF file
       pdf.loadFromFile("/input/Booklet.pdf");

       // Loop through each page to insert watermarks
       String text = "SAMPLE";
       PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", Font.BOLD, 50));
       float set1 = (float) (font.measureString(text).getWidth() * Math.sqrt(2)/4);
       float set2 = (float) (font.measureString(text).getHeight() * Math.sqrt(2)/4);
       for (int i = 0; i < pdf.getPages().getCount(); i++){
           // Get a page
           PdfPageBase page = pdf.getPages().get(i);
           // Set the transparency of the watermark
           page.getCanvas().setTransparency(0.8f);
           // Set the translation of the coordinate
           page.getCanvas().translateTransform(page.getCanvas().getSize().getWidth()/2 - set1 - set2, page.getCanvas().getSize().getHeight()/2 + set1 - set2);
           // Set the rotation angle
           page.getCanvas().rotateTransform(-45);
           // Draw watermark text on the page
           page.getCanvas().drawString(text, font, PdfBrushes.getDarkGray(), 0, 0);
       }

       // Save the document
       pdf.saveToFile("/output/addwatermark.pdf");
   }
}

(3) Result Preview

Java Adding a Text Watermark to a PDF

Alt: Java: Adding a Text Watermark to a PDF

💡
If you're working on simple tasks or just testing Spire.PDF, you can try the free version of Spire.PDF for Java. It doesn't add any watermarks to the output and is a great choice for lightweight documents or small projects.

Setting Permissions: Restrict Printing and Editing in PDF Files

In scenarios involving sensitive information or controlled distribution, adding just a signature or watermark may not be enough. To further protect your PDF content, you can encrypt the document and set permissions that restrict actions such as printing, copying, and editing.

Spire.PDF for Java allows you to apply password protection and configure a wide range of permission settings—helping ensure document security and compliance during distribution.

(1) Overview of Key Steps

  • Load the original PDF file

  • Set an open password and a permissions password (optional to require opening password)

  • Specify allowed operations (e.g., allow printing only)

  • Choose encryption strength

  • Save the protected PDF document

(2) Complete Code Example (Ready to Use in IntelliJ IDEA)

Below is the full working code to encrypt a PDF file and set operation-level permissions using Java.

import com.spire.pdf.PdfDocument;
import com.spire.pdf.security.PdfEncryptionKeySize;
import com.spire.pdf.security.PdfPermissionsFlags;

import java.util.EnumSet;
public class ChangeSecurityPermissions {
   public static void main(String[] args) {

       // Create a PdfDocument object
       PdfDocument doc = new PdfDocument();

       // Load a PDF file
       doc.loadFromFile("/input/Booklet.pdf");

       // Set the open password (empty means no password)
       String openPsd = "";

       // Set the permission password
       String permissionPsd = "PDFpassword";

       // Set allowed operations
       EnumSet permissionsFlags = EnumSet.of(
               PdfPermissionsFlags.Print,
               PdfPermissionsFlags.Full_Quality_Print);

       // Encrypt the document, set the password, permissions and encryption method
       doc.getSecurity().encrypt(
               openPsd, permissionPsd, permissionsFlags, PdfEncryptionKeySize.Key_128_Bit);

       // Save the PDF file
       doc.saveToFile("/output/Addpermision.pdf");
   }
}

(3) Result Preview

Java Adding Permissions to a PDF

Summary and Further Application: Building a More Secure, Professional PDF Workflow

In this guide, we've explored how to use Spire.PDF for Java to add digital signatures, text watermarks, and permission controls to PDF documents. These features are especially valuable when handling electronic contracts, internal reports, policy documents, and other sensitive files in enterprise environments.

They help you achieve the following:

  • Signature Authentication: Ensures the document’s origin is trusted and content remains unaltered.

  • Visual Cues: Watermarks reinforce confidentiality awareness and prevent misuse or leaks.

  • Permission Management: Restricts actions like printing, copying, or editing to reduce the risk of unauthorized changes or distribution.

If you encounter any issues, feel free to leave a comment—I’m happy to help! For more tutorials, you can check my homepage.

0
Subscribe to my newsletter

Read articles from Casie Liu directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Casie Liu
Casie Liu