Skip to content
DemoSampleAPI ReferenceFAQ

PDF Generation Template Editor

Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.

View on GitHub

Invisible Digital Signatures ​

An invisible digital signature does not display signature text, an image, a logo, or any other visible signature appearance on the page. The signature information is embedded in the PDF and can still be inspected and verified through the ComPDF SDK signature APIs or a PDF reader's signature panel.

ComPDF SDK creates a standard PDF signature form field (/FT /Sig) and its Widget annotation (/Subtype /Widget). The field value /V refers to a signature dictionary containing /Contents and /ByteRange. When you set the signature field's bounds to CGRectZero, the Widget rectangle written to the PDF is /Rect [0 0 0 0], with zero width and height, following the PDF specification's field and annotation representation for invisible signatures. See section 1.2 of the PDF Association's Digital Signatures technical note for this representation. The appearance stream that ComPDF SDK writes for this Widget also has a [0 0 0 0] bounding box and contains no drawing content, so the page renders the same after signing as before.

Invisibility affects the page appearance. It does not change the signature's cryptographic integrity or the requirements for trusting its certificate.

Step 1: Prepare the signing materials

Apply a license that includes the digital signature feature as described in Apply the License Key before running the following code. Prepare:

  • A saved input PDF containing at least one page. ComPDF SDK reads the file at the CPDFDocument's documentURL when signing.
  • A PKCS#12 identity (.p12 or .pfx) containing the private key, signer certificate, and any required intermediate certificates, together with its password.
  • An output path different from the input path.

The following blocks form one sequential workflow inside a single method. Signing and verification read and write the whole file, so run them on a background queue when processing large files.

swift
// Input PDF, PKCS#12 identity and its password, and an output path different from the input path.
let documentsPath = NSHomeDirectory() + "/Documents"
let inputURL = URL(fileURLWithPath: documentsPath + "/input.pdf")
let outputURL = URL(fileURLWithPath: documentsPath + "/invisible-signed.pdf")
let certificatePath = documentsPath + "/signer.p12"
let certificatePassword = "YOUR_PKCS12_PASSWORD"

guard FileManager.default.fileExists(atPath: inputURL.path),
      FileManager.default.fileExists(atPath: certificatePath) else {
    print("Cannot find the input PDF or the PKCS#12 file")
    return
}
guard inputURL.standardizedFileURL != outputURL.standardizedFileURL else {
    print("Use different input and output paths")
    return
}
objective-c
// Input PDF, PKCS#12 identity and its password, and an output path different from the input path.
NSString *documentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSURL *inputURL = [NSURL fileURLWithPath:[documentsPath stringByAppendingPathComponent:@"input.pdf"]];
NSURL *outputURL = [NSURL fileURLWithPath:[documentsPath stringByAppendingPathComponent:@"invisible-signed.pdf"]];
NSString *certificatePath = [documentsPath stringByAppendingPathComponent:@"signer.p12"];
NSString *certificatePassword = @"YOUR_PKCS12_PASSWORD";

if (![[NSFileManager defaultManager] fileExistsAtPath:inputURL.path] ||
    ![[NSFileManager defaultManager] fileExistsAtPath:certificatePath]) {
    NSLog(@"Cannot find the input PDF or the PKCS#12 file");
    return;
}
if ([inputURL.URLByStandardizingPath isEqual:outputURL.URLByStandardizingPath]) {
    NSLog(@"Use different input and output paths");
    return;
}

Step 2: Import the certificate and check its password and validity

Import means reading the signing identity from the supplied PKCS#12 file. It does not install the identity into the system keychain or make its certificate trusted. certificateWithPKCS12Path:password: returns nil when the password is wrong or the file cannot be parsed. The returned CPDFSignatureCertificate can be used to inspect certificate information such as the subject and validity period.

swift
// Read the signing certificate from the PKCS#12 file. nil means the password is wrong or the file cannot be parsed.
guard let signingCertificate = CPDFSignatureCertificate(pkcs12Path: certificatePath,
                                                        password: certificatePassword) else {
    print("Cannot unlock the PKCS#12 file")
    return
}
// Check the validity period.
let now = Date()
guard let validityStarts = signingCertificate.validityStarts,
      let validityEnds = signingCertificate.validityEnds,
      validityStarts <= now, now <= validityEnds else {
    print("Certificate is outside its validity period")
    return
}
print("Signing certificate: \(signingCertificate.subjectDict["CN"] ?? "")")
objective-c
// Read the signing certificate from the PKCS#12 file. nil means the password is wrong or the file cannot be parsed.
CPDFSignatureCertificate *signingCertificate = [CPDFSignatureCertificate certificateWithPKCS12Path:certificatePath
                                                                                          password:certificatePassword];
