When you protect your PDF documents with passwords you can optionally specify a set of permissions. The permissions determine how users can interact with the file. For example, you can apply permissions to your document to prohibit the user from printing or using cut and paste operations. This article demonstrates how to change security permissions of PDF documents using Spire.PDF for .NET in C# and VB.NET.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Change Security Permissions of a PDF Document

The following are the steps to apply security permissions to a PDF document using Spire.PDF for .NET.

  • Create a PdfDocument object.
  • Load a sample PDF file using PdfDocument.LoadFileFile() method.
  • Specify open password and permission password. The open password can be set to empty so that the generated document will not require a password to open.
  • Encrypt the document with the open password and permission password, and set the security permissions using PdfDocument.Security.Encypt() method. This method takes PdfPermissionsFlags enumeration as a parameter, which defines user access permissions for an encrypted document.
  • Save the document to another PDF file using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Security;

namespace ChangeSecurityPermission
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            //Load a sample PDF file
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

            //Specify open password
            string openPsd = string.Empty;

            //Specify permission password
            string permissionPsd = "e-iceblue";

            //Encrypt the document with open password and permission password, and set the permissions and encryption key size
            doc.Security.Encrypt(openPsd, permissionPsd, PdfPermissionsFlags.FullQualityPrint, PdfEncryptionKeySize.Key128Bit);

            //Save the document to another PDF file
            doc.SaveToFile("SecurityPermissions.pdf");
        }
    }
}

C#/VB.NET: Change Security Permissions of PDF Documents

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Published in Security

C# Digitally Signing PDF Files

Digital signatures provide authenticity, integrity, and non-repudiation for PDF documents, making them essential for legal contracts, financial documents, and other sensitive materials. In this comprehensive tutorial, we'll explore how to digitally sign PDFs in C# using the powerful Spire.PDF for .NET library. We'll cover basic signing, custom appearances, timestamps, and signature fields with detailed code explanations.

Table of Contents:

.NET Library for Adding Digital Signatures to PDF

Spire.PDF for .NET is a robust library that enables developers to create, read, edit, and convert PDF documents programmatically. For digital signatures, it provides comprehensive support through key classes in the Spire.Pdf.Interactive.DigitalSignatures namespace:

  • PdfOrdinarySignatureMaker : The primary class for creating standard PDF signatures
  • PdfSignature : Represents a digital signature in a PDF document
  • PdfSignatureAppearance : Controls the visual representation of the signature
  • PdfPKCS7Formatter : Handles cryptographic formatting including timestamps
  • PdfSignatureField : Represents a signature field in an interactive PDF form

Before implementing any signature functionality, ensure you have:

  • Spire.PDF for .NET installed via NuGet
  • A valid digital certificate (PFX file) with private key access
  • Proper permissions to sign documents

How to Digitally Sign PDFs in C#

  • Step 1. Install Spire.PDF for .NET.
  • Step 2. Use PdfDocument to load the PDF file.
  • Step 3. Load the digital certificate file using X509Certificate2.
  • Step 4. Use PdfOrdinarySignatureMaker to create and apply the digital signature.
  • Step 5. Save the signed PDF with the embedded signature.

Digitally Sign a PDF with a Certificate

To sign a PDF, you need a digital certificate, usually in PFX format. This certificate serves to verify the identity of the signer. Below is a code snippet demonstrating how to sign a PDF using a digital certificate.

Code Example:

using Spire.Pdf;
using Spire.Pdf.Interactive.DigitalSignatures;
using System.Security.Cryptography.X509Certificates;

