Tabla de contenido
Instalado a través de NuGet
PM> Install-Package Spire.PDF
enlaces relacionados
A medida que los documentos PDF se vuelven cada vez más populares en los negocios, garantizar su autenticidad se ha convertido en una preocupación clave. Firmar archivos PDF con una firma basada en certificado puede proteger el contenido y también permitir que otros sepan quién firmó o aprobó el documento. En este artículo, aprenderá cómo firmar digitalmente PDF con una firma invisible o visible y cómo eliminar firmas digitales de PDF usando Spire.PDF for .NET.
- Agregar una firma digital invisible a PDF
- Agregar una firma digital visible a PDF
- Eliminar firmas digitales de PDF
Instalar Spire.PDF for .NET
Para empezar, debe agregar los archivos DLL incluidos en el paquete Spire.PDF for .NET como referencias en su proyecto .NET. Los archivos DLL se pueden descargar desde este enlace o instalado a través de NuGet.
PM> Install-Package Spire.PDF
Agregar una firma digital invisible a PDF
Los siguientes son los pasos para agregar una firma digital invisible a un PDF usando Spire.PDF for .NET.
- Cree un objeto PdfDocument.
- Cargue un archivo PDF de muestra utilizando el método PdfDocument.LoadFromFile().
- Cargue un archivo de certificado pfx mientras inicializa el objeto PdfCertificate.
- Cree un objeto PdfSignature basado en el certificado.
- Establezca los permisos del documento a través del objeto PdfSignature.
- Guarde el documento en otro archivo PDF utilizando el método PdfDocument.SaveToFile().
- 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();
}
}
}

Agregar una firma digital visible a PDF
Los siguientes son los pasos para agregar una firma digital visible a un PDF usando Spire.PDF for .NET.
- Cree un objeto PdfDocument.
- Cargue un archivo PDF de muestra utilizando el método PdfDocument.LoadFromFile().
- Cargue un archivo de certificado pfx mientras inicializa el objeto PdfCertificate.
- Cree un objeto PdfSignature y especifique su posición y tamaño en el documento.
- Configure los detalles de la firma, incluida la fecha, el nombre, la ubicación, el motivo, la imagen de la firma manuscrita y los permisos del documento.
- Guarde el documento en otro archivo PDF utilizando el método PdfDocument.SaveToFile().
- 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();
}
}
}

Eliminar firmas digitales de PDF
Los siguientes son los pasos para eliminar firmas digitales de PDF usando Spire.PDF for .NET.
- Cree un objeto PdfDocument.
- Obtenga widgets de formulario del documento a través de la propiedad PdfDocument.Form.
- Recorra los widgets y determine si un widget específico es un PdfSignatureFieldWidget.
- Elimine el widget de firma utilizando el método PdfFieldCollection.RemoveAt().
- Guarde el documento en otro archivo PDF utilizando el método PdfDocument.SaveToFile().
- 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");
}
}
}
Solicite una licencia temporal
Si desea eliminar el mensaje de evaluación de los documentos generados o deshacerse de las limitaciones de la función, por favor solicitar una licencia de prueba de 30 días para ti.