Learn More





New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

Welcome to the DNN Community Forums, your preferred source of online community support for all things related to DNN.
In order to participate you must be a registered DNNizen

HomeHomeDevelopment and...Development and...Building ExtensionsBuilding ExtensionsModulesModulesSending file to browser for downloadSending file to browser for download
New Post
6/7/2016 9:09 AM
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) {
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;

// 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); }

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;

iTextSharp.text.Paragraph footer = new iTextSharp.text.Paragraph("Page: ", iTextSharp.text.FontFactory.GetFont(iTextSharp.text.FontFactory.HELVETICA, 9, iTextSharp.text.Font.NORMAL));

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");

HttpContext.Current.Response.Buffer = true;
catch (Exception ex)
lblError.Text = ex.Message;

HomeHomeDevelopment and...Development and...Building ExtensionsBuilding ExtensionsModulesModulesSending file to browser for downloadSending file to browser for download

These Forums are dedicated to discussion of DNN Platform and Evoq Solutions.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
  2. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  4. No Flaming or Trolling.
  5. No Profanity, Racism, or Prejudice.
  6. Site Moderators have the final word on approving / removing a thread or post or comment.
  7. English language posting only, please.

Content Layout

Subscribe to DNN Digest

Subscribe to DNN Digest

DNN Digest is our monthly email newsletter. It highlights news and content from around the DNN ecosystem, such as new modules and themes, messages from leadership, blog posts and notable tweets. Keep your finger on the pulse of the ecosystem by subscribing.  

What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out