Click here to Skip to main content
15,895,256 members
Articles / Productivity Apps and Services / Microsoft Office

Export Tabular Data in PDF Format through the Web

Rate me:
Please Sign up or sign in to vote.
4.57/5 (12 votes)
13 Oct 2011CPOL5 min read 47.2K   1.3K   21  
This article presents an example to export tabular data in PDF format through the web.
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using iTextSharp.text;
using iTextTabularReport.Utilities;
using iTextTabularReport.Models;

namespace iTextTabularReport.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        [HttpGet]
        public ActionResult Index()
        {
            return new RedirectResult("~/Default.htm");
        }

        [HttpGet]
        public void GetPdf()
        {
            var configuration = new ReportConfiguration();
            configuration.PageOrientation = PageSize.LETTER_LANDSCAPE.Rotate();
            configuration.LogoPath
                = Server.MapPath(Url.Content("~/Content/Images/Logo.jpg"));
            configuration.LogImageScalePercent = 50;
            configuration.ReportTitle
                = "Export Tabular Data in Pdf Format through the Web";
            configuration.ReportSubTitle = "Created by iText Report Tool";

            var report = new PdfTabularReport();
            report.ReportConfiguration = configuration;

            List<ReportColumn> columns = new List<ReportColumn>();
            columns.Add(new ReportColumn { ColumnName = "Id", Width = 100 });
            columns.Add(new ReportColumn { ColumnName = "Name", Width = 100 });
            columns.Add(new ReportColumn { ColumnName = "Enrollment", Width = 100 });
            columns.Add(new ReportColumn { ColumnName = "Score", Width = 100 });

            var stream = report.GetPdf(StudentRepository.GetStudentsTable(1000), columns);

            Response.Clear();
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition",
                "attachment;filename=ExampleReport.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.BinaryWrite(stream.ToArray());
            Response.End();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
I have been working in the IT industry for some time. It is still exciting and I am still learning. I am a happy and honest person, and I want to be your friend.

Comments and Discussions