Articles in this section
Category / Section

How to digitally sign PDF with certificate in Xamarin Forms?

3 mins read

Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can digitally sign a PDF with certificate in Xamarin Forms.

Steps to digitally sign a PDF with certificate programmatically:

  1. Create a new C# Xamarin.Forms application project. Create Xamarin Forms application project
  2. Select a project template and required platforms to deploy the application. In this application, to share the portable assemblies across multiple platforms, the .NET Standard code sharing strategy has been selected. For more details about code sharing, refer here.
    Note:

    If .NET Standard is not available in the code sharing strategy, the Portable Class Library (PCL) can be selected.

Project template screenshot

  1. Install the Syncfusion.Xamarin.Pdf NuGet package as reference to your .NET Framework application from NuGet.org. NuGet package reference
  2. Add new Forms XAML page in portable project if there is no XAML page is defined in the App class. Otherwise, proceed to the next step.
  • To add the new XAML page, right-click the project and select Add>New Items and add a Forms XAML page from the list. Name it as MainPage.
  • In the App class of portable project (App.cs), replace the existing constructor of App class with the following code snippet, which invokes the MainPage.

C#

public App ()
{
    InitializeComponent();
    //The root page of your application
    MainPage = new MainPage();
}

 

  1. In the MainPage.Xaml, add new button as follows.
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:local="clr-namespace:DigitalSignatureSample"
                 x:Class="DigitalSignatureSample.MainPage">
        <StackLayout VerticalOptions="Center">
            <Button Text="Generate Document" Clicked="OnButtonClicked" HorizontalOptions="Center"/>
        </StackLayout>
    </ContentPage>
    

 

  1. Include the following namespaces in the MainPage.xmal.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Security;
using Syncfusion.Drawing;

 

  1. Use the following code snippet in the click event method in MainPage.Xaml.cs to digitally sign a PDF with certificate and save it in a stream.

C#

//Creates a new PDF document
PdfDocument document = new PdfDocument();
//Adds a new page
PdfPageBase page = document.Pages.Add();
PdfGraphics graphics = page.Graphics;
graphics.DrawString("Signature", new PdfStandardFont(PdfFontFamily.Helvetica, 10, PdfFontStyle.Bold), PdfBrushes.Black, new PointF(10,30));
//Creates a certificate instance from PFX file with private key
Stream certificateStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("DigitalSignatureSample.Assets.PDF.pfx");
PdfCertificate pdfCert = new PdfCertificate(certificateStream, "syncfusion");
//Creates a digital signature
PdfSignature signature = new PdfSignature(document, page, pdfCert, "Signature");
//Sets an image
Stream imageStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("DigitalSignatureSample.Assets.signature.jpg");
PdfBitmap signatureImage = new PdfBitmap(imageStream);
//Sets signature information
signature.Bounds = new RectangleF(new PointF(100, 30), new SizeF(100,30));
signature.ContactInfo = "johndoe@owned.us";
signature.LocationInfo = "Honolulu, Hawaii";
signature.Reason = "I am author of this document.";
//Draws the signature image
graphics.DrawImage(signatureImage, 100, 30,100,30);
//Save the PDF document to stream
MemoryStream stream = new MemoryStream();
document.Save(stream);
//Closes the document
document.Close(true);
//Save the stream into PDF file
Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("DigitalSignature.pdf", "application/pdf", stream);

 

  1. Download the helper files from this link and add them into the mentioned project. These helper files saves the stream as a physical file and open the file for viewing.
  1. Compile and execute the application. This creates a digitally signed PDF document.

By executing the program, you will get the PDF document as follows. Output PDF document

Download the complete work sample from DigitalSignatureSample.zip.

Take a moment to peruse the documentation, where you can find other options like adding a digital signature using stream, signing an existing document, adding a timestamp in digital signature and features like redacting PDF document and protect PDF document with code examples.

Refer here to explore the rich set of Syncfusion Essential PDF features.

See Also:

Create a digitally signed PDF file in Windows Forms

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

 

Conclusion

I hope you enjoyed learning about how to digitally sign PDF with certificate in Xamarin Forms.

You can refer to our PDF feature tour page to know about its other groundbreaking feature representations. You can also explore our documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied