【问题标题】:Printing ASP.NET 3.5 Chart Control打印 ASP.NET 3.5 图表控件
【发布时间】:2026-01-09 14:30:02
【问题描述】:

关于如何以 PDF 格式打印生成的图表的任何信息?它们在屏幕上看起来很棒,但最终用户希望打印它们并根据需要归档。

【问题讨论】:

  • 在下面@CheGueVerra 的回答下查看我的评论。

标签: asp.net printing charts controls


【解决方案1】:

我正在寻找一种方法来做到这一点,并为 winforms 找到了这个答案

这就是我打印 asp:chart 的方式

在网页上添加javascript:

<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();

}

这是链接到一个输入按钮

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

下一步是向 web.config 添加元素

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

system.web标签下

    <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>

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>

【讨论】:

  • 为了打印我页面上的图表,我只需要从
【解决方案2】:

所以我在使用 JavaScript 函数 PrintPage(); 时遇到了同样的问题。 它将打印网页,但不包括 asp.net 图表控件。您可以看到图表的边框,但看不到数据。所以我为解决这个问题所做的就是将用于调用 printPage() 函数的按钮移到 updatePanel 之外,并且它起作用了。

希望这对某人有所帮助。

<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);
    }

【讨论】:

    【解决方案3】:

    从 MSDN 下载图表控件示例。有几个如何打印的示例。要获取 PDF,您需要 PDF 打印驱动程序。

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

    查看 \WinSamples\ChartFeatures\Printing\

    简单的方法是:

    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);
    
    ...
    

    【讨论】:

    • 我不明白为什么这个答案被标记为正确。问题是关于在 ASP.NET 中打印 MS 图表,而答案是关于在 WinForms 中打印它。而且,在 ASP.NET 图表示例中,没有打印示例。