Examples

Adding Charts to PDF

Different types of charts can be added to the PDF document including an Area chart, Bar chart, Column chart, Line chart, Pie chart, and XYScatter chart.

How to Add Area Chart to PDF in C#

Area Chart can be added to PDF using Chart and Area series of DynamicPDF Core Suite. The following steps and sample code illustrates adding an Area Chart to a PDF document using DynamicPDF Core Suite.

Steps for Adding an Area Chart to a PDF Document

  1. Create a Document object.
  2. Create a Page object and add it to the Document instance.
  3. Create a Chart object by specifying its parameters in the constructor.
  4. Create a PlotArea object and associate it with the Chart instance.
  5. Create a Title object and associate it with the Chart instance.
  6. Create a IndexedAreaSeries object and values.
  7. Create a AutoGradient object and assign it to a Series.
  8. Add IndexedAreaSeries to the PlotArea.
  9. Add a Title for Y axis.
  10. Add AxisLables to X axis.
  11. Add the chart instance to the Page instance.
  12. Save the PDF document.

Sample Code - C#

Document document = new Document();     
Page page = new Page();
document.Pages.Add(page);

Chart chart = new Chart(0, 0, 400, 230);
PlotArea plotArea = chart.PrimaryPlotArea;

Title title1 = new Title("Website Visitors");
Title title2 = new Title("Year - 2007");
chart.HeaderTitles.Add(title1);
chart.HeaderTitles.Add(title2);                       

IndexedAreaSeries areaSeries1 = new IndexedAreaSeries("Website A");
areaSeries1.Values.Add(new float[]{5, 7, 9, 6});

AutoGradient autogradient1 = new AutoGradient(90f, CmykColor.Red, CmykColor.IndianRed);
areaSeries1.Color = autogradient1;

plotArea.Series.Add(areaSeries1);

Title lTitle = new Title("Visitors (in millions)");
areaSeries1.YAxis.Titles.Add(lTitle);  

areaSeries1.XAxis.Labels.Add(new IndexedXAxisLabel("Q1", 0));
areaSeries1.XAxis.Labels.Add(new IndexedXAxisLabel("Q2", 1));
areaSeries1.XAxis.Labels.Add(new IndexedXAxisLabel("Q3", 2));
areaSeries1.XAxis.Labels.Add(new IndexedXAxisLabel("Q4", 3));

page.Elements.Add(chart);    
document.Draw(@"Output.pdf");        

How to Add Bar Chart to PDF in C#

Bar Chart can be added to PDF using Chart and Bar series of DynamicPDF Core Suitet. The following steps and sample code illustrates adding a Bar Chart to a PDF document using DynamicPDF Core Suite.

Steps for Adding a Bar Chart to a PDF Document

  1. Create a Document object.
  2. Create a Page object and add it to the Document object.
  3. Create a Chart object by specifying its parameters in the constructor.
  4. Create a PlotArea object and associate it with the Chart instance.
  5. Create a Title object and associate it with the Chart instance.
  6. Create a IndexedBarSeries object and values.
  7. Create a AutoGradient object and assign it to a Series.
  8. Add an IndexedBarSeries to the PlotArea instance.
  9. Add a Title for X axis.
  10. Add AxisLables to Y axis.
  11. Add the Chart instance to the Page instance.
  12. Save the PDF document.

Sample Code - C#

Document document = new Document();
Page page = new Page();
document.Pages.Add(page);

Chart chart = new Chart(0, 0, 400, 230);
PlotArea plotArea = chart.PrimaryPlotArea;

Title title1 = new Title("Website Visitors");
Title title2 = new Title("Year 2007");
chart.HeaderTitles.Add(title1);
chart.HeaderTitles.Add(title2);

IndexedBarSeries barSeries1 = new IndexedBarSeries("Website A");
barSeries1.Values.Add(new float[] { 5, 7, 9, 6 });
IndexedBarSeries barSeries2 = new IndexedBarSeries("Website B");
barSeries2.Values.Add(new float[] { 4, 2, 5, 8 });
IndexedBarSeries barSeries3 = new IndexedBarSeries("Website C");
barSeries3.Values.Add(new float[] { 2, 4, 6, 9 });

