Aspose.Chart and Aspose.Pdf
Aspose.Chart is a specialized product by Aspose to produce professional charts of many types. If developers need to add Chart objects from Aspose.Chart to their PDF documents, it is possible. Currently, the Chart object cannot be added directly to the Document Object Model of Aspose.Pdf but there is another way to do so which is explained in next section.
How to Add Chart in PDF?
Developers need to understand the steps below to perform this task:
After performing the steps above, you are done and the Chart will be added to Aspose.Pdf as an Image .
Code Snippet
[C#]
Chart chart = new Chart();
chart.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
Series s = new Series();
s.ChartType = ChartType.Bar;
s.DataPoints.Add(new DataPoint(2, -2));
s.DataPoints.Add(new DataPoint(4, 7));
s.DataPoints.Add(new DataPoint(6, -9));
chart.SeriesCollection.Add(s);
System.IO.MemoryStream mstream = new System.IO.MemoryStream();
chart.Save(mstream,System.Drawing.Imaging.ImageFormat.Png);
Pdf pdf = new Pdf();
Section sec = pdf.Sections.Add();
Image img = new Image();
img.ImageInfo.ImageFileType = ImageFileType.Png;
img.ImageInfo.ImageStream = mstream;
sec.Paragraphs.Add(img);
pdf.Save("Chart.pdf");
[VB.NET]
'Create a chart object using Aspose.Chart and set its smoothing mode to
'high quality
Dim chart As Chart = New Chart()
chart.SmoothingMode = SmoothingMode.HighQuality
'Set the chart as bart chart and plot the data points on the chart using Series
Dim s As Series = New Series()
s.ChartType = ChartType.Bar
s.DataPoints.Add(new DataPoint(2, -2))
s.DataPoints.Add(new DataPoint(4, 7))
s.DataPoints.Add(new DataPoint(6, -9))
chart.SeriesCollection.Add(s)
'Save the chart as Bitmap image
Dim bm As Bitmap = chart.Save()
'Create a MemoryStream object and save th Bitmap image to it
Dim mstream As System.IO.MemoryStream = New System.IO.MemoryStream()
bm.Save(mstream, System.Drawing.Imaging.ImageFormat.Png)
Dim pdf1 As Pdf = New Pdf()
Dim sec1 As Section = pdf1.Sections.Add()
Dim img As Image = new Image(sec1)
image1.ImageInfo.ImageFileType = ImageFileType.Png
image1.ImageInfo.ImageStream = msstream
sec1.Paragraphs.Add(image1)
pdf1.Save("Chart.pdf");