How to print text in a circle?

Hi,

We have a requirement where I have to print alphabet inside Circle as shown in below screenshot on each Page of given PDF.

image.png (2.5 KB)

Could you please help with the API /code to be used for the same?

Thanks,

@PoonamB

Thanks for contacting support.

Please check following code snippet to achieve the requirement. For you reference, I have also attached an output generated by the code snippet.

Document doc = new Document();
// Add page to pages collection of PDF file
Page page = doc.Pages.Add();
// Create Graph instance
Aspose.Pdf.Drawing.Graph graph = new Aspose.Pdf.Drawing.Graph(100, 400);
// Add graph object to paragraphs collection of page instance
page.Paragraphs.Add(graph);
// Create Circle instance
Aspose.Pdf.Drawing.Circle circ = new Aspose.Pdf.Drawing.Circle(20, 5, 10);
// Specify fill color for Graph object
//circ.GraphInfo.FillColor = Aspose.Pdf.Color.Red;
// Add Text into Circle
circ.Text = new TextFragment("A") ;
// Add Circle object to shapes collection of Graph object
graph.Shapes.Add(circ);
dataDir = dataDir + "CreateCirclewithText_out.pdf";
// Save PDF file
doc.Save(dataDir); 

CreateCirclewithText_out.pdf (2.0 KB)

In case of any further assistance, please feel free to contact us.

Hi ,

Thank you for your reply.
I am able to create Circle using above code.
I have below 2 queries :

(1) When I try to add Text to Circle using

 circ.Text = new TextFragment("A") ;

It says that :- ‘Circle’ does not contain definition for ‘Text’ and no extension Method ‘Text’ accepting a first argument of type ‘Circle’ could be found(are you missing a using directive or assembly reference?)

I am using ASPOSE Product Version : 7.7.1.0

(2) How to fix the position of Circle on given PDF Page based on the requirement specified in previous attached
screenshot?
In which Unit are coordinates indicated?
How to convert the “mm” to coordinate Units?

Thanks

@PoonamB

Thanks for your inquiry.

I am afraid that you are using quite old version of the API, whereas it is always recommended to use latest version because it contains all fixes and enhancements. In latest version, which is Aspose.Pdf for .NET 17.7, you can find the property of Circle.Text and use it as per your requirement.

The basic unit of measurement in Aspose.Pdf is Point, which means 72 points = 1 inch. In the snapshot which you have shared earlier, the circle was drawn at low left corner of the page with 7mm distance from X and Y coordinates and origin of the page inside PDF is used to be lower left (0,0). As per the measurements, 72 point = 1 inch = 25.4 mm, so 1 mm = 72 points/25.4 mm = 2.83465 points, and 7mm = 7 * 2.83465 = 19.84255 points.

In order to draw the circle with 7mm distance from X and Y coordinates, you need to draw it according to above calculations. Please check following code snippet where I have placed an Graph with page width and height inside page and drawn a circle with text at specified coordinates.

Document doc = new Document();
// Add page to pages collection of PDF file
Page page = doc.Pages.Add();
page.PageInfo.Margin = new MarginInfo() { Bottom = 0, Left = 0, Right = 0, Top = 0 };
// Create Graph instance
Aspose.Pdf.Drawing.Graph graph = new Aspose.Pdf.Drawing.Graph((float)(page.PageInfo.Width), (float)page.PageInfo.Height);
// Add graph object to paragraphs collection of page instance
page.Paragraphs.Add(graph);
// Create Circle instance (19.8425f = 7 mm)
Aspose.Pdf.Drawing.Circle circ = new Aspose.Pdf.Drawing.Circle(19.8425f, 19.8425f, 10);
// Specify fill color for Graph object
//circ.GraphInfo.FillColor = Aspose.Pdf.Color.Red;
// Add Text into Circle
circ.Text = new TextFragment("A") ;
// Add Circle object to shapes collection of Graph object
graph.Shapes.Add(circ);
dataDir = dataDir + "CreateCirclewithText_out.pdf";
// Save PDF file
doc.Save(dataDir);

For your reference, I have attached an output generated by above code snippet. In event of any further query, please feel free to ask.

CreateCirclewithText_out.pdf (2.0 KB)

Thank you so much. :slight_smile:
Above code works perfect.

@PoonamB

Thanks for your feedback.

It is good to know that your requirement has been accomplished by suggested code. Please keep using our API and in case of any further assistance, please feel free to contact us.