if (!signingCertificate) {
    NSLog(@"Cannot unlock the PKCS#12 file");
    return;
}
// Check the validity period.
NSDate *now = [NSDate date];
if (!signingCertificate.validityStarts || !signingCertificate.validityEnds ||
    [now compare:signingCertificate.validityStarts] == NSOrderedAscending ||
    [now compare:signingCertificate.validityEnds] == NSOrderedDescending) {
    NSLog(@"Certificate is outside its validity period");
    return;
}
NSLog(@"Signing certificate: %@", signingCertificate.subjectDict[@"CN"]);

writeSignatureToURL:withWidget:PKCS12Cert:password:location:reason:isSetSignAp:permissions: below loads the private key and signing identity from the same PKCS#12 file.

Step 3: Create the invisible signature field and apply the signature

Create the signature field, assign a field name, and set its bounds to CGRectZero. Do not call signAppearanceConfig: to configure a text, image, or logo appearance, and pass NO (false in Swift) for isSetSignAp. The output below forbids subsequent document modifications through the DocMDP permission CPDFSignaturePermissionsForbidChange.

swift
guard let document = CPDFDocument(url: inputURL), let page = document.page(at: 0),
      let widget = CPDFSignatureWidgetAnnotation(page: page, document: document) else {
    print("Cannot create the signature field")
    return
}
// Zero-size signature field: no text, image, or logo appearance is configured.
widget.setFieldName("InvisibleSignature")
widget.bounds = .zero
page.updateAndAddAnnotation(widget)

// isSetSignAp: false: do not generate a dynamic signature appearance.
// .forbidChange: DocMDP permission, no changes are permitted after signing.
let didSign = document.writeSignature(to: outputURL,
                                      withWidget: widget,
                                      pkcs12Cert: certificatePath,
                                      password: certificatePassword,
                                      location: "Singapore",
                                      reason: "Approval",
                                      isSetSignAp: false,
                                      permissions: .forbidChange)
guard didSign else {
    print("Failed to write the invisible signature")
    return
}
objective-c
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:inputURL];
CPDFPage *page = [document pageAtIndex:0];
CPDFSignatureWidgetAnnotation *widget = page ? [[CPDFSignatureWidgetAnnotation alloc] initWithPage:page document:document] : nil;
if (!widget) {
    NSLog(@"Cannot create the signature field");
    return;
}
// Zero-size signature field: no text, image, or logo appearance is configured.
[widget setFieldName:@"InvisibleSignature"];
widget.bounds = CGRectZero;
[page updateAndAddAnnotation:widget];

// isSetSignAp:NO: do not generate a dynamic signature appearance.
// CPDFSignaturePermissionsForbidChange: DocMDP permission, no changes are permitted after signing.
BOOL didSign = [document writeSignatureToURL:outputURL
                                  withWidget:widget
                                  PKCS12Cert:certificatePath
                                    password:certificatePassword
                                    location:@"Singapore"
                                      reason:@"Approval"
                                 isSetSignAp:NO
                                 permissions:CPDFSignaturePermissionsForbidChange];
if (!didSign) {
    NSLog(@"Failed to write the invisible signature");
    return;
}

Note: After signing succeeds, ComPDF SDK adds the signing certificate to the trusted certificate folder on the device (see Trust Certificate). As a result, the signer certificate is reported as trusted on the device that signed the document. On other devices, whether the certificate is trusted depends on that device's trust settings.

Step 4: Reopen the output and verify the signature

Verify the exact saved output. Check cryptographic integrity, document modification permissions, certificate validity, and certificate trust separately:

  • verifySignature:: whether the data covered by the signature is unaltered.
  • verifyDocument:: whether changes made after signing comply with the signature's permissions.
  • verifySignatureWithDocument:: performs both checks above and writes the result to CPDFSigner's isSignVerified, which is YES only when both pass.
  • isCertTrusted: whether the signer certificate can be trusted through the certificates in the trusted certificate folder.
