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

Line Chart can be added to PDF using Chart and line series.

How to add Line Chart to PDF in C#

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

Over 75 Page Elements

DynamicPDF Generator contains over 75 page elements so you can add rich content to the PDFs you create:

  • Images
  • Text Objects (TextArea and Label)
  • HTML
  • Barcodes (47 types)
  • Form Fields
  • Charts

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!