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();
document.open(FileUtils.getAssetsTempFile("CommonFivePage.pdf"));
CPDFPage cpdfPage = document.pageAtIndex(0);
RectF signatureRect = new RectF(28, 420, 150, 370);
CPDFSignatureWidget signatureWidget = (CPDFSignatureWidget) cpdfPage.addFormWidget(CPDFWidget.WidgetType.Widget_SignatureFields,document);
signatureWidget.setFieldName("Signature1");
signatureWidget.setFillColor(0xFF96B4D2);
signatureWidget.setRect(signatureRect);
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 = new File(document.getFilePath()).getName();
File file = new File(rootDir + "/digitalSignature/" + fileName);
file.getParentFile().mkdirs();
String password = "ComPDFKit";

String certPath = rootDir+"/certificate/Certificate.pfx";
CPDFX509 cpdfx509 = CPDFSignature.getX509ByPKCS12Cert(certPath, password);
String location = cpdfx509.getCertInfo().getSubject().getCountry();
String reason = "I am the owner of the document.";
BufferedImage image = ImageIO.read(new File(rootDir + "/TestFiles/ComPDFKit.png"));
Bitmap bitmap = new Bitmap();
int width = image.getWidth();
int height = image.getHeight();
int[] pixels = new int[width * height];
bitmap.setWidth(width);
bitmap.setHeight(height);
bitmap.setPixels(image.getRGB(0, 0, width, height, pixels, 0, width));

// Describe signature appearance information.
CPDFDigitalSigConfig sigConfig = new CPDFDigitalSigConfig();
sigConfig.setText(cpdfx509.getCertInfo().getSubject().getCommonName());
sigConfig.setTextColor(0xFF000000);
sigConfig.setContentColor(0xFF000000);
sigConfig.setLogo(bitmap);
String builder = "Name: " + cpdfx509.getCertInfo().getSubject().getCommonName() + "\n" +
    "Date: " + "2024.04.16 18:20:30" + "\n" +
    "Reason: " + reason + "\n" +
    "Location: " + location + "\n" +
    "DN: " + cpdfx509.getCertInfo().getSubject();
sigConfig.setContent(builder);
sigConfig.setContentAlginLeft(false);
sigConfig.setDrawLogo(true);
System.out.println("updateApWithDigitalSigConfig.");
boolean updateSignAp = signatureWidget.updateApWithDigitalSigConfig(sigConfig);
if (updateSignAp){
    boolean writeSignResult = document.writeSignature(
        signatureWidget,
        file.getAbsolutePath(),
        certPath,
        password,
        location,
        reason,
        CPDFDocument.PDFDocMdpP.PDFDocMdpPForbidAllModify
    );
    System.out.println("signWithinTheSignatureField result: " + writeSignResult);
}