swift
guard let signedDocument = CPDFDocument(url: outputURL),
      let signatures = signedDocument.signatures(), signatures.count == 1,
      let signature = signatures.first else {
    print("Expected one signature in the output PDF")
    return
}
// Verify the signature and the document modification state; the result is written to each CPDFSigner.
signature.verifySignature(with: signedDocument)

let integrity = signature.verifySignature(signedDocument)
let permissionsValid = signature.verifyDocument(signedDocument)
var signersVerified = !signature.signers.isEmpty
var certificatesTrusted = !signature.signers.isEmpty
var certificatesWithinValidity = !signature.signers.isEmpty
for signer in signature.signers {
    signersVerified = signersVerified && signer.isSignVerified
    certificatesTrusted = certificatesTrusted && signer.isCertTrusted
    if let certificate = signer.certificates.first,
       let starts = certificate.validityStarts, let ends = certificate.validityEnds {
        certificatesWithinValidity = certificatesWithinValidity && starts <= now && now <= ends
    } else {
        certificatesWithinValidity = false
    }
}
print("Field: \(signature.fieldName ?? ""), bounds: \(signature.bounds)")
print("Signature integrity: \(integrity)")
print("Document permissions valid: \(permissionsValid)")
print("Signer verified: \(signersVerified)")
print("Certificate within validity: \(certificatesWithinValidity)")
print("Certificate trusted: \(certificatesTrusted)")
objective-c
CPDFDocument *signedDocument = [[CPDFDocument alloc] initWithURL:outputURL];
NSArray<CPDFSignature *> *signatures = [signedDocument signatures];
if (signatures.count != 1) {
    NSLog(@"Expected one signature in the output PDF");
    return;
}
CPDFSignature *signature = signatures.firstObject;
// Verify the signature and the document modification state; the result is written to each CPDFSigner.
[signature verifySignatureWithDocument:signedDocument];

BOOL integrity = [signature verifySignature:signedDocument];
BOOL permissionsValid = [signature verifyDocument:signedDocument];
BOOL signersVerified = signature.signers.count > 0;
BOOL certificatesTrusted = signature.signers.count > 0;
BOOL certificatesWithinValidity = signature.signers.count > 0;
for (CPDFSigner *signer in signature.signers) {
    signersVerified = signersVerified && signer.isSignVerified;
    certificatesTrusted = certificatesTrusted && signer.isCertTrusted;
    CPDFSignatureCertificate *certificate = signer.certificates.firstObject;
    BOOL withinValidity = certificate.validityStarts && certificate.validityEnds &&
        [now compare:certificate.validityStarts] != NSOrderedAscending &&
        [now compare:certificate.validityEnds] != NSOrderedDescending;
    certificatesWithinValidity = certificatesWithinValidity && withinValidity;
}
NSLog(@"Field: %@, bounds: %@", signature.fieldName, NSStringFromCGRect(signature.bounds));
NSLog(@"Signature integrity: %d", integrity);
NSLog(@"Document permissions valid: %d", permissionsValid);
NSLog(@"Signer verified: %d", signersVerified);
NSLog(@"Certificate within validity: %d", certificatesWithinValidity);
NSLog(@"Certificate trusted: %d", certificatesTrusted);

When the signed file is modified (for example, bytes covered by the signature are altered), both verifySignature: and isSignVerified return NO. The bounds of an invisible signature reads back as a zero rectangle, and pageIndexWithDocument: returns the index of the page that holds the signature field.

This is basic signature verification. It does not check revocation or verify an LT/LTA profile. For an OCSP check, use CPDFSignatureCertificate's verifyOCSPWithCompletionHandler:. For structured validation reports or long-term validation, see Signature Workflow and Validation Reports and PAdES and Long-Term Validation (LTV).

Inspect the result

The signed PDF page displays no visible signature appearance. Use signatures to read the signature's field name, signers, signing time, reason, location, and other information. PDF readers that support digital signatures can also show the signature information in their signature panel. When a reader does not trust the signer certificate (for example, a self-signed certificate), it may report an unknown signer identity; this does not affect the signature integrity result.