以下是这个过程。
c#源代码
private static string certificate = @"E:\CertificatePath";//Certificate
private static string password = XXXXXXXX!";
private static string srcFile = @"xxxx\Merged.pdf";
private static string dstFile = @"xxxx\Output.pdf";// output pdf path
static void Main(string[] args)
{
try
{
try
{
LicenseKey.LoadLicenseFil(@"xxx\x.xml");//key
var licenseInfo = LicenseKey.GetLicenseeInfo();
bool isTrial = LicenseKey.IsTrial();
Console.WriteLine("License loaded successfully...");
Console.WriteLine();
}
catch (Exception ex)
{
Console.WriteLine("Exception occurred while loading license - " + ex.Message);
}
DateTime start = DateTime.Now;
Console.WriteLine("Started the process for adding digital signature on pdf");
Console.WriteLine();
File.Delete(dstFile);
AddDigitalSignature(certificate, password, srcFile, "", dstFile);
Console.WriteLine(string.Format("Process completed in {0} ms", (DateTime.Now - start).TotalMilliseconds));
Console.WriteLine();
}
catch (Exception ex)
{
Console.WriteLine("Exception occurred while adding digital certificate on file...");
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
public static void AddDigitalSignature(string _certficate, string _password, string _srcPDF, string _srcPDFPassword, string _dstPDF)
{
try
{
using (PdfReader reader = new PdfReader(_srcPDF))
{
var signer = new PdfSigner(reader, new FileStream(_dstPDF, FileMode.OpenOrCreate, FileAccess.Write), false);
Rectangle rect = new Rectangle(0, 0, 0, 0);
DigitalCert digitalCert = new DigitalCert(certificate, password);
// Creating the appearance
PdfSignatureAppearance appearance = signer.GetSignatureAppearance()
.SetSignatureCreator("XXXX")
.SetReason("XXXXX")
.SetLocation("XXXXX")
.SetReuseAppearance(false)
.SetPageRect(rect)
.SetPageNumber(1);
signer.SetSignDate(DateTime.Now);
signer.SetFieldName("XXXXX");
// Creating the signature
IExternalSignature pks = new PrivateKeySignature(digitalCert.pk, digitalCert.DigestAlgorithm);
signer.SignDetached(pks, digitalCert.chain, null, null, null, 0, PdfSigner.CryptoStandard.CADES);
reader.Close();
}
}
catch (Exception ex)
{`enter code here`
throw ex;
}
}