Example T227361
Visible to All Users

Reporting for ASP.NET WebForms - How to print or export a report without showing a preview

This example demonstrates how to print or export a report in an ASP.NET WebForms application without displaying this report's preview.

Report Prine or Export Without Preview

The report uses one of the ExportTo…  methods to export a report document to the page's Response.

To print the report, export it to the PDF format. Then write the PDF stream to the page's Response with the Content-Disposition header set to the "inline" value. The browser's built-in PDF viewer opens the PDF file. An alternative method exports the PDF to a separate iframe. In this case, only the print dialog is displayed.

Files to Look At

Documentation

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

T227361/Default.aspx.cs(vb)
C#
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using DevExpress.XtraReports.UI; using System.IO; using DevExpress.XtraPrinting; namespace T227361 { public partial class Default : System.Web.UI.Page { void WriteDocumentToResponse(byte[] documentData, string format, bool isInline, string fileName) { string contentType; string disposition = (isInline) ? "inline" : "attachment"; switch (format.ToLower()) { case "xls": contentType = "application/vnd.ms-excel"; break; case "xlsx": contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; break; case "mht": contentType = "message/rfc822"; break; case "html": contentType = "text/html"; break; case "txt": case "csv": contentType = "text/plain"; break; case "png": contentType = "image/png"; break; default: contentType = String.Format("application/{0}", format); break; } Response.Clear(); Response.ContentType = "application/pdf"; Response.AddHeader("Content-Disposition", String.Format("{0}; filename={1}", disposition, fileName)); Response.BinaryWrite(documentData); Response.End(); } protected void Page_Load(object sender, EventArgs e) { bool printFlag; if (Request.QueryString["Print"] != null && Boolean.TryParse((string)Request.QueryString["Print"], out printFlag) && printFlag) { //Printing XtraReport report = new ProductsReport(); using (MemoryStream ms = new MemoryStream()) { report.ExportToPdf(ms, new PdfExportOptions() { ShowPrintDialogOnOpen = true }); WriteDocumentToResponse(ms.ToArray(), "pdf", true, "Report.pdf"); } } } protected void btExport_Click(object sender, EventArgs e) { //Exporting XtraReport report = new ProductsReport(); string format = ddlExportFormat.SelectedValue; string fileName = String.Format("Report.{0}", format); using (MemoryStream ms = new MemoryStream()) { switch (format) { case "pdf": report.ExportToPdf(ms); break; case "xls": report.ExportToXls(ms); break; case "xlsx": report.ExportToXlsx(ms); break; case "rtf": report.ExportToRtf(ms); break; case "mht": report.ExportToMht(ms); break; case "html": report.ExportToHtml(ms); break; case "txt": report.ExportToText(ms); break; case "csv": report.ExportToCsv(ms); break; case "png": report.ExportToImage(ms, new ImageExportOptions() { Format = System.Drawing.Imaging.ImageFormat.Png }); break; default: return; } WriteDocumentToResponse(ms.ToArray(), format, false, fileName); } } } }
T227361/Default.aspx
ASPx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="T227361.Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript"> function Print() { var url = document.URL.split('?')[0]; window.open(url + "?Print=true", "PrintingFrame"); } function PrintWithiFrame() { var url = document.URL.split('?')[0]; window.open(url + "?Print=true", "PrintingFrame"); var frameElement = document.getElementById("FrameToPrint"); frameElement.addEventListener("load", function (e) { if (frameElement.contentDocument.contentType !== "text/html") frameElement.contentWindow.print(); }); } function PrintInNewWindow() { var url = document.URL.split('?')[0]; var printWindowWrapper = window.open(url + "?Print=true", "_blank"); printWindowWrapper.addEventListener("load", function (e) { if (printWindowWrapper.document.contentType !== "text/html") printWindowWrapper.print(); }); } </script> </head> <body> <form id="form1" runat="server"> <div> <table style="text-align: center" > <tr"> <td style="background-color: #C0C0C0">Print </td> <td style="background-color: #C0C0C0;" colspan="2">Export </td> </tr> <tr> <td style="border-style: none solid none none; border-width: 1px; width: 500px"> <input type="button" value="Print With iFrame" onclick="PrintWithiFrame(); return false;" /> <br /> <b>Note:</b> Printing with an invisible iFrame element is not recommended. Although this solution provides more desktop-like experience, it's not guaranteed to work reliably across all browsers. For example, it won't work in the Edge or Safary browser. <br /> <br /> <input type="button" value="Print in New Window" onclick="PrintInNewWindow(); return false;" /> <br /> <b>Note:</b> clicking this button will open a separate tab in the browser. This is the recommented approach. <iframe id="FrameToPrint" name="PrintingFrame" style="position:absolute; left: -10000px; top: -10000px;"></iframe> </td> <td> <asp:DropDownList ID="ddlExportFormat" runat="server"> <asp:ListItem Value="pdf" Text="Pdf" Selected="True"></asp:ListItem> <asp:ListItem Value="xls" Text="Xls"></asp:ListItem> <asp:ListItem Value="xlsx" Text="Xlsx"></asp:ListItem> <asp:ListItem Value="rtf" Text="Rtf"></asp:ListItem> <asp:ListItem Value="mht" Text="Mht"></asp:ListItem> <asp:ListItem Value="html" Text="Html"></asp:ListItem> <asp:ListItem Value="txt" Text="Text"></asp:ListItem> <asp:ListItem Value="csv" Text="Csv"></asp:ListItem> <asp:ListItem Value="png" Text="Image"></asp:ListItem> </asp:DropDownList> </td> <td> <asp:Button ID="btExport" runat="server" Text="Export" OnClick="btExport_Click" /> </td> </tr> </table> </div> </form> </body> </html>

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.