I am using Itext Sharp to create pdf’s for a existing order on my site on a button click. It has been added as a web part on Sharepoint site.
On the Same Page , there is another web part with many server controls. But, when I click on any of the buttons , button click event is not triggerning. Only when I refresh the page, the event is getting triggered.
I am not able to simultaneously create a pdf and then immediately click on another button without page refresh. I dont understand what am I doing wrong.
Below is my code.
protected void PdfGenerate_OnClick(object sender, EventArgs e)
{
Font Arial = FontFactory.GetFont("Arial", 12, BaseColor.BLACK);
string imagename = "AAlogo.gif";
string imagepath = Page.Server.MapPath("/_layouts/Images/ImagesFolder/Image1.gif" ) ;
using (var ms = new MemoryStream())
{
using (var document = new Document(PageSize.A4,50,50,15,15))
{
PdfWriter.GetInstance(document, ms);
document.Open();
Paragraph img = new Paragraph();
Image aaImg = iTextSharp.text.Image.GetInstance(imagepath);
img.Add(aaImg);
img.IndentationRight = 5f;
//code to add all the elements here
PdfPTable table = new PdfPTable(3);
PdfPCell col1 = new PdfPCell(new Phrase("oRDER Number"));
table.AddCell(col1);
// code for the table...
document.Add(table);
}
document.Close();
}
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("content-disposition", "attachment;filename= Order Number" + .pdf");
Response.Buffer = true;
Response.Clear();
var bytes = ms.ToArray();
Response.OutputStream.Write(bytes, 0, bytes.Length);
Response.OutputStream.Flush();
}
}
When you Clear and Flush the Response, you are interrupting/aborting asp.net’s page life cycle. It’s better to put that download section in another file (Page_Load of the separate file) and call it from the current page. more info: + and +