I’m attempting to concatenate several PDF documents that are saved into MemoryStream’s and placed into an array. However, Concatenate only seems to get through about half of the bytes in the MemoryStream’s, which basically corrupts the output memory stream to be used to create a byte array to output to a browser using BinaryWrite. I cannot use FileStreams since I do not want these files written anywhere ever. Here’s the code I have now. I have attempted to take examples from this forum and use them for my purposes.
private void GeneratePDF(string TranIDs)
{
byte[] bytPDF;
System.IO.FileInfo f;
int intTotalBytes = 0;
f = new FileInfo(Server.MapPath("./PDFForms/f8283.pdf"));
if (f.Exists)
{
try
{
MemoryStream[] cs;
MemoryStream os;
PdfFileEditor pdfEditor = new PdfFileEditor();
DataTable dtb = GetData(TranIDs);
ArrayList arrStreams = new ArrayList();
int i = 0;
//Loop through each transaction to create a single PDF document
foreach (DataRow row in dtb.Rows)
{
MemoryStream s = new MemoryStream();
Form docPDF = new Form(f.FullName, s);
docPDF.FillField(“f1-1”, (string)row[“sname”]); //Donor Name
docPDF.FlattenAllFields();
docPDF.Save();
s.Position = 0;
arrStreams.Add(s);
intTotalBytes += (int)s.Length;
i++;
}
cs = new MemoryStream[dtb.Rows.Count];
arrStreams.CopyTo(cs, 0);
os = new MemoryStream();
pdfEditor.Concatenate(cs, os);
os.Position = 0;
bytPDF = new byte[intTotalBytes];
os.Read(bytPDF, 0, intTotalBytes);
os.Close();
//Write file to browser
Response.Clear();
Response.AddHeader(“Content-disposition”, “attachment; filename=” + f.Name);
Response.ContentType = “Application/pdf”;
Response.BinaryWrite(bytPDF);
Response.End();
}
catch (Exception ex)
{
lblErr.Text= ex.Message + “
” + ex.StackTrace + “
”;}
}
}