Detect whether a signed PDF was modified or not using C#

After a PDF document is digitally signed with signature, the PDF has been locked to prevent changes or allow the detection of changes. In this article, we'll introduce how to detect if a signed PDF was modified using Spire.PDF.

In order to test this function, we created a PDF document and signed the PDF with digital signature, then changed the word 'PDF' in the sample document into 'Pdf' and saved it as another file. Here is what the modified PDF document looking like:

How to detect whether a signed PDF was modified or not using C#

Code Snippet:

Step 1: Create a Window Forms Application and design form1 as following.

How to detect whether a signed PDF was modified or not using C#

Step 2: Double click 'Load' button to write following code, which will allow us to find a PDF file from folder and return the file path in textBox1.Text.

private void btnLoad_Click(object sender, EventArgs e)
{
    OpenFileDialog fileName = new OpenFileDialog();
    fileName.InitialDirectory = Application.StartupPath;
    fileName.Filter = "All files|*.pdf";
    if (fileName.ShowDialog() == DialogResult.OK)
    {
        string Path = fileName.FileName.ToString();
        textBox1.Text = Path;
    }
}

Step 3: Enter following code to the button of 'Check'. In this part, we get all signatures in the PDF document, and then call PdfSignature.VerifyDocModified() method to detect if the document was altered after signed. If it was modified return true, otherwise false.

private void btnCheck_Click(object sender, EventArgs e)
{
    //get signatures from PDF
    List signatures = new List();
    using (PdfDocument pdf = new PdfDocument(textBox1.Text))
    {
        PdfFormWidget form = pdf.Form as PdfFormWidget;
        for (int i = 0; i < form.FieldsWidget.Count; i++)
        {
            PdfSignatureFieldWidget field = form.FieldsWidget[i] as PdfSignatureFieldWidget;
            if (field != null && field.Signature != null)
            {
                PdfSignature signature = field.Signature;
                signatures.Add(signature);
            }
        }
        PdfSignature signatureOne = signatures[0];
        //detect if the PDF was modified
        bool modified = signatureOne.VerifyDocModified();
        if (modified == true)
        {
            MessageBox.Show("The document was modified");
        }
    }
}

Run the program and load the modified document, you'll get following output after clicking 'Check' button.

How to detect whether a signed PDF was modified or not using C#

Full Code:

private void btnLoad_Click(object sender, EventArgs e)
{
    OpenFileDialog fileName = new OpenFileDialog();
    fileName.InitialDirectory = Application.StartupPath;
    fileName.Filter = "All files|*.pdf";
    if (fileName.ShowDialog() == DialogResult.OK)
    {
        string Path = fileName.FileName.ToString();
        textBox1.Text = Path;
    }
}
private void btnCheck_Click(object sender, EventArgs e)
{
    //get signatures from PDF
    List signatures = new List();
    using (PdfDocument pdf = new PdfDocument(textBox1.Text))
    {
        PdfFormWidget form = pdf.Form as PdfFormWidget;
        for (int i = 0; i < form.FieldsWidget.Count; i++)
        {
            PdfSignatureFieldWidget field = form.FieldsWidget[i] as PdfSignatureFieldWidget;
            if (field != null && field.Signature != null)
            {
                PdfSignature signature = field.Signature;
                signatures.Add(signature);
            }
        }
        PdfSignature signatureOne = signatures[0];
        //detect if the PDF was modified
        bool modified = signatureOne.VerifyDocModified();
        if (modified == true)
        {
            MessageBox.Show("The document was modified");
        }
    }
}