Skip to content
Guides

Create Digital Signatures

Creating a digital signature involves two steps:

  1. Create a Signature Field
  2. Sign within the Signature Field

By following these two steps, you can either self-sign a document or invite others to sign within the signature field you've created.

Create a Signature Field

ComPDFKit offers support for customizing the styles of the signature form field and allows you to customize the appearance of your signature using drawn, image, and typed signatures.

This example shows how to create a signature field:

java
// Create a signature field.
// 
// Page Index: 0
// Rect: CRect(28, 420, 150, 370)
// Border RGB:{ 0, 0, 0 }  
// Widget Background RGB: { 150, 180, 210 }
// 
CPDFDocument document = new CPDFDocument(context);
document.open(FileUtils.getAssetsTempFile(context, "CommonFivePage.pdf"));
// Insert a form signature Widget (unsigned)
CPDFPage cpdfPage = document.pageAtIndex(0);
RectF pageSize = cpdfPage.getSize();
RectF signatureRect = new RectF(28, 420, 150, 370);
signatureRect = cpdfPage.convertRectToPage(false, pageSize.width(), pageSize.height(), signatureRect);
CPDFSignatureWidget signatureWidget = (CPDFSignatureWidget) cpdfPage.addFormWidget(CPDFWidget.WidgetType.Widget_SignatureFields);
signatureWidget.setFieldName("Signature1");
signatureWidget.setFillColor(0xFF96B4D2);
signatureWidget.setRect(signatureRect);
signatureWidget.updateAp();
kotlin
// Create a signature field.
// 
// Page Index: 0
// Rect: CRect(28, 420, 150, 370)
// Border RGB:{ 0, 0, 0 }  
// Widget Background RGB: { 150, 180, 210 }
// 
val document = CPDFDocument(context())
document.open(getAssetsTempFile(context(), "CommonFivePage.pdf"))
// Insert a form signature Widget (unsigned)
val cpdfPage = document.pageAtIndex(0)
val pageSize = cpdfPage.size
val signatureWidget = cpdfPage.addFormWidget(CPDFWidget.WidgetType.Widget_SignatureFields) as CPDFSignatureWidget
signatureWidget.fieldName = "Signature1"
signatureWidget.fillColor = Color.parseColor("#FF96B4D2")
signatureWidget.rect = kotlin.run {
  cpdfPage.convertRectToPage(false, pageSize.width(), pageSize.height(), RectF(28f, 420f, 150f, 370f))
}
signatureWidget.updateAp()

Sign Within the Signature Field

To sign within the signature field, you need to do three things:

  • Possess a certificate that conforms to the PKCS12 standard (in PFX or P12 format) and ensure that you know its password. You can create a compliant digital certificate using the built-in methods within the ComPDFKit SDK.
  • Set the appearance of the digital signature.
  • Write the data into the signature field.

This example shows how to sign within the signature field:

java
//	   Sign in the signature field.
//
// 	   Text: Grantor Name
// 	   Content:
//     Name: get grantor name from certificate
//     Date: now(yyyy.mm.dd)
//     Reason: I am the owner of the document
//     DN: Subject
//     IsContentAlginLeft: false
//     IsDrawLogo: True
//     LogoBitmap: logo.png
//     text color RGB: { 0, 0, 0 }
//     Output file name: document.FileName + "_Signed.pdf"
//
// Make a digital signature.
String fileName = FileUtils.getNameWithoutExtension(document.getFileName()) + "_Signed.pdf";
File file = new File(outputDir(), "digitalSignature/" + fileName);
file.getParentFile().mkdirs();
String password = "ComPDFKit";

String certPath = FileUtils.getAssetsTempFile(context, "Certificate.pfx");
CPDFX509 cpdfx509 = CPDFSignature.getX509ByPKCS12Cert(certPath, password);
String location = cpdfx509.getCertInfo().getSubject().getCountry();
String reason = "I am the owner of the document.";

Bitmap bitmap = BitmapFactory.decodeFile(FileUtils.getAssetsTempFile(context, "ComPDFKit.png"));
// Describe signature appearance information.
CPDFDigitalSigConfig sigConfig = new CPDFDigitalSigConfig();
sigConfig.setText(cpdfx509.getCertInfo().getSubject().getCommonName());
sigConfig.setTextColor(0xFF000000);
sigConfig.setContentColor(0xFF000000);
sigConfig.setLogo(bitmap);
StringBuilder builder = new StringBuilder();
builder.append("Name: ").append(cpdfx509.getCertInfo().getSubject().getCommonName()).append("\n")
  .append("Date: ").append(DateUtil.getDataTime("yyyy.MM.dd HH:mm:ss")).append("\n")
  .append("Reason: ").append(reason).append("\n")
  .append("Location: ").append(location).append("\n")
  .append("DN: ").append(cpdfx509.getCertInfo().getSubject());
sigConfig.setContent(builder.toString());
sigConfig.setContentAlginLeft(false);
sigConfig.setDrawLogo(true);

boolean updateSignAp = signatureWidget.updateApWithDigitalSigConfig(sigConfig);
if (updateSignAp){
  boolean writeSignResult = document.writeSignature(
    signatureWidget,
    file.getAbsolutePath(),
    certPath,
    password,
    location,
    reason,
    CPDFDocument.PDFDocMdpP.PDFDocMdpPForbidAllModify
  );
}
kotlin
//	   Sign in the signature field.
//
// 	   Text: Grantor Name
// 	   Content:
//     Name: get grantor name from certificate
//     Date: now(yyyy.mm.dd)
//     Reason: I am the owner of the document
//     DN: Subject
//     IsContentAlginLeft: false
//     IsDrawLogo: True
//     LogoBitmap: logo.png
//     text color RGB: { 0, 0, 0 }
//     Output file name: document.FileName + "_Signed.pdf"
//
// Make a digital signature.
val fileName = getNameWithoutExtension(document.fileName) + "_Signed.pdf"
val file = File(outputDir(), "digitalSignature/$fileName")
file.parentFile?.mkdirs()
val password = "ComPDFKit"
val certPath = getAssetsTempFile(context(), "Certificate.pfx")
val cpdfx509 = CPDFSignature.getX509ByPKCS12Cert(certPath, password)
val location = cpdfx509.certInfo.subject.country
val reason = "I am the owner of the document."
val bitmap = BitmapFactory.decodeFile(getAssetsTempFile(context(), "ComPDFKit.png"))
// Describe signature appearance information.
val sigConfig = CPDFDigitalSigConfig().apply {
  text = cpdfx509.certInfo.subject.commonName
  textColor = Color.BLACK
  contentColor = Color.BLACK
  logo = bitmap
  val builder = StringBuilder()
  builder.append("Name: ").append(cpdfx509.certInfo.subject.commonName).append("\n")
  .append("Date: ").append(DateUtil.getDataTime("yyyy.MM.dd HH:mm:ss")).append("\n")
  .append("Reason: ").append(reason).append("\n")
  .append("Location: ").append(location).append("\n")
  .append("DN: ").append(cpdfx509.certInfo.subject)
  content = builder.toString()
  isContentAlginLeft = false
  isDrawLogo = true
}
val updateSignAp = signatureWidget.updateApWithDigitalSigConfig(sigConfig)
if (updateSignAp) {
  val writeSignResult = document.writeSignature(
    signatureWidget,
    file.absolutePath,
    certPath,
    password,
    location,
    reason,
    CPDFDocument.PDFDocMdpP.PDFDocMdpPForbidAllModify
  )
}