namespace DigitallySignPdf
{
    class Program
    {
        static void Main(string[] args) 
        {
            // Create a PdfDocument object to work with PDF files
            PdfDocument doc = new PdfDocument();

            // Load an existing PDF file from the specified path
            doc.LoadFromFile("C:/Users/Administrator/Desktop/Input.pdf");

            // Specify the path to the PFX certificate and its password
            string filePath = "C:/Users/Administrator/Desktop/certificate.pfx";
            string password = "e-iceblue";

            // Load the X.509 certificate from the PFX file
            X509Certificate2 x509 = new X509Certificate2(filePath, password,
                X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.EphemeralKeySet);

            // Create a PdfOrdinarySignatureMaker to handle the signature process using the loaded certificate
            PdfOrdinarySignatureMaker signatureMaker = new PdfOrdinarySignatureMaker(doc, x509);

            // Create the signature on the PDF document with a specified signature name
            signatureMaker.MakeSignature("signature 1");

            // Save the signed PDF to a new file
            doc.SaveToFile("Signed.pdf");

            // Release resources
            doc.Dispose();
        }
    }
}

Key Components Explained:

  1. PdfDocument : The central class representing the PDF document. It provides methods for loading, manipulating, and saving PDF files.
  2. X509Certificate2 : The .NET class for handling digital certificates. The key storage flags are crucial:
    • MachineKeySet: Stores keys in the machine-level key store
    • EphemeralKeySet: Prevents key persistence in memory for better security
  3. PdfOrdinarySignatureMaker : The workhorse for digital signatures. It:
    • Manages the signing process
    • Handles cryptographic operations
    • Embeds the signature in the PDF
  4. MakeSignature() : The method that actually applies the signature. The string parameter names the signature, which must be unique within the document.

This basic implementation creates an invisible digital signature. The signature validates the document's integrity but doesn't provide a visual representation.

Output:

An invisible digital signature in PDF.

Customize PDF Signature Appearance

A digital signature can be customized to include additional information such as the signer's name, contact info, and even a visual signature image. This enhances the visual appeal and informational value of the signature.

Code Example:

using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Interactive.DigitalSignatures;
using System.Security.Cryptography.X509Certificates;

namespace CustomSignatureAppearance
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a PdfDocument object to work with PDF files
            PdfDocument doc = new PdfDocument();

            // Load an existing PDF file from the specified path
            doc.LoadFromFile("C:/Users/Administrator/Desktop/Input.pdf");

            // Specify the path to the PFX certificate and its password
            string filePath = "C:/Users/Administrator/Desktop/certificate.pfx";
            string password = "e-iceblue";

            // Load the X.509 certificate from the PFX file
            X509Certificate2 x509 = new X509Certificate2(filePath, password,
                X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.EphemeralKeySet);

            // Create a PdfOrdinarySignatureMaker to handle the signature process using the loaded certificate
            PdfOrdinarySignatureMaker signatureMaker = new PdfOrdinarySignatureMaker(doc, x509);

            // Get the signature
            PdfSignature signature = signatureMaker.Signature;

            // Configure the signature properties like the signer's name, contact information, location, and sign reason
            signature.Name = "Gary";
            signature.ContactInfo = "726349";
            signature.Location = "U.S.";
            signature.Reason = "This is the final version.";

            // Create a signature appearance
            PdfSignatureAppearance appearance = new PdfSignatureAppearance(signature);

            // Set labels for the signature
            appearance.NameLabel = "Signer: ";
            appearance.ContactInfoLabel = "Phone: ";
            appearance.LocationLabel = "Location: ";
            appearance.ReasonLabel = "Reason: ";

            // Load an image
            PdfImage image = PdfImage.FromFile("C:/Users/Administrator/Desktop/signature.png");

            // Set the image as the signature image
            appearance.SignatureImage = image;

            // Set the graphic mode as SignImageAndSignDetail
            appearance.GraphicMode = GraphicMode.SignImageAndSignDetail;

            // Get the last page
            PdfPageBase page = doc.Pages[doc.Pages.Count - 1];

            // Add the signature to a specified location of the page
            signatureMaker.MakeSignature("signature 1", page, 54.0f,  330.0f, 280.0f, 90.0f, appearance);

            // Save the signed PDF to a new file
            doc.SaveToFile("Signed.pdf");

            // Release resources
            doc.Dispose();
        }
    }
}

