ITextSharp Adding Multiple PDFStampers to memoryStream

576 views Asked by At

I have a asp.net application that uses ITextSharp 5.4.5.0 to generate certificates. I have the code that generates one certificate working fine, but when I try to bundle these one page certificates into a single multi-page PDF and output the memorystream to the browser I get an error from Adobe Acrobat.

I have a feeling that I'm not adding eachof my byte arroys correctly, but I'm at a loss.

Here is the calling code:

        protected void btnMakeCerts_Click(object sender, EventArgs e)
    {
        ICAgileEntities icae = new ICAgileEntities();

        int classid =int.Parse(Request.QueryString["id"]);

        var currentClass = (from c in icae.Classes
                            where c.id == classid
                            select c).FirstOrDefault();

        Response.Clear();
        Response.BufferOutput = true;
        Response.ClearHeaders();
        Response.ContentType = "application/pdf";
        Response.AddHeader("Content-Disposition", "attachment; filename=AllCertificates-" + currentClass.Title + ".pdf");

        var pagesAll = new List<byte[]>();
        pagesAll = Helper.Certificate.ProcessClassCerts(classid);

        MemoryStream ms = new MemoryStream();
        PdfConcatenate whole = new PdfConcatenate(ms);

        foreach (byte[] pageAll in pagesAll)
        {
            PdfReader partReader = new PdfReader(pageAll);
            whole.AddPages(partReader);
            partReader.Close();
        }

        Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
        Response.Flush();
        Response.End(); 

    }

And this creates the individual pages and returns a byte array:

public static List<byte[]> ProcessClassCerts(int classId)
    {
        var icae = new ICAgileEntities();

        string filename = DateTime.Now.ToString().Replace(@"/", "").Replace(":", "").Replace(" ", "") + ".pdf";
        string sFileDir = ConfigurationManager.AppSettings["ServerPath"] + (@"\images\sigs\");
        var currentClass = (from c in icae.Classes
                            where c.id == classId
                            select c).FirstOrDefault();

        //make the Instructor Names
        String instName = "test";

        var students = (from up in icae.UserProfiles
                        from cl in up.UserProfile_Class_Details
                        where cl.ClassId == classId && cl.IsCertPaid == true && cl.IsClassPaid == true
                        select up);

        var currentCourse = (from s in icae.Courses
                             where s.id == currentClass.CourseID
                             select s).FirstOrDefault();



        var pageBytes = (byte[])null;
        var pagesAll = new List<byte[]>();

        try
        {


            string path = ConfigurationManager.AppSettings["ServerPath"] + @"\PDFs\";


            int i = 1;
            foreach (var s in students)
            {
                PdfStamper pst = null;
                MemoryStream mstr = null;
                using (mstr = new MemoryStream())
                {
                    try
                    {
                        PdfReader reader = new PdfReader(path + @"\certFormPDF.pdf");  //new PdfReader(GetTemplateBytes());

                        pst = new PdfStamper(reader, mstr);
                        var acroFields = pst.AcroFields;

                        acroFields.SetField("Awardee Name", s.DisplayName);


                        pst.FormFlattening = true;
                        pst.SetFullCompression();

                    }
                    finally
                    {
                        if (pst != null)
                            pst.Close();
                    }
                }
                pageBytes = mstr.ToArray();
                pagesAll.Add(pageBytes);
                i++;
            }
        }
        finally
        {
        }

        return pagesAll;
    }

Thanks for any help you can provide

0

There are 0 answers