0
 private MemoryStream ConvertWebChartChartToImage(WebChartControl chart)
{
    using (var pcl = new PrintableComponentLink(new PrintingSystem())
    {
        PageHeaderFooter = new PageHeaderFooter(new PageHeaderArea(new string[] { "A", "Header" },
            SystemFonts.DialogFont, BrickAlignment.Center),
            new PageFooterArea(new string[] { "B" },
                SystemFonts.DialogFont, BrickAlignment.Center)),
        Component = ((IChartContainer)chart).Chart,
        Landscape = true
    })
    {
        ((Chart)pcl.Component).OptionsPrint.SizeMode = DevExpress.XtraCharts.Printing.PrintSizeMode.Stret  ch;

        TransDistributionWCh.Legend.AlignmentHorizontal = LegendAlignmentHorizontal.Right;
        pcl.CreateDocument();
        var stream = new MemoryStream();
        pcl.PrintingSystem.ExportToPdf(stream);
        return stream;
    }

}
    private void ConvertHTMLStringToPDF()
    {
        using (var stream = new MemoryStream())
        {
            var listChartControl = new List<WebChartControl>(new List<WebChartControl>
            {
                SuccTransDistributionWCh,
                AmountPerDayWCh,
                TransPerDayWCh,
                AmountPerTransPerDayWCh,
                ActiveTerminalPerDayWCh,
                TransNoWCh,
                TransAmountWCh,
                TransNoAmountWCh
            });
            foreach (var item in listChartControl)
            {

                var temp = ConvertWebChartChartToImage(item);
                stream.Write(temp.ToArray(), 0, temp.ToArray().Length);

            }
            HttpContext.Current.Response.ContentType = "application/pdf";
            HttpContext.Current.Response.AddHeader("Accept-Header", stream.Length.ToString(CultureInfo.InvariantCulture));
            HttpContext.Current.Response.AddHeader("Content-Disposition", ("Attachment") + "; filename=chart.pdf");
            HttpContext.Current.Response.AddHeader("Content-Length", stream.Length.ToString(CultureInfo.InvariantCulture));
            HttpContext.Current.Response.ContentEncoding = Encoding.Default;
            HttpContext.Current.Response.BinaryWrite(stream.ToArray());
        }
        HttpContext.Current.Response.End();}

我正在使用 web control chart dev express,需要将 web control chart 转换为 pdf 我的问题:如何将数据添加到内存流?此代码显示最后一个 web 图表我建议零号错误 stream.Write(temp.ToArray(), 0, temp.ToArray().Length); 搜索谷歌和这个网站不幸的是问题没有解决

4

1 回答 1

0

我不知道ExportToPdf方法是如何工作的,但是如果它是由人类编写的,那么使用单个流就足够了:

private void ConvertWebChartChartToImage(WebChartControl chart, Stream stream)
{
    // ...
    pcl.PrintingSystem.ExportToPdf(stream);
}

private void ConvertHTMLStringToPDF()
{
    using (var stream = new MemoryStream())
    {
        // ...
        foreach (var item in listChartControl)
        {
            ConvertWebChartChartToImage(item, stream);
        }
        // ...
    }
}

请注意,您的原始代码会导致不必要的内存分配:

stream.Write(
    temp.ToArray(), // allocate temporary array, copy stream content into array
    0, 
    temp.ToArray().Length // allocate another array, copy stream content into array
    );

并且不处理从方法MemoryStream返回的实例。ConvertWebChartChartToImage

此外,如果您想将一个内容复制Stream到另一个Stream,还有CopyTo/CopyToAsync方法。

于 2013-10-30T14:24:36.727 回答