Key Features Explained:

  1. PdfSignature Metadata :

    • Name: Identifies the signer
    • ContactInfo: Provides contact details
    • Location: Geographical signing location
    • Reason: Purpose of signing
  2. PdfSignatureAppearance : Controls visual elements:

    • Label customization for metadata fieldsŸ
    • Image integration via SignatureImage property
    • Layout controlthrough GraphicMode (options: SignImageOnly, SignDetailOnly, SignImageAndSignDetail)
  3. Precise Placement : The extended MakeSignature overload allows specifying:

    • Target page
    • X/Y coordinates
    • Width/Height dimensions

Output:

A visible digital signature with custom appearance in PDF.

To enhance the visibility and trustworthiness of your digitally signed PDF when opened in Adobe Reader, you can enable a validation indicator by applying the following method:

signatureMaker.SetAcro6Layers(false);

Digital signature with a validation indicator.

Add a Timestamp to PDF Digital Signature

Timestamping a digital signature adds an additional layer of security by proving when the document was signed. This is particularly important for long-term validity.

Code Example:

using Spire.Pdf;
using Spire.Pdf.Interactive.DigitalSignatures;
using Spire.Pdf.Security;
using System.Security.Cryptography.X509Certificates;

namespace SignPdfWithTimestamp
{
    class Program
    {
        static void Main(string[] args)
        {
            // Load PDF document
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile("C:/Users/Administrator/Desktop/Input.pdf");

            // Specify the path to the PFX certificate and its password
            string filePath = "C:/Users/Administrator/Desktop/certificate.pfx";
            string password = "e-iceblue";

            // Load the X.509 certificate from the PFX file
            X509Certificate2 x509 = new X509Certificate2(filePath, password,
                X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.EphemeralKeySet);

            // Initialize PDFPKCS#7Formatter
            PdfPKCS7Formatter formatter = new PdfPKCS7Formatter(x509, false);

            // Set the timestamp service to a public timestamp server
            formatter.TimestampService = new TSAHttpService("http://tsa.cesnet.cz:3161/tsa");

            // Initialize OCSP service for online certificate status checking
            formatter.OCSPService = new OCSPHttpService(null);

            // Apply signature
            PdfOrdinarySignatureMaker signatureMaker = new PdfOrdinarySignatureMaker(doc, formatter);
            signatureMaker.MakeSignature("signature 1");

            // Save and cleanup
            doc.SaveToFile("SignWithTimeStamp.pdf");
            doc.Dispose();
        }
    }
}

Timestamp Implementation Details:

  1. PdfPKCS7Formatter : Enhances basic signing with:
    • Timestamp support
    • OCSP revocation checking
    • Advanced cryptographic formatting
  2. TSAHttpService : Connects to a Time Stamp Authority (TSA) server. Public TSAs include:
    • http://timestamp.digicert.com
    • http://tsa.cesnet.cz:3161/tsa
    • http://timestamp.sectigo.com
  3. OCSPHttpService : Optional Online Certificate Status Protocol service for real-time certificate validity checking.

Output:

PDF digital signature with an embedded timestamp.

Create a Signable Signature Field in PDF

Digital signature fields allow users to sign PDF documents interactively. This is essential for forms that require user signatures.

Code Example:

using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Fields;
using Spire.Pdf.Graphics;

namespace AddDigitalSignatureFiled
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize a new PdfDocument object
            PdfDocument doc = new PdfDocument();

            // Load the existing PDF from the specified path
            doc.LoadFromFile("C:/Users/Administrator/Desktop/Input.pdf");

            // Retrieve the last page of the document
            PdfPageBase page = doc.Pages[doc.Pages.Count - 1];

            // Create a signature field on the specified page
            PdfSignatureField signatureField = new PdfSignatureField(page, "signature");

            // Customize the appearance of the signature field
            signatureField.BorderWidth = 1.0f;
            signatureField.BorderStyle = PdfBorderStyle.Solid;
            signatureField.BorderColor = new PdfRGBColor(System.Drawing.Color.Black);
            signatureField.HighlightMode = PdfHighlightMode.Outline;
            signatureField.Bounds = new RectangleF(54.0f, 350.0f, 200.0f, 100.0f);

            // Enable form creation if none exists in the document
            doc.AllowCreateForm = (doc.Form == null);

            // Add the signature field to the document's form
            doc.Form.Fields.Add(signatureField);

            // Save the modified document to a new file
            doc.SaveToFile("SignatureField.pdf", FileFormat.PDF);
            doc.Dispose();
        }
    }
}

