)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to digitally sign PDF with certificate in Xamarin Forms

Platform: Xamarin.Forms |
Control: PDF |
Published Date: August 14, 2017 |
Last Revised Date: April 25, 2019

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.

Project

File Name

Summary

Portable project

ISave.cs

Represent the base interface for save operation.

iOS Project

SaveIOS.cs

Save implementation for iOS device.

PreviewControllerDS.cs

Helper class for viewing the PDF file in iOS device.

Android project

SaveAndroid.cs

Save implementation for Android device.

WinPhone project

SaveWinPhone.cs

Save implementation for Windows phone device.

UWP project

SaveWindows.cs

Save implementation for UWP device.

Windows(8.1) project

SaveWindows81.cs

Save implementation for WinRT device.

 

  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 document screenshot

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.

 

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
Jordan Capa
Apr 23, 2019

thanks for the information! for the beginners and want to save time, I found zetpdf.com i heard a lot of this. I suggest it because it's the fastest pdf SKD for .NET applications.

Reply

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon