Creating a Bar Chart PDF Report in C#
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:
- Create a blank canvas (bitmap) for drawing.
- Draw the axes of the chart.
- Add example data for the chart.
- Draw the bars based on this data.
- 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:
- Create a new PDF document.
- Add a page to the document.
- Insert the bar chart image into the PDF.
- 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!