We are trying to send an SSRS report as encrypted pdf document via email. Below code is working fine in dev environment - But while moving the same code to production environment its not working and throwing error as "Cannot access a closed stream".
Binary binaryData;
container binData;
System.Int32 netIndex=1,netCount=1;
int bytecount = 0 ;
System.IO.MemoryStream mstream = new System.IO.MemoryStream();
while(bytecount < reportBytes.Length)
{
mstream.WriteByte(reportBytes.GetValue(bytecount));
bytecount++;
}
mstream.Position = 0 ;
pdfreader = new iText.Kernel.Pdf.PdfReader(mstream);
writeproperties.SetStandardEncryption(System.Text.Encoding::UTF8.GetBytes(pwd),System.Text.Encoding::UTF8.GetBytes(pwd),4+2048,2|8);
pdfwriter = new iText.Kernel.Pdf.PdfWriter(mstream,writeproperties);
pdfdocumentation = new iText.Kernel.Pdf.PdfDocument(pdfreader,pdfwriter);
pdfdocumentation.Close();
pdfEncryption = mstream.GetBuffer();
int bytecountEncryp = 0 ;
System.IO.MemoryStream encryptedstream = new System.IO.MemoryStream();
while(bytecountEncryp < pdfEncryption.Length)
{
encryptedstream.WriteByte(pdfEncryption.GetValue(bytecountEncryp));
bytecountEncryp++;
}
encryptedstream.Position = 0;
binaryData = Binary::constructFromMemoryStream(encryptedstream);
if(binaryData)
{
binData = binaryData.getContainer();
}