SignDocument(Stream,Stream,Int32,PdfAConverter) Method (PdfPageDigitalSignatureHelper)
Adds digital signature to the PDF page and saves document as a PDF/A document.
Here is an example that shows how to sign a PDF/A document using the specified digital signature:
Imports System.Drawing
Imports System.Security.Cryptography.X509Certificates
Imports Vintasoft.Imaging.Pdf
Imports Vintasoft.Imaging.Pdf.Processing
Imports Vintasoft.Imaging.Pdf.Processing.PdfA
''' <summary>
''' Adds digital signature to a PDF page and converts PDF document to a PDF/A format.
''' </summary>
Public Class PdfPageDigitalSignatureHelper_PdfA
''' <summary>
''' Signs a PDF/A document using specified PFX file.
''' </summary>
''' <param name="inputFilename">The name of input PDF file.</param>
''' <param name="outputFilename">The name of output PDF file.</param>
''' <param name="pfxFileName">The name of PFX file.</param>
''' <param name="pfxFilePassword">The password for PDF file.</param>
''' <param name="pdfAConformance">PDF/A conformance.</param>
Public Shared Sub SignPdfDocument(inputFilename As String, outputFilename As String, pfxFileName As String, pfxFilePassword As String, pdfAConformance As PdfDocumentConformance)
Using certificate As New X509Certificate2(pfxFileName, pfxFileName)
' create a helper that alows to add digital signature to a PDF document
Dim digitalSignatureHelper As New PdfPageDigitalSignatureHelper(certificate, New RectangleF(10, 10, 250, 75))
' set the reason for signing
digitalSignatureHelper.SigningReason = "Test signing"
' set text for signature appearance
digitalSignatureHelper.SignatureText = String.Format("Digitally signed by" & vbLf & "{0}", digitalSignatureHelper.SignerName)
Dim pdfAConverter As PdfAConverter = Nothing
' if PDF document should be converted to a PDF/A format
If pdfAConformance <> PdfDocumentConformance.Undefined Then
' create PDF/A converter
pdfAConverter = DirectCast(PdfDocumentConverter.Create(pdfAConformance), PdfAConverter)
pdfAConverter.DefaultCmykIccProfileFilename = "DefaultCMYK.icc"
pdfAConverter.DefaultRgbIccProfileFilename = "DefaultRGB.icc"
End If
' add digital signature to the first PDF page, sign PDF document and save PDF document to the output file
digitalSignatureHelper.SignDocument(inputFilename, outputFilename, 0, pdfAConverter)
End Using
End Sub
End Class
using System.Drawing;
using System.Security.Cryptography.X509Certificates;
using Vintasoft.Imaging.Pdf;
using Vintasoft.Imaging.Pdf.Processing;
using Vintasoft.Imaging.Pdf.Processing.PdfA;
/// <summary>
/// Adds digital signature to a PDF page and converts PDF document to a PDF/A format.
/// </summary>
public class PdfPageDigitalSignatureHelper_PdfA
{
/// <summary>
/// Signs a PDF/A document using specified PFX file.
/// </summary>
/// <param name="inputFilename">The name of input PDF file.</param>
/// <param name="outputFilename">The name of output PDF file.</param>
/// <param name="pfxFileName">The name of PFX file.</param>
/// <param name="pfxFilePassword">The password for PDF file.</param>
/// <param name="pdfAConformance">PDF/A conformance.</param>
public static void SignPdfDocument(
string inputFilename,
string outputFilename,
string pfxFileName,
string pfxFilePassword,
PdfDocumentConformance pdfAConformance)
{
using (X509Certificate2 certificate = new X509Certificate2(pfxFileName, pfxFileName))
{
// create a helper that alows to add digital signature to a PDF document
PdfPageDigitalSignatureHelper digitalSignatureHelper = new PdfPageDigitalSignatureHelper(certificate, new RectangleF(10, 10, 250, 75));
// set the reason for signing
digitalSignatureHelper.SigningReason = "Test signing";
// set text for signature appearance
digitalSignatureHelper.SignatureText = string.Format("Digitally signed by\n{0}", digitalSignatureHelper.SignerName);
PdfAConverter pdfAConverter = null;
// if PDF document should be converted to a PDF/A format
if (pdfAConformance != PdfDocumentConformance.Undefined)
{
// create PDF/A converter
pdfAConverter = (PdfAConverter)PdfDocumentConverter.Create(pdfAConformance);
pdfAConverter.DefaultCmykIccProfileFilename = "DefaultCMYK.icc";
pdfAConverter.DefaultRgbIccProfileFilename = "DefaultRGB.icc";
}
// add digital signature to the first PDF page, sign PDF document and save PDF document to the output file
digitalSignatureHelper.SignDocument(inputFilename, outputFilename, 0, pdfAConverter);
}
}
}
Target Platforms: .NET 10; .NET 9; .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5