Table of Contents

Class PdfSigner

Namespace
MiPdf.Signatures
Assembly
MiPdf.dll

Signs PDF documents. Signatures are always added as incremental update, so existing signatures stay valid.

public static class PdfSigner
Inheritance
PdfSigner
Inherited Members

Examples

var signer = CertificateSigner.FromPfx("me.pfx", "password");
byte[] signed = PdfSigner.Sign(File.ReadAllBytes("in.pdf"), signer, new PdfSignatureOptions
{
    Reason = "Approval", Location = "Vienna",
    Rectangle = new PdfRectangle(350, 50, 550, 110),
    TimestampClient = new HttpTimestampClient("http://timestamp.digicert.com"),
});

Methods

AddDocumentTimestamp(byte[], ITimestampClient, string?, string?, HashAlgorithmName?)

Adds a document time-stamp (PAdES, /SubFilter ETSI.RFC3161) as incremental update, e.g. to extend the validity of existing signatures (B-LTA).

public static byte[] AddDocumentTimestamp(byte[] pdf, ITimestampClient tsa, string? fieldName = null, string? password = null, HashAlgorithmName? digestAlgorithm = null)

Parameters

pdf byte[]
tsa ITimestampClient
fieldName string
password string
digestAlgorithm HashAlgorithmName?

Returns

byte[]

CompleteExternalSignature(PreparedSignature, byte[])

Step 2 of a deferred signature: embeds the CMS container returned by the signing service.

public static byte[] CompleteExternalSignature(PreparedSignature prepared, byte[] cmsContainer)

Parameters

prepared PreparedSignature
cmsContainer byte[]

Returns

byte[]

CompleteExternalSignature(byte[], string, byte[], string?)

Embeds a CMS container into a prepared PDF given only its bytes and the field name.

public static byte[] CompleteExternalSignature(byte[] preparedPdf, string fieldName, byte[] cmsContainer, string? password = null)

Parameters

preparedPdf byte[]
fieldName string
cmsContainer byte[]
password string

Returns

byte[]

PrepareExternalSignature(byte[], PdfSignatureOptions, X509Certificate2?, int)

Step 1 of a deferred signature: prepares the document and returns the data to sign. Send GetDocumentDigest() (or the signed data) to the signing service, which returns a detached CMS/PKCS#7 container, then call CompleteExternalSignature(PreparedSignature, byte[]).

public static PreparedSignature PrepareExternalSignature(byte[] pdf, PdfSignatureOptions options, X509Certificate2? certificate = null, int containerSize = 16384)

Parameters

pdf byte[]

The document.

options PdfSignatureOptions

Signature options (time-stamps must be added by the signing service).

certificate X509Certificate2

Optional signer certificate, used for the visible appearance and /Name.

containerSize int

Space reserved for the signature container in bytes.

Returns

PreparedSignature

Sign(PdfDocument, IExternalSigner, PdfSignatureOptions?)

Signs a PdfDocument (it is saved first) and returns the signed bytes.

public static byte[] Sign(PdfDocument document, IExternalSigner signer, PdfSignatureOptions? options = null)

Parameters

document PdfDocument
signer IExternalSigner
options PdfSignatureOptions

Returns

byte[]

Sign(byte[], IExternalSigner, PdfSignatureOptions?)

Signs a document with any IExternalSigner (local key, HSM, smart card, remote service).

public static byte[] Sign(byte[] pdf, IExternalSigner signer, PdfSignatureOptions? options = null)

Parameters

pdf byte[]
signer IExternalSigner
options PdfSignatureOptions

Returns

byte[]

Sign(byte[], X509Certificate2, PdfSignatureOptions?, IEnumerable<X509Certificate2>?)

Signs with a certificate that has a private key (e.g. loaded from a .pfx file).

public static byte[] Sign(byte[] pdf, X509Certificate2 certificate, PdfSignatureOptions? options = null, IEnumerable<X509Certificate2>? chain = null)

Parameters

pdf byte[]
certificate X509Certificate2
options PdfSignatureOptions
chain IEnumerable<X509Certificate2>

Returns

byte[]

SignDetailed(byte[], IExternalSigner, PdfSignatureOptions?)

Signs like Sign(byte[], IExternalSigner, PdfSignatureOptions?) and reports the reached level and the CMS.

public static PdfSigningResult SignDetailed(byte[] pdf, IExternalSigner signer, PdfSignatureOptions? options = null)

Parameters

pdf byte[]
signer IExternalSigner
options PdfSignatureOptions

Returns

PdfSigningResult

SignFile(string, string, IExternalSigner, PdfSignatureOptions?)

Signs a file and writes the signed document to outputPath, which may be the input file. The document is read on demand and the signed byte ranges are hashed from the file, so files of any size (also larger than 2 GB) are signed with little memory. Pdf is empty; the signed document is the output file. Damaged documents are not repaired: save them with a full save first.

public static PdfSigningResult SignFile(string inputPath, string outputPath, IExternalSigner signer, PdfSignatureOptions? options = null)

Parameters

inputPath string
outputPath string
signer IExternalSigner
options PdfSignatureOptions

Returns

PdfSigningResult

Exceptions

PdfException

The document is damaged, or the signature does not fit into the reserved space.

SignFile(string, string, X509Certificate2, PdfSignatureOptions?, IEnumerable<X509Certificate2>?)

Signs a file with a certificate that has a private key; see SignFile(string, string, IExternalSigner, PdfSignatureOptions?).

public static PdfSigningResult SignFile(string inputPath, string outputPath, X509Certificate2 certificate, PdfSignatureOptions? options = null, IEnumerable<X509Certificate2>? chain = null)

Parameters

inputPath string
outputPath string
certificate X509Certificate2
options PdfSignatureOptions
chain IEnumerable<X509Certificate2>

Returns

PdfSigningResult