In .NET development when working with PDF documents, you may encounter a common issue: receiving a PDF with a digital signature and wanting to modify its content or adjust the layout, only to find it protected by the signature, which triggers errors during any operation.
The solution is straightforward: Digital signatures in PDFs are embedded as specific form field controls, and with C# and the free Free Spire.PDF library, you can remove all signatures with just a few lines of code.
Core Mechanism
A digital signature in a PDF is a special type of form field, represented by the class PdfSignatureFieldWidget. The main logic is to locate and remove this signature field, therreby removing the PDF's signature protection.
- Load the PDF document with a digital signature
- Retrieve all form fields in the document
- Traverse the fields in reverse order to identify and delete signature-type fields
- Save the modified PDF file
- Release the document resources to avoid memory leaks
Complete Code
using Spire.Pdf;
using Spire.Pdf.Widget;
namespace RemoveSignature
{
class Program
{
static void Main(string[] args)
{
// Load the PDF file to be processed
PdfDocument pdf = new PdfDocument("C:\\Users\\Administrator\\Desktop\\Signature.pdf");
// Get the collection of form fields in the document
PdfFormWidget widgets = pdf.Form as PdfFormWidget;
// Traverse in reverse order (to prevent index misalignment)
for (int i = widgets.FieldsWidget.List.Count - 1; i >= 0; i--)
{
PdfFieldWidget widget = widgets.FieldsWidget.List[i] as PdfFieldWidget;
// Check if the current field is a digital signature field
if (widget is PdfSignatureFieldWidget)
{
// Remove the signature
widgets.FieldsWidget.RemoveAt(i);
Console.WriteLine($"Removed signature field at index {i}");
}
}
// Save the result
pdf.SaveToFile("RemoveSignatures.pdf");
Console.WriteLine("Processing complete!");
// Remember to release resources
pdf.Close();
}
}
}
Key Points Explained
1. Why Traverse in Reverse?
After calling List.RemoveAt(i), the indices of subsequent elements in the list will automatically shift forward.
✅ Traversing in forward order may skip the next element after deletion, leading to incomplete removal;
✅ Traversing in reverse order ensures no index misalignment, which is the standard way to delete elements from a collection.
2. Support for Removing All Signature Types
This method can remove:
- Visible signature images/stamps on the page
- Hidden electronic verification signatures in the background
- Single or multiple digital signatures
3. Free Spire.PDF Free Edition Limitations
The free edition has a PDF page limit (≤10 pages), which is sufficient for:
- Personal testing
- Small file processing
- Internal tool development for companies
Environment Setup (One Step)
Open the NuGet Package Manager Console in Visual Studio and run the following command to install the dependency library:
Install-Package FreeSpire.PDF
Or directly search for FreeSpire.PDF in the NuGet search bar and install it.
Important Notes
- Always back up the original file The code generates a new file without overwriting the original, but it is recommended to manually back up important documents before making changes.
- File path format
For Windows paths, it is recommended to use the
@symbol to avoid errors caused by escape characetrs (e.g.,@"C:\Test.pdf"). - Legal Effect Reminder Digital signatures usually have legal effect. Ensure you have the right to modify/delete the file before proceeding, and do not perform any illegal operations.