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 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#.

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#.

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#.

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