AutoGradient autogradient1 = new AutoGradient(180f, CmykColor.Red, CmykColor.IndianRed);
barSeries1.Color = autogradient1;
AutoGradient autogradient2 = new AutoGradient(180f, CmykColor.Green, CmykColor.YellowGreen);
barSeries2.Color = autogradient2;
AutoGradient autogradient3 = new AutoGradient(180f, CmykColor.Blue, CmykColor.LightBlue);
barSeries3.Color = autogradient3;

plotArea.Series.Add(barSeries1);
plotArea.Series.Add(barSeries2);
plotArea.Series.Add(barSeries3);

Title lTitle = new Title("Visitors (in millions)");
barSeries1.XAxis.Titles.Add(lTitle);

barSeries1.YAxis.Labels.Add(new IndexedYAxisLabel("Q1", 0));
barSeries1.YAxis.Labels.Add(new IndexedYAxisLabel("Q2", 1));
barSeries1.YAxis.Labels.Add(new IndexedYAxisLabel("Q3", 2));
barSeries1.YAxis.Labels.Add(new IndexedYAxisLabel("Q4", 3));

page.Elements.Add(chart);
document.Draw(@"Output.pdf");        

How to Add Column Chart to PDF in C#

A Column Chart can be added to a PDF using Chart element and column series. The following steps and sample code illustrate adding a Column Chart to a PDF document using DynamicPDF Core Suite.

Steps for Adding Column Chart to a PDF Document

  1. Create a Document object.
  2. Create a Page object and add it to the Document instance
  3. Create a Chart object by specifying its parameters.
  4. Create a PlotArea object and associate it with the Chart object.
  5. Create a Title object and associate it with the Chart instance.
  6. Create a IndexedColumnSeries object and values.
  7. Create a AutoGradient object and assign it to a Series.
  8. Add IndexedColumnSeries to the PlotArea.
  9. Add a Title for Y axis.
  10. Add AxisLables to X axis.
  11. Add the Chart instance to the Page instance.
  12. Save the PDF document.

Sample Code - C#

Document document = new Document();
Page page = new Page();
document.Pages.Add(page);

Chart chart = new Chart(0, 0, 400, 230);
PlotArea plotArea = chart.PrimaryPlotArea;

Title title1 = new Title("Website Visitors");
Title title2 = new Title("Year - 2007");
chart.HeaderTitles.Add(title1);
chart.HeaderTitles.Add(title2);

IndexedColumnSeries columnSeries1 = new IndexedColumnSeries("Website A");
columnSeries1.Values.Add(new float[] { 5, 7, 9, 6 });
IndexedColumnSeries columnSeries2 = new IndexedColumnSeries("Website B");
columnSeries2.Values.Add(new float[] { 4, 2, 5, 8 });
IndexedColumnSeries columnSeries3 = new IndexedColumnSeries("Website C");
columnSeries3.Values.Add(new float[] { 2, 4, 6, 9 });

AutoGradient autogradient1 = new AutoGradient(180f, CmykColor.Red, CmykColor.IndianRed);
columnSeries1.Color = autogradient1;
AutoGradient autogradient2 = new AutoGradient(180f, CmykColor.Green, CmykColor.YellowGreen);
columnSeries2.Color = autogradient2;
AutoGradient autogradient3 = new AutoGradient(180f, CmykColor.Blue, CmykColor.LightBlue);
columnSeries3.Color = autogradient3;

plotArea.Series.Add(columnSeries1);
plotArea.Series.Add(columnSeries2);
plotArea.Series.Add(columnSeries3);

Title lTitle = new Title("Visitors (in millions)");
columnSeries1.YAxis.Titles.Add(lTitle);

columnSeries1.XAxis.Labels.Add(new IndexedXAxisLabel("Q1", 0));
columnSeries1.XAxis.Labels.Add(new IndexedXAxisLabel("Q2", 1));
columnSeries1.XAxis.Labels.Add(new IndexedXAxisLabel("Q3", 2));
columnSeries1.XAxis.Labels.Add(new IndexedXAxisLabel("Q4", 3));

page.Elements.Add(chart);
document.Draw(@"Output.pdf");        

