C# Digital Signature: Sign PDFs Through Programming
2024-09-27 19:04:05 • Filed to: Signature Tips • Proven solutions
You don't need an eSign tool to sign digital documents. But how can you do that? You can use programming languages like C#. So, all you need is an IDE instead of a specialized eSign tool. If you are a programmer, you likely already have one. Thus, you can jump right into it. If you are curious about C# digital signature, continue reading.
Do not worry if you are not a programmer. We also provided an alternative tool, Wondershare PDFelement, which is very user-friendly!
- Part 1. How To Add a Certified Signature Field to a PDF Using C# Digital Signature
- Part 2. How To Sign a PDF Using C# Digital Signature
- Part 3. How To Verify a Certified Signature on a PDF Using C# Digital Signature
- Part 4. Add a Certified Signature to a PDF without Using C#: Use Wondershare PDFelement
Part 1. How To Add a Certified Signature Field to a PDF Using C# Digital Signature
Before you can sign a PDF using C#, you first need to add a signature field. It is where the signature goes. Do not worry.
- Get the PDF and page using this code:
using (PDFDoc doc = new PDFDoc(docpath))
{ Page page1 = doc.GetPage(1); }
- Create a text field that you can lock with the field permission feature. Use this code:
TextWidget annot1 = TextWidget.Create(doc, new Rect(50, 550, 350, 600), "new_text_field");
page1.AnnotPushBack(annot1);
- Create a signature field in the PDF using his code snippet:
DigitalSignatureField certification_sig_field = doc.CreateDigitalSignatureField(cert_field_name);
SignatureWidget widgetAnnot = SignatureWidget.Create(doc, new Rect(0, 100, 200, 150), certification_sig_field); page1.AnnotPushBack(widgetAnnot);
- Suppose you have prepared a signature field appearance. Add it to the PDF using this code snippet:
Image img = Image.Create(doc, appearance_image_path); widgetAnnot.CreateSignatureAppearance(img);
If you did not make a signature field appearance, feel free to skip this step.
- Prepare the document locking permission level, which will be applied after the document certification process.
certification_sig_field.SetDocumentPermissions(DigitalSignatureField.DocumentPermissions.e_annotating_formfilling_signing_allowed);
- Now, prepare to lock the text field that you have created in step 2.
string[] fields_to_lock = new string[1]; fields_to_lock[0] = "new_text_field"; certification_sig_field.SetFieldPermissions(DigitalSignatureField.FieldPermissions.e_include, fields_to_lock);
certification_sig_field.CertifyOnNextSave(private_key_file_path, keyfile_password);
- Save the document by calling the Save method. PDFNet will sign the document using the information provided.
doc.Save(outpath, 0);
The code should look like this:
using (PDFDoc doc = new PDFDoc(docpath))
{
Page page1 = doc.GetPage(1);
TextWidget annot1 = TextWidget.Create(doc, new Rect(50, 550, 350, 600), "new_text_field");
page1.AnnotPushBack(annot1);
DigitalSignatureField certification_sig_field = doc.CreateDigitalSignatureField(cert_field_name);
SignatureWidget widgetAnnot = SignatureWidget.Create(doc, new Rect(0, 100, 200, 150), certification_sig_field); page1.AnnotPushBack(widgetAnnot);
Image img = Image.Create(doc, appearance_image_path); widgetAnnot.CreateSignatureAppearance(img);
certification_sig_field.SetDocumentPermissions(DigitalSignatureField.DocumentPermissions.e_annotating_formfilling_signing_allowed);
string[] fields_to_lock = new string[1]; fields_to_lock[0] = "asdf_test_field";
certification_sig_field.SetFieldPermissions(DigitalSignatureField.FieldPermissions.e_include, fields_to_lock);
certification_sig_field.CertifyOnNextSave(private_key_file_path, keyfile_password);
doc.Save(outpath, 0);
}
Part 2. How To Sign a PDF Using C# Digital Signature
Now that there is a signature field on the PDF, you can finally sign it. Follow these steps to sign it with C# digital signature:
- Retrieve the unsigned signature field using this code snippet:
using (PDFDoc doc = new PDFDoc(docpath))
{
Field found_approval_field = doc.GetField(approval_field_name);
DigitalSignatureField approval_digsig_field = new DigitalSignatureField(found_approval_field);
}
- Prepare the signature and signature handler for signing using this C# snippet:
approval_digsig_field.SignOnNextSave(private_key_file_path, keyfile_password);
- Perform an incremental save operation to save the document with the signature.
doc.Save(outpath, SDFDoc.SaveOptions.e_incremental);
The final code should look like this:
using (PDFDoc doc = new PDFDoc(docpath))
{
Field found_approval_field = doc.GetField(approval_field_name);
DigitalSignatureField approval_digsig_field = new DigitalSignatureField(found_approval_field);
approval_digsig_field.SignOnNextSave(private_key_file_path, keyfile_password);
doc.Save(outpath, SDFDoc.SaveOptions.e_incremental);
}
Part 3. How To Verify a Certified Signature on a PDF Using C# Digital Signature
C# also allows you to verify a certified signature. However, you need a special digital signature API like the apryse Docs digital signature verification API.
Why should you do this? You need to ensure digital signatures are authentic and not forged. It also ensures the agreement is legally binding.
To verify a digital signature with C#, follow these steps:
- Choose a security level to use. Then, change any verification options that you wish to change.
VerificationOptions opts = new VerificationOptions(VerificationOptions.SignatureVerificationSecurityLevel.e_compatibility_and_archiving);
- Add trust root to store the trusted certificates contained in VerificationOptions.
opts.AddTrustedCertificate(in_public_key_file_path);
- Verify the digital signature.
PDFDoc.SignaturesVerificationStatus result = doc.VerifySignedDigitalSignatures(opts);
The complete code should look like this:
using (PDFDoc doc = new PDFDoc(in_docpath))
{
VerificationOptions opts = new VerificationOptions(VerificationOptions.SignatureVerificationSecurityLevel.e_compatibility_and_archiving);
opts.AddTrustedCertificate(in_public_key_file_path);
PDFDoc.SignaturesVerificationStatus result = doc.VerifySignedDigitalSignatures(opts);
}
You may get the following results:
- SignaturesVerificationStatus.e_unsigned: The PDF has no signed signature fields.
- SignaturesVerificationStatus.e_failure: There is a hard failure in verification on at least one signature. It does not include trust issues.
- SignaturesVerificationStatus.e_untrusted: Trust for at least one signature could not be verified.
- SignaturesVerificationStatus.e_verified: All the signed signatures in the document are verified. Please note that unsigned fields are skipped.
Part 4. Add a Certified Signature to a PDF without Using C#: Use Wondershare PDFelement
There is one glaring problem with using C# digital signature to sign PDFs. It is too technical. You need to be a programmer or at least know C# to do it. Thus, for non-tech-savvy people, it is impossible to do.
Thankfully, there are simpler tools you can use to sign digital documents, one of which is Wondershare PDFelement. Wondershare PDFelement is such a universal tool because anybody can use it. One quality it is known for is user-friendliness. Indeed, it lets you sign or even collect signatures very easily.
Please note that Wondershare PDFelement eSign adheres to legal and industry standards like ISO 27001 and HIPAA. So rest assured that signed documents are authentic and secured. It even has a built-in certificate verifier!
But enough of the introduction. Let us get you started on how to use Wondershare PDFelement eSign.
Part 4.1. How To Send PDFs for Signatures
That is right. Wondershare PDFelement does not just allow you to sign PDFs. It also allows you to collect signatures from other people. With this, you can complete agreements even if you are not physically together. Wondershare PDFelement also provides notifications when a person has signed the document. So you'll never miss a beat and proceed to the next step immediately.
Here are the steps to collect signatures with Wondershare PDFelement:
- Open the agreement that needs other people's signatures on Wondershare PDFelement. Click Protect > Request eSign.
- A new window will pop up. Add the signers' email addresses in the "Add Recipients" field.
- Select whether to sign the document first or last. Check the appropriate checkbox for your decision.
- Under "Settings," set the signing order and frequency of updates.
- Click Next.
- Add and assign the essential signature fields through drag and drop.
- Click Send.
Part 4.2. How To Sign PDFs with Wondershare PDFelement
Now, what if you are the one who needs to sign the document? As mentioned above, Wondershare PDFelement also lets you do that. And there are two methods. You can sign the PDF using the desktop app or the email link. We will discuss both methods:
A. Sign PDFs Using the Wondershare PDFelement Desktop App
- Open Wondershare PDFelement and log into your account. Click Agreement on the left panel. Open an agreement labeled "Waiting for me."
- Click the Sign tag to get directed to the signature fields where you need to sign.
- Select the signature field to sign it.
- Create your signature by typing it, drawing it, or uploading an image. Click OK.
- Click Click to Sign at the notification bar that will appear at the bottom, then click Next.
B. Sign PDFs Using the Wondershare PDFelement Desktop App
Suppose you are away from home. You don't have your laptop with you. You can still sign documents using the sign button on the email notification. Follow this set of instructions to do so:
- Open the email notification about the eSign request. Click the Sign the Document button.
- Tick the checkbox for I agree with EULA and Privacy to use electronic records and signature. Click Continue.
- Click the signature field that you need to sign. If this is your first time signing documents like this, create your signature. You can Type, Draw, or Upload an image.
- Click Save to apply the signature to the document and save the signature for future use.
Conclusion
You can use C# to add digital signatures to PDFs. But it does not mean you have to study programming just to do it. There are alternative methods, many of which are easier to do.
One of the best tools to sign PDFs is Wondershare PDFelement. It is a PDF solution boasting so many useful tools. Its standards-compliant eSign feature allows you to sign PDFs easily. Not only that, but it also lets you send signature requests to others. As such, many people choose Wondershare PDFelement as their eSign tool. That said, it is one you should check out.
Free Download or Buy PDFelement right now!
Free Download or Buy PDFelement right now!
Buy PDFelement right now!
Buy PDFelement right now!
Up to 18% OFF: Choose the right plan for you and your team.
PDFelement for Individuals
Edit, print, convert, eSign, and protect PDFs on Windows PC or Mac.
PDFelement for Team
Give you the flexibility to provision, track and manage licensing across groups and teams.
Audrey Goodwin
chief Editor
Generally rated4.5(105participated)