Add Ellipse Object to PDF file
Contents
[
Hide
]
The following code snippet also work with Aspose.PDF.Drawing library.
Add Ellipse object
Aspose.PDF for .NET supports to add Ellipse objects to PDF documents. It also offers the feature to fill ellipse object with a certain color.
private static void Ellipse()
{
// The path to the document directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Images();
// Create Document instance using 'using' block to ensure proper disposal
using (var document = new Aspose.Pdf.Document())
{
// Add page to pages collection of PDF file
var page = document.Pages.Add();
// Create Drawing object with certain dimensions
var graph = new Aspose.Pdf.Drawing.Graph(400, 400);
// Set border for Drawing object
var borderInfo = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, Aspose.Pdf.Color.Green);
graph.Border = borderInfo;
// Create first ellipse with specified coordinates and radii
var ellipse1 = new Aspose.Pdf.Drawing.Ellipse(150, 100, 120, 60)
{
GraphInfo = { Color = Aspose.Pdf.Color.GreenYellow },
Text = new Aspose.Pdf.Text.TextFragment("Ellipse")
};
graph.Shapes.Add(ellipse1);
// Create second ellipse with different dimensions and color
var ellipse2 = new Aspose.Pdf.Drawing.Ellipse(50, 50, 18, 300)
{
GraphInfo = { Color = Aspose.Pdf.Color.DarkRed }
};
graph.Shapes.Add(ellipse2);
// Add Graph object to paragraphs collection of page
page.Paragraphs.Add(graph);
// Save PDF file
document.Save(dataDir + "DrawingEllipse_out.pdf");
}
}
Create Filled Ellipse Object
The following code snippet shows how to add a Ellipse object that is filled with color.
private static void EllipseFilled()
{
// The path to the document directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Images();
// Create Document instance using 'using' block to ensure proper disposal
using (var document = new Aspose.Pdf.Document())
{
// Add page to pages collection of PDF file
var page = document.Pages.Add();
// Create Drawing object with certain dimensions
var graph = new Aspose.Pdf.Drawing.Graph(400, 400);
// Set border for Drawing object
var borderInfo = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, Aspose.Pdf.Color.Green);
graph.Border = borderInfo;
// Create first ellipse and set its fill color
var ellipse1 = new Aspose.Pdf.Drawing.Ellipse(100, 100, 120, 180)
{
GraphInfo =
{
FillColor = Aspose.Pdf.Color.GreenYellow
}
};
graph.Shapes.Add(ellipse1);
// Create second ellipse and set its fill color
var ellipse2 = new Aspose.Pdf.Drawing.Ellipse(200, 150, 180, 120)
{
GraphInfo =
{
FillColor = Aspose.Pdf.Color.DarkRed
}
};
graph.Shapes.Add(ellipse2);
// Add Graph object to paragraphs collection of page
page.Paragraphs.Add(graph);
// Save PDF file
document.Save(dataDir + "DrawingEllipse_out.pdf");
}
}
Add Text inside the Ellipse
Aspose.PDF for .NET supports to add text inside the Graph Object. Text property of Graph Object provides option to set text of the Graph Object. The following code snippet shows how to add text inside a Rectangle object.
private static void EllipseWithText()
{
// The path to the document directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Images();
// Create Document instance using 'using' block to ensure proper disposal
using (var document = new Aspose.Pdf.Document())
{
// Add page to pages collection of PDF file
var page = document.Pages.Add();
// Create Drawing object with certain dimensions
var graph = new Aspose.Pdf.Drawing.Graph(400, 400);
// Set border for Drawing object
var borderInfo = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, Aspose.Pdf.Color.Green);
graph.Border = borderInfo;
// Create TextFragment for adding text to shapes
var textFragment = new Aspose.Pdf.Text.TextFragment("Ellipse")
{
TextState =
{
Font = Aspose.Pdf.Text.FontRepository.FindFont("Helvetica"),
FontSize = 24
}
};
// Create first ellipse and set properties
var ellipse1 = new Aspose.Pdf.Drawing.Ellipse(100, 100, 120, 180)
{
GraphInfo =
{
FillColor = Aspose.Pdf.Color.GreenYellow
},
Text = textFragment
};
graph.Shapes.Add(ellipse1);
// Create second ellipse and set properties
var ellipse2 = new Aspose.Pdf.Drawing.Ellipse(200, 150, 180, 120)
{
GraphInfo =
{
FillColor = Aspose.Pdf.Color.DarkRed
},
Text = textFragment
};
graph.Shapes.Add(ellipse2);
// Add Graph object to paragraphs collection of page
page.Paragraphs.Add(graph);
// Save PDF file
document.Save(dataDir + "DrawingEllipseText_out.pdf");
}
}