Thursday, March 3, 2011

Telerik MVC Grid - Export to PDF VB.NET

I was thinking since last few weeks to do this task, finally I got time and success. I saw some post saying that "How to export grid data to PDF". This is what I have done as per my understanding.

Version Info

a) Visual Studio 2010

b) ASP.NET MVC 2 ( Framework 4)

c) MS Sql 2005

d) Telerik ASP.NET MVC Components (2010.3.1116.235)

c) itextsharp (5.0.5.0)

Steps

1. Create New Telerik Project; I named it GridToPdfExport.

2. I used standard northwind database.

3. Model

a) create a new dbml file; say DbContext.dbml; drag and drop customer table to this file.

b) Add ICustomer interface. I have declared only one method i.e. FindAllCustomers()

c) Implement interface to SqlCustomers.vb

3. Controller

a) use the standard home controller. As I am going to use ajax binding; I wrote _Index method.

4. View

a) In "Home" folder; index.aspx use the telerik grid. I used the ajax binding and wrote _Index method in Home Controller ( Step 3, point (b))

b) In Telerik Grid; you have to define export to pdf custom button just like we used to do it when we want to export data to excel / csv. And write the method in the same way. There will be a change in that method.

c) ExportToPdf Method

1. Signature

Public Function ExportToPdf(ByVal page As Integer, ByVal groupBy As String, ByVal orderBy As String, ByVal filter As String) As ActionResult

2. Get the data

Dim customers = _repository.FindAllCustomers()
customers = customers.ToGridModel(1, customers.Count(), groupBy, orderBy, filter).Data

3. Create of a document-object with pagesize and margin ( this will be from itextsharpe )

Dim document As Document
document = New Document(PageSize.A4.Rotate, 10, 10, 10, 10)

4. Create a memory stream that listens to the document.

Dim output As New MemoryStream()
pdf.PdfWriter.GetInstance(document, output)

5. Open the document

document.Open()

6. Add content to the document

Dim numOfColumns As Integer = 8
Dim dataTable As pdf.PdfPTable
dataTable = New pdf.PdfPTable(numOfColumns)
dataTable.DefaultCell.Padding = 3
dataTable.DefaultCell.BorderWidth = 2
dataTable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER

7. Add headers

dataTable.AddCell("Customer Id")
dataTable.AddCell("Contact Name")
dataTable.AddCell("Company Name")
dataTable.AddCell("Address")
dataTable.AddCell("City")
dataTable.AddCell("Phone")
dataTable.AddCell("Postal Code")
dataTable.AddCell("Region")

dataTable.HeaderRows = 1
dataTable.DefaultCell.BorderWidth = 1

For Each cust In customers
dataTable.AddCell(cust.CustomerID)
dataTable.AddCell(cust.ContactName)
dataTable.addCell(cust.CompanyName)
dataTable.AddCell(cust.Address)
dataTable.AddCell(cust.City)
dataTable.AddCell(cust.Phone)
dataTable.AddCell(cust.PostalCode)
dataTable.AddCell(cust.Region)
Next

8. Add table to the document

document.Add(dataTable)

9. Close the document don’t forget this

document.Close()

10. Return file as ajax response

Return File(output.ToArray(), "application/pdf", "CustomerData.pdf")

That’s it. Your data will be exported in pdf.