{"id":39,"date":"2012-12-26T15:01:00","date_gmt":"2012-12-26T15:01:00","guid":{"rendered":"http:\/\/tpodolak.com.hostingasp.pl\/blog\/2012\/12\/26\/asp-net-mvc-custom-actionresult-pdfresult\/"},"modified":"2016-01-30T23:59:59","modified_gmt":"2016-01-30T23:59:59","slug":"asp-net-mvc-custom-actionresult-pdfresult","status":"publish","type":"post","link":"https:\/\/tpodolak.com\/blog\/2012\/12\/26\/asp-net-mvc-custom-actionresult-pdfresult\/","title":{"rendered":"ASP.NET MVC &#8211; Custom ActionResult &#8211; PDFResult"},"content":{"rendered":"<h3>1. Introduction<\/h3>\n<p>In the company I&#8217;m currently working for, it is a common practice to use Devexpress XtraReports to create all kind of reports. Usually these reports are embedded into html page and used along with DevexpressReportViewer. However, lately I have been asked to open a report as a PDF file, without putting a viewer into a html page.<\/p>\n<h3>2. First approach<\/h3>\n<p>After some digging, I created an action in controller which looks like this<\/p>\n<pre lang=\"csharp\">\r\npublic void ExportToPdf()\r\n{\r\n    using (MemoryStream stream = new MemoryStream())\r\n    {\r\n        var testReport = new TestReport();\r\n        testReport.CreateDocument();\r\n        testReport.ExportToPdf(stream);\r\n        Response.Clear();\r\n        Response.ContentType = \"application\/pdf\"\r\n        Response.BinaryWrite(stream.ToArray());\r\n    }\r\n}\r\n<\/pre>\n<p>In the first step I made a new instance of a devexpress report(TestReport), then I called a method <i>CreateDocument<\/i> which is responsible for creating a content of report. Having prepared the report, I could use the <i>ExportToPdf<\/i> function which creates a stream in a PDF format. In the last step, I cleared Response property and added an array of bytes into it. I also had to change <i>Response.ContentType<\/i> to &#8220;application\/pdf&#8221; in order to inform a browser that I&#8217;m sending a pdf file. <\/p>\n<h3>3. Second approach &#8211; custom ActionResult<\/h3>\n<p>The solution presented above works fine, however, I wanted to create something more reusable, that is why I decided to create a custom <i>ActionResult &#8211; PDFActionResult<\/i>. In order to create my own <i>ActionResult<\/i>, I created a <i>PDFActionResult<\/i> class which inherits from an abstract ActionResult class.<\/p>\n<pre lang=\"csharp\">\r\npublic class PDFActionResult : ActionResult\r\n{\r\n    private readonly byte[] _byteArray;\r\n    private const string ContentType = \"application\/pdf\";\r\n    \r\n    public PDFActionResult(byte[] byteArray)\r\n    {\r\n        _byteArray = byteArray;\r\n    }\r\n    \r\n    public PDFActionResult(XtraReport report)\r\n    {\r\n        using(MemoryStream stream = new MemoryStream())\r\n        {\r\n            report.CreateDocument();\r\n            report.ExportToPdf(stream);\r\n            _byteArray = stream.ToArray();\r\n        }\r\n    }\r\n    \r\n    public override void ExecuteResult(ControllerContext context)\r\n    {\r\n        var response = context.HttpContext.Response;\r\n        response.Clear();\r\n        response.ContentType = ContentType;\r\n        response.BinaryWrite(_byteArray);\r\n    }\r\n}\r\n<\/pre>\n<p>As you can see, all logic from the <i>ExportToPdf<\/i> action was transferred to the PDFActionResult class. Now, all you have to do to return Pdf from DevexpressReport is to return the PDFActionResult from your controller action.<\/p>\n<pre lang=\"csharp\">\r\npublic PDFActionResult ExportToPdfUsingCustomResult()\r\n{\r\n    var testReport = new TestReport();\r\n    return new PDFActionResult(testReport);\r\n}\r\n<\/pre>\n<p><a href=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2012\/12\/asp-net-mvc-custom-actionresult-pdfresult\/ResultPDF.bmp\" rel=\"attachment wp-att-374\"><img decoding=\"async\" src=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2012\/12\/asp-net-mvc-custom-actionresult-pdfresult\/ResultPDF.bmp\" alt=\"custom ActionResult\" width=\"700\" class=\"aligncenter size-full wp-image-374\" \/><\/a><br \/>\nSource code for this post can be found <a href=\"https:\/\/github.com\/tpodolak\/Blog\/tree\/master\/AspCustomActionResult\">here<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Introduction In the company I&#8217;m currently working for, it is a common practice to use Devexpress XtraReports to create all kind of reports. Usually these reports are embedded into html page and used along with DevexpressReportViewer. However, lately I have been asked to open a report as a PDF file, without putting a viewer [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79,83,82,80],"tags":[222,223,224,225],"class_list":["post-39","post","type-post","status-publish","format-standard","hentry","category-actionresult","category-asp-net-mvc","category-customactionresult","category-pdfacionresult","tag-actionresult","tag-asp-net-mvc","tag-customactionresult","tag-pdfacionresult"],"_links":{"self":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/39","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/comments?post=39"}],"version-history":[{"count":8,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/39\/revisions"}],"predecessor-version":[{"id":544,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/39\/revisions\/544"}],"wp:attachment":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/media?parent=39"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/categories?post=39"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/tags?post=39"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}