Add Bar Chart to PDF in C# (.NET Core)

Bar Chart can be added to PDF using Chart and Bar series of DynamicPDF Generator product.

How to add Bar Chart to PDF in C#

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");        

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 Logo
NuGet Package ID: ceTe.DynamicPDF.CoreSuite.NET

Example Project

An example project showing this functionality can be cloned or viewed from GitHub:

GitHub Logo
Clone or View Example Project on GitHub

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!