|
| 1 | +--- |
| 2 | +title: Getting Started |
| 3 | +description: The digital signature feature enables you to sign and validate a PDF document. |
| 4 | +page_title: Digital Signature - Getting Started |
| 5 | +slug: radpdfprocessing-features-digital-signature-getting-started |
| 6 | +tags: digital, signature, getting, started |
| 7 | +position: 1 |
| 8 | +--- |
| 9 | + |
| 10 | +# Getting Started |
| 11 | + |
| 12 | +RadPdfProcessing allows adding a digital signature while editing a created from scratch document (or importing an existing one). |
| 13 | + |
| 14 | +>To use the signing functionality in PdfProcessing for **.NET Standard/.NET Core**, you must add a reference to the **System.Security.Cryptography.Pkcs** NuGet package, version 6 or newer (This functionality is available since R1 2022 SP1). |
| 15 | +
|
| 16 | +## Signing a Document |
| 17 | + |
| 18 | +To sign a document, follow the steps: |
| 19 | + |
| 20 | +1\. Create a **Signature** object which takes a [X509Certificate2](https://msdn.microsoft.com/en-us/library/system.security.cryptography.x509certificates.x509certificate2(v=vs.110).aspx) object as a parameter. This is the certificate that will be used to sign the PDF document. |
| 21 | + |
| 22 | +2\. When instantiated, add the **Signature** to the document's content using a [SignatureField]({%slug radpdfprocessing-model-interactive-forms-form-fields-signaturefield%}). |
| 23 | + |
| 24 | +3\. To create a signature, which has a visual representation, you must create a [SignatureWidget]({%slug radpdfprocessing-model-annotations-widgets%}) and associate the Widget annotation with the signed [SignatureField]({%slug radpdfprocessing-model-interactive-forms-form-fields-signaturefield%}). The widget also needs a [FormSource]({%slug radpdfprocessing-model-formsource-overview%}) object applied to its Content.**NormalContentSource** property. A **FormSource** could be filled with data using the FixedContentEditor. |
| 25 | + |
| 26 | +>caution When exporting a digitally signed document, a stream that allows both, reading and writing, should be passed. Otherwise, an exception is thrown: *NotSupportedException: 'Stream does not support reading.'*. |
| 27 | +
|
| 28 | +The following example shows a full code snippet for a simple signing of a newly created document: |
| 29 | + |
| 30 | +#### **[C#] Example: Sign a document** |
| 31 | + |
| 32 | +{{region radpdfprocessing-features-digital-signature_2}} |
| 33 | + |
| 34 | + using System; |
| 35 | + using Telerik.Windows.Documents.Fixed.Model.Annotations; |
| 36 | + using System.Security.Cryptography.X509Certificates; |
| 37 | + using Telerik.Windows.Documents.Fixed.Model.Editing; |
| 38 | + using Telerik.Windows.Documents.Fixed.Model.InteractiveForms; |
| 39 | + using Telerik.Windows.Documents.Fixed.Model.Objects; |
| 40 | + using Telerik.Windows.Documents.Fixed.Model.Resources; |
| 41 | + using Telerik.Windows.Documents.Fixed.Model; |
| 42 | + using Telerik.Windows.Documents.Fixed.Model.DigitalSignatures; |
| 43 | + using System.Windows; |
| 44 | + using System.IO; |
| 45 | + |
| 46 | + namespace ConsoleNetFramework |
| 47 | + { |
| 48 | + internal class Program |
| 49 | + { |
| 50 | + static void Main(string[] args) |
| 51 | + { |
| 52 | + int signatureFieldWidth = 200; |
| 53 | + int signatureFieldHeight = 50; |
| 54 | + int signaturePositionLeft = 10; |
| 55 | + int signaturePositionTop = 10; |
| 56 | + |
| 57 | + X509Certificate2 certificate = new System.Security.Cryptography.X509Certificates.X509Certificate2("Certificate.pfx", "johndoe"); |
| 58 | + SignatureField pdfSignature = new SignatureField("SignatureField"); |
| 59 | + pdfSignature.Signature = new Signature(certificate); |
| 60 | + |
| 61 | + Form pdfForm = new Telerik.Windows.Documents.Fixed.Model.Objects.Form(); |
| 62 | + pdfForm.FormSource = new FormSource(); |
| 63 | + pdfForm.FormSource.Size = new Size(signatureFieldWidth, signatureFieldHeight); |
| 64 | + FixedContentEditor editor = new FixedContentEditor(pdfForm.FormSource); |
| 65 | + pdfForm.Position.Translate(signaturePositionLeft, signaturePositionTop); |
| 66 | + editor.DrawText($"{certificate.GetNameInfo(X509NameType.SimpleName, false)} {DateTime.Now.ToString("yyyy.MM.dd HH:mm")}"); |
| 67 | + |
| 68 | + SignatureWidget signatureWidget = pdfSignature.Widgets.AddWidget(); |
| 69 | + signatureWidget.Content.NormalContentSource = pdfForm.FormSource; |
| 70 | + signatureWidget.Rect = new Rect(signaturePositionLeft,signaturePositionTop,signatureFieldWidth,signatureFieldHeight); |
| 71 | + signatureWidget.RecalculateContent(); |
| 72 | + |
| 73 | + RadFixedDocument document = new RadFixedDocument(); |
| 74 | + RadFixedPage pdfPage = document.Pages.AddPage(); |
| 75 | + pdfPage.Annotations.Add(signatureWidget); |
| 76 | + |
| 77 | + FixedContentEditor pageEditor = new FixedContentEditor(pdfPage); |
| 78 | + pageEditor.Position.Translate(signaturePositionLeft, signaturePositionTop); |
| 79 | + pageEditor.DrawForm(pdfForm.FormSource); |
| 80 | + document.AcroForm.FormFields.Add(pdfSignature); |
| 81 | + signatureWidget.RecalculateContent(); |
| 82 | + |
| 83 | + string signedDocumentFilePath = "signed.pdf"; |
| 84 | + File.Delete(signedDocumentFilePath); |
| 85 | + using (System.IO.Stream output = new System.IO.FileStream(signedDocumentFilePath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite)) |
| 86 | + { |
| 87 | + new Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider().Export(document, output); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | +{{endregion}} |
| 94 | + |
| 95 | +>important In .NET Standard use __Telerik.Documents.Primitives.Rect__ instead of __System.Windows.Rect__. |
| 96 | +
|
| 97 | + |
| 98 | + |
| 99 | +>important When signing an existing document (after the import) we must be sure the AcroForm's ViewersShouldRecalculateWidgetAppearances property is set to false, otherwise, the exported and signed PDF document could not be shown as a signed. |
| 100 | +
|
| 101 | +## Signature Encodings |
| 102 | + |
| 103 | +RadPdfProcessing enables you to sign and validate signature fields using standard signature encodings: |
| 104 | + |
| 105 | +* adbe.x509.rsa_sha1 (PKCS #1) |
| 106 | + |
| 107 | +* adbe.pkcs7.sha1 (PKCS #7) |
| 108 | + |
| 109 | +* adbe.pkcs7.detached (PKCS #7 Detached) |
| 110 | + |
| 111 | +## Signature Flags |
| 112 | + |
| 113 | +The signature flags were introduced in R2022 SP1. You can set the flags with the following code: |
| 114 | + |
| 115 | +#### **[C#] Example: Set signature flags** |
| 116 | + |
| 117 | +{{region radpdfprocessing-features-digital-signature_5}} |
| 118 | + |
| 119 | + pdfDocument.AcroForm.SignatureFlags = SignatureFlags.None; |
| 120 | + |
| 121 | +{{endregion}} |
| 122 | + |
| 123 | +The possible values are: |
| 124 | +* __None__: Indicates no signature fields exist. |
| 125 | +* __SignaturesExist:__ If set, the document contains at least one signature field. This flag allows a viewer application to enable user interface items (such as menu items or pushbuttons) related to signature processing without having to scan the entire document for the presence of signature fields. |
| 126 | +* __AppendOnly:__ The document contains signatures that may be invalidated if the file is saved in a way that alters its previous contents. Viewer applications can use this flag to present a user requesting a full save with an additional alert box warning that signatures will be invalidated and requiring explicit confirmation before continuing with the operation. |
| 127 | + |
| 128 | +## See Also |
| 129 | + |
| 130 | +* [Form]({%slug radpdfprocessing-model-form%}) |
| 131 | +* [Form Fields]({%slug radpdfprocessing-model-interactive-forms-form-fields%}) |
| 132 | +* [AcroForm]({%slug radpdfprocessing-model-interactive-forms-acroform%}) |
| 133 | +* [SignatureField]({%slug radpdfprocessing-model-interactive-forms-form-fields-signaturefield%}) |
| 134 | +* [Signing a document with a digital signature]({%slug signing-a-document-with-digital-signature%}) |
| 135 | +* [Widgets Types]({%slug radpdfprocessing-model-annotations-widgets%}) |
| 136 | +* [How to Create Invisible Signatures for PDF Documents]({%slug pdf-invisible-signatures%}) |
| 137 | +* [Signing a PDF Document with a SignatureWidget]({%slug sign-pdf-with-signature-widget%}) |
| 138 | +* [Verifying If Digital Signatures Exist in PDF Documents]({%slug verify-digital-signatures-radpdfprocessing%}) |
| 139 | +* [Signing an Unsigned PDF Document that Contains a Digital Signature with RadPdfProcessing]({%slug pdfprocessing-sign-an-unsigned-pdf%}) |
| 140 | +* [Digitally Sign Document](https://demos.telerik.com/document-processing/pdfprocessing/digitally_sign_document) |
0 commit comments