Printing ASP.NET 3.5 Chart Control

asp.netchartscontrolsprinting

Any info out there on how to print the generated charts in say PDF format? They look fabulous on screen but the end users want to print them and file as needed.

Best Solution

I was looking for a way to do this and found this answer for winforms

This is the way that I got the asp:chart to print

Add javascript on the web page:

<script type="text/javascript" language="javascript">
    function printChart() {

    var html = '<HTML>\n<HEAD>\n';
    html += '<link rel="stylesheet" type="text/css" href="../../../Styles/print.css" media="print"> \n';

    html += '\n</HEAD>\n<BODY>\n';
    html += '\n<div>';

    var printReadyElement = document.getElementById("printChart");

    if (printReadyElement != null) {
        html += printReadyElement.innerHTML;
    }
    else {
        alert("Trouble printing Chart");
        return;
    }

    html += '\n</div>';

    html += '\n</BODY>\n</HTML>';

    var printWin = window.open("", "printSpecial");
    printWin.document.open();
    printWin.document.write(html);
    printWin.document.close();

    printWin.print();

}

this is linked to a input button

<input type="button" value="Print" onclick="printChart()" style="width:99px; height:26px;" />

The next step was to add elements to the web.config

<appSettings> 
   <add key="ChartImageHandler" value="storage=memory;timeout=20;deleteAfterServicing=false;" />
</appSettings>

Under the system.web tag

    <httpHandlers>      
        <add path="ChartImg.axd" verb="GET,HEAD,POST" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false" />
    </httpHandlers>

Under the system.webServer

<handlers>
  <remove name="ChartImageHandler">
  <add name="ChartImageHandler" preCondition="integratedMode" verb="GET,HEAD,POST" path="ChartImg.axd" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</handlers>