Verify Signature in PDF File

Verify Whether the PDF File is Signed Using a Signature

To verify whether a PDF file is signed using a particular signature, use the VerifySigned method of the PdfFileSignature class. This method requires the signature name and returns true if the PDF is signed using that signature name. It is also possible to verify that a PDF is signed, without verifying which signature it is signed with.

Verifying that a PDF is Signed with a Given Signature

The following code snippet shows you how to verify whether PDF is signed using a given signature.

// For complete examples and data files, check for https://github.com/aspose-pdf/Aspose.PDF-for-.NET

private static void IsPdfSigned()
{
    // The path to the documents directory
    string dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures();
    
    using (var pdFileSignature = new Aspose.Pdf.Facades.PdfFileSignature())
    {      
        pdFileSignature.BindPdf(dataDir + "signed_rsa.pdf");
        if (pdFileSignature.ContainsSignature())
        {
            Console.WriteLine("Document Signed");
        }

        pdFileSignature.Close();
    }
}

Verifying that a PDF is Signed

To determine if a file is singed, without providing the signature name, use the following code.

// For complete examples and data files, check for https://github.com/aspose-pdf/Aspose.PDF-for-.NET

private static void IsPdfSignedWithGivenSignature()
{
    // The path to the documents directory
    string dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures();
    
    using (var pdFileSignature = new Aspose.Pdf.Facades.PdfFileSignature())
    {
        pdFileSignature.BindPdf(dataDir + "signed_rsa.pdf");
        if (pdFileSignature.VerifySignature("Signature1"))
        {
            Console.WriteLine("PDF Signed");
        }
    }
}

Verify whether the Signature is Valid

VerifySignature method of PdfFileSignature class allows you to validate a particular signature. This method requires signature name as input and returns true if the signature is valid. The following code snippet shows you how to validate a signature.

// For complete examples and data files, check for https://github.com/aspose-pdf/Aspose.PDF-for-.NET

private static void IsPdfSignatureValid()
{
    // The path to the documents directory
    string dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures();
    
    using (var pdFileSignature = new Aspose.Pdf.Facades.PdfFileSignature())
    {
        pdFileSignature.BindPdf(dataDir + "signed_rsa.pdf");
        if (pdFileSignature.VerifySignature("Signature1"))
        {
            Console.WriteLine("Signature Verified");
        }
    }
}