Create Graphs and Charts in PDF in C#

In several PDF documents, graphs and charts are used to visually represent the data or information. In some cases, charts are used to graphically describe the flow of the activities or operations in a system such as flow charts, whereas, some are used to draw the data patterns. This article covers how to create graphs and charts in PDF files in C# .NET. We will explicitly demonstrate how to create different graphical objects in a PDF file.

C# .NET API to Create Graphs and Charts in PDF - Free Download

We will use Aspose.PDF for .NET to create charts and graphs in PDF files. The API is designed to perform PDF generation and manipulation within the .NET applications. It allows you to create PDF files of simple and complex layouts seamlessly. You can download the API’s binaries or install it using NuGet.

PM> Install-Package Aspose.PDF

Create an Arc in a PDF in C#

The following are the steps to add an arc in PDF in C#.

  1. Create a new PDF or load an existing one using the Document class.
  2. Create a Graph object with dimensions.
  3. Set BorderInfo for graph object.
  4. Create an instance of Arc class and set its dimensions and other parameters.
  5. Add arc to the shapes collection of Graph using Graph.Shapes.Add(Arc) method.
  6. Add graph to the page using Page.Paragraphs.Add(Graph) method.
  7. Save the PDF file using Document.Save(String) method.

The following code sample shows how to add arcs in a PDF in C#.

The following are the arcs that we have generated using the above code sample.

Create an Arc in PDF in C#

C# Create a Circle in a PDF

The following are the steps to add a circle to a PDF file in C#.

  1. Create a new PDF or load an existing one using Document class.
  2. Create a Graph object with dimensions.
  3. Set BorderInfo for graph object.
  4. Create an instance of Circle class and set its dimensions.
  5. Add circle to the shapes collection of Graph using Graph.Shapes.Add(Circle) method.
  6. Add graph to the page using Page.Paragraphs.Add(Graph) method.
  7. Save the PDF file using Document.Save(String) method.

The following code sample shows how to add a circle in PDF in C#.

The following is the circle that we have generated using the above code sample.

Create a Circle in PDF in C#

Add a Curve in a PDF in C#

The following are the steps to add a curve in a PDF file in C#.

  1. Create a new PDF or load an existing one using Document class.
  2. Create a Graph object with dimensions.
  3. Set BorderInfo for graph object.
  4. Create an instance of Curve class and set its dimensions.
  5. Add curve to the shapes collection of Graph using Graph.Shapes.Add(Curve) method.
  6. Add graph to the page using Page.Paragraphs.Add(Graph) method.
  7. Save the PDF file using Document.Save(String) method.

The following code sample shows how to add curves to a PDF file in C#.

The following are the curves that we have generated using the above code sample.

Add a Curve in PDF in C#

C# Add a Line in a PDF

The following are the steps to add a line in a PDF using C#.

  1. Create a new PDF or load an existing one using Document class.
  2. Create a Graph object with dimensions.
  3. Set BorderInfo for graph object.
  4. Create an instance of Line class and set its dimensions.
  5. Set other properties of line such as style, width, etc.
  6. Add line to the shapes collection of Graph using Graph.Shapes.Add(Line) method.
  7. Add graph to the page using Page.Paragraphs.Add(Graph) method.
  8. Save the PDF file using Document.Save(String) method.

The following code sample shows how to add lines in PDF in C#.

The following are the lines that we have created using the above code sample.

Add a Line in PDF in C#

Create a Rectangle in PDF in C#

Similar to other graphical objects, you can add a rectangle to the PDF. Also, you can fill the rectangle with a certain color, control Z-Order, add gradient color, etc. The following are the steps to create a rectangle in a PDF file in C#.

  1. Create a new PDF or load an existing one using Document class.
  2. Create a Graph object with dimensions.
  3. Set BorderInfo for graph object.
  4. Create an instance of Rectangle class and set its dimensions.
  5. Set other properties of the rectangle such as fill color, gradient, etc.
  6. Add a rectangle to the shapes collection of Graph using Graph.Shapes.Add(Rectangle) method.
  7. Add graph to the page using Page.Paragraphs.Add(Graph) method.
  8. Save the PDF file using Document.Save(String) method.

The following code sample shows how to add rectangles in PDF in C#.

The following are the rectangles that we have created using the above code sample.

Create a Rectangle in PDF in C#

Create an Ellipse in PDF in C#

Last but not the least, let’s have a look at how to create an ellipse in PDF in C#.

  1. Create a new PDF or load an existing one using Document class.
  2. Create a Graph object with dimensions.
  3. Set BorderInfo for graph object.
  4. Create an instance of Ellipse class and set its dimensions.
  5. Set other properties of ellipse such as fill color, etc.
  6. Add ellipse to the shapes collection of Graph using Graph.Shapes.Add(Ellipse) method.
  7. Add graph to the page using Page.Paragraphs.Add(Graph) method.
  8. Save the PDF file using Document.Save(String) method.

The following code sample shows how to add an ellipse in a PDF file in C#.

The following are the ellipses that we have created using the above code sample.

Create an Ellipse in PDF in C#

Fee C# Library to Create PDF Charts and Graphs

You can get a free temporary license to create graphs and charts in PDF without evaluation limitations.

Explore C# PDF Library

You can explore more about C# PDF library using the documentation. In case you would have any questions or queries, you can contact us via our forum.

Conclusion

In this article, you have learned how to create different objects of charts and graphs in PDF files using C#. You have seen how to add arcs, circles, rectangles, lines, curves, and ellipses in a PDF. You can easily use the provided code samples in your applications. Just make sure to install the C# PDF library first.

See Also