How to Draw Figures in PDF documents
Environment
Version | Product | Author |
---|---|---|
2024.1.124 | RadPdfProcessing | Desislava Yordanova |
Description
This article demonstrates a sample approach on how to draw a small figure containing an arc and some lines using RadPdfProcessing.
Solution
To draw an arc using the ArcSegment class, follow these steps:
- Create a
Path
object and set itsIsFilled
andIsStroked
properties totrue
. - Create a
PathGeometry
object and set itsFillRule
property toFillRule.EvenOdd
. - Add a
PathFigure
to thePathGeometry
and set itsStartPoint
property to the starting point of the figure. - Create an instance of the
ArcSegment
class and set itsPoint
,RadiusX
, andRadiusY
properties. ThePoint
property represents the endpoint of the arc, whileRadiusX
andRadiusY
represent the radii for the X and Y coordinates respectively. - Add the
ArcSegment
to theSegments
collection of thePathFigure
. - Set the
Geometry
property of thePath
to thePathGeometry
.
Here is an example of how to draw an arc:
private static void AddArcSegment(RadFixedPage page)
{
Telerik.Windows.Documents.Fixed.Model.Graphics.Path path = page.Content.AddPath();
path.IsFilled = true;
path.IsStroked = true;
PathGeometry pathGeometry = new PathGeometry();
pathGeometry.FillRule = FillRule.EvenOdd;
PathFigure figure = pathGeometry.Figures.AddPathFigure();
figure.StartPoint = new Point(100, 200);
var segment = new ArcSegment()
{
Point = new Point(200, 300),
RadiusX = 20,
RadiusY = 50,
};
figure.Segments.Add(segment);
path.Geometry = pathGeometry;
}
To draw lines, you can use the LineSegment class. Here is an example of how to draw a triangle:
private void AddLineSegment(RadFixedPage page)
{
Telerik.Windows.Documents.Fixed.Model.Graphics.Path path = page.Content.AddPath();
path.IsFilled = false;
path.IsStroked = true;
PathGeometry pathGeometry = new PathGeometry();
pathGeometry.FillRule = FillRule.EvenOdd;
PathFigure figure1 = pathGeometry.Figures.AddPathFigure();
ApplyLine(figure1, new Point(300, 200), new Point(400, 200));
PathFigure figure2 = pathGeometry.Figures.AddPathFigure();
ApplyLine(figure2, new Point(400, 200), new Point(250, 100));
PathFigure figure3 = pathGeometry.Figures.AddPathFigure();
ApplyLine(figure3, new Point(250, 100), new Point(300, 200));
path.Geometry = pathGeometry;
}
private static void ApplyLine(PathFigure figure, Point startPoint, Point endPoint)
{
figure.StartPoint = startPoint;
var segment = new LineSegment
{
Point = endPoint
};
figure.Segments.Add(segment);
}
The following code snippet shows how to use the above methods:
RadFixedDocument fixedDocument = new RadFixedDocument();
RadFixedPage fixedPage = fixedDocument.Pages.AddPage();
AddArcSegment(fixedPage);
AddLineSegment(fixedPage);
PdfFormatProvider provider = new PdfFormatProvider();
string filePath = @"..\..\output.pdf";
File.Delete(filePath);
using (Stream output = File.OpenWrite(filePath))
{
provider.Export(fixedDocument, output);
}
For more information on using geometries, figures, and segments, you can refer to our Geometry help article.
You can find the result of the combined arc and triangle in the below screenshot: