Event Calendar PDF Export Tutorial (C#, VB, ASP.NET)

This tutorial shows how to export the event calendar to PDF in ASP.NET. Includes a sample Visual Studio project with source code.
Feb 20, 2014
event calendar pdf acrobat

This tutorial shows how to export a weekly event calendar created using DayPilot event calendar to a PDF file.

Features:

  • Export to PNG
  • Export to PDF
  • Customized event calendar appearance (fonts, colors)
  • Multiple PDF page sizes (letter, A4)
  • PDF page orientation (portrait, landscape)
  • Custom PDF page header
  • Automatic image scaling to fill the page

Sample code:

C#

private void ExportToPdf()
{
  PdfDocument doc = new PdfDocument();
  doc.Info.Title = "DayPilot Calendar PDF Export";
  doc.Info.Author = "DayPilot";

  PdfPage page = doc.AddPage();

  page.Size = (PageSize) Enum.Parse(typeof (PageSize), ListPageSize.SelectedValue);
  page.Orientation = (PageOrientation)Enum.Parse(typeof(PageOrientation), ListPageOrientation.SelectedValue);

  MemoryStream mem = new MemoryStream();
  doc.Save(mem, false);

  Response.Clear();
  Response.ContentType = "application/pdf";
  Response.AddHeader("content-disposition", "attachment;filename=calendar.pdf");
  mem.WriteTo(Response.OutputStream);
  Response.End();
}

VB

Private Sub ExportToPdf()
  ' create a new PDF document
  Dim doc As New PdfDocument()
  doc.Info.Title = "DayPilot Calendar PDF Export"
  doc.Info.Author = "DayPilot"

  ' add a page
  Dim page_Renamed As PdfPage = doc.AddPage()

  ' set PDF page properties (size and orientation)
  page_Renamed.Size = CType(System.Enum.Parse(GetType(PageSize), ListPageSize.SelectedValue), PageSize)
  page_Renamed.Orientation = CType(System.Enum.Parse(GetType(PageOrientation), ListPageOrientation.SelectedValue), PageOrientation)

  ' save the PDF file to MemoryStream
  Dim mem As New MemoryStream()
  doc.Save(mem, False)

  ' send the output stream to the browser
  Response.Clear()
  Response.ContentType = "application/pdf"
  Response.AddHeader("content-disposition", "attachment;filename=calendar.pdf")
  mem.WriteTo(Response.OutputStream)
  Response.End()
End Sub

The sample project with C# and VB source code is available for download.