Signature Field Features:

  1. PdfSignatureField : Represents a signable field in a PDF form with properties for:
    • Visual styling (border, color)
    • Positioning and sizing
    • Interaction behavior
  2. Form Handling : The code automatically handles PDF form creation if none exists.
  3. Deferred Signing : Fields can be added now and signed later by end-users or additional processes.

Output:

Unsigned signature field.

Conclusion

Digitally signing PDFs with C# using Spire.PDF provides a robust solution for document authentication. Throughout this tutorial, we've explored:

  1. Basic certificate-based signing
  2. Custom signature appearances with images and metadata
  3. Timestamp integration for long-term validation
  4. Signature fields for form-based workflows

By implementing these techniques, you can enhance document security, compliance, and user trust in your applications. Whether for contracts, legal documents, or internal approvals, Spire.PDF simplifies end-to-end digital signing while maintaining industry standards.

FAQs

Q1: How do I verify a digitally signed PDF?

Spire.PDF provides verification capabilities through the PdfSignature class. You can check the VerifySignature method to validate signatures programmatically. For detailed guide, refer to: Verify Digital Signature in PDF with C#.

Q2: What certificate formats are supported?

Spire.PDF works with standard X.509 certificates, typically in PFX/P12 format for signing as they contain both public and private keys.

Q3: Can I add multiple signatures to a PDF?

Yes, you can add multiple signatures either by creating multiple signature fields or by incrementally signing the document.

Q4: How do I handle certificate expiration?

Using timestamps ensures signatures remain valid after certificate expiration. For long-term validation, consider using LTV (Long-Term Validation) enabled signatures.

Q5: Does Spire.PDF offer additional security options beyond digital signatures?

Yes, Spire.PDF allows you to password-protect your PDF documents and set specific document permissions. These features can be used alongside digital signatures to further enhance security.

Get a Free License

To fully experience the capabilities of Spire.PDF for .NET without any evaluation limitations, you can request a free 30-day trial license.

Published in Security

Securing PDFs with digital signatures is essential for ensuring the integrity and non-repudiation of the documents. With this in mind, the ability to verify the digital signatures is equally important. A valid signature means that the document hasn't been altered since it was signed and that it is indeed originated from the claimed source.

While dealing with the digital signatures, there are also times when you may want to get the certificates of the signatures to learn its issuer Information, subject information, serial number, and validity period, etc. In this article, you will learn how to verify or get the digital signatures in PDF in C# using Spire.PDF for .NET.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF 

Verify Digital Signatures in PDF Using C#

Spire.PDF for .NET provides the PdfSignature.VerifySignature() method to check the validity of the digital signatures in a PDF document directly. The following are the detailed steps.

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Get the form in the PDF file using PdfDocument.Form property, and then get a collection of form fields using PdfFormWidget.FieldsWidget property.
  • Iterate through all fields and get the signature fields.
  • Get PDF signatures using PdfSignatureFieldWidget.Signature property.
  • Check the validity of the PDF signatures using PdfSignature.VerifySignature() method.
  • C#
using Spire.Pdf;
using Spire.Pdf.Security;
using Spire.Pdf.Widget;

