Adding signature to your pdf is very easy using the PDFBox library. Lets see an example on how to add signature in PDF using Apache PDFBox using PDSignature.
Important class : PDSignature
Example : Add Signature in PDF using Apache PDFBox
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
package com.kscodes.examples.pdfbox; import java.io.IOException; import java.util.Calendar; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature; public class PdfDocSignExample { public static void main(String args[]) { // Create a Document object. PDDocument pdDocument = new PDDocument(); // Create a Page object PDPage pdPage = new PDPage(); // Add the page to the document and save the document to a desired file. pdDocument.addPage(pdPage); try { PDSignature pdSignature = new PDSignature(); pdSignature.setFilter(PDSignature.FILTER_VERISIGN_PPKVS); pdSignature.setSubFilter(PDSignature.SUBFILTER_ADBE_PKCS7_SHA1); pdSignature.setName("KSCodes"); pdSignature.setLocation("WFH"); pdSignature.setReason("Sample Signature test"); pdSignature.setSignDate(Calendar.getInstance()); pdDocument.addSignature(pdSignature); pdDocument.save("K:\\Kscodes\\pdf\\signature-sample.pdf"); pdDocument.close(); System.out.println("PDF saved to the location !!!"); } catch (IOException ioe) { System.out.println("Error while saving pdf" + ioe.getMessage()); } } } |
Output