Wednesday, 06 July 2011 03:48

Draw PDF Arc in C#, VB.NET

An 'arc' is part of the circumference of a circle. In geometry, an arc is a closed segment of a differentiable curve in the two-dimensional plane; for example, a circular arc is a segment of the circumference of a circle. If the arc is part of a great circle (or great ellipse), it is called a great arc. This article will introduce a solution to draw PDF arc and set arc position and size via this .NET PDF component Spire.PDF for .NET in C#, VB.NET. First let us see the PDF arc which is drawn by code in the end of this section:

Draw Arcs in PDF

When we draw PDF arcs, we first need to know this method provided by Spire.PDF for .NET: Spire.Pdf.PdfPageBase.Canvas.DrawArc(PdfPen pen, float x, float y, float width, float height, float startAngle, float sweepAngle); As we see, there are seven parameters passed in this method. The first parameter is a class: Spire.Pdf.Graphics.PdfPen. By using this class, we can draw arc in PDF. If we want to set the color of PDF arc and the thickness of the arc line, we can use another two classes which are offered by Microsoft: System.Drawing.Color and System.Single. The second and third parameters: "float x" and "float y" determine the distance between the arcs and PDF left and top margin. The fourth and fifth parameters are the width and height of PDF arc. The last two parameters start angle and sweep angle which decide which kind of arc we draw.

Here we can download Spire.PDF for .NET; and install it on system. After adding Spire.Pdf dll, we can start our task according to below code:

[C#]
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;

namespace pdfcircle
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a pdf document.
            PdfDocument doc = new PdfDocument();
            // Create one page
            PdfPageBase page = doc.Pages.Add();
            //draw PDF arcs and set its size and position
            PdfGraphicsState state = page.Canvas.Save();
            PdfPen pen = new PdfPen(Color.DeepSkyBlue, 4f);
            PdfPen pen1 = new PdfPen(Color.Red, 4f);
            PdfPen pen2 = new PdfPen(Color.Red, 4f);
            PdfPen pen3 = new PdfPen(Color.DeepSkyBlue, 4f);
            PdfPen pen4 = new PdfPen(Color.Black, 4f);
            PdfPen pen5 = new PdfPen(Color.Black, 4f);
            PdfPen pen6 = new PdfPen(Color.Black, 4f);
            PdfPen pen7 = new PdfPen(Color.Black, 4f);
            PdfPen pen8 = new PdfPen(Color.Red, 1f);
            PdfPen pen9= new PdfPen(Color.Red, 1f);
            page.Canvas.DrawArc(pen, 130, 100, 20, 90, 20, 300);
            page.Canvas.DrawArc(pen1, 60, 100, 20, 90, 20,300);
            page.Canvas.DrawArc(pen2, 90, 100, 20, 90, 218, 300);
            page.Canvas.DrawArc(pen3, 160, 100, 20, 90, 218, 300);
            page.Canvas.DrawArc(pen4, 70, 130, 5, 5, 0, 360);
            page.Canvas.DrawArc(pen5, 95, 130, 5, 5, 0, 360);
            page.Canvas.DrawArc(pen6, 140, 130, 5, 5, 0, 360);
            page.Canvas.DrawArc(pen7, 161, 130, 5, 5, 0, 360);
            page.Canvas.DrawArc(pen8, 83, 145, 5, 5, 350,200);
            page.Canvas.DrawArc(pen9, 150, 145, 5, 5, 350, 200);
            //restor graphics
            page.Canvas.Restore(state);
            doc.SaveToFile("Arcs.pdf");
            System.Diagnostics.Process.Start("Arcs.pdf");
        }
    }
}
[VB.NET]
Imports System.Drawing
Imports Spire.Pdf
Imports Spire.Pdf.Graphics

Namespace pdfcircle
	Class Program
		Private Shared Sub Main(args As String())
			'Create a pdf document.
			Dim doc As New PdfDocument()
			' Create one page
			Dim page As PdfPageBase = doc.Pages.Add()
			'draw PDF arcs and set its size and position
			Dim state As PdfGraphicsState = page.Canvas.Save()
			Dim pen As New PdfPen(Color.DeepSkyBlue, 4F)
			Dim pen1 As New PdfPen(Color.Red, 4F)
			Dim pen2 As New PdfPen(Color.Red, 4F)
			Dim pen3 As New PdfPen(Color.DeepSkyBlue, 4F)
			Dim pen4 As New PdfPen(Color.Black, 4F)
			Dim pen5 As New PdfPen(Color.Black, 4F)
			Dim pen6 As New PdfPen(Color.Black, 4F)
			Dim pen7 As New PdfPen(Color.Black, 4F)
			Dim pen8 As New PdfPen(Color.Red, 1F)
			Dim pen9 As New PdfPen(Color.Red, 1F)
			page.Canvas.DrawArc(pen, 130, 100, 20, 90, 20, _
				300)
			page.Canvas.DrawArc(pen1, 60, 100, 20, 90, 20, _
				300)
			page.Canvas.DrawArc(pen2, 90, 100, 20, 90, 218, _
				300)
			page.Canvas.DrawArc(pen3, 160, 100, 20, 90, 218, _
				300)
			page.Canvas.DrawArc(pen4, 70, 130, 5, 5, 0, _
				360)
			page.Canvas.DrawArc(pen5, 95, 130, 5, 5, 0, _
				360)
			page.Canvas.DrawArc(pen6, 140, 130, 5, 5, 0, _
				360)
			page.Canvas.DrawArc(pen7, 161, 130, 5, 5, 0, _
				360)
			page.Canvas.DrawArc(pen8, 83, 145, 5, 5, 350, _
				200)
			page.Canvas.DrawArc(pen9, 150, 145, 5, 5, 350, _
				200)
			'restor graphics
			page.Canvas.Restore(state)
			doc.SaveToFile("Arcs.pdf")
			System.Diagnostics.Process.Start("Arcs.pdf")
		End Sub
	End Class
End Namespace

Spire.PDF for .NET is a .NET PDF library that enables users to draw different kinds of shapes in PDF document in C#, VB.NET.

Published in Shapes