How to Add Line Chart to PDF in C#

Line Chart can be added to PDF using Chart and line series. The following steps and sample code add a Line Chart to PDF document using DynamicPDF Core Suite.

Steps for Adding a Line Chart to a PDF Document

  1. Create a Document object.
  2. Create a Page object and add it to the Document instance.
  3. Create a Chart object by specifying its parameters.
  4. Create a PlotArea object and associate with the Chart instance.
  5. Create a Title object and associate with the Chart instance.
  6. Create a IndexedLineSeries object and values.
  7. Create a AutoGradient object and assign it to a Series.
  8. Add an IndexedLineSeries to the PlotArea.
  9. Add Title for Y axis.
  10. Add AxisLables to X axis.
  11. Add the Chart instance to the Page instance.
  12. Save the PDF document.

Sample Code - C#

// Create a PDF Document
Document document = new Document();
// Create a Page and add it to the document
Page page = new Page();
document.Pages.Add(page);

// Create a chart
Chart chart = new Chart(0, 0, 400, 230);
// Create a plot area
PlotArea plotArea = chart.PrimaryPlotArea;

// Create header titles and add it to the chart
Title title1 = new Title("Website Visitors");
Title title2 = new Title("Year - 2007");
chart.HeaderTitles.Add(title1);
chart.HeaderTitles.Add(title2);

// Create a indexed line series and add values to it
IndexedLineSeries lineSeries1 = new IndexedLineSeries("Website A");
lineSeries1.Values.Add(new float[] { 5, 7, 9, 6 });
IndexedLineSeries lineSeries2 = new IndexedLineSeries("Website B");
lineSeries2.Values.Add(new float[] { 4, 2, 5, 8 });
IndexedLineSeries lineSeries3 = new IndexedLineSeries("Website C");
lineSeries3.Values.Add(new float[] { 2, 4, 6, 9 });

// Add indexed line series to the plot area
plotArea.Series.Add(lineSeries1);
plotArea.Series.Add(lineSeries2);
plotArea.Series.Add(lineSeries3);

// Create a title and add it to the yaxis
Title lTitle = new Title("Visitors (in millions)");
lineSeries1.YAxis.Titles.Add(lTitle);

//Adding AxisLabels to the XAxis
lineSeries1.XAxis.Labels.Add(new IndexedXAxisLabel("Q1", 0));
lineSeries1.XAxis.Labels.Add(new IndexedXAxisLabel("Q2", 1));
lineSeries1.XAxis.Labels.Add(new IndexedXAxisLabel("Q3", 2));
lineSeries1.XAxis.Labels.Add(new IndexedXAxisLabel("Q4", 3));

// Add the chart to the page
page.Elements.Add(chart);
// Save the PDF
document.Draw(@"Output.pdf");        

How to Add Pie Chart to PDF in C#

Pie Chart can be added to PDF using Chart and PieSeries. The following steps and sample code illustrate adding a Pie Chart to a PDF document using DynamicPDF Core Suite.

Steps for Adding a Pie Chart to a PDF Document

  1. Create a Document object.
  2. Create a Page object and add it to the Document instance.
  3. Create a Chart object by specifying its parameters.
  4. Create a PlotArea object and associate it with the Chart object instance.
  5. Create a Title object and associate it with the Chart object instance.
  6. Create a AutoGradient object and assign it to a Series.
  7. Create a ScalarDataLabel object.
  8. Create a PieSeries object and assign the data label, values and color.
  9. Add the Chart instance to the Page instance.
  10. Save the PDF document.

Sample Code - C#

Document document = new Document();
Page page = new Page(PageSize.A4, PageOrientation.Landscape);
document.Pages.Add(page);

Chart chart = new Chart(0, 0, 700, 400);
PlotArea plotArea = chart.PlotAreas.Add(50, 50, 300, 300);

Title tTitle = new Title("Website Viewers (in millions)");
Title tTitle1 = new Title("Year 2007");
chart.HeaderTitles.Add(tTitle);
chart.HeaderTitles.Add(tTitle1);

