我想使用telerik控件将radchart导出为pdf am

时间:2013-03-20 06:29:00

标签: charts telerik

我使用此代码获取此图表,并且我想将此图表导出为pdf。

 private void InitializeGraph(DataTable poDt)
 {
    Telerik.Charting.ChartSeries chartseries = new Telerik.Charting.ChartSeries();
    try
    {

      chartseries.Type = Telerik.Charting.ChartSeriesType.Bar;
      Telerik.Charting.ChartSeriesItem csItem;
      RadChart1.PlotArea.XAxis.AutoScale = true;

      RadChart1.PlotArea.XAxis.AddRange(1, poDt.Rows.Count, 1);
      RadChart1.PlotArea.XAxis.AutoShrink = true;

      for (int iRow = 0; iRow < poDt.Rows.Count; iRow++)
      {
        chartseries = new Telerik.Charting.ChartSeries();
        chartseries.Type = Telerik.Charting.ChartSeriesType.Bar;
        chartseries.Name = poDt.Rows[iRow]["Name"].ToString().Trim();

        csItem = new Telerik.Charting.ChartSeriesItem();
        csItem.Name = poDt.Rows[iRow]["Name"].ToString();
        csItem.Label.TextBlock.Text = poDt.Rows[iRow]["Value"].ToString();//+ "(" + poDt.Rows[iRow]["Percentage"].ToString() + "%)";
        RadChart1.PlotArea.XAxis.Appearance.TextAppearance.AutoTextWrap = Telerik.Charting.Styles.AutoTextWrap.True;

        csItem.YValue = Int32.Parse(poDt.Rows[iRow]["Value"].ToString());

        RadChart1.PlotArea.XAxis.AutoShrink = false;
        chartseries.AddItem(csItem);
        RadChart1.Series.Add(chartseries);
      }


        RadChart1.PlotArea.XAxis.AutoShrink = true;

        RadChart1.PlotArea.Appearance.Border.Visible = false;
        RadChart1.Appearance.Border.Visible = false;
        RadChart1.PlotArea.YAxis.IsLogarithmic = true;
        RadChart1.PlotArea.YAxis.AutoScale = true;
        RadChart1.PlotArea.YAxis.Appearance.ValueFormat = Telerik.Charting.Styles.ChartValueFormat.Number;
        RadChart1.Appearance.BarWidthPercent = 50;

        RadChart1.Chart.Appearance.FillStyle.MainColor = System.Drawing.Color.Red;
        RadChart1.Chart.Appearance.FillStyle.MainColor = System.Drawing.Color.Transparent;
        RadChart1.Legend.Appearance.FillStyle.MainColor = System.Drawing.Color.Transparent;

        }
        catch (Exception Ex)
        {
           //throw;
        }
        finally
        {
          poDt.Clear();
          poDt = null;
          chartseries = null;
        }
   }

1 个答案:

答案 0 :(得分:0)

你好,我得到了这个问题的答案我使用了itextsharp并且工作正常

void ExportToPdf()   {

       DataTable poDt = new DataTable();
       InitializeGraph(poDt);

       RadChart1.Save(Server.MapPath("~/images/Chart.png"), System.Drawing.Imaging.ImageFormat.Png);
        Document document = new Document(PageSize.LETTER, 72, 72, 82, 72);
        MemoryStream msReport = new MemoryStream();

        try
        {

            PdfWriter writer = PdfWriter.GetInstance(document, msReport);
            document.AddAuthor("Test");
            document.AddSubject("Export to PDF");
            document.Open();
            Chunk c = new Chunk("Export chart to PDF", FontFactory.GetFont("VERDANA", 15));
            Paragraph p = new Paragraph();
            p.Alignment = Element.ALIGN_CENTER;
            iTextSharp.text.Image hImage;
            hImage = iTextSharp.text.Image.GetInstance(MapPath("~/images/Chart.png"));

            float NewWidth = 500;
            float MaxHeight = 400;

            if (hImage.Width <= NewWidth) { NewWidth = hImage.Width; } float NewHeight = hImage.Height * NewWidth / hImage.Width; if (NewHeight > MaxHeight)
            {
                NewWidth = hImage.Width * MaxHeight / hImage.Height;
                NewHeight = MaxHeight;
            }

            float ratio = hImage.Width / hImage.Height;
            hImage.ScaleAbsolute(NewWidth, NewHeight);
            document.Add(p);
            document.Add(hImage);
            document.Close();

            Response.AddHeader("Content-type", "application/pdf");
            Response.AddHeader("Content-Disposition", "attachment; filename=chart.pdf");
            Response.OutputStream.Write(msReport.GetBuffer(), 0, msReport.GetBuffer().Length);

        }
        catch (System.Threading.ThreadAbortException ex)
        {
            throw new Exception("Error occured: " + ex);
        }

    }

protected void Export_To_Pdf_Click(object sender,EventArgs e)         {             ExportToPdf();

    }