the below method i got the exception i don't know what's wrong can anyone tell me
the exception is occurred in below line
// Create PdfFormatProvider and load the existing document
PdfFormatProvider pdfFormatProvider = new PdfFormatProvider();
RadFixedDocument document = pdfFormatProvider.Import(inputStream);
static byte[] GenerateSignedPdf(Cert objCert, byte[] inDocByte)
{
// Dimensions and positions for the signature field
int signatureFieldWidth = 200; // Width of the signature field
int signatureFieldHeight = 50;
int signaturePositionLeft = 10;
int signaturePositionTop = 10;
// Load the certificate
X509Certificate2 certificate = new X509Certificate2(objCert.CertArray, objCert.Password);
// Create a SignatureField and assign the digital signature to it
SignatureField pdfSignature = new SignatureField("SignatureField");
pdfSignature.Signature = new Telerik.Windows.Documents.Fixed.Model.DigitalSignatures.Signature(certificate);
// Set signature details
string strNotAvailable = HttpContext.GetGlobalResourceObject("GlobalResources", "lblNotAvlbl").ToString();
pdfSignature.Signature.Properties.Reason = strNotAvailable;
pdfSignature.Signature.Properties.ContactInfo = strNotAvailable;
pdfSignature.Signature.Properties.Location = strNotAvailable;
// Load the existing PDF from the byte array using PdfFormatProvider
using (MemoryStream inputStream = new MemoryStream(inDocByte))
{
// Create PdfFormatProvider and load the existing document
PdfFormatProvider pdfFormatProvider = new PdfFormatProvider();
RadFixedDocument document = pdfFormatProvider.Import(inputStream);
// Create a new form to place the signature field
Form pdfForm = new Form();
pdfForm.FormSource = new FormSource();
pdfForm.FormSource.Size = new Telerik.Documents.Primitives.Size(signatureFieldWidth, signatureFieldHeight);
FixedContentEditor editor = new FixedContentEditor(pdfForm.FormSource);
// Path to the image (ensure this is correct)
var signatureImage = (System.Drawing.Image)HttpContext.Current.Session["imagesyncfusion"];
// Drawing the image at the starting position (left side of the signature field)
int imageWidth = signatureFieldWidth / 2; // Half the width for the image
int imageHeight = signatureFieldHeight; // Use full height of the signature field
// Create a memory stream to hold the image
using (MemoryStream ms = new MemoryStream())
{
// Save the image to the memory stream
signatureImage.Save(ms, signatureImage.RawFormat);
// Reset the stream position to the beginning
ms.Position = 0;
// Pass the memory stream to the DrawImage method
editor.DrawImage(ms, new Telerik.Documents.Primitives.Size(imageWidth, imageHeight));
}
string textToDraw = $"Digitally signed by {certificate.FriendlyName} \nDate: {DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss zzz")} \nReason: {pdfSignature.Signature.Properties.Reason} \nLocation: {pdfSignature.Signature.Properties.Location}";
// Set the width of the text area (remaining space after the image)
int textWidth = signatureFieldWidth - imageWidth;
double dsd = CalculateFontSize(textWidth - 23, signatureFieldHeight, textToDraw);
// Now, draw the text next to the image (on the right side), within the remaining space
Telerik.Windows.Documents.Fixed.Model.Editing.Block textBlock = new Telerik.Windows.Documents.Fixed.Model.Editing.Block();
int textPositionLeft = signaturePositionLeft + imageWidth; // Adding a 5px gap after the image
textBlock.TextProperties.FontSize = dsd;
textBlock.InsertText($"Digitally signed by {certificate.FriendlyName}");
textBlock.InsertText($" Date: {DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss zzz")}");
textBlock.InsertText($" Reason: {pdfSignature.Signature.Properties.Reason}");
textBlock.InsertText($" Location: {pdfSignature.Signature.Properties.Location}");
Rect boundingRect = new Rect(textPositionLeft - 8, signaturePositionTop - 12, textWidth, signatureFieldHeight);
textBlock.Draw(editor, boundingRect);
// Create the SignatureWidget and position it on the PDF page
SignatureWidget signatureWidget = pdfSignature.Widgets.AddWidget();
signatureWidget.Content.NormalContentSource = pdfForm.FormSource;
signatureWidget.Rect = new Rect(signaturePositionLeft, signaturePositionTop, signatureFieldWidth, signatureFieldHeight);
signatureWidget.RecalculateContent();
// Add content from the form to the page at the specified position
RadFixedPage pdfPage = document.Pages[0]; // Assuming you're adding it to the first page
FixedContentEditor pageEditor = new FixedContentEditor(pdfPage);
pageEditor.Position.Translate(signaturePositionLeft, signaturePositionTop);
pageEditor.DrawForm(pdfForm.FormSource);
// Add the signature field to the document's AcroForm
document.AcroForm.FormFields.Add(pdfSignature);
// Use MemoryStream to capture the PDF output and return as a byte array
using (MemoryStream memoryStream = new MemoryStream())
{
// Export the document to the memory stream as a PDF
pdfFormatProvider.Export(document, memoryStream);
return memoryStream.ToArray(); // Return the byte array of the signed PDF
}
}
}
the exception is occurred in below line
// Create PdfFormatProvider and load the existing document
PdfFormatProvider pdfFormatProvider = new PdfFormatProvider();
RadFixedDocument document = pdfFormatProvider.Import(inputStream);
static byte[] GenerateSignedPdf(Cert objCert, byte[] inDocByte)
{
// Dimensions and positions for the signature field
int signatureFieldWidth = 200; // Width of the signature field
int signatureFieldHeight = 50;
int signaturePositionLeft = 10;
int signaturePositionTop = 10;
// Load the certificate
X509Certificate2 certificate = new X509Certificate2(objCert.CertArray, objCert.Password);
// Create a SignatureField and assign the digital signature to it
SignatureField pdfSignature = new SignatureField("SignatureField");
pdfSignature.Signature = new Telerik.Windows.Documents.Fixed.Model.DigitalSignatures.Signature(certificate);
// Set signature details
string strNotAvailable = HttpContext.GetGlobalResourceObject("GlobalResources", "lblNotAvlbl").ToString();
pdfSignature.Signature.Properties.Reason = strNotAvailable;
pdfSignature.Signature.Properties.ContactInfo = strNotAvailable;
pdfSignature.Signature.Properties.Location = strNotAvailable;
// Load the existing PDF from the byte array using PdfFormatProvider
using (MemoryStream inputStream = new MemoryStream(inDocByte))
{
// Create PdfFormatProvider and load the existing document
PdfFormatProvider pdfFormatProvider = new PdfFormatProvider();
RadFixedDocument document = pdfFormatProvider.Import(inputStream);
// Create a new form to place the signature field
Form pdfForm = new Form();
pdfForm.FormSource = new FormSource();
pdfForm.FormSource.Size = new Telerik.Documents.Primitives.Size(signatureFieldWidth, signatureFieldHeight);
FixedContentEditor editor = new FixedContentEditor(pdfForm.FormSource);
// Path to the image (ensure this is correct)
var signatureImage = (System.Drawing.Image)HttpContext.Current.Session["imagesyncfusion"];
// Drawing the image at the starting position (left side of the signature field)
int imageWidth = signatureFieldWidth / 2; // Half the width for the image
int imageHeight = signatureFieldHeight; // Use full height of the signature field
// Create a memory stream to hold the image
using (MemoryStream ms = new MemoryStream())
{
// Save the image to the memory stream
signatureImage.Save(ms, signatureImage.RawFormat);
// Reset the stream position to the beginning
ms.Position = 0;
// Pass the memory stream to the DrawImage method
editor.DrawImage(ms, new Telerik.Documents.Primitives.Size(imageWidth, imageHeight));
}
string textToDraw = $"Digitally signed by {certificate.FriendlyName} \nDate: {DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss zzz")} \nReason: {pdfSignature.Signature.Properties.Reason} \nLocation: {pdfSignature.Signature.Properties.Location}";
// Set the width of the text area (remaining space after the image)
int textWidth = signatureFieldWidth - imageWidth;
double dsd = CalculateFontSize(textWidth - 23, signatureFieldHeight, textToDraw);
// Now, draw the text next to the image (on the right side), within the remaining space
Telerik.Windows.Documents.Fixed.Model.Editing.Block textBlock = new Telerik.Windows.Documents.Fixed.Model.Editing.Block();
int textPositionLeft = signaturePositionLeft + imageWidth; // Adding a 5px gap after the image
textBlock.TextProperties.FontSize = dsd;
textBlock.InsertText($"Digitally signed by {certificate.FriendlyName}");
textBlock.InsertText($" Date: {DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss zzz")}");
textBlock.InsertText($" Reason: {pdfSignature.Signature.Properties.Reason}");
textBlock.InsertText($" Location: {pdfSignature.Signature.Properties.Location}");
Rect boundingRect = new Rect(textPositionLeft - 8, signaturePositionTop - 12, textWidth, signatureFieldHeight);
textBlock.Draw(editor, boundingRect);
// Create the SignatureWidget and position it on the PDF page
SignatureWidget signatureWidget = pdfSignature.Widgets.AddWidget();
signatureWidget.Content.NormalContentSource = pdfForm.FormSource;
signatureWidget.Rect = new Rect(signaturePositionLeft, signaturePositionTop, signatureFieldWidth, signatureFieldHeight);
signatureWidget.RecalculateContent();
// Add content from the form to the page at the specified position
RadFixedPage pdfPage = document.Pages[0]; // Assuming you're adding it to the first page
FixedContentEditor pageEditor = new FixedContentEditor(pdfPage);
pageEditor.Position.Translate(signaturePositionLeft, signaturePositionTop);
pageEditor.DrawForm(pdfForm.FormSource);
// Add the signature field to the document's AcroForm
document.AcroForm.FormFields.Add(pdfSignature);
// Use MemoryStream to capture the PDF output and return as a byte array
using (MemoryStream memoryStream = new MemoryStream())
{
// Export the document to the memory stream as a PDF
pdfFormatProvider.Export(document, memoryStream);
return memoryStream.ToArray(); // Return the byte array of the signed PDF
}
}
}