Creating a Bar Chart PDF Report in C#

Osama HaiDer
3 min readSep 30, 2024

--

This blog will teach us how to create a simple bar chart image and save it in a PDF report using C#. We will use two libraries: System.Drawing to make the chart image and PdfSharp to generate the PDF file.

What You Need

  • C# Development Environment: You can use Visual Studio or any other C# IDE.
  • PdfSharp Library: Make sure to install the PdfSharp library in your project. You can add it via NuGet Package Manager.

Step 1: Setting Up Your Project

First, create a new C# console application. You will need to include the necessary libraries at the top of your code file:

using System.Drawing;
using PdfSharp.Pdf;
using PdfSharp.Drawing;
using static System.Console;

Step 2: Creating the Bar Chart Image

We will start by creating a method to generate the bar chart image. This method will:

  1. Create a blank canvas (bitmap) for drawing.
  2. Draw the axes of the chart.
  3. Add example data for the chart.
  4. Draw the bars based on this data.
  5. Save the image as a PNG file.

Here’s the code for creating the chart:

static void CreateSampleChartImage(string imagePath)
{
const int width = 600;
const int height = 300;
using var bmp = new Bitmap(width, height);
using var g = Graphics.FromImage(bmp);
g.Clear(Color.White);

// Draw the axes
g.DrawLine(Pens.Black, 50, height - 50, 50, 50); // Y-axis
g.DrawLine(Pens.Black, 50, height - 50, width - 50, height - 50); // X-axis

// Example data for the chart
string[] categories = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
int[] values = { 10, 20, 30, 40, 30, 50, 20, 40, 10, 30, 20, 50 };

for (var i = 0; i < categories.Length; i++)
{
var x = 60 + i * 40; // Position of the bar
var y = height - 50 - values[i] * 4; // Height of the bar
g.FillRectangle(Brushes.Blue, x, y, 30, values[i] * 4); // Draw the bar
g.DrawString(categories[i], new Font("Arial", 8), Brushes.Black, x, height - 40); // Label
}

// Save the chart as PNG
bmp.Save(imagePath);
}

Step 3: Converting the Image to PDF

Next, we will create a method to convert the image we created into a PDF. This method will:

  1. Create a new PDF document.
  2. Add a page to the document.
  3. Insert the bar chart image into the PDF.
  4. Save the PDF file.

Here’s the code for converting the image to a PDF:

static void ConvertImageToPdf(string imagePath, string pdfPath)
{
using PdfDocument pdf = new PdfDocument();
var page = pdf.AddPage();
var gfx = XGraphics.FromPdfPage(page);
var image = XImage.FromFile(imagePath);

// Center the image on the page
double xPos = (page.Width - image.PixelWidth) / 2;
double yPos = (page.Height - image.PixelHeight) / 2;

gfx.DrawImage(image, xPos, yPos, image.PixelWidth, image.PixelHeight);
pdf.Save(pdfPath);
}

Step 4: Putting It All Together

Now we can combine everything in the Main method to create the chart and save it as a PDF:

private static void Main(string[] args)
{
var chartImagePath = "BarChart.png";
CreateSampleChartImage(chartImagePath);

var pdfPath = "BarChartReport.pdf";
ConvertImageToPdf(chartImagePath, pdfPath);

WriteLine($"PDF generated at: {Path.GetFullPath(pdfPath)}");
}

Conclusion

With just a few lines of code, you can create a bar chart image and save it in a PDF file using C#. This is a great way to visualize data and generate reports. Try experimenting with different data sets and chart styles to customize your reports!

--

--

Osama HaiDer
Osama HaiDer

Written by Osama HaiDer

SSE at TEO International | .Net | Azure | AWS | Web APIs | C#

No responses yet