Articles in this section
Category / Section

How to draw the circular shape image in a PDF using C# and VB.NET

2 mins read

Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can draw the image in circular shape in the PDF document using C# and VB.NET.

Steps to draw circular shape image on PDF programmatically:

  1. Create a new C# console application project. Create a new C# console application project
  2. Install the Syncfusion.Pdf.WinForms NuGet packages as reference to your .NET Framework application from NuGet.org. NuGet package reference
  3. Include the following namespaces in the Program.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;

 

VB.NET

Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics

 

  1. Use the following code snippet to create circular image in PDF.

C#

//Create a PdfDocument
PdfDocument document = new PdfDocument();
 
//Add page to PdfDocument
PdfPage page = document.Pages.Add();
 
//Create image from file
PdfImage image = PdfImage.FromFile("../../data/Xamarin_JPEG.jpg");
 
PdfGraphics pageGraphics = page.Graphics;
 
//Save Pdf graphics state
pageGraphics.Save();
 
//Create GraphicsPath
GraphicsPath path = new GraphicsPath();
 
//Add rectangle to GraphicsPath
path.AddEllipse(new RectangleF(150, 50, 200, 200));
 
//Create PdfPath with pathPoints and pathTypes
PdfPath pdfpath = new PdfPath(path.PathPoints, path.PathTypes);
 
//Clip the rectangle bounds in page graphics
pageGraphics.SetClip(pdfpath);
 
//Draw the image in page rectangle clip bounds
pageGraphics.DrawImage(image, new RectangleF(150, 50, 200, 200));
 
//Restore the page graphics
pageGraphics.Restore();
 
//Save the PDF document
document.Save("Circular_Image.pdf");
 
//Close the document
document.Close(true);
 
//This will open the PDF file so, the result will be seen in default PDF viewer
Process.Start("Circular_Image.pdf");

 

VB.NET

'Create a PdfDocument
Dim document As New PdfDocument()
 
'Add page to PdfDocument
Dim page As PdfPage = document.Pages.Add()
 
'Create image from file
Dim image As PdfImage = PdfImage.FromFile("../../data/Xamarin_JPEG.jpg")
 
Dim pageGraphics As PdfGraphics = page.Graphics
 
'Save Pdf graphics state
pageGraphics.Save()
 
'Create GraphicsPath
Dim path As New GraphicsPath()
 
'Add rectangle to GraphicsPath
path.AddEllipse(New RectangleF(150, 50, 200, 200))
 
'Create PdfPath with pathPoints and pathTypes
Dim pdfpath As New PdfPath(path.PathPoints, path.PathTypes)
 
'Clip the rectangle bounds in page graphics
pageGraphics.SetClip(pdfpath)
 
'Draw the image in page rectangle clip bounds
pageGraphics.DrawImage(image, New RectangleF(150, 50, 200, 200))
 
'Restore the page graphics
pageGraphics.Restore()
 
'Save the PDF document
document.Save("Circular_Image.pdf")
 
'Close the document
document.Close(True)
 
'This will open the PDF file so, the result will be seen in default PDF Viewer
Process.Start("Circular_Image.pdf")

 

A complete working sample can be downloaded from Circular Image Sample.zip

By executing the program, you will get the PDF document as follows. Screenshot of output PDF file

Take a moment to peruse the documentation for working with images, where you will find other options like inserting, replacing image in PDF document, and image masking.

Refer here to explore the rich set of Syncfusion Essential PDF features.

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied