DigitalSignature - Run Time
How to insert the signature by digital certificate at runtime.
The goal is to generate the PDF with the signature already in place without user intervention.
The PDF will have several pages, and each page will have a place for the digital signature.
The signature file will be "ANY NAME.PFX, "PASSWORD""
FastReport .NET
Version=2022.2.8.0
This discussion has been closed.
Comments
I found a solution:
try
{
string protocolo = "1234567890";
string certificado = "SEU_CERTIFICADO.PFX";
string senha = "999999";
WebReport webReport = new WebReport();
webReport.Report.Load(Server.MapPath("") + "\\SEU_RELATORIO.frx");
webReport.Report.SetParameterValue("ID_PROTOCOLO", protocolo);
webReport.Report.Prepare();
X509Certificate2 cert = new X509Certificate2(certificado, senha);
using (MemoryStream memoryStream = new MemoryStream())
{
PDFExport pdfExport = new PDFExport
{
AllowPrint = true,
Compressed = true,
EmbeddingFonts = true,
ShowProgress = true,
DigitalSignCertificate = cert,
DigitalSignCertificatePassword = senha,
IsDigitalSignEnable = true,
DigitalSignReason = "MOTIVO DO RELATÓRIO: " + protocolo,
DigitalSignLocation = "CIDADE / UF",
DigitalSignContactInfo = "contato@contato.com.br",
Creator = "NOME DE QUEM ESTÁ GERANDO O RELATÓRIO",
HasMultipleFiles = true,
PrintOptimized = true,
};
webReport.Report.Export(pdfExport, memoryStream);
byte[] pdfBytes = memoryStream.ToArray();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=SEU_RELATORIO" + protocolo + ".pdf");
Response.BinaryWrite(pdfBytes);
Response.End();
}
}
catch (Exception erro)
{
string error = erro.Message;
labMensagem.Text = error ;
}
Hello!
We have carried out further testing of this code on the latest version of FastReport.Core 2026.2.8 using a multi-page report and an X509Certificate2 runtime certificate — the signing process is performed correctly, and a valid digital signature dictionary (/ByteRange) is generated in the final PDF.
The IsDigitalSignEnable = true property and the PDFExport object are only available in the commercial PDF export package (there is no digital signature in FastReport.OpenSource.Export.PdfSimple).
After calling report.Export(pdfExport, ms), be sure to reset ms.Position = 0 before reading or releasing the stream.