Tutorial: Gantt Control PDF Export (ASP.NET)

This tutorial shows how to export the Gantt chart ASP.NET control to PDF. Sample C# and VB.NET projects available for download.
Jun 2, 2014
gantt asp.net control setup



  • ASP.NET Gantt control
  • Customizable scale - we are displaying one cell per day
  • Customizable time header - we are displaying days grouped by month
  • Adding a new task using a modal dialog
  • Editing a task using a modal dialog
  • PNG Export
  • PDF Export
  • Customizable PDF page format (Letter, A4)
  • Customizable PDF page orientation (portrait, landscape)
  • Visual Studio 2012 solution

The tutorial source code (C#, VB.NET) is available for download.


Basic Gantt configuration (ASP.NET):

      <DayPilot:TimeHeader GroupBy="Month" Format="MMMM yyyy" />
      <DayPilot:TimeHeader GroupBy="Cell" />
      <DayPilot:RowHeaderColumn Title="Task" Width="120" />
      <DayPilot:RowHeaderColumn Title="Duration" Width="90" />

The PDF export method uses the open-source PDFSharp library to create the PDF file (C#):

private void ExportToPdf()
  // create a new PDF document
  PdfDocument doc = new PdfDocument();
  doc.Info.Title = "Gantt Chart PDF Export";
  doc.Info.Author = "DayPilot";

  // add a page
  PdfPage page = doc.AddPage();

  // set PDF page properties (size and orientation)
  page.Size = (PageSize) Enum.Parse(typeof (PageSize), ListPageSize.SelectedValue);
  page.Orientation = (PageOrientation)Enum.Parse(typeof(PageOrientation), ListPageOrientation.SelectedValue);

  // create graphics object for PDF page modification
  XGraphics gfx = XGraphics.FromPdfPage(page);

  // write title
  XRect titleRect = new XRect(new XPoint(), gfx.PageSize);
  titleRect.Inflate(-10, -15);
  XFont font = new XFont("Tahoma", 14, XFontStyle.Bold);
  gfx.DrawString("DayPilot Gantt PDF Export", font, XBrushes.DarkGray, titleRect, XStringFormats.TopCenter);

  // create the Gantt image
  Bitmap bitmap = GanttControl.ExportBitmap();

  // add the image to the PDF page
  XImage image = XImage.FromGdiPlusImage(bitmap);
  XRect imageRect = GetPaddedRectForImage(gfx, image, 10);
  double y = 40;
  imageRect.Y = y;
  gfx.DrawImage(image, imageRect);

  // save the PDF file to MemoryStream
  MemoryStream mem = new MemoryStream();
  doc.Save(mem, false);

  // send the output stream to the browser
  Response.ContentType = "application/pdf";
  Response.AddHeader("content-disposition", "attachment;filename=gantt.pdf");