Examples

Adding Charts to PDF (.NET Core/Framework)

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

How to add Area Chart to PDF in C#

Area Chart can be added to PDF using Chart and Area series of DynamicPDF Generator product. Below are the steps and sample code to add an Area Chart to PDF document using the Generator product.

Steps for adding Area Chart to a PDF Document

  1. Create a Document object.
  2. Create a Page object and add it to the Document.
  3. Create a Chart object by specifying the parameters.
  4. Create a PlotArea object and associate with Chart.
  5. Create a Title object and associate with Chart.
  6. Create a IndexedAreaSeries object and values.
  7. Create a AutoGradient object and assign to Series.
  8. Add IndexedAreaSeries to the PlotArea.
  9. Add Title for Y axis.
  10. Add AxisLables to X axis.
  11. Add Chart to the Page.
  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(@"C:\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 Generator product. Below are the steps and sample code to add an Bar Chart to PDF document using the Generator product.

Steps for adding Bar Chart to a PDF Document

  1. Create a Document object.
  2. Create a Page object and add it to the Document.
  3. Create a Chart object by specifying the parameters.
  4. Create a PlotArea object and associate with Chart.
  5. Create a Title object and associate with Chart.
  6. Create a IndexedBarSeries object and values.
  7. Create a AutoGradient object and assign to Series.
  8. Add IndexedBarSeries to the PlotArea.
  9. Add Title for X axis.
  10. Add AxisLables to Y axis.
  11. Add Chart to the Page.
  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(@"C:\Output.pdf");        

How to add Column Chart to PDF in C#

Column Chart can be added to PDF using Chart element and column series. Below are the steps and sample code to add an Column Chart to PDF document using the Generator product.

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.
  3. Create a Chart object by specifying the parameters.
  4. Create a PlotArea object and associate with Chart.
  5. Create a Title object and associate with Chart.
  6. Create a IndexedColumnSeries object and values.
  7. Create a AutoGradient object and assign to Series.
  8. Add IndexedColumnSeries to the PlotArea.
  9. Add Title for Y axis.
  10. Add AxisLables to X axis.
  11. Add Chart to the Page.
  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(@"C:\Output.pdf");        

How to add Line Chart to PDF in C#

Line Chart can be added to PDF using Chart and line series. Below are the steps and sample code to add an Line Chart to PDF document using the Generator product.

Steps for adding Line Chart to a PDF Document

  1. Create a Document object.
  2. Create a Page object and add it to the Document.
  3. Create a Chart object by specifying the parameters.
  4. Create a PlotArea object and associate with Chart.
  5. Create a Title object and associate with Chart.
  6. Create a IndexedLineSeries object and values.
  7. Create a AutoGradient object and assign to Series.
  8. Add IndexedLineSeries to the PlotArea.
  9. Add Title for Y axis.
  10. Add AxisLables to X axis.
  11. Add Chart to the Page.
  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(@"C:\Output.pdf");        

How to add Pie Chart to PDF in C#

Pie Chart can be added to PDF using Chart and PieSeries. Below are the steps and sample code to add an Pie Chart to PDF document using the Generator product.

Steps for adding Pie Chart to a PDF Document

  1. Create a Document object.
  2. Create a Page object and add it to the Document.
  3. Create a Chart object by specifying the parameters.
  4. Create a PlotArea object and associate with Chart.
  5. Create a Title object and associate with Chart.
  6. Create a AutoGradient object and assign to Series.
  7. Create a ScalarDataLabel object.
  8. Create a PieSeries object and assign the Data label, values and color.
  9. Add Chart to the Page.
  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(@"C:\Output.pdf");        

How to add XYScatter Chart to PDF in C#

XYScatter Chart can be added to PDF using the Chart and XYScatterSeries of DynamicPDF Generator. Below are the steps and sample code to add an XYScatter Chart to PDF document using the Generator product.

Steps for adding XYScatter Chart to a PDF Document

  1. Create a Document object.
  2. Create a Page object and add it to the Document.
  3. Create a Chart object by specifying the parameters.
  4. Create a PlotArea object and associate with Chart.
  5. Create a Title object and associate with Chart.
  6. Create a XYScatterSeries object and assign to values.
  7. Add Series to PlotArea.
  8. Add Title to X and Y axis.
  9. Add Chart to the Page.
  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(@"C:\Output.pdf");        

Getting Started

NuGet Package

DynamicPDF Generator is available on NuGet and is part of the ceTe.DynamicPDF.CoreSuite.NET package. The Core Suite package includes Generator, Merger and ReportWriter.

NuGet Package ID: ceTe.DynamicPDF.CoreSuite.NET

DynamicPDF Generator Information

More information can be found here:

Available in other platforms

DynamicPDF Generator is available for the following 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 18 Years)
  • Free Evaluation
  • .NET Core Support (Most Products)
  • Flexible Licensing

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