Render PDF File onto Client's Browser
Generic Procedure for Rendering given file to Client's Browser:
Usage:
protected void WriteToResponse(string fileName, bool saveAsFile, string fileFormat, System.IO.MemoryStream stream)
{
if (Page == null Page.Response == null) return;
string disposition = saveAsFile ? "attachment" : "inline";
Page.Response.Clear();
Page.Response.Buffer = false;
Page.Response.AppendHeader("Content-Type", string.Format("application/{0}", fileFormat));
Page.Response.AppendHeader("Content-Transfer-Encoding", "binary");
Page.Response.AppendHeader("Content-Disposition", string.Format("{0}; filename={1}.{2}", disposition, fileName, fileFormat));
Page.Response.BinaryWrite(stream.GetBuffer());
Page.Response.End();
}
//Saves File on Server - For given Location
protected void SaveMemoryStream(MemoryStream stream, string FileName)
{
FileStream outStream = File.OpenWrite(FileName);
stream.WriteTo(outStream);
outStream.Flush();
outStream.Close();
}
Usage:
MemoryStream stream = new MemoryStream();
DevExpress.Web.ASPxTreeList.Export.Helper.ASPxTreeListLink link = new DevExpress.Web.ASPxTreeList.Export.Helper.ASPxTreeListLink(treeViewPDFExporter);
link.CreateReportHeaderArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateReportHeaderArea);
link.PrintingSystem = new DevExpress.XtraPrinting.PrintingSystem();
link.CreateDocument();
link.PrintingSystem.ExportToPdf(stream);
//Renders Document to Client's Browser
WriteToResponse(DPkg_MapFile, true, "pdf", stream);
//Saves File on to Server location
SaveMemoryStream(stream, DPkg_MapFile);
Comments