Wednesday, September 28, 2011

Create PDF Dynamically and Download PDF from server side using iTextsharp


 MemoryStream MStream = new MemoryStream();
  Document document = new Document(PageSize.A4, 80, 50, 30, 65);
        try
        {
            PdfWriter writer = PdfWriter.GetInstance(document, MStream);
            document.Open();
            document.Add(new iTextSharp.text.Paragraph("This is test and must work"));
            document.Close();
        }
        catch (Exception e)
        {
            throw e;
        }
        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.ClearHeaders();
        HttpContext.Current.Response.ContentType = "application/pdf";
        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=myPDFNew.pdf");
        HttpContext.Current.Response.BinaryWrite(MStream.GetBuffer());
        HttpContext.Current.Response.End();

No comments:

Post a Comment