PDFelement
PDFelement: PDF Editor, Scanner
Edit, Convert, Annotate PDF
Edit, Annotate PDF
OPEN OPEN

C# Digital Signature: Sign PDFs Through Programming

author avatar

2024-04-18 14:00:40 • 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

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.

  1. Get the PDF and page using this code:

using (PDFDoc doc = new PDFDoc(docpath)) 
{ Page page1 = doc.GetPage(1); }
  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);
  1. 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);

  1. 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.

  1. 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);
  
  1. 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);
  1. 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:

  1. 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);
}
  1. Prepare the signature and signature handler for signing using this C# snippet:

approval_digsig_field.SignOnNextSave(private_key_file_path, keyfile_password);
  1. 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:

  1. 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);
  1. Add trust root to store the trusted certificates contained in VerificationOptions.

opts.AddTrustedCertificate(in_public_key_file_path);
  1. 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.

signing document using wondershare pdfelement

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:

  1. Open the agreement that needs other people's signatures on Wondershare PDFelement. Click Protect > Request eSign.
request ensign on wondershare pdfelement
  1. A new window will pop up. Add the signers' email addresses in the "Add Recipients" field.
  2. Select whether to sign the document first or last. Check the appropriate checkbox for your decision.
  3. Under "Settings," set the signing order and frequency of updates.
setting up the ensign request
  1. Click Next.
  2. Add and assign the essential signature fields through drag and drop.
adding the signature fields
  1. 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
  1. Open Wondershare PDFelement and log into your account. Click Agreement on the left panel. Open an agreement labeled "Waiting for me."

opening an agreement that needs signature
  1. Click the Sign tag to get directed to the signature fields where you need to sign.
  2. Select the signature field to sign it.
finding the right signature fields
  1. Create your signature by typing it, drawing it, or uploading an image. Click OK.
creating the digital signature
  1. Click Click to Sign at the notification bar that will appear at the bottom, then click Next.
signing the agreement using wondershare pdfelement
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:

  1. Open the email notification about the eSign request. Click the Sign the Document button.

email notification of esign request
  1. Tick the checkbox for I agree with EULA and Privacy to use electronic records and signature. Click Continue.
agreeing with eula and privacy policy
  1. 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.
creating the digital signature
  1. 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.

Perpetual Plan
$129.99 $159
$129.99 $159
PDFelement for Team

Give you the flexibility to provision, track and manage licensing across groups and teams.

Perpetual Plan
$139/user $159
Contact us to learn more about the custom services we can do for you.
author avatar

Audrey Goodwin

chief Editor

(Click to rate this post)

Generally rated4.5(105participated)

Home > Signature Tips > C# Digital Signature: Sign PDFs Through Programming
Increase your productivity today.