In a number of PDF documents, graphs and charts are used to visually represent the data or information. In some cases, charts 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 programmatically in C#. We will explicitly demonstrate how to create different graphical objects in a PDF file.
- .NET API to Create Graphs and Charts in PDF
- Create an Arc in PDF
- Create a Circle in PDF
- Add a Curve in PDF
- Add a Line in PDF
- Create a Rectangle in PDF
- Create an Ellipse in PDF
C# .NET API to Create Graphs and Charts in PDF
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#.
- Create a new PDF or load an existing one using the Document class.
- Create a Graph object with dimensions.
- Set BorderInfo for graph object.
- Create an instance of Arc class and set its dimensions and other parameters.
- Add arc to the shapes collection of Graph using Graph.Shapes.Add(Arc) method.
- Add graph to the page using Page.Paragraphs.Add(Graph) method.
- 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 a Circle in a PDF in C#
The following are the steps to add a circle to a PDF file in C#.
- Create a new PDF or load an existing one using Document class.
- Create a Graph object with dimensions.
- Set BorderInfo for graph object.
- Create an instance of Circle class and set its dimensions.
- Add circle to the shapes collection of Graph using Graph.Shapes.Add(Circle) method.
- Add graph to the page using Page.Paragraphs.Add(Graph) method.
- 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.
Add a Curve in a PDF in C#
The following are the steps to add a curve in a PDF file in C#.
- Create a new PDF or load an existing one using Document class.
- Create a Graph object with dimensions.
- Set BorderInfo for graph object.
- Create an instance of Curve class and set its dimensions.
- Add curve to the shapes collection of Graph using Graph.Shapes.Add(Curve) method.
- Add graph to the page using Page.Paragraphs.Add(Graph) method.
- 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 Line in a PDF in C#
The following are the steps to add a line in a PDF using C#.
- Create a new PDF or load an existing one using Document class.
- Create a Graph object with dimensions.
- Set BorderInfo for graph object.
- Create an instance of Line class and set its dimensions.
- Set other properties of line such as style, width, etc.
- Add line to the shapes collection of Graph using Graph.Shapes.Add(Line) method.
- Add graph to the page using Page.Paragraphs.Add(Graph) method.
- 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.
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#.
- Create a new PDF or load an existing one using Document class.
- Create a Graph object with dimensions.
- Set BorderInfo for graph object.
- Create an instance of Rectangle class and set its dimensions.
- Set other properties of the rectangle such as fill color, gradient, etc.
- Add rectangle to the shapes collection of Graph using Graph.Shapes.Add(Rectangle) method.
- Add graph to the page using Page.Paragraphs.Add(Graph) method.
- 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 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#.
- Create a new PDF or load an existing one using Document class.
- Create a Graph object with dimensions.
- Set BorderInfo for graph object.
- Create an instance of Ellipse class and set its dimensions.
- Set other properties of ellipse such as fill color, etc.
- Add ellipse to the shapes collection of Graph using Graph.Shapes.Add(Ellipse) method.
- Add graph to the page using Page.Paragraphs.Add(Graph) method.
- 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.
Get a Free License
You can get a free temporary license in order to use Aspose.PDF for .NET without evaluation limitations.
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. Furthermore, you can explore more about .NET PDF API using the documentation. In case you would have any questions or queries, you can contact us via our forum.