C# 3.0 Save itextsharp pdf to database using MemoryStream

15,987
function byte[] CreatePdf(){
            byte[] result;
            using (MemoryStream ms = new MemoryStream())
            {
                Document pDoc = new Document(PageSize.A4, 0, 0, 0, 0);
                PdfWriter writer = PdfWriter.GetInstance(pDoc, ms);
                pDoc.Open();

                //here you can create your own pdf.

                pDoc.Close();
                result = ms.GetBuffer();
            }

            return result;
}
Share:
15,987
André Miranda
Author by

André Miranda

As a software developer, I have around 15 years of experience in programming, being 12 of them in .NET. I worked on several types of projects for different sizes of companies (from 10 employees to 2000), projects such as MVC, background and migration processes and Windows Forms applications in three different countries. I have worked on projects for different sizes of companies (from 10 employees to 2000), using ASP.NET MVC/Core applications, background services, migration of legacy code and Windows Forms applications. About working places, I worked in three different countries: Brazil, Portugal and currently Belgium. Moreover, that multicultural opportunity made me gain knowledge, improved relationships with co-workers and improved my adaptability capabilities to different environments. I have a System Analysis and Development degree and I have achieved some Microsoft® certificates, such as Microsoft® Certified: Azure Fundamentals, Microsoft® Certified Solutions Associate: Web Applications and Microsoft® Certified Technology Specialist: .NET Framework 4, Web Applications. Through all those years, I have been working mostly with C#, ASP .NET MVC and Core (more recently), SOLID fundamentals, Design Patterns (when suitable), REST WebApi, Linq, modeling and programming with SQL Server and unit/integration tests. To support all developments, I have already worked with cache technologies like Redis and queue mechanism like RabbitMQ in Event-Driven architecture. On the Frontend domain, I worked more in the past with jQuery and more recently with Angular 6 and RxJS. On code versioning and cloud, I have experience working with Git, VSTS Azure DevOps, Azure Functions and PaaS.

Updated on June 05, 2022

Comments

  • André Miranda
    André Miranda almost 2 years

    I'm trying to save to databse a pdf file generated by itextsharp. But, I haven't been successfully so far.

    I'm using Linq to sql.

    Here's the code:

                MemoryStream ms = new MemoryStream();
                Document d = new Document(PageSize.A4, 60, 60, 40, 40);
                PdfWriter w = PdfWriter.GetInstance(d, ms);
                w.CloseStream = false;
    
                string txtTemplate = "";
                Encoding en = Encoding.GetEncoding("iso-8859-1");
                StreamReader sr = new StreamReader(HttpContext.Current.Server.MapPath("~/Content/templates/CessaoDireitosDica.txt"), en);
                txtTemplate  = sr.ReadToEnd();
                sr.Close();
                string conselhos = "";
    
                Font font = new Font(Font.HELVETICA, 11, Font.NORMAL);
                font.SetColor(0xC6, 0xC6, 0xC6);
    
                Paragraph txtBody = new Paragraph(txtTemplate, font);
    
                txtBody .SetAlignment(ElementTags.ALIGN_JUSTIFIED);
    
                d.Open();
                d.Add(txtBody);
                d.Close();
    
                byte[] pdfDone = ms.ToArray();
                w.Flush();
                ms.Flush();
                ms.Close();
    
                return pdfDone;
    

    It throws no error, but it doesn't save nothing in DB. The DB field is an "image" field type. I also use this code to render a pdf on the fly (I cut off the byte[] pdfDone... and return the MemoryStream).

    I don't know what can be wrong... And debugging, I could also see that byte[] pdfDone has a value (something like 3487), but nothing is saved to DB.

    Thanks in advance!