Digitally Sign PDF with QR Code in C#

Digitally Sign PDF with QR Code in C#

In the modern era, the digitally signed PDFs with QR code have become more secure and quickly verifiable for automated digital processing systems. Another good reason to use QR codes is that they are capable of containing a lot of useful information. If you are keen to learn how to eSign PDF files with QR code in C#, you have landed at the right place.

The following points will be covered in this article:

C# Library to Digitally Sign PDF with QR Code

You can download the DLL or install it by running the command (as given below) in the NuGet package manager console.

Install-Package GroupDocs.Signature

Steps to Digitally Sign PDF with QR Code in C#

After installation, let’s start to write a few lines of code.

Please follow the steps and the code snippet mentioned below:

  1. Instantiate the Signature class and pass the source document’s path as a constructor parameter.
  2. Initialize an instance of QrCodeSignOptions class and set EncodeType and Text properties.
  3. Invoke Sign method of Signature class and pass output file’s path and QrCodeSignOptions to it.

You can simply copy & paste the following code sample into your C# file:

using (Signature signature = new Signature("sample.pdf"))
{
// create QRCode option with predefined QRCode text
QrCodeSignOptions options = new QrCodeSignOptions("JohnSmith")
{
// setup QRCode encoding type
EncodeType = QrCodeTypes.QR,
// set signature position
Left = 100,
Top = 100
};
signature.Sign("SampleSigned.pdf", options);
}

The output can be seen in the image below:

QR Code Sign

eSign PDF with QR code in C# - Advanced Options

PDF eSign library provides some additional useful options in QrCodeSignOptions class. You can use this class to specify signature alignment, margins, borders, font, colors, and background. Moreover, you can verify the results of a newly created signature by using SignResult class.

Following are the steps to create QR Code with advanced options:

  1. Initialize a new instance of the Signature class by providing the source document’s path as a constructor parameter.
  2. Instantiate an instance of the QrCodeSignOptions class.
  3. Set the signature’s text and encoding type.
  4. Set the signature’s position.
  5. Specify the signature’s alignment, margins, borders, font, colors, and background.
  6. Invoke Sign method of Signature class and pass output file’s path and QrCodeSignOptions to it.
  7. Optionally you can obtain SignResult object to analyze or verify the newly created signatures.

The following C# code shows how to sign a PDF with QR code using advanced options:

using (Signature signature = new Signature("sample.pdf"))
{
// create QRCode option with predefined QRCode text
QrCodeSignOptions options = new QrCodeSignOptions("12345678")
{
// setup QRCode encoding type
EncodeType = QrCodeTypes.QR,
// set signature position
Left = 100,
Top = 100,
// set signature alignment
// when VerticalAlignment is set the Top coordinate will be ignored.
// Use Margin properties Top, Bottom to provide vertical offset
VerticalAlignment = VerticalAlignment.Top,
// when HorizontalAlignment is set the Left coordinate will be ignored.
// Use Margin properties Left, Right to provide horizontal offset
HorizontalAlignment = HorizontalAlignment.Right,
Margin = new Padding() { Top = 20, Right = 20 },
// adjust signature appearance
// setup signature border
Border = new Border()
{
Color = Color.DarkGreen,
DashStyle = GroupDocs.Signature.Domain.DashStyle.DashLongDashDot,
Transparency = 0.5,
Visible = true,
Weight = 2
},
// set text color and Font
ForeColor = Color.Red,
Font = new SignatureFont { Size = 12, FamilyName = "Comic Sans MS" },
// setup background
Background = new Background()
{
Color = Color.LimeGreen,
Transparency = 0.5,
Brush = new LinearGradientBrush(Color.LimeGreen, Color.DarkGreen)
}
};
// sign document to file and pouplate the SignResut
SignResult signResult = signature.Sign("signed.pdf", options);
Console.WriteLine("\nList of newly created signatures:");
int number = 1;
foreach (BaseSignature temp in signResult.Succeeded)
{
Console.WriteLine($"Signature #{number++}: Type: {temp.SignatureType} Id:{temp.SignatureId}, Location: {temp.Left}x{temp.Top}. Size: {temp.Width}x{temp.Height}");
}
}

You will get the output as seen in the image below:

eSign PDF

Get a Free License

You can avail a free temporary license to test the API without evaluation limitations.

Conclusion

In this article, we have learned how to Digitally Sign PDF with QR Code in C# using an electronic signature library. Besides, you also have gained knowledge about some advanced options to add the QR Code Sign in your PDF documents. You may visit the documentation to know about other features of the digital signature library.

Additionally, we suggest you to follow our Getting Started guide.

Ask a question

You can let us know about your questions or queries on our forum.

See Also