namespace GetSignatureCertificate
{
    class Program
    {
        static void Main(string[] args)
        {

            //Create a PdfDocument object
            PdfDocument pdf = new PdfDocument();

            //Load a PDF file
            pdf.LoadFromFile("PDFSignature.pdf");

            //Get a collection of form fields in the PDF file
            PdfFormWidget pdfFormWidget = (PdfFormWidget)pdf.Form;
            PdfFormFieldWidgetCollection pdfFormFieldWidgetCollection = pdfFormWidget.FieldsWidget;

            //Iterate through all fields
            for (int i = 0; i < pdfFormFieldWidgetCollection.Count; i++)
            {
                //Get the signature fields
                if (pdfFormFieldWidgetCollection[i] is PdfSignatureFieldWidget)
                {
                    PdfSignatureFieldWidget signatureFieldWidget = (PdfSignatureFieldWidget)pdfFormFieldWidgetCollection[i];

                    //Get the signatures
                    PdfSignature signature = signatureFieldWidget.Signature;

                    //Verify signatures
                    bool valid = signature.VerifySignature();
                    if (valid)
                    {
                        Console.WriteLine("Valid signatures");
                    }
                    else
                    {
                        Console.WriteLine("Invalid signatures");
                    }
                }
            }
        }
    }
}

C#: Verify or Get Digital Signatures in PDF

Detect Whether a Signed PDF Has Been Modified Using C#

To verify if a PDF document has been modified after signing, you can use the PdfSignature.VerifyDocModified() method. If the result shows that document has been tampered with, this means that the signature will become invalid and the integrity of the document will be compromised. The following are the detailed steps.

  • Create a PdfDocument object.
  • Load a PDF document using PdfDocument.LoadFromFile() method.
  • Get the form in the PDF document using PdfDocument.Form property, and then get a collection of form fields using PdfFormWidget.FieldsWidget property.
  • Iterate through all fields and get the signature fields.
  • Get PDF signatures using PdfSignatureFieldWidget.Signature property.
  • Verify if the document has been modified after signing using PdfSignature.VerifyDocModified() method.
  • C#
using Spire.Pdf;
using Spire.Pdf.Security;
using Spire.Pdf.Widget;

namespace GetSignatureCertificate
{
    class Program
    {
        static void Main(string[] args)
        {

            //Create a PdfDocument object
            PdfDocument pdf = new PdfDocument();

            //Load a PDF document
            pdf.LoadFromFile("PDFSignature.pdf");

            //Get a collection of form fields in the PDF file
            PdfFormWidget pdfFormWidget = (PdfFormWidget)pdf.Form;
            PdfFormFieldWidgetCollection pdfFormFieldWidgetCollection = pdfFormWidget.FieldsWidget;

            for (int i = 0; i < pdfFormFieldWidgetCollection.Count; i++)
            {
                //Get the signature fields
                if (pdfFormFieldWidgetCollection[i] is PdfSignatureFieldWidget)
                {
                    PdfSignatureFieldWidget signatureFieldWidget = (PdfSignatureFieldWidget)pdfFormFieldWidgetCollection[i];

                    //Get the signatures
                    PdfSignature signature = signatureFieldWidget.Signature;

                    //Check if the document has been modified after signing
                    bool modified = signature.VerifyDocModified();
                    if (modified)
                    {
                        Console.WriteLine("The document has been modified.");
                    }
                    else
                    {
                        Console.WriteLine("The document has not been modified.");
                    }
                }
            }
        }
    }
}

C#: Verify or Get Digital Signatures in PDF

Get the Certificates of Digital Signatures in PDF Using C#

The digital certificates used to sign PDF files typically contain various pieces of information that verifies the identity of the issuer. With Spire.PDF for .NET, you can get the certificates in a PDF file through the PdfSignatureFieldWidget.Signature.Certificate property. The following are the detailed steps.

  • Create a PdfDocument object.
  • Load a PDF document using PdfDocument.LoadFromFile() method.
  • Get the form in the PDF document using PdfDocument.Form property, and then get a collection of form fields using PdfFormWidget.FieldsWidget property.
  • Iterate through all fields and get the signature fields.
  • Get the certificate of the signature using PdfSignatureFieldWidget.Signature.Certificate property.
  • Set to display the certificate in text format using PdfCertificate.ToString() method.
  • Get the format of the certificate using PdfCertificate.GetFormat() method.
  • Output the obtained certificate information.
  • C#
