As a comprehensive PDF component, Spire.PDF supports to sign a PDF digitally, embed certificate in PDF as well as delete signatures in existing PDF documents. In this article, you'll learn how to remove all digital signatures from a PDF with C#, VB.NET.
Test File:
Code Snippet:
Step 1: Create a new PdfDocument object and load the test file.
PdfDocument pdf = new PdfDocument("test.pdf");
Step 2: Get loaded form from PDF.
PdfFormWidget widgets = pdf.Form as PdfFormWidget;
Step 3: Get the list of filed collection, and judge if each filed is a signature filed. If yes, remove the signature field using PdfFieldCollection.RemoveAt(int index) method.
for (int i = 0; i < widgets.FieldsWidget.List.Count; i++) { PdfFieldWidget widget = widgets.FieldsWidget.List[i] as PdfFieldWidget; if (widget is PdfSignatureFieldWidget) { widgets.FieldsWidget.RemoveAt(i); } }
Step 4: Save and launch the result file.
pdf.SaveToFile("result.pdf"); System.Diagnostics.Process.Start("result.pdf");
Result:
Full Code:
[C#]
using Spire.Pdf; using Spire.Pdf.Widget; namespace RemoveDigitalSignature { class Program { static void Main(string[] args) { PdfDocument pdf = new PdfDocument("test.pdf"); PdfFormWidget widgets = pdf.Form as PdfFormWidget; for (int i = 0; i < widgets.FieldsWidget.List.Count; i++) { PdfFieldWidget widget = widgets.FieldsWidget.List[i] as PdfFieldWidget; if (widget is PdfSignatureFieldWidget) { widgets.FieldsWidget.RemoveAt(i); } } pdf.SaveToFile("result.pdf"); System.Diagnostics.Process.Start("result.pdf"); } } }
[VB.NET]
Imports Spire.Pdf Imports Spire.Pdf.Widget Namespace RemoveDigitalSignature Class Program Private Shared Sub Main(args As String()) Dim pdf As New PdfDocument("test.pdf") Dim widgets As PdfFormWidget = TryCast(pdf.Form, PdfFormWidget) For i As Integer = 0 To widgets.FieldsWidget.List.Count - 1 Dim widget As PdfFieldWidget = TryCast(widgets.FieldsWidget.List(i), PdfFieldWidget) If TypeOf widget Is PdfSignatureFieldWidget Then widgets.FieldsWidget.RemoveAt(i) End If Next pdf.SaveToFile("result.pdf") System.Diagnostics.Process.Start("result.pdf") End Sub End Class End Namespace