11340 : Printing from a web page

Question

I want to print the gantt shown on a webpage.

Answer

You will need to perform the rending server side. The general idea is the same as explained in this article https://plexityhide.com/faqs_gen_GTPNET/10374.htm.

To be more precise this is how it is done if you want the result to returned as jpeg:

     protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Bitmap bm = new Bitmap(Gantt_ASP1.Gantt.Width, 300 + Gantt_ASP1.Gantt.DateScalerHeight);
        Graphics g = Graphics.FromImage(bm);
        Rectangle r = Rectangle.FromLTRB(0, 0, Gantt_ASP1.Gantt.Width, 300 + Gantt_ASP1.Gantt.DateScalerHeight);
        g.FillRectangle(Brushes.White, r);
        bool hasMorePages = false;

        Gantt_ASP1.Gantt.PrintInit(null);
        Gantt_ASP1.Gantt.PrintPage(g, r, Gantt_ASP1.Gantt.GridWidth + 10, Gantt_ASP1.Gantt.DateScalerHeight, ref hasMorePages);
        Gantt_ASP1.Gantt.PrintEnd();

        Response.Clear();
        Response.ContentType = “image/jpeg”;
        Response.AddHeader(“Content-Disposition”, “attachment; filename=GanttASP.jpeg”);
        bm.Save(Response.OutputStream, ImageFormat.Jpeg);
        Response.End();
        bm.Dispose();
        g.Dispose();
    }

If you want to to pdf or something like that, the basic principle applies, but you will need an additional tool to render the pdf.

Leave a Reply