using Spire.Pdf;
using Spire.Pdf.Widget;

namespace GetSignatureCertificate
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument pdf = new PdfDocument();

            //Load a PDF file
            pdf.LoadFromFile("PDFSignature.pdf");

            //Get a collection of form fields in the PDF file
            PdfFormWidget pdfFormWidget = (PdfFormWidget)pdf.Form;
            PdfFormFieldWidgetCollection pdfFormFieldWidgetCollection = pdfFormWidget.FieldsWidget;

            //Iterate through all fields
            for (int i = 0; i < pdfFormFieldWidgetCollection.Count; i++)
            {
                //Get the signature fields
                if (pdfFormFieldWidgetCollection[i] is PdfSignatureFieldWidget)
                {
                    PdfSignatureFieldWidget signatureFieldWidget = (PdfSignatureFieldWidget)pdfFormFieldWidgetCollection[i];

                    //Get the certificate of the signature
                    string certificateInfo = signatureFieldWidget.Signature.Certificate.ToString();

                    //Get the format of the certificate
                    string format = signatureFieldWidget.Signature.Certificate.GetFormat();

                    //Output the certificate information
                    Console.WriteLine(certificateInfo + "\n" + "[CertificateFormat]\n " + format);
                }
            }
            Console.ReadKey();
        }
    }
}

C#: Verify or Get Digital Signatures in PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Published in Security

Modifying passwords of PDF file is really a rational choice especially when the passwords are known by someone and your PDF file is no longer safe. Spire.PDF for .NET enables you to modify passwords of your encrypted PDF file in C#, VB.NET. You can modify your owner password as well as user password and set user restrictions when access the PDF file. Now please see the process of modifying encrypted PDF passwords as below picture:

Modify PDF Passwords

From above picture, you can easily find that the first step is to decrypt PDF file by owner password. So the original owner password is necessary. You can decrypt it by this method: Spire.Pdf.PdfDocument(string filename, string password)

Then, modify passwords by resetting owner password and user password. PDFSecurity class which is in the namespace Spire.PDFDocument.Security can help you not only to set owner password and user password, but also can set user permissions to restrict user access.

Below shows the whole code of modifying passwords of encrypted PDF file, please download Spire.PDF for .NET and install it on system before following the code:

