Create PDF File Using Microsoft Report

Introduction

This article walks you through the steps for creating a report in Word or PDF format, using Microsoft Report without using Report Viewer.

Step 1

Create ASP.NET Web Application

Check the link given below to see all the steps to create a Web API with an Entity Framework Code First implementation.

http://social.technet.microsoft.com/wiki/contents/articles/26795.asp-net-webapi-entity-framework-code-first.aspx

Step 2 -

Install Microsoft Report Viewer Runtime into machine

To have the ability to create reports into your project, you will need to install the Runtime of Microsoft Report Viewer.

Follow the link given below and download it, as per your Visual Studio version.

http://www.microsoft.com/en-us/download/details.aspx?id=35747

ASP.NET

Step 3

Create Report

Add new item to our project of type Report.

On the left menu, select Reporting option, followed by selecting Report item, as shown below.

Call it Contacts.rdlc.

ASP.NET

Create new datasource associated with our connection string, which is defined on web.config.

Select Next button.

ASP.NET

On this demo, we will use table Contacts, which is defined on our database.

ASP.NET

Rename the dataset to DataSetContacts and click OK option.

ASP.NET

After the creation of dataset, we need to design our report.

For this, create a table with the three columns, as shown below.

This will display the Name, Address and City of each contact existing on the database table.

ASP.NET

Step 4

PDF Generate Class

Create new Controller called ReportControllers and use the GetPDFreport method.

C#

  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Data.Entity;   
  4. using System.Data.Entity.Infrastructure;   
  5. using System.Linq;   
  6. using System.Net;   
  7. using System.Net.Http;   
  8. using System.Web.Http;   
  9. using SampleEF6.Models;   
  10. using System.Threading.Tasks;   
  11. using System.Web;   
  12. using System.IO;   
  13. using System.Net.Http.Headers;   
  14.    
  15. namespace SampleEF6.Controllers   
  16. {   
  17.     public class ReportController : ApiController   
  18.     {   
  19.         // GET api/<controller>   
  20.         [HttpGet]   
  21.         public async Task<HttpResponseMessage> GetPDFReport()   
  22.         {   
  23.             string fileName = string.Concat("Contacts.pdf");   
  24.             string filePath = HttpContext.Current.Server.MapPath("~/Report/" + fileName);   
  25.    
  26.             ContactController contact = new ContactController();   
  27.             List<Contact> contacList = contact.Get().ToList();   
  28.    
  29.             await SampleEF6.Report.ReportGenerator.GeneratePDF(contacList, filePath);   
  30.    
  31.             HttpResponseMessage result = null;   
  32.             result = Request.CreateResponse(HttpStatusCode.OK);   
  33.             result.Content = new StreamContent(new FileStream(filePath, FileMode.Open));   
  34.             result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");   
  35.             result.Content.Headers.ContentDisposition.FileName = fileName;   
  36.    
  37.             return result;   
  38.         }   
  39.     }   
  40. }   

Create the GeneratePDF method, as shown below.

C#

  1. using Microsoft.Reporting.WebForms;   
  2. using SampleEF6.Models;   
  3. using System;   
  4. using System.Collections.Generic;   
  5. using System.IO;   
  6. using System.Linq;   
  7. using System.Reflection;   
  8. using System.Threading.Tasks;   
  9. using System.Web;   
  10.    
  11. namespace SampleEF6.Report   
  12. {   
  13.     public class ReportGenerator   
  14.     {   
  15.         public static string Report = "SampleEF6.Report.Contacts.rdlc";   
  16.    
  17.         public static Task GeneratePDF(List<Contact> datasource, string filePath)   
  18.         {   
  19.             return Task.Run(() =>   
  20.             {   
  21.                 string binPath = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "bin");   
  22.                 var assembly = Assembly.Load(System.IO.File.ReadAllBytes(binPath + "\\SampleEF6.dll"));   
  23.    
  24.                 using (Stream stream = assembly.GetManifestResourceStream(Report))   
  25.                 {   
  26.                     var viewer = new ReportViewer();   
  27.                     viewer.LocalReport.EnableExternalImages = true;   
  28.                     viewer.LocalReport.LoadReportDefinition(stream);   
  29.    
  30.                     Warning[] warnings;   
  31.                     string[] streamids;   
  32.                     string mimeType;   
  33.                     string encoding;   
  34.                     string filenameExtension;   
  35.    
  36.                     viewer.LocalReport.DataSources.Add(new ReportDataSource("DataSetContacts", datasource));   
  37.    
  38.                     viewer.LocalReport.Refresh();   
  39.    
  40.                     byte[] bytes = viewer.LocalReport.Render(   
  41.                         "PDF"null, out mimeType, out encoding, out filenameExtension,   
  42.                         out streamids, out warnings);   
  43.    
  44.                     using (FileStream fs = new FileStream(filePath, FileMode.Create))   
  45.                     {   
  46.                         fs.Write(bytes, 0, bytes.Length);   
  47.                     }   
  48.                 }   
  49.             });   
  50.         }   
  51.     }   
  52. }   

Step 5

Run the Application

ASP.NET



Similar Articles