I am building a module which displays the results of a query and then offers those results in a pdf for download. Running and displaying the query work as expected, but when the link is clicked to build and download the pdf the file is never sent to the browser (the user isn't given the option to download the file). Is there something about DNN that modifies this behaviour? Am I missing something? My code is as follows (sorry for the awful formatting):
protected void imgbtnPDF_Click(object sender, System.Web.UI.ImageClickEventArgs e) {
try
{
DataTable dt = (DataTable)Session["results_table"];
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
StringReader sr = new StringReader(sw.ToString());
iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(iTextSharp.text.PageSize.LETTER, 30.0F, 30.0F, 30.0F, 30.0F);
iTextSharp.text.pdf.PdfPTable pdfTable = new PdfPTable(dt.Columns.Count);
PdfPCell head_cell = new PdfPCell();
PdfPCell cell = new PdfPCell();
// Add header row
for (int i = 0; i < dt.Columns.Count; i++)
{
head_cell = new PdfPCell(new iTextSharp.text.Phrase(dt.Rows[0].ItemArray[i].ToString(), iTextSharp.text.FontFactory.GetFont(iTextSharp.text.FontFactory.HELVETICA, 10, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.WHITE)));
head_cell.BackgroundColor = new iTextSharp.text.BaseColor(162, 40, 0);
head_cell.PaddingBottom = 5.0F;
head_cell.PaddingTop = 5.0F;
head_cell.HorizontalAlignment = 1;
pdfTable.AddCell(head_cell);
}
// Add content cells
for (int j = 0; j < dt.Rows.Count; j++)
{
for (int k = 0; k < dt.Columns.Count; k++)
{
cell = new PdfPCell(new iTextSharp.text.Phrase(dt.Rows[j].ItemArray[k].ToString(), iTextSharp.text.FontFactory.GetFont(iTextSharp.text.FontFactory.HELVETICA,10, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.BLACK)));
cell.HorizontalAlignment = 1;
if (k % 2 != 0) { cell.BackgroundColor = new iTextSharp.text.BaseColor(220, 220, 220); }
pdfTable.AddCell(cell);
}
}
PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);
iTextSharp.text.Phrase title_select, new_line;
title_select = new iTextSharp.text.Phrase("Query Information", iTextSharp.text.FontFactory.GetFont(iTextSharp.text.FontFactory.HELVETICA, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.BLACK));
new_line = new iTextSharp.text.Phrase("\n");
string s = lblCountries.Text + "\n" + lblDivisions.Text + "\n" + lblSpecies.Text + "\n" + lblYears.Text;
string image_file_path = Server.MapPath(".") + "/DesktopModules/Statlant_DNN_Module/img/logo.png";
iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(image_file_path);
logo.Alignment = iTextSharp.text.Image.ALIGN_CENTER;
logo.ScalePercent(10);
iTextSharp.text.Paragraph footer = new iTextSharp.text.Paragraph("Page: ", iTextSharp.text.FontFactory.GetFont(iTextSharp.text.FontFactory.HELVETICA, 9, iTextSharp.text.Font.NORMAL));
pdfDoc.Open();
iTextSharp.text.Phrase select_head = new iTextSharp.text.Phrase(title_select);
iTextSharp.text.Phrase pre = new iTextSharp.text.Phrase(s + "\n\n" + "Extraction Date: " + DateTime.Now.ToString("yyyy-MM-dd") + "\n" + "Last Database Update: " + lblLastUpdate.Text, iTextSharp.text.FontFactory.GetFont(iTextSharp.text.FontFactory.HELVETICA, 8, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.BLACK));
pdfDoc.AddTitle("STATLANT21A Data Extraction");
pdfDoc.AddSubject("Catch in the NAFO Area");
pdfDoc.Add(logo);
pdfDoc.Add(new_line);
pdfDoc.Add(select_head);
pdfDoc.Add(new_line);
pdfDoc.Add(pre);
pdfDoc.Add(new_line);
pdfDoc.Add(new_line);
pdfDoc.Add(pdfTable);
pdfDoc.Add(footer);
pdfDoc.Close();
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Write(pdfDoc);
HttpContext.Current.Response.Close();
}
catch (Exception ex)
{
lblError.Text = ex.Message;
}
}