[C#]
using Spire.Pdf;
using Spire.Pdf.Security;

namespace modify_PDF_passwords
{
    class Program
    {
        static void Main(string[] args)
        {

            //load a encrypted file and decrypt it
            String encryptedPdf = @"..\Encrypt.pdf";
            PdfDocument doc = new PdfDocument(encryptedPdf, "e-iceblue");

            //reset PDF passwords and set user password permission
            doc.Security.OwnerPassword = "Spire.PDF";
            doc.Security.UserPassword = "pdfcomponent";
            doc.Security.Permissions = PdfPermissionsFlags.Print | PdfPermissionsFlags.FillFields;

            //Save pdf file.
            doc.SaveToFile("Encryption.pdf");
            doc.Close();
            //Launching the Pdf file.
            System.Diagnostics.Process.Start("Encryption.pdf");      
            
        }    
    }
}
[VB.NET]
Imports Spire.Pdf
Imports Spire.Pdf.Security

Namespace modify_PDF_passwords
	Class Program
		Private Shared Sub Main(args As String())

			'load a encrypted file and decrypt it
			Dim encryptedPdf As [String] = "..\Encrypt.pdf"
			Dim doc As New PdfDocument(encryptedPdf, "e-iceblue")

			'reset PDF passwords and set user password permission
			doc.Security.OwnerPassword = "Spire.PDF"
			doc.Security.UserPassword = "pdfcomponent"
			doc.Security.Permissions = PdfPermissionsFlags.Print Or PdfPermissionsFlags.FillFields

			'Save pdf file.
			doc.SaveToFile("Encryption.pdf")
			doc.Close()
			'Launching the Pdf file.
			System.Diagnostics.Process.Start("Encryption.pdf")

		End Sub
	End Class
End Namespace

Spire.PDF for .NET is a .NET PDF component that enables you to generate, read, edit and manipulate PDF files in C#, VB.NET.

Published in Security

As PDF documents become increasingly popular in business, ensuring their authenticity has become a key concern. Signing PDFs with a certificate-based signature can protect the content and also let others know who signed or approved the document. In this article, you will learn how to digitally sign PDF with an invisible or a visible signature, and how to remove digital signatures from PDF by using Spire.PDF for .NET.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Add an Invisible Digital Signature to PDF

The following are the steps to add an invisible digital signature to PDF using Spire.PDF for .NET.

  • Create a PdfDocument object.
  • Load a sample PDF file using PdfDocument.LoadFromFile() method.
  • Load a pfx certificate file while initializing the PdfCertificate object.
  • Create a PdfSignature object based on the certificate.
  • Set the document permissions through the PdfSignature object.
  • Save the document to another PDF file using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Security;

namespace AddInvisibleSignature
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            //Load a sample PDF file
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");

            //Load the certificate 
            PdfCertificate cert = new PdfCertificate("C:\\Users\\Administrator\\Desktop\\MyCertificate.pfx", "e-iceblue");

            //Create a PdfSignature object 
            PdfSignature signature = new PdfSignature(doc, doc.Pages[doc.Pages.Count - 1], cert, "MySignature");
          
            //Set the document permission to forbid changes but allow form fill
            signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges | PdfCertificationFlags.AllowFormFill;

            //Save to another PDF file 
            doc.SaveToFile("InvisibleSignature.pdf");
            doc.Close();
        }
    }
}

C#/VB.NET: Add or Remove Digital Signatures in PDF

Add a Visible Digital Signature to PDF

The following are the steps to add a visible digital signature to PDF using Spire.PDF for .NET.

  • Create a PdfDocument object.
  • Load a sample PDF file using PdfDocument.LoadFromFile() method.
  • Load a pfx certificate file while initializing the PdfCertificate object.
  • Create a PdfSignature object and specify its position and size on the document.
  • Set the signature details including date, name, location, reason, handwritten signature image, and document permissions.
  • Save the document to another PDF file using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using System;
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Security;
using Spire.Pdf.Graphics;

namespace AddVisibleSignature
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            //Load a sample PDF file
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");

            //Load the certificate 
            PdfCertificate cert = new PdfCertificate("C:\\Users\\Administrator\\Desktop\\MyCertificate.pfx", "e-iceblue");

            //Create a PdfSignature object and specify its position and size 
            PdfSignature signature = new PdfSignature(doc, doc.Pages[doc.Pages.Count - 1], cert, "MySignature");
            RectangleF rectangleF = new RectangleF(doc.Pages[0].ActualSize.Width - 260 - 54 , 200, 260, 110);
            signature.Bounds = rectangleF;
            signature.Certificated = true;

            //Set the graphics mode to ImageAndSignDetail
            signature.GraphicsMode = GraphicMode.SignImageAndSignDetail;

            //Set the signature content 
            signature.NameLabel = "Signer:";
            signature.Name = "Gary";
            signature.ContactInfoLabel = "Phone:";
            signature.ContactInfo = "0123456";
            signature.DateLabel = "Date:";
            signature.Date = DateTime.Now;
            signature.LocationInfoLabel = "Location:";
            signature.LocationInfo = "USA";
            signature.ReasonLabel = "Reason:";
            signature.Reason = "I am the author";
            signature.DistinguishedNameLabel = "DN:";
            signature.DistinguishedName = signature.Certificate.IssuerName.Name;

            //Set the signature image source
            signature.SignImageSource = PdfImage.FromFile("C:\\Users\\Administrator\\Desktop\\handwrittingSignature.png");

            //Set the signature font 
            signature.SignDetailsFont = new PdfTrueTypeFont(new Font("Arial Unicode MS", 12f, FontStyle.Regular));

            //Set the document permission to forbid changes but allow form fill
            signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges | PdfCertificationFlags.AllowFormFill;

            //Save to file 
            doc.SaveToFile("VisiableSignature.pdf");
            doc.Close();
        }
    }
}

