|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | +using Datalogics.PDFL; |
| 5 | + |
| 6 | +/* |
| 7 | + * |
| 8 | + * This sample program demonstrates the use of AddDigitalSignature for PAdES |
| 9 | + * (PDF Advanced Electronic Signatures) with an explicit signature policy. |
| 10 | + * PAdES policy signatures (EPES) conform to the ETSI EN 319 142 standard, |
| 11 | + * use the ETSI.CAdES.detached SubFilter, and embed one or more signature |
| 12 | + * policy identifiers along with optional policy qualifiers. |
| 13 | + * |
| 14 | + * Copyright (c) 2026, Datalogics, Inc. All rights reserved. |
| 15 | + * |
| 16 | + */ |
| 17 | +namespace AddPAdESPolicySignature |
| 18 | +{ |
| 19 | + class AddPAdESPolicySignature |
| 20 | + { |
| 21 | + static void Main(string[] args) |
| 22 | + { |
| 23 | + Console.WriteLine("AddPAdESPolicySignature Sample:"); |
| 24 | + |
| 25 | + using (new Library()) |
| 26 | + { |
| 27 | + Console.WriteLine("Initialized the library."); |
| 28 | + |
| 29 | + String sInput = Library.ResourceDirectory + "Sample_Input/SixPages.pdf"; |
| 30 | + String sLogo = Library.ResourceDirectory + "Sample_Input/ducky_alpha.tif"; |
| 31 | + String sOutput = "PAdESPolicySignature-out.pdf"; |
| 32 | + |
| 33 | + String sPEMCert = Library.ResourceDirectory + "Sample_Input/Credentials/PEM/ecSecP521r1Cert.pem"; |
| 34 | + String sPEMKey = Library.ResourceDirectory + "Sample_Input/Credentials/PEM/ecSecP521r1Key.pem"; |
| 35 | + |
| 36 | + if (args.Length > 0) |
| 37 | + sInput = args[0]; |
| 38 | + |
| 39 | + if (args.Length > 1) |
| 40 | + sOutput = args[1]; |
| 41 | + |
| 42 | + if (args.Length > 2) |
| 43 | + sLogo = args[2]; |
| 44 | + |
| 45 | + Console.WriteLine("Input file: " + sInput); |
| 46 | + Console.WriteLine("Writing to output: " + sOutput); |
| 47 | + |
| 48 | + using (Document doc = new Document(sInput)) |
| 49 | + { |
| 50 | + using (Datalogics.PDFL.SignDoc sigDoc = new Datalogics.PDFL.SignDoc()) |
| 51 | + { |
| 52 | + // Setup Sign params |
| 53 | + sigDoc.FieldID = SignatureFieldID.CreateFieldWithQualifiedName; |
| 54 | + sigDoc.FieldName = "Signature_es_:signatureblock"; |
| 55 | + |
| 56 | + // Set credential related attributes |
| 57 | + // PAdES signatures use SHA-384 digest with EC credentials |
| 58 | + sigDoc.DigestCategory = DigestCategory.Sha384; |
| 59 | + sigDoc.CredentialDataFormat = CredentialDataFmt.NonPFX; |
| 60 | + sigDoc.SetNonPfxSignerCert(sPEMCert, 0, CredentialStorageFmt.OnDisk); |
| 61 | + sigDoc.SetNonPfxPrivateKey(sPEMKey, 0, CredentialStorageFmt.OnDisk); |
| 62 | + |
| 63 | + // Set the signature type to PAdES (PDF Advanced Electronic Signatures). |
| 64 | + // NOTE: Signature type must be set prior to adding policies. |
| 65 | + sigDoc.DocSignType = SignatureType.PADES; |
| 66 | + |
| 67 | + // Define a signature policy (SigPolicyId) using an OID. |
| 68 | + sigDoc.AddSigPolicy("2.16.724.1.3.1.1.2.1.9"); |
| 69 | + |
| 70 | + // Add a policy qualifier (SPuri) pointing to the policy specification document. |
| 71 | + sigDoc.AddSigPolicyQualifierURI( |
| 72 | + "https://sede.administracion.gob.es/politica_de_firma_anexo_1.pdf"); |
| 73 | + |
| 74 | + // Setup the signer information |
| 75 | + // (Logo image is optional) |
| 76 | + sigDoc.SetSignerInfo(sLogo, 0.5F, "John Doe", "Chicago, IL", "Approval", "Datalogics, Inc.", |
| 77 | + DisplayTraits.KDisplayAll); |
| 78 | + |
| 79 | + // Set the size and location of the signature box (optional) |
| 80 | + // If not set, invisible signature will be placed on first page |
| 81 | + sigDoc.SignatureBoxPageNumber = 0; |
| 82 | + sigDoc.SignatureBoxRectangle = new Rect(100, 300, 400, 400); |
| 83 | + |
| 84 | + // Setup Save params |
| 85 | + sigDoc.OutputPath = sOutput; |
| 86 | + |
| 87 | + // Finally, sign and save the document |
| 88 | + sigDoc.AddDigitalSignature(doc); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments