1PTPDFDoc* doc = [[PTPDFDoc alloc] initWithFilepath: in_docpath];
2
3PTPage* page1 = [doc GetPage: 1];
4
5// Create a digital signature field and associated widget.
6PTDigitalSignatureField* digsig_field = [doc CreateDigitalSignatureField: in_sig_field_name];
7PTSignatureWidget* widgetAnnot = [PTSignatureWidget CreateWithDigitalSignatureField: doc pos: [[PTPDFRect alloc] initWithX1: 143 y1: 287 x2: 219 y2: 306] field: digsig_field];
8[page1 AnnotPushBack: widgetAnnot];
9
10// Create a digital signature dictionary inside the digital signature field, in preparation for signing.
11// For security reasons, set the contents size to a value greater than but as close as possible to the size you expect your final signature to be, in bytes.
12// ... or, if you want to apply a certification signature, use createSigDictForCustomCertification instead.
13[digsig_field CreateSigDictForCustomSigning: @"Adobe.PPKLite" in_subfilter_type: e_ptadbe_pkcs7_detached in_contents_size_to_reserve: 7500];
14
15// (OPTIONAL) Set the signing time in the signature dictionary, if no secure embedded timestamping support is available from your signing provider.
16PTDate* current_date = [[PTDate alloc] init];
17[current_date SetCurrentTime];
18[digsig_field SetSigDictTimeOfSigning: current_date];
19
20// Save the document incrementally to avoid invalidating any previous signatures.
21[doc SaveToFile: in_outpath flags: e_ptincremental];
22
23// Digest the relevant bytes of the document in accordance with ByteRanges surrounding the signature.
24NSData* pdf_digest = [digsig_field CalculateDigest: e_ptsha256];
25
26PTX509Certificate* signer_cert = [[PTX509Certificate alloc] initWithIn_certificate_path: in_cert_path];
27
28// Optionally, you can add a custom signed attribute at this point, such as one of the PAdES ESS attributes.
29// The function we provide takes care of generating the correct PAdES ESS attribute depending on your digest algorithm.
30NSData* pades_versioned_ess_signing_cert_attribute = [PTDigitalSignatureField GenerateESSSigningCertPAdESAttribute: signer_cert in_digest_algorithm_type: e_ptsha256];
31
32// Generate the signedAttrs component of CMS, passing any optional custom signedAttrs (e.g. PAdES ESS). The signedAttrs are certain attributes that become protected by their inclusion in the signature.
33NSData* signedAttrs = [PTDigitalSignatureField GenerateCMSSignedAttributes: pdf_digest in_custom_signedattributes_buf: pades_versioned_ess_signing_cert_attribute];
34
35// Calculate the digest of the signedAttrs (i.e. not the PDF digest, this time).
36NSData* signedAttrs_digest = [PTDigestAlgorithm CalculateDigest: e_ptsha256 in_message_buf: signedAttrs];