|
@@ -46,15 +46,49 @@ namespace DigitalSignatureTest
|
|
|
//Sample 3: Verify certificate
|
|
|
VerifyCertificate(certificatePath, password);
|
|
|
|
|
|
- //Sample 4: Trust Certificate
|
|
|
+ //Sample 4: Print digital signature info
|
|
|
+ PrintDigitalSignatureInfo(signedDoc);
|
|
|
+
|
|
|
+
|
|
|
+ //Sample 5: Trust Certificate
|
|
|
TrustCertificate(signedDoc);
|
|
|
|
|
|
- //Sample 5: Remove digital signature
|
|
|
+ //Sample 6: Remove digital signature
|
|
|
RemoveDigitalSignature(signedDoc);
|
|
|
signedDoc.Release();
|
|
|
+ Console.WriteLine("Done.\n");
|
|
|
+
|
|
|
Console.ReadLine();
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// this samples shows how to get main properties in digital signature.
|
|
|
+ /// read API reference to see all of the properties can get
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="document"></param>
|
|
|
+ private static void PrintDigitalSignatureInfo(CPDFDocument document)
|
|
|
+ {
|
|
|
+ Console.WriteLine("--------------------");
|
|
|
+ Console.WriteLine("Print digital signature info.");
|
|
|
+ foreach (var signature in document.GetSignatureList())
|
|
|
+ {
|
|
|
+ signature.VerifySignatureWithDocument(document);
|
|
|
+ Console.WriteLine("Name: " + signature.Name);
|
|
|
+ Console.WriteLine("Location: " + signature.Location);
|
|
|
+ Console.WriteLine("Location: " + signature.Reason);
|
|
|
+ foreach (var signer in signature.SignerList)
|
|
|
+ {
|
|
|
+ Console.WriteLine("Name: " + signer.AuthenDate);
|
|
|
+ foreach (var certificate in signer.CertificateList)
|
|
|
+ {
|
|
|
+ Console.WriteLine("Subject: " + certificate.Subject);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Console.WriteLine("Print digital signature info done.");
|
|
|
+ Console.WriteLine("--------------------");
|
|
|
+ }
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// in the core function "CPDFPKCS12CertHelper.GeneratePKCS12Cert":
|
|
|
///
|
|
@@ -114,6 +148,7 @@ namespace DigitalSignatureTest
|
|
|
/// IsDrawLogo: True
|
|
|
/// LogoBitmap: logo.png
|
|
|
/// text color RGB: { 0, 0, 0 }
|
|
|
+ /// content color RGB: { 0, 0, 0 }
|
|
|
/// Output file name: document.FileName + "_Signed.pdf"
|
|
|
/// </summary>
|
|
|
private static void CreateDigitalSignature(CPDFDocument document, string certificatePath, string password)
|
|
@@ -136,12 +171,13 @@ namespace DigitalSignatureTest
|
|
|
"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" +
|
|
|
+ "Location: "+ certificate.SubjectDict["C"] + "\n" +
|
|
|
"DN: " + certificate.Subject + "\n",
|
|
|
IsContentAlginLeft = false,
|
|
|
IsDrawLogo = true,
|
|
|
LogoBitmap = new Bitmap("Logo.png"),
|
|
|
- textColor = new float[] { 0, 0, 0 }
|
|
|
+ textColor = new float[] { 0, 0, 0 },
|
|
|
+ contentColor = new float[] { 0, 0, 0 }
|
|
|
};
|
|
|
signatureField.UpdataApWithSignature(signatureConfig);
|
|
|
if (document.WriteSignatureToFilePath(signatureField,
|
|
@@ -162,7 +198,8 @@ namespace DigitalSignatureTest
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// Remove signature
|
|
|
+ /// Remove digital signature
|
|
|
+ /// after removed, both of the apperence and data will be removed
|
|
|
/// </summary>
|
|
|
/// <param name="document"></param>
|
|
|
private static void RemoveDigitalSignature(CPDFDocument document)
|
|
@@ -171,8 +208,9 @@ namespace DigitalSignatureTest
|
|
|
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");
|
|
|
+ string filePath = outputPath + "\\" + document.FileName + "_RemovedSign.pdf";
|
|
|
+ document.WriteToFilePath(filePath);
|
|
|
+ Console.WriteLine("File saved in " + filePath);
|
|
|
Console.WriteLine("Remove digital signature done.");
|
|
|
Console.WriteLine("--------------------");
|
|
|
}
|
|
@@ -194,7 +232,12 @@ namespace DigitalSignatureTest
|
|
|
|
|
|
Console.WriteLine("---Begin trusted---");
|
|
|
|
|
|
- CPDFSignature.SignCertTrustedFolder = AppDomain.CurrentDomain.BaseDirectory + @"\TrustedFolder\";
|
|
|
+ string trustedFolder = AppDomain.CurrentDomain.BaseDirectory + @"\TrustedFolder\";
|
|
|
+ if (!Directory.Exists(trustedFolder))
|
|
|
+ {
|
|
|
+ Directory.CreateDirectory(trustedFolder);
|
|
|
+ }
|
|
|
+ CPDFSignature.SignCertTrustedFolder = trustedFolder;
|
|
|
if (signatureCertificate.AddToTrustedCertificates())
|
|
|
{
|
|
|
Console.WriteLine("Certificate trusted status: " + signatureCertificate.IsTrusted.ToString());
|
|
@@ -239,15 +282,25 @@ namespace DigitalSignatureTest
|
|
|
/// <summary>
|
|
|
/// Verify digital signature
|
|
|
///
|
|
|
- ///
|
|
|
- ///
|
|
|
+ /// Refresh the validation status before reading the attributes, or else you may obtain inaccurate results.
|
|
|
+ /// Is the signature verified: indicating whether the document has been tampered with.
|
|
|
+ /// Is the certificate trusted: referring to the trust status of the certificate.
|
|
|
/// </summary>
|
|
|
private static void VerifyDigitalSignature(CPDFDocument document)
|
|
|
{
|
|
|
- bool isSignVerified = document.GetSignatureList()[0].SignerList[0].IsSignVerified;
|
|
|
- bool isCertTrusted = document.GetSignatureList()[0].SignerList[0].IsCertTrusted;
|
|
|
-
|
|
|
-
|
|
|
+ Console.WriteLine("--------------------");
|
|
|
+ Console.WriteLine("Verify digital signature.");
|
|
|
+ foreach (var signature in document.GetSignatureList())
|
|
|
+ {
|
|
|
+ signature.VerifySignatureWithDocument(document);
|
|
|
+ foreach (var signer in signature.SignerList)
|
|
|
+ {
|
|
|
+ Console.WriteLine("Is the certificate trusted: " + signer.IsCertTrusted.ToString());
|
|
|
+ Console.WriteLine("Is the signature verified: " + signer.IsSignVerified.ToString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Console.WriteLine("Verify digital signature done.");
|
|
|
+ Console.WriteLine("--------------------");
|
|
|
}
|
|
|
|
|
|
public static string GetGrantorFromDictionary(Dictionary<string, string> dictionary)
|