Add Shapes in PDF Documents using C#

You may need to add various types of graphs or shapes to your PDF files to present data or information interactively. As a C# developer, you can add drawing shapes in PDF documents programmatically. In this article, you will learn how to add shapes in PDF documents using C#.

The following topics are discussed/covered in this article:

  1. C# API to Draw Shapes in PDF Documents
  2. Create Filled Rectangle in PDF Documents using C#
  3. Add Circle in PDF Documents using C#
  4. Draw Lines Across the Page in PDF Documents using C#
  5. Add Ellipse in PDF Documents using C#

C# API to Draw Shapes in PDF Documents

For adding shapes in PDF files, we will be using Aspose.PDF for .NET API. It allows you to generate, modify, convert, render, secure and print supported documents without using Adobe Acrobat. It also provides compression options, table creation & manipulation, graph & image functions, stamp and watermark tasks, extended security controls, and custom font handling.

You can either download the DLL of the API or install it using NuGet.

Install-Package Aspose.PDF

Create Filled Rectangle in PDF Documents using C#

You can create a filled rectangle in PDF documents programmatically by following the steps given below:

  • Firstly, create a new document using the Document class.
  • Now, call the Document.Pages.Add() method to add an empty page to pages collection of the PDF file.
  • Create an instance of the Graph class with height and width for the graph.
  • Then, call the Page.Paragraphs.Add() method to add graph object to paragraphs collection of the page instance.
  • Now, create an instance of the Rectangle class and set its left and bottom position, along with its width and height.
  • Optionally, specify the fill color for the Graph object.
  • Then, add the rectangle object to the shape collection of the Graph object.
  • Finally, call the Document.Save() method with output file path to save the file.

The following code sample shows how to create a filled rectangle in PDF documents using C#.

// Create Document instance
Document doc = new Document();
// Add page to pages collection of PDF file
Page page = doc.Pages.Add();
// Create Graph instance
Graph graph = new Graph(100, 400);
// Add graph object to paragraphs collection of page instance
page.Paragraphs.Add(graph);
// Create Rectangle instance
Aspose.Pdf.Drawing.Rectangle rect = new Aspose.Pdf.Drawing.Rectangle(100, 100, 200, 120);
// Specify fill color for Graph object
rect.GraphInfo.FillColor = Color.Gray;
// Add rectangle object to shapes collection of Graph object
graph.Shapes.Add(rect);
// Save PDF file
doc.Save(@"C:\Files\PDF\FilledRectangle_out.pdf");
Create Filled Rectangle in PDF Documents using C#.

Create Filled Rectangle in PDF Documents using C#.

Add Circle in PDF Documents using C#

You can add a circle in PDF documents programmatically by following the steps given below:

  • Firstly, create a new document using the Document class.
  • Now, call the Document.Pages.Add() method to add an empty page to pages collection of the PDF file.
  • Create an instance of the Graph class with height and width for the graph.
  • Then, call the Page.Paragraphs.Add() method to add graph object to paragraphs collection of the page instance.
  • Now, create an instance of the Circle class and set its X and Y position, along with its radius.
  • Then, set the color and fill color for the circle.
  • Add the circle object to the shape collection of the Graph object.
  • Finally, call the Document.Save() method with output file path to save the file.

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

// Create Document instance
Document doc = new Document();
// Add page to pages collection of PDF file
Page page = doc.Pages.Add();
// Create Drawing object with certain dimensions
Graph graph = new Graph(400, 200);
// create circle
Circle circle = new Circle(100, 100, 40);
circle.GraphInfo.Color = Color.Green;
circle.GraphInfo.FillColor = Color.GreenYellow;
graph.Shapes.Add(circle);
// Add Graph object to paragraphs collection of page
page.Paragraphs.Add(graph);
// Save PDF file
doc.Save(@"C:\Files\PDF\FilledCircle_out.pdf");
Add Circle in PDF Documents using C#.

Add Circle in PDF Documents using C#.

Draw Lines Across the Page in PDF Documents using C#

