|
@@ -0,0 +1,274 @@
|
|
|
+using ComPDFKit.DigitalSign;
|
|
|
+using ComPDFKit.Import;
|
|
|
+using ComPDFKit.PDFAnnotation;
|
|
|
+using ComPDFKit.PDFAnnotation.Form;
|
|
|
+using ComPDFKit.PDFDocument;
|
|
|
+using ComPDFKit.PDFPage;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Drawing;
|
|
|
+using System.IO;
|
|
|
+using System.Xml.Linq;
|
|
|
+
|
|
|
+namespace DigitalSignatureTest
|
|
|
+{
|
|
|
+ internal class DigitalSignatureTest
|
|
|
+ {
|
|
|
+ static private string outputPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))) + "\\Output\\DigitalSignature";
|
|
|
+ static void Main()
|
|
|
+ {
|
|
|
+ #region Preparation work
|
|
|
+ Console.WriteLine("Running digital signature sample...\n");
|
|
|
+
|
|
|
+ SDKLicenseHelper.LicenseVerify();
|
|
|
+ string certificatePath = "Certificate.pfx";
|
|
|
+ string password = "ComPDFKit";
|
|
|
+
|
|
|
+ if (!Directory.Exists(outputPath))
|
|
|
+ {
|
|
|
+ Directory.CreateDirectory(outputPath);
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ //Sample 0: Create certificate
|
|
|
+ GenerateCertificate();
|
|
|
+
|
|
|
+
|
|
|
+ //Sample 1: Create digital signature
|
|
|
+ CPDFDocument document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
|
|
|
+ CreateDigitalSignature(document, certificatePath, password);
|
|
|
+ document.Release();
|
|
|
+
|
|
|
+ //Sample 2: Verify signature
|
|
|
+ CPDFDocument signedDoc = CPDFDocument.InitWithFilePath("Signed.pdf");
|
|
|
+ VerifyDigitalSignature(signedDoc);
|
|
|
+
|
|
|
+ //Sample 3: Verify certificate
|
|
|
+ VerifyCertificate(certificatePath, password);
|
|
|
+
|
|
|
+ //Sample 4: Trust Certificate
|
|
|
+ TrustCertificate(signedDoc);
|
|
|
+
|
|
|
+ //Sample 5: Remove digital signature
|
|
|
+ RemoveDigitalSignature(signedDoc);
|
|
|
+ signedDoc.Release();
|
|
|
+ Console.ReadLine();
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// in the core function "CPDFPKCS12CertHelper.GeneratePKCS12Cert":
|
|
|
+ ///
|
|
|
+ /// Generate certificate
|
|
|
+ ///
|
|
|
+ /// Password: ComPDFKit
|
|
|
+ ///
|
|
|
+ /// info: /C=SG/O=ComPDFKit/D=R&D Department/CN=Alan/emailAddress=xxxx@example.com
|
|
|
+ ///
|
|
|
+ /// C=SG: This represents the country code "SG," which typically stands for Singapore.
|
|
|
+ /// O=ComPDFKit: This is the Organization (O) field, indicating the name of the organization or entity, in this case, "ComPDFKit."
|
|
|
+ /// D=R&D Department: This is the Department (D) field, indicating the specific department within the organization, in this case, "R&D Department."
|
|
|
+ /// CN=Alan: This is the Common Name (CN) field, which usually represents the name of the individual or entity. In this case, it is "Alan."
|
|
|
+ /// emailAddress=xxxx@example.com: Email is xxxx@example.com
|
|
|
+ ///
|
|
|
+ /// CPDFCertUsage.CPDFCertUsageAll: Used for both digital signing and data validation simultaneously.
|
|
|
+ ///
|
|
|
+ /// is_2048 = true: Enhanced security encryption.
|
|
|
+ /// </summary>
|
|
|
+ private static void GenerateCertificate()
|
|
|
+ {
|
|
|
+ Console.WriteLine("--------------------");
|
|
|
+ Console.WriteLine("Create digital signature.");
|
|
|
+
|
|
|
+ string info = "/C=SG/O=ComPDFKit/D=R&D Department/CN=Alan/emailAddress=xxxx@example.com";
|
|
|
+ string password = "ComPDFKit";
|
|
|
+ if (CPDFPKCS12CertHelper.GeneratePKCS12Cert(info, password, outputPath + "/Certificate.pfx", CPDFCertUsage.CPDFCertUsageAll, true))
|
|
|
+ {
|
|
|
+ Console.WriteLine("Generate PKCS12 certificate done.");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Console.WriteLine("Generate PKCS12 certificate failed.");
|
|
|
+
|
|
|
+ }
|
|
|
+ Console.WriteLine("--------------------");
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ ///
|
|
|
+ /// Adding a signature is divided into two steps:
|
|
|
+ /// creating a signature field and filling in the signature.
|
|
|
+ ///
|
|
|
+ /// Page Index: 0
|
|
|
+ /// Rect: CRect(28, 420, 150, 370)
|
|
|
+ /// Border RGB:{ 0, 0, 0 }
|
|
|
+ /// Widget Background RGB: { 150, 180, 210 }
|
|
|
+ ///
|
|
|
+ /// 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
|
|
|
+ /// Location: Singapor
|
|
|
+ /// IsContentAlginLeft: false
|
|
|
+ /// IsDrawLogo: True
|
|
|
+ /// LogoBitmap: logo.png
|
|
|
+ /// text color RGB: { 0, 0, 0 }
|
|
|
+ /// Output file name: document.FileName + "_Signed.pdf"
|
|
|
+ /// </summary>
|
|
|
+ private static void CreateDigitalSignature(CPDFDocument document, string certificatePath, string password)
|
|
|
+ {
|
|
|
+ Console.WriteLine("--------------------");
|
|
|
+ Console.WriteLine("Create digital signature.");
|
|
|
+ CPDFSignatureCertificate certificate = CPDFPKCS12CertHelper.GetCertificateWithPKCS12Path("Certificate.pfx", "ComPDFKit");
|
|
|
+
|
|
|
+ CPDFPage page = document.PageAtIndex(0);
|
|
|
+ CPDFSignatureWidget signatureField = page.CreateWidget(C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS) as CPDFSignatureWidget;
|
|
|
+ signatureField.SetRect(new CRect(28, 420, 150, 370));
|
|
|
+
|
|
|
+ signatureField.SetWidgetBorderRGBColor(new byte[] { 0, 0, 0 });
|
|
|
+ signatureField.SetWidgetBgRGBColor(new byte[] { 150, 180, 210 });
|
|
|
+
|
|
|
+ CPDFSignatureConfig signatureConfig = new CPDFSignatureConfig
|
|
|
+ {
|
|
|
+ Text = GetGrantorFromDictionary(certificate.SubjectDict),
|
|
|
+ Content =
|
|
|
+ "Name: " + GetGrantorFromDictionary(certificate.SubjectDict) + "\n" +
|
|
|
+ "Date: " + DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss") + "\n" +
|
|
|
+ "Reason: I am the owner of the document.\n" +
|
|
|
+ "Location: Singapor\n" +
|
|
|
+ "DN: " + certificate.Subject + "\n",
|
|
|
+ IsContentAlginLeft = false,
|
|
|
+ IsDrawLogo = true,
|
|
|
+ LogoBitmap = new Bitmap("Logo.png"),
|
|
|
+ textColor = new float[] { 0, 0, 0 }
|
|
|
+ };
|
|
|
+ signatureField.UpdataApWithSignature(signatureConfig);
|
|
|
+ if (document.WriteSignatureToFilePath(signatureField,
|
|
|
+ outputPath + "/" + document.FileName + "_Signed.pdf",
|
|
|
+ certificatePath, password,
|
|
|
+ "Singapore",
|
|
|
+ "I am the owner of the document.", CPDFSignaturePermissions.CPDFSignaturePermissionsNone))
|
|
|
+ {
|
|
|
+ Console.WriteLine("File saved in " + outputPath + "/" + document.FileName + "_Signed.pdf.");
|
|
|
+ Console.WriteLine("Create digital signature done.");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Console.WriteLine("Create digital signature failed.");
|
|
|
+
|
|
|
+ }
|
|
|
+ Console.WriteLine("--------------------");
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Remove signature
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="document"></param>
|
|
|
+ private static void RemoveDigitalSignature(CPDFDocument document)
|
|
|
+ {
|
|
|
+ Console.WriteLine("--------------------");
|
|
|
+ Console.WriteLine("Remove digital signature.");
|
|
|
+ CPDFSignature signature = document.GetSignatureList()[0];
|
|
|
+ document.RemoveSignature(signature, true);
|
|
|
+ document.WriteToFilePath(outputPath + "/" + document.FileName + "_RemovedSign.pdf");
|
|
|
+ Console.WriteLine("File saved in " + outputPath + "/" + document.FileName + "_RemovedSign.pdf");
|
|
|
+ Console.WriteLine("Remove digital signature done.");
|
|
|
+ Console.WriteLine("--------------------");
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// There are two steps can help you to trust a certificate.
|
|
|
+ /// Set "CPDFSignature.SignCertTrustedFolder" as a folder path,
|
|
|
+ /// then call CPDFSignatureCertificate.AddToTrustedCertificates()
|
|
|
+ /// </summary>
|
|
|
+ private static void TrustCertificate(CPDFDocument document)
|
|
|
+ {
|
|
|
+ Console.WriteLine("--------------------");
|
|
|
+ Console.WriteLine("Trust certificate.");
|
|
|
+
|
|
|
+ CPDFSignature signature = document.GetSignatureList()[0];
|
|
|
+ CPDFSignatureCertificate signatureCertificate = signature.SignerList[0].CertificateList[0];
|
|
|
+
|
|
|
+ Console.WriteLine("Certificate trusted status: " + signatureCertificate.IsTrusted.ToString());
|
|
|
+
|
|
|
+ Console.WriteLine("---Begin trusted---");
|
|
|
+
|
|
|
+ CPDFSignature.SignCertTrustedFolder = AppDomain.CurrentDomain.BaseDirectory + @"\TrustedFolder\";
|
|
|
+ if (signatureCertificate.AddToTrustedCertificates())
|
|
|
+ {
|
|
|
+ Console.WriteLine("Certificate trusted status: " + signatureCertificate.IsTrusted.ToString());
|
|
|
+ Console.WriteLine("Trust certificate done.");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Console.WriteLine("Trust certificate failed.");
|
|
|
+ }
|
|
|
+ Console.WriteLine("--------------------");
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Verify certificate
|
|
|
+ ///
|
|
|
+ /// To verify the trustworthiness of a certificate,
|
|
|
+ /// you need to verify that all certificates in the certificate chain are trustworthy.
|
|
|
+ ///
|
|
|
+ /// In ComPDFKit,this progess is automatic.
|
|
|
+ /// You should call the "CPDFSignatureCertificate.CheckCertificateIsTrusted" first.
|
|
|
+ /// then you can view the "CPDFSignatureCertificate.IsTrusted" property.
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="document">A signed document</param>
|
|
|
+ private static void VerifyCertificate(string certificatePath, string password)
|
|
|
+ {
|
|
|
+ Console.WriteLine("--------------------");
|
|
|
+ Console.WriteLine("Verify certificate.");
|
|
|
+ CPDFSignatureCertificate certificate = CPDFPKCS12CertHelper.GetCertificateWithPKCS12Path(certificatePath, password);
|
|
|
+ certificate.CheckCertificateIsTrusted();
|
|
|
+ if (certificate.IsTrusted)
|
|
|
+ {
|
|
|
+ Console.WriteLine("Certificate is trusted");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Console.WriteLine("Certificate is not trusted");
|
|
|
+ }
|
|
|
+ Console.WriteLine("Verify certificate done.");
|
|
|
+ Console.WriteLine("--------------------");
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Verify digital signature
|
|
|
+ ///
|
|
|
+ ///
|
|
|
+ ///
|
|
|
+ /// </summary>
|
|
|
+ private static void VerifyDigitalSignature(CPDFDocument document)
|
|
|
+ {
|
|
|
+ bool isSignVerified = document.GetSignatureList()[0].SignerList[0].IsSignVerified;
|
|
|
+ bool isCertTrusted = document.GetSignatureList()[0].SignerList[0].IsCertTrusted;
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static string GetGrantorFromDictionary(Dictionary<string, string> dictionary)
|
|
|
+ {
|
|
|
+ string grantor = string.Empty;
|
|
|
+ dictionary.TryGetValue("CN", out grantor);
|
|
|
+ if (string.IsNullOrEmpty(grantor))
|
|
|
+ {
|
|
|
+ dictionary.TryGetValue("OU", out grantor);
|
|
|
+ }
|
|
|
+ if (string.IsNullOrEmpty(grantor))
|
|
|
+ {
|
|
|
+ dictionary.TryGetValue("O", out grantor);
|
|
|
+ }
|
|
|
+ if (string.IsNullOrEmpty(grantor))
|
|
|
+ {
|
|
|
+ grantor = "Unknown Signer";
|
|
|
+ }
|
|
|
+ return grantor;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+}
|