首先,从IIS导出一个证书,其中包含密码 protected PFX文件中的private 和 public 密钥。 你可以使用以下代码对创建的PDF文档进行签名://Create a HTML to PDF converter object with default settings
HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();
htmlToPdfConverter.TableOfContentsOptions.AutoTocItemsEnabled = true;
Document pdfDocument = null;
try
{
string htmlWithDigitalSignatureMarker = htmlStringTextBox.Text;
string baseUrl = baseUrlTextBox.Text;
//Convert a HTML string with a marker for digital signature to a PDF document object
pdfDocument = htmlToPdfConverter.ConvertHtmlToPdfDocumentObject(htmlWithDigitalSignatureMarker, baseUrl);
//Make the HTML element with 'digital_signature_element' mapping ID a link to digital signature properties
HtmlElementMapping digitalSignatureMapping = htmlToPdfConverter.HtmlElementsMappingOptions.HtmlElementsMappingResult.GetElementByMappingId("digital_signature_element");
if (digitalSignatureMapping!= null)
{
PdfPage digitalSignaturePage = digitalSignatureMapping.PdfRectangles[0].PdfPage;
RectangleF digitalSignatureRectangle = digitalSignatureMapping.PdfRectangles[0].Rectangle;
string certificateFilePath = Server.MapPath("~/DemoAppFiles/Input/Certificates/wnvpdf.pfx");
//Get the certificate from password protected PFX file
DigitalCertificatesCollection certificates = DigitalCertificatesStore.GetCertificates(certificateFilePath,"wnvpdf");
DigitalCertificate certificate = certificates[0];
//Create the digital signature
DigitalSignatureElement signature = new DigitalSignatureElement(digitalSignatureRectangle, certificate);
signature.Reason ="Protect the document from unwanted changes";
signature.ContactInfo ="The contact email is support@winnovative-software.com";
signature.Location ="Development server";
digitalSignaturePage.AddElement(signature);
}
//Save the PDF document in a memory buffer
byte[] outPdfBuffer = pdfDocument.Save();
}
finally
{
//Close the PDF document
if (pdfDocument!= null)
pdfDocument.Close();
}