You can draw lines across the page in PDF documents programmatically by following the steps given below:

  • Firstly, create a new document using the Document class.
  • Now, call the Document.Pages.Add() method to add an empty page to pages collection of the PDF file.
  • Then, set page margins on all sides as 0.
  • Create an instance of the Graph class with page width and page height.
  • Now, create an instance of the Line class with line position array to create a line starting from lower-left to top-right corner of the page.
  • Then, add the line object to the shape collection of the Graph object.
  • Now, create another instance of the Line class with line position array to draw a line from top-left corner of the page to bottom-right corner of the page.
  • Then, add the second instance of the line object to the shape collection of the Graph object.
  • Call the Page.Paragraphs.Add() method to add graph object to paragraphs collection of the page instance.
  • Finally, call the Document.Save() method with output file path to save the file.

The following code sample shows how to draw lines across the page in PDF documents using C#.

// Create Document instance
Document doc = new Document();
// Add page to pages collection of PDF file
Page page = doc.Pages.Add();
// Set page margin on all sides as 0
page.PageInfo.Margin.Left = 0;
page.PageInfo.Margin.Right = 0;
page.PageInfo.Margin.Bottom = 0;
page.PageInfo.Margin.Top = 0;
// Create Graph object with Width and Height equal to page dimensions
Graph graph = new Graph((float)page.PageInfo.Width, (float)page.PageInfo.Height);
// Create first line object starting from Lower-Left to Top-Right corner of page
Line line = new Line(new float[] { (float) page.Rect.LLX, 0, (float) page.PageInfo.Width,
(float) page.Rect.URY });
// Add line to shapes collection of Graph object
graph.Shapes.Add(line);
// Draw line from Top-Left corner of page to Bottom-Right corner of page
Line line2 = new Line(new float[] { 0, (float) page.Rect.URY, (float) page.PageInfo.Width,
(float) page.Rect.LLX });
// Add line to shapes collection of Graph object
graph.Shapes.Add(line2);
// Add Graph object to paragraphs collection of page
page.Paragraphs.Add(graph);
// Save PDF file
doc.Save(@"C:\Files\PDF\DrawLineAcrossPage_out.pdf");
Draw a Line Across the Page in PDF Documents using C#.

Draw a Line Across the Page in PDF Documents using C#.

Add Ellipse in PDF Documents using C#

You can add an ellipse with text inside in PDF documents programmatically by following the steps given below:

  • Firstly, create a new document using the Document class.
  • Now, call the Document.Pages.Add() method to add an empty page to pages collection of the PDF file.
  • Create an instance of the Graph class with height and width for the graph.
  • Create an instance of the TextFragment class with the text value to show inside the graph object.
  • Then, set font and size for the text.
  • Now, create an instance of the Ellipse class and set its left and bottom position, along with its width and height.
  • Then, set the color, fill color and assign the TextFragment object to its text property.
  • Now, add the ellipse object to the shape collection of the Graph object.
  • Then, call the Page.Paragraphs.Add() method to add graph object to paragraphs collection of the page instance.
  • Finally, call the Document.Save() method with output file path to save the file.

The following code sample shows how to create an ellipse with text in PDF documents using C#.

// Create Document instance
Document doc = new Document();
// Add page to pages collection of PDF file
Page page = doc.Pages.Add();
// Create Drawing object with certain dimensions
Graph graph = new Graph(400, 400);
// Define text
TextFragment textFragment = new TextFragment("Ellipse");
textFragment.TextState.Font = FontRepository.FindFont("Helvetica");
textFragment.TextState.FontSize = 24;
// Draw ellipse
Ellipse ellipse = new Ellipse(100, 100, 120, 180);
ellipse.GraphInfo.FillColor = Color.GreenYellow;
ellipse.GraphInfo.Color = Color.Red;
ellipse.Text = textFragment;
// Add ellipse to shapes collection of Graph object
graph.Shapes.Add(ellipse);
// Add Graph object to paragraphs collection of page
page.Paragraphs.Add(graph);
// Save PDF file
doc.Save(@"C:\Files\PDF\EclipseWithText_out.pdf");
Add Ellipse in PDF Documents using C#.

Add Ellipse in PDF Documents using C#.

Get a Free License

You can try the API without evaluation limitations by requesting a free temporary license.

Conclusion

In this article, you have learned how to add shapes in PDF documents using C#. Particularly, you have seen how to add a filled rectangle, circle, line, and ellipse in PDF documents programmatically. Similarly, you can create an arc and a curve in PDF files. You can learn more about Aspose.PDF for .NET API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.

See Also