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

The digital signature ensures that the signed document cannot be altered by anyone other than its author. Adding signatures is the most common way to assure the authenticity of the document content. A visual digital signature in a PDF document can show text or an image (e.g., handwritten signature). This article introduces how to digitally sign PDFs using Spire.PDF for .NET from the following three aspects.

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

Digitally Sign PDF with Text

The following are the steps to add a plain text signature to a PDF document.

  • Create a PdfDocument object, and load a sample PDF file using PdfDocument.LoadFromFile() method.
  • Load a .pfx certificate file while initializing the PdfCertificate object.
  • Create a PdfSignature object, specifying its position and size in the document.
  • Set the signature graphics mode to SignDetail, which will show signature details in plain text.
  • Set the signature details, including name, contact information, reason, date, etc. through the properties under the PdfSignature object.
  • Set the permissions of the certificated document to ForbidChanges and AllowFormFill.
  • Save the document to another file using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Security;
using System;
using System.Drawing;
using Spire.Pdf.Graphics;

namespace AddTextSignature
{
    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 - 340, 150, 290, 100);
            signature.Bounds = rectangleF;
            signature.Certificated = true;

            //Set the graphics mode to sign detail
            signature.GraphicsMode = GraphicMode.SignDetail;

            //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 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("TextSignature.pdf");
            doc.Close();
        }
    }
}

C#/VB.NET: Digitally Sign PDF with Text or/and Image

Digitally Sign PDF with an Image

The following are the steps to add an image signature to a PDF document.

  • Create a PdfDocument object, and load a sample PDF file using PdfDocument.LoadFromFile() method.
  • Load a .pfx certificate file while initializing the PdfCertificate object.
  • Create a PdfSignature object, specifying its position and size in the document.
  • Set the signature graphics mode to SignImageOnly, which will show signature image only.
  • Set the signature image through PdfSignature.SignImageSource property.
  • Set the permissions of the certificated document to ForbidChanges and AllowFormFill.
  • Save the document to another file using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Security;
using System.Drawing;

namespace AddImageSignature
{
    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 - 200, 150, 130, 130);
            signature.Bounds = rectangleF;
            signature.Certificated = true;

            //Set the graphics mode to image only
            signature.GraphicsMode = GraphicMode.SignImageOnly;

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

            //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("ImageSignature.pdf");
            doc.Close();
        }
    }
}

C#/VB.NET: Digitally Sign PDF with Text or/and Image

Digitally Sign PDF with Text and an Image

The following are the steps to digitally sign a PDF document with text and an image.

  • Create a PdfDocument object, and load a sample PDF file using PdfDocument.LoadFromFile() method.
  • Load a .pfx certificate file while initializing the PdfCertificate object.
  • Create a PdfSignature object, specifying its position and size in the document.
  • Set the signature graphics mode to SignImageAndSignDetail, which will show both signature image and details.
  • Set the signature image through PdfSignature.SignImageSource property, and set the signature details, including name, contact information, reason, date, etc. through other properties under the PdfSignature object.
  • Set the permissions of the certificated document to ForbidChanges and AllowFormFill.
  • Save the document to another file using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Security;
using System;
using System.Drawing;
using Spire.Pdf.Graphics;

namespace AddTextAndImageSignature
{
    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 - 320, 150, 260, 110);
            signature.Bounds = rectangleF;
            signature.Certificated = true;

            //Set the graphics mode to image and sign detail
            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\\handSignature.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("TextAndImageSignature.pdf");
            doc.Close();
        }
    }
}

C#/VB.NET: Digitally Sign PDF with Text or/and Image

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

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