search envelope-o feed check
Home Unanswered Active Tags New Question
user comment-o

How to print a calendar?

Asked by Jorn Mortensen
15 years ago.

When using your demo and clicking the print/export I get a page with only the calendar, but how to get this out on paper? or saved as an image?

Everytime I thy anything like clicking the printsymbol I just get a red cross, but not the calendar. Right click on the calendar is also not an option - gives an errorlike "The filetype is being blocked"

Comment posted by Dan Letecky
15 years ago.

In Firefox the printing works fine but in IE it does what you describe. It's probably because the image is a result of a PostBack (it looks like a IE limitation). If you make a copy of the page and call the exporting code in a Page_Load instead of ButtonExport_Click you should get rid of that problem:

protected void Page_Load(object sender, EventArgs e)
{
  DayPilotCalendar1.StartDate = Convert.ToDateTime(Request.QueryString["StartDate"]);
  DayPilotCalendar1.DataBind();

  int hourHeight = DayPilotCalendar1.CellsPerHour*DayPilotCalendar1.CellHeight;

  Response.Clear();
  Response.ContentType = "image/png";
  MemoryStream img = DayPilotCalendar1.Export(ImageFormat.Png, 9 * hourHeight);
  img.WriteTo(Response.OutputStream);
  Response.End();
}

You will need to pass the parameters in the URL (typically the StartDate):

<a href="Print.aspx?StartDate=2009-01-01">Print</a>

Comment posted by Dan Letecky
15 years ago.

I will update the demo so it will work this way.

Comment posted by Jorn Mortensen
15 years ago.

Thanks Dan

Had hoped on a simpler solution, because the calendar I have is very dynamic for everyone of the 2000 users in the school, so with a new page I have to keep strict track of all the choises made...

Answer posted by Dan Letecky
15 years ago.

After some experimenting, I've found a workaround. The following code prompts the user to open/save the image. This makes it printable from IE as well.

        Response.Clear();
        Response.ContentType = "image/png";
        Response.AddHeader("content-disposition", "attachment;filename=print.png");
        MemoryStream img = DayPilotCalendar1.Export(ImageFormat.Png, 9 * hourHeight);
        img.WriteTo(Response.OutputStream);
        Response.End();

It seems that in order to print it properly directly from IE, it has to be a GET request, not POST. This could be done by encoding the current state into the URL. However, the URL length has a limit and I'm not sure if it's worth the effort at this moment.

Another, a bit more dirty solution would be to store the state in the Session under a special GUID and pass just the GUID in the URL.

Both these alternative solutions would need a special method for serialization/deserialization of the control state.

This question is more than 1 months old and has been closed. Please create a new question if you have anything to add.