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

How To Digitally Sign A PDF Document In Java

Using PDFOne (for Java)
By L. Santhanam

In PDFOne (for Java) Version 5, we introduced support for digital signing. In this article, you will see how to sign PDF documents.

To sign PDF documents, you need a digital signature from a Certificate Authorities (CA). The certificate will be made available to you by the CA in the form of a PFX file. If you have this file and PDFOne, you can start signing documents. When you created the PFX file, the CA would have provided you a password. Keep this password handy, as we will need it a couple of times in this article.

Create A Blank Signature Field

Sometimes, you do not get sign documents but only create documents that others sign. For these situations, you can create "blank" signature fields.

import java.awt.Color;
import java.io.IOException;

import com.gnostice.pdfone.*;

public class Sign_PDF {

  public static void main(String[] args) throws PdfException, IOException {
        
    // Create a new document
    PdfDocument doc = new PdfDocument();

    // Create a blank signature form field
    PdfFormSignatureField signatureField =
       new PdfFormSignatureField(new PdfRect(10, 40, 300, 120));
    signatureField.setName("sigFld1");
        
    // Create a PDF page
    PdfPage page1 = new PdfPage(PdfPageSize.A4);
        
    // Add the form field to the page
    page1.addFormField(signatureField);
        
    page1.writeText("This is a blank signature form field", 10, 10);
        
    // Add the page to the document
    doc.add(page1);
        
    // Save the document to file
    doc.save("blank_signature_doc.pdf");
        
    // Close IO resources
    doc.close();
  }
}
A document with a blank signature field.

Add Signature To A Blank Signature Field

To embed a signature to a blank signature field, first get hold of your PFX file. (This code snippet uses a trial digital certificate from Ascertia.) Then, load the document and iterate through its form fields, find the blank signature field and use the PdfFormSignatureField.fill(PdfSignature) method.

import java.io.IOException;
import java.util.List;

import com.gnostice.pdfone.PDFOne;
import com.gnostice.pdfone.PdfDocument;
import com.gnostice.pdfone.PdfException;
import com.gnostice.pdfone.PdfFormField;
import com.gnostice.pdfone.PdfFormSignatureField;
import com.gnostice.pdfone.PdfSignature;

public class Fill_Blank_Signature_Field {

  public static void main(String[] args) throws IOException, PdfException {

    // Create a PDF signature
    PdfSignature pdfSignature =
        new PdfSignature("testmenow@mailinator.com.pfx",  // pathname of PFX 
                         "TestCase",                      // password for PFX
                         "I approve this document",       // description
                         "Moonbase, Moon",                // reason
                         "test@example.com",              // contact info
                         1);                              // page number

    // Load a document with blank form fields
    PdfDocument doc = new PdfDocument();

    // Load a PDF document with a blank signature
    doc.load("blank_signature_doc.pdf");

    // Retrieve a list of all signature fields
    List fields = doc.getAllFormFieldsOnPage(0, PdfFormField.TYPE_SIGNATURE);

    PdfFormSignatureField signatureField;
    // Iterate the list and fill the signature fields
    for (int i = 0; i < fields.size(); i++) {
      signatureField = (PdfFormSignatureField) fields.get(i);
      if (signatureField.isUnsigned()) {
        signatureField.fill(pdfSignature);
        break;
      }
    }
  
    doc.drawLine(10, 18, 200, 18);

    // Save the document to file
    doc.save("filled_signature_doc.pdf");
 
    // Close IO resources
    doc.close();
  }
}
A signature has been added to the blank signature field.

Add Signature Just Like That

You do not have to create a blank signature field to add a signature. You can do that straightaway using PdfDocument.AddSignature() method, as shown below. PDFOne will take care of adding the signature field.

import java.io.IOException;
import java.util.List;

import com.gnostice.pdfone.PDFOne;
import com.gnostice.pdfone.PdfDocument;
import com.gnostice.pdfone.PdfException;
import com.gnostice.pdfone.PdfFormField;
import com.gnostice.pdfone.PdfFormSignatureField;
import com.gnostice.pdfone.PdfRect;
import com.gnostice.pdfone.PdfSignature;


public class Add_Signature {

  public static void main(String[] args) throws IOException, PdfException {

    // Load an existing PDF document
    PdfDocument doc = new PdfDocument();
    doc.load("transfer_details.pdf");

    // Add signature to the  document
    doc.addSignature("testmenow@mailinator.com.pfx",  // pathname of PFX 
            "TestCase",                               // password for PFX
            "This is 100% genuine.",                  // description
            "Bank of Money, Internet",                // reason
            "test@example.com",                       // contact info
            1,                                        // page number
            "fldsig",                                 // field name
            new PdfRect(400,10,250,70));              // location

    // Save the document to file
    doc.save("signed_doc.pdf");

    // Close IO resources
    doc.close();
  }
}

---o0O0o---

Our .NET Developer Tools
XtremeDocumentStudio .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
XtremeDocumentStudio 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
XtremeDocumentStudio (for 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-2017 Gnostice Information Technologies Private Limited. All rights reserved.