C#/VB.NET: Add or Remove Digital Signatures in PDF

Remove Digital Signatures from PDF

The following are the steps to remove digital signatures from PDF using Spire.PDF for .NET.

  • Create a PdfDocument object.
  • Get form widgets from the document through PdfDocument.Form property.
  • Loop through the widgets and determine if a specific widget is a PdfSignatureFieldWidget.
  • Remove the signature widget using PdfFieldCollection.RemoveAt() method.
  • Save the document to another PDF file using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Widget;

namespace RemoveSignature
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument("C:\\Users\\Administrator\\Desktop\\VisiableSignature.pdf");

            //Get form widgets from the document
            PdfFormWidget widgets = doc.Form as PdfFormWidget;

            //Loop through the widgets
            for (int i = 0; i < widgets.FieldsWidget.List.Count; i++)
            {
                //Get the specific widget
                PdfFieldWidget widget = widgets.FieldsWidget.List[i] as PdfFieldWidget;

                //Determine if the widget is a PdfSignatureFieldWidget
                if (widget is PdfSignatureFieldWidget)
                {
                    //Remove the widget
                    widgets.FieldsWidget.RemoveAt(i);
                }
            }

            //Save the document to another PDF file
            doc.SaveToFile("RemoveSignatures.pdf");
        }
    }
}

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Published in Security
Monday, 04 July 2011 08:19

Encrypt PDF Document in C#/VB.NET

Encrypting PDF is a way people commonly used to protect PDF. Whether for a company or for individual, using PDF encryption to place some certain restrictions is indispensable. In order to make the PDF document available to read but unable to modify by unauthorized users, two passwords are required for an encrypted PDF document: owner password and user password. This section will particularly introduce a simple solution to quickly encrypt PDF with C#, VB.NET via Spire.PDF for .NET.

Spire.PDF for .NET as a .NET PDF component, can encrypt your PDF by owner and user password. Owner password is provided to fully access to PDF file such as reset password and restrictions. While user password allows users to open the document as well as subject to the restrictions placed by owner.

In the encryption solution, an object of the PDFSecurity class which is included in the namespace Spire.PDFDocument.Security is used to set the owner and user password. Please feel free to download Spire.PDF for .NET and load your PDF file and then protect it.

Protect PDF by setting password and specify document restrictions.

Step 1: Set PDF key size by the enum."Spire.Pdf.Security.PdfEncryptionKeySize".Three kinds of key size are available here: Key128Bit, Key256Bit and Key40Bit, you can use any one among the three.

[C#]
doc.Security.KeySize = PdfEncryptionKeySize.Key256Bit;
[VB.NET]
doc.Security.KeySize = PdfEncryptionKeySize.Key256Bit 

Step 2: Encrypt PDF file by setting owner and user password. The password size you set should not be over the key size.

[C#]
doc.Security.OwnerPassword = "e-iceblue";
doc.Security.UserPassword = "pdfcomponent";
[VB.NET]
doc.Security.OwnerPassword = "e-iceblue"
doc.Security.UserPassword = "pdfcomponent" 

Step 3: Specify access restrictions of user password. There are nine permissions are available in the solution. You can see them as below picture.

Encrypt PDF Document

[C#]
doc.Security.Permissions = PdfPermissionsFlags.Print | PdfPermissionsFlags.CopyContent;
[VB.NET]
doc.Security.Permissions = PdfPermissionsFlags.Print Or PdfPermissionsFlags. CopyContent

After running your project, you will be requested a password when you open this encrypted PDF file. Please look at the effective screenshot below:

Encrypt PDF Document

Published in Security
Page 2 of 2