Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active May 25, 2022 07:58
Create Graphs and Charts in PDF in C# .NET
// Create Document instance
Document pdfDocument = new Document();
// Add page to pages collection of PDF file
var page = pdfDocument.Pages.Add();
// Create Graph object with certain dimensions
Graph graph = new Graph(400, 400);
// Set border for Drawing object
BorderInfo borderInfo = new BorderInfo(BorderSide.All, Color.Green);
graph.Border = borderInfo;
// Create an arc
Arc arc1 = new Arc(100, 100, 95, 0, 90);
arc1.GraphInfo.Color = Color.GreenYellow;
graph.Shapes.Add(arc1);
Graph graph2 = new Graph(400, 400);
// Create a filled arc
Arc arc = new Arc(100, 100, 95, 0, 90);
arc.GraphInfo.FillColor=Color.GreenYellow;
graph2.Shapes.Add(arc);
Line line = new Line(new float[] { 195, 100, 100, 100, 100, 195 });
line.GraphInfo.FillColor=Color.GreenYellow;
graph2.Shapes.Add(line);
// Add graph object to paragraphs collection of page
page.Paragraphs.Add(graph);
page.Paragraphs.Add(graph2);
// Save PDF file
pdfDocument.Save("create-arc.pdf");
// Create Document instance
Document pdfDocument = new Document();
// Add page to pages collection of PDF file
var page = pdfDocument.Pages.Add();
// Create Graph object with certain dimensions
Graph graph = new Graph(400, 400);
// Set border for Drawing object
BorderInfo borderInfo = new BorderInfo(BorderSide.All, Color.Green);
graph.Border = borderInfo;
// Create circle
Circle circle = new Circle(100, 100, 40);
circle.GraphInfo.Color = Color.GreenYellow;
circle.GraphInfo.FillColor=Color.GreenYellow;
graph.Shapes.Add(circle);
// Add graph object to paragraphs collection of page
page.Paragraphs.Add(graph);
// Save PDF file
pdfDocument.Save("create-circle.pdf");
// Create Document instance
Document pdfDocument = new Document();
// Add page to pages collection of PDF file
var page = pdfDocument.Pages.Add();
// Create Graph object with certain dimensions
Graph graph = new Graph(400, 400);
// Set border for Drawing object
BorderInfo borderInfo = new BorderInfo(BorderSide.All, Color.Green);
graph.Border = borderInfo;
// Create a curve
Curve curve1 = new Curve(new float[] { 10, 10, 50, 60, 70, 10, 100, 120 });
curve1.GraphInfo.Color = Color.GreenYellow;
graph.Shapes.Add(curve1);
// Create Drawing object with certain dimensions
Graph graph2 = new Graph(400, 200);
// Create filled curve
Curve curve2 = new Curve(new float[] { 50, 30, 200, 60, 70, 10, 100, 120 });
curve2.GraphInfo.FillColor = Color.GreenYellow;
graph2.Shapes.Add(curve2);
// Add graph object to paragraphs collection of page
page.Paragraphs.Add(graph);
page.Paragraphs.Add(graph2);
// Save PDF file
pdfDocument.Save("create-curve.pdf");
// Create Document instance
Document pdfDocument = new Document();
// Add page to pages collection of PDF file
var page = pdfDocument.Pages.Add();
// Create Graph object with certain dimensions
Graph graph = new Graph(400, 400);
// Set border for Drawing object
BorderInfo borderInfo = new BorderInfo(BorderSide.All, Color.Green);
graph.Border = borderInfo;
// Create ellipse
Ellipse ellipse1 = new Ellipse(100, 100, 120, 60);
ellipse1.GraphInfo.Color = Color.GreenYellow;
ellipse1.Text = new TextFragment("Ellipse");
graph.Shapes.Add(ellipse1);
// Create filled ellipse
Ellipse ellipse2 = new Ellipse(200, 100, 120, 180);
ellipse2.GraphInfo.FillColor = Color.GreenYellow;
graph.Shapes.Add(ellipse2);
// Add graph object to paragraphs collection of page
page.Paragraphs.Add(graph);
// Save PDF file
pdfDocument.Save("create-ellipse.pdf");
// Create Document instance
Document pdfDocument = new Document();
// Add page to pages collection of PDF file
var page = pdfDocument.Pages.Add();
// Create Graph object with certain dimensions
Graph graph = new Graph(400, 400);
// Set border for Drawing object
BorderInfo borderInfo = new BorderInfo(BorderSide.All, Color.Green);
graph.Border = borderInfo;
// Create Line instance
Line line = new Line(new float[] { 300, 200, 200, 100 });
line.GraphInfo.LineWidth = 5;
// Add rectangle object to shapes collection of Graph object
graph.Shapes.Add(line);
// Create dashed line
Line line2 = new Line(new float[] { 100, 100, 200, 100 });
// Set color for Line object
line2.GraphInfo.Color = Color.Red;
// Set line width
line2.GraphInfo.LineWidth = 5;
// Specify dash array for line object
line2.GraphInfo.DashArray=new int[] { 0, 1, 0 };
// Set the dash phase for Line instance
line2.GraphInfo.DashPhase = 1;
// Add line to shapes collection of drawing object
graph.Shapes.Add(line2);
// Add graph object to paragraphs collection of page
page.Paragraphs.Add(graph);
// Save PDF file
pdfDocument.Save("create-line.pdf");
// Create Document instance
Document pdfDocument = new Document();
// Add page to pages collection of PDF file
var page = pdfDocument.Pages.Add();
// Create Graph object with certain dimensions
Graph graph = new Graph(400, 400);
// Set border for Drawing object
BorderInfo borderInfo = new BorderInfo(BorderSide.All, Color.Green);
graph.Border = borderInfo;
Aspose.Pdf.Drawing.Rectangle rect = new Aspose.Pdf.Drawing.Rectangle(300, 100, 200, 120);
// Specify fill color for Graph object
rect.GraphInfo.FillColor = Color.Red;
// Add rectangle object to shapes collection of Graph object
graph.Shapes.Add(rect);
// Fill rectangle with gradient
Aspose.Pdf.Drawing.Rectangle rect2 = new Aspose.Pdf.Drawing.Rectangle(0, 0, 300, 300);
graph.Shapes.Add(rect2);
// Specify Gradient fill color for Graph object and fill
Color gradientFill = new Color();
rect2.GraphInfo.FillColor = gradientFill;
// Set gradient
GradientAxialShading gradientAxialShading = new GradientAxialShading(Color.Red, Color.Blue);
gradientAxialShading.Start = new Point(0, 0);
gradientAxialShading.End = new Point(300, 300);
gradientFill.PatternColorSpace = gradientAxialShading;
// Add graph object to paragraphs collection of page
page.Paragraphs.Add(graph);
// Save PDF file
pdfDocument.Save("create-rectangle.pdf");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment