Question

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.

Was it helpful?

Solution

download the Chart Control samples from MSDN. there are several samples of how to print. To get PDF, you need a PDF print driver.

http://code.msdn.microsoft.com/mschart/Release/ProjectReleases.aspx?ReleaseId=1591

look in \WinSamples\ChartFeatures\Printing\

the simple way is:

using System.Windows.Forms.DataVisualization.Charting;
...

// Show Page Setup dialog
chart1.Printing.PageSetup();

// Print preview chart
chart1.Printing.PrintPreview();

// Print chart (without Printer dialog)
chart1.Printing.Print(false);

...

OTHER TIPS

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>

So I was having the same problem when using the JavaScript function PrintPage(); It would print the web page ,but would not include asp.net chart controls. You could see the border of the chart but not the data. So What I did to fix this problem was move the button that was being used to to call my printPage() function outside the updatePanel and it worked.

Hope this helps someone.

<asp:Button runat="server" ID="btnPrint" OnClick="btnPrint_Click" CssClass="Floater" Text="Print Customer Report" Visible="True" />

<script>
    function PrintPage() {

        window.print();
    }
</script>

protected void btnPrint_Click(object sender, EventArgs e)
    { 
        ScriptManager.RegisterStartupScript(Page, GetType(), "JsStatus", "PrintPage();", true);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top