AutoGradient autogradient1 = new AutoGradient(90f, CmykColor.Red, CmykColor.IndianRed);
AutoGradient autogradient2 = new AutoGradient(90f, CmykColor.Green, CmykColor.YellowGreen);
AutoGradient autogradient3 = new AutoGradient(90f, CmykColor.Blue, CmykColor.LightBlue);

ScalarDataLabel da = new ScalarDataLabel(true, false, false);
PieSeries pieSeries = new PieSeries();
pieSeries.DataLabel = da;            
plotArea.Series.Add(pieSeries);

pieSeries.Elements.Add(27, "Website A");
pieSeries.Elements.Add(19, "Website B");
pieSeries.Elements.Add(21, "Website C");

pieSeries.Elements[0].Color = autogradient1;
pieSeries.Elements[1].Color = autogradient2;
pieSeries.Elements[2].Color = autogradient3;

page.Elements.Add(chart);
document.Draw(@"Output.pdf");        

How to Add XYScatter Chart to PDF in C#

An XYScatter Chart can be added to a PDF using the Chart and XYScatterSeries of DynamicPDF Core Suite. The following steps and sample code illustrate adding an XYScatter Chart to a PDF document.

Steps for Adding an XYScatter Chart to a PDF Document

  1. Create a Document object.
  2. Create a Page object and add it to the Document instance.
  3. Create a Chart object by specifying its parameters.
  4. Create a PlotArea object and associate it with the Chart object instance.
  5. Create a Title object and associate it with the Chart instance.
  6. Create a XYScatterSeries object and assign its values.
  7. Add the series to PlotArea.
  8. Add Title to the X and Y axis.
  9. Add the Chart instance to the Page instance.
  10. Save the PDF document.

Sample Code - C#

Document document = new Document();
Page page = new Page();
document.Pages.Add(page);

Chart chart = new Chart(0, 0, 450, 200);
PlotArea plotArea = chart.PrimaryPlotArea;

Title tTitle = new Title("Player Height and Weight");
chart.HeaderTitles.Add(tTitle);

XYScatterSeries xyScatterSeries1 = new XYScatterSeries("Team A");
xyScatterSeries1.Values.Add(112, 110);
xyScatterSeries1.Values.Add(125, 120);
xyScatterSeries1.Values.Add(138, 136);
xyScatterSeries1.Values.Add(150, 146);
xyScatterSeries1.Values.Add(172, 164);
XYScatterSeries xyScatterSeries2 = new XYScatterSeries("Team B");
xyScatterSeries2.Values.Add(110, 108);
xyScatterSeries2.Values.Add(128, 124);
xyScatterSeries2.Values.Add(140, 140);
xyScatterSeries2.Values.Add(155, 150);
xyScatterSeries2.Values.Add(170, 160);

plotArea.Series.Add(xyScatterSeries1);
plotArea.Series.Add(xyScatterSeries2);           

Title title1 = new Title("Height (Inches)");
Title title2 = new Title("Weight (Pounds)");
xyScatterSeries1.YAxis.Titles.Add(title1);
xyScatterSeries1.XAxis.Titles.Add(title2);

page.Elements.Add(chart);
document.Draw(@"Output.pdf");        

GitHub Project

Clone or view the example project at GitHub. This examples contained on this page are in the Examples/ChartsExample.cs file.

Clone or View Example Project on GitHub

Getting Started

NuGet Package

DynamicPDF Core Suite is available on NuGet and is part of the ceTe.DynamicPDF.CoreSuite.NET package. The easiest way to install the package is through the Visual Studio Package Manager. You can also download the package directly from NuGet.

NuGet Package ID: ceTe.DynamicPDF.CoreSuite.NET

DynamicPDF Core Suite Information

More information can be found on the DynamicPDF Core Suite webpage.

Available on Other Platforms

DynamicPDF Core Suite is available for the Java and COM/ActiveX platforms. Refer to the respective product pages for more details.

Why Choose DynamicPDF?

  • Transparent Pricing
  • Lots of Features
  • Easy to Use
  • Great Support
  • Efficient Performance
  • Product Maturity (Over 22 Years)
  • Free Evaluation
  • .NET Core Support (Most Products)
  • Flexible Licensing

We’re Not The Only Ones That Think We’re Great!