0

我正在使用此代码获取此图表,并且我想将此图表导出为 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;
        }
   }
4

1 回答 1

0

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

无效的 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();

    }
于 2013-03-21T09:39:57.740 回答