package com.qoppa; import java.io.FileInputStream; import java.security.KeyStore; import java.util.Enumeration; import android.app.Activity; import android.graphics.RectF; import android.os.Bundle; import android.util.Log; import com.qoppa.android.pdf.SignatureAppearance; import com.qoppa.android.pdf.SigningInformation; import com.qoppa.android.pdf.form.SignatureField; import com.qoppa.android.pdfProcess.PDFDocument; import com.qoppa.android.pdfProcess.PDFPage; import com.qoppa.android.pdfViewer.fonts.StandardFontTF; public class SignSample extends Activity { public void onCreate(Bundle saveInstBundle) { super.onCreate(saveInstBundle); StandardFontTF.mAssetMgr = getAssets(); try { //intializing a PDFDocument with no parameters creates a new document. //use one of the the other constructors for PDFDocument to load an existent document. PDFDocument pdf = new PDFDocument(); PDFPage page = pdf.appendNewPage(800, 1000); //add a field to sign. if the document contains a signature field, it can be retrieved with //PDFDocument.getAcroForm().getSignatureFields() SignatureField field = page.addSignatureField("test", new RectF(100, 100, 500, 200)); //path to a "p12" or "pfx" file String pfxLoc = "/sdcard/certificate.p12"; String pfxPassword = "thepassword"; FileInputStream pfxStream = new FileInputStream (pfxLoc); //pfx files are a type pkcs12 files; keytore should be instantiated with "PKCS12" for both //.p12 files and .pfx files KeyStore store = KeyStore.getInstance("PKCS12"); store.load(pfxStream, pfxPassword.toCharArray()); pfxStream.close(); //An alias is required to sign the document. String alias = ""; //If the alias for the keystore is not known, it can probably be determined with this code Enumeration aliases =store.aliases(); while(aliases.hasMoreElements()) { String element = aliases.nextElement(); if(!element.equals("")) { alias = element; break; } } //The appearance can be customized using the setters exposed by SignatureAppearance - these can //be used to modify the text displayed, add an image background, etc... SignatureAppearance appearance = new SignatureAppearance(); SigningInformation signingInfo = new SigningInformation(store, alias, pfxPassword); signingInfo.setSignatureAppearance(appearance); //calling signDocument will sign the document and write it to the sdcard. //the 3rd parameter is the path the signed document will be saved to. pdf.signDocument(field, signingInfo, "/sdcard/signed.pdf"); Log.e("Signing test","done signing"); } catch (Exception e) { Log.e("error while signing", Log.getStackTraceString(e)); } } }