Foxit PDF SDK for Windows

How to Apply Signatures in Foxit PDF SDK (.NET)

The PDF Signature module can be used to create and sign with digital signatures for PDF documents. This protects the security of documents and prevents it from being tampered with maliciously. It can let the receiver ensure that the document is released by the signer and the contents of the document are complete and unchanged. Foxit PDF SDK provides APIs to create a digital signature, verify the validity of a signature, delete an existing digital signature, get and set properties of a digital signature, display a signature and customize the appearance of the signature form fields.

Note: Foxit PDF SDK provides default Signature callbacks which supports the following two types of signature filter and subfilter:

(1) filter: Adobe.PPKLite – subfilter: adbe.pkcs7.detached
(2) filter: Adobe.PPKLite – subfilter: adbe.pkcs7.sha1

If you use one of the above signature filters and subfilters, you can sign a PDF document and verify the validity of signature by default without needing to register a custom callback.

Example:

How to sign the PDF document with a signature

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using foxit.common;
using foxit.pdf;
using foxit;
using foxit.pdf.annots;
using foxit.common.fxcrt;
using System.Runtime.InteropServices;
using foxit.pdf.interform;

static foxit.common.DateTime GetLocalDateTime()
{
System.DateTimeOffset rime = System.DateTimeOffset.Now;
foxit.common.DateTime datetime = new foxit.common.DateTime();
datetime.year = (UInt16)rime.Year;
datetime.month = (UInt16)rime.Month;
datetime.day = (ushort)rime.Day;
datetime.hour = (UInt16)rime.Hour;
datetime.minute = (UInt16)rime.Minute;
datetime.second = (UInt16)rime.Second;
datetime.utc_hour_offset = (short)rime.Offset.Hours;
datetime.utc_minute_offset = (ushort)rime.Offset.Minutes;
return datetime;
}

static Signature AddSiganture(PDFPage pdf_page, string sub_filter) {
float page_height = pdf_page.GetHeight();
float page_width = pdf_page.GetWidth();
RectF new_sig_rect = new RectF(0, (float)(page_height*0.9), (float)(page_width*0.4), page_height);
// Add a new signature to page.
Signature new_sig = pdf_page.AddSignature(new_sig_rect);
if (new_sig.IsEmpty()) return null;
// Set values for the new signature.
new_sig.SetKeyValue(Signature.KeyName.e_KeyNameSigner, "Foxit PDF SDK");
String new_value = String.Format("As a sample for subfilter \"{0}\"", sub_filter);
new_sig.SetKeyValue(Signature.KeyName.e_KeyNameReason, new_value);
new_sig.SetKeyValue(Signature.KeyName.e_KeyNameContactInfo, "[email protected]");
new_sig.SetKeyValue(Signature.KeyName.e_KeyNameDN, "CN=CN,[email protected]");
new_sig.SetKeyValue(Signature.KeyName.e_KeyNameLocation, "Fuzhou, China");
new_value = String.Format("As a sample for subfilter \"{0}\"", sub_filter);
new_sig.SetKeyValue(Signature.KeyName.e_KeyNameText, new_value);
foxit.common.DateTime sign_time = GetLocalDateTime();
new_sig.SetSignTime(sign_time);
String image_file_path = input_path + "FoxitLogo.jpg";
using (Image image = new Image(image_file_path))
{
new_sig.SetImage(image, 0);
// Set appearance flags to decide which content would be used in appearance.
int ap_flags = Convert.ToInt32(Signature.APFlags.e_APFlagLabel | Signature.APFlags.e_APFlagSigner |
Signature.APFlags.e_APFlagReason | Signature.APFlags.e_APFlagDN |
Signature.APFlags.e_APFlagLocation | Signature.APFlags.e_APFlagText |
Signature.APFlags.e_APFlagSigningTime | Signature.APFlags.e_APFlagBitmap);
new_sig.SetAppearanceFlags(ap_flags);
}

return new_sig;
}

static void AdobePPKLiteSignature(PDFDoc pdf_doc) {
string filter = "Adobe.PPKLite";
string sub_filter = "adbe.pkcs7.detached";

using (PDFPage pdf_page = pdf_doc.GetPage(0))
{
// Add a new signature to first page.
using (Signature new_signature = AddSiganture(pdf_page, sub_filter))
{
// Set filter and subfilter for the new signature.
new_signature.SetFilter(filter);
new_signature.SetSubFilter(sub_filter);

// Sign the new signature.
String signed_pdf_path = output_directory + "signed_newsignature.pdf";

String cert_file_path = input_path + "foxit_all.pfx";
byte[] cert_file_password = Encoding.ASCII.GetBytes("123456");
new_signature.StartSign(cert_file_path, cert_file_password,
Signature.DigestAlgorithm.e_DigestSHA1, signed_pdf_path, IntPtr.Zero, null);
Console.WriteLine("[Sign] Finished!");
}
}
}

static void Main(String[] args)
{
...
AdobePPKLiteSignature(pdf_doc);
...
}

 

Updated on April 18, 2019

Was this article helpful?
Thanks for your feedback. If you have a comment on how to improve the article, you can write it here: