Shapes

Shapes (7)

Thursday, 19 January 2017 08:25

How to Create Spot Color in PDF in C#, VB.NET

Written by support iceblue

Colors created without screens or dots are referred to in the industry as spot or solid colors. Spot colors are the preferred method of producing stationery inexpensively, and also the method used where color accuracy is deemed essential, for instance a company logo.

This article presents how to define a spot color based on RGB color spaces, create variations of a spot color with different tint value, and how to apply the spot color to text and graphic objects using Spire.PDF.

Step 1: Create a PdfDcoument object and add a page to it.

PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.Pages.Add();

Step 2: Define the spot color "MySpotColor" from the built-in color.

PdfSeparationColorSpace cs = new PdfSeparationColorSpace("MySpotColor", Color.Purple);

Step 3: Set the spot color with a tint value of 1.

PdfSeparationColor color = new PdfSeparationColor(cs, 1f);

Step 4: Apply the spot color while drawing content on the page.

PdfSolidBrush brush = new PdfSolidBrush(color);
page.Canvas.DrawPie(brush, 10, 30, 60, 60, 360, 360);
page.Canvas.DrawString("1.0 tint!", new PdfFont(PdfFontFamily.Helvetica, 10f), brush, new PointF(20,100));

Step 5: Set the spot color with a tint value of 0.5 and apply it to some other content.

color = new PdfSeparationColor(cs, 0.5f);
brush = new PdfSolidBrush(color);
page.Canvas.DrawPie(brush, 80, 30, 60, 60, 360, 360);
page.Canvas.DrawString("Tint=0.5", new PdfFont(PdfFontFamily.Helvetica, 10f), brush, new PointF(92,100));

Step 6: Save the file.

pdf.SaveToFile("SpotColor.pdf");

Output:

How to Create Spot Color in PDF in C#, VB.NET

Full Code:

[C#]
PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.Pages.Add();

PdfSeparationColorSpace cs = new PdfSeparationColorSpace("MySpotColor", Color.Purple);
PdfSeparationColor color = new PdfSeparationColor(cs, 1f);
PdfSolidBrush brush = new PdfSolidBrush(color);
page.Canvas.DrawPie(brush, 10, 30, 60, 60, 360, 360);
page.Canvas.DrawString("Tint=1.0", new PdfFont(PdfFontFamily.Helvetica, 10f), brush, new PointF(22,100));

color = new PdfSeparationColor(cs, 0.5f);
brush = new PdfSolidBrush(color);
page.Canvas.DrawPie(brush, 80, 30, 60, 60, 360, 360);
page.Canvas.DrawString("Tint=0.5", new PdfFont(PdfFontFamily.Helvetica, 10f), brush, new PointF(92,100));

color = new PdfSeparationColor(cs, 0.25f);
brush = new PdfSolidBrush(color);
page.Canvas.DrawPie(brush, 150, 30, 60, 60, 360, 360);
page.Canvas.DrawString("Tint=0.25", new PdfFont(PdfFontFamily.Helvetica, 10f), brush, new PointF(162, 100));
pdf.SaveToFile("SpotColor.pdf");
[VB.NET]
Dim pdf As PdfDocument = New PdfDocument()
Dim page As PdfPageBase = pdf.Pages.Add()

Dim cs As PdfSeparationColorSpace = New PdfSeparationColorSpace("MySpotColor", Color.Purple)
Dim color As PdfSeparationColor = New PdfSeparationColor(cs,1f)
Dim brush As PdfSolidBrush = New PdfSolidBrush(color)
page.Canvas.DrawPie(brush, 10, 30, 60, 60, 360, 360)
page.Canvas.DrawString("Tint=1.0", New PdfFont(PdfFontFamily.Helvetica, 10f), brush, New PointF(22,100))
 
color = New PdfSeparationColor(cs, 0.5f)
brush = New PdfSolidBrush(color)
page.Canvas.DrawPie(brush, 80, 30, 60, 60, 360, 360)
page.Canvas.DrawString("Tint=0.5", New PdfFont(PdfFontFamily.Helvetica, 10f), brush, New PointF(92,100))
 
color = New PdfSeparationColor(cs, 0.25f)
brush = New PdfSolidBrush(color)
page.Canvas.DrawPie(brush, 150, 30, 60, 60, 360, 360)
page.Canvas.DrawString("Tint=0.25", New PdfFont(PdfFontFamily.Helvetica, 10f), brush, New PointF(162, 100))
pdf.SaveToFile("SpotColor.pdf")
Monday, 09 January 2012 07:31

Set Graphic Overlay in PDF File

Written by support iceblue

This section is designed to provide developers a solution to set graphic overlay in PDF file with C#, VB.NET via a .NET PDF library Spire.PDF for .NET.

Spire.PDF for .NET enables you to set graphic overlay for PDF pages in an easy way. In order to see content in both PDF files, we can set the transparency for the overlay. Now, let us see the code detail step by step.

First, we need to load two PDF documents from system which are used for creating overlay PDF document. Then, we can create the page template as one page of the first PDF file. In my project, I set the first page in the first PDF document as the template. Finally draw this template page into every page of the second PDF file by this method: PdfPageBase.Canvas.DrawTemplate(PdfTemplate template, PointF location) and set the transparency for the page: PdfPageBase.Canvas.SetTransparency(float alphaPen, float alphaBrush, PdfBlendMode blendMode).

Here you can know more about Spire.PDF for .NET. Spire.PDF for .NET is a PDF component that is applied to manipulate PDF files in .NET applications. You can download Spire.PDF for .NET. After installing it on system, you can start your project by C# or VB.NET in either Console Application or Windows Forms Application. Since we will use Spire.PDF, you need add Spire.Pdf.dll in the Bin folder, the default path is: "..\Spire.Pdf\Bin\NET4.0\Spire.Pdf.dll". Below is the whole code of the task:

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

namespace pdf_graphic_overlay
{
    class Program
    {
        static void Main(string[] args)
        {
            //load two documents
            PdfDocument doc1 = new PdfDocument();
            doc1.LoadFromFile(@"..\ Sample1.pdf");
            PdfDocument doc2 = new PdfDocument();
            doc2.LoadFromFile(@"..\Sample3.pdf");
            //Create page template
            PdfTemplate template = doc1.Pages[0].CreateTemplate();
            //set PDF overlay effect and set transparency mode
            foreach (PdfPageBase page in doc2.Pages)
            {
                //set transparency 
                page.Canvas.SetTransparency(0.25f, 0.25f, PdfBlendMode.Overlay);
                //add the first page of doc1 to every page of doc2
                page.Canvas.DrawTemplate(template, PointF.Empty);
            }
            //Save pdf file.
            doc2.SaveToFile("Overlay.pdf");
            doc1.Close();
            doc2.Close();
            //Launching the Pdf file.
            System.Diagnostics.Process.Start("Overlay.pdf");
        }
    }
}
[VB.NET]
Imports System.Drawing
Imports Spire.Pdf
Imports Spire.Pdf.Graphics

Namespace pdf_graphic_overlay
     Class Program
	    Private Shared Sub Main(args As String())
		'load two documents
		Dim doc1 As New PdfDocument()
		doc1.LoadFromFile("..\Sample1.pdf")
		Dim doc2 As New PdfDocument()
		doc2.LoadFromFile("..\Sample3.pdf")
		'Create page template
		Dim template As PdfTemplate = doc1.Pages(0).CreateTemplate()
		'set PDF overlay effect and set transparency mode
		For Each page As PdfPageBase In doc2.Pages
			'set transparency 
			page.Canvas.SetTransparency(0.25F, 0.25F, PdfBlendMode.Overlay)
			'add the first page of doc1 to every page of doc2
			page.Canvas.DrawTemplate(template, PointF.Empty)
		Next
		'Save pdf file.
		doc2.SaveToFile("Overlay.pdf")
		doc1.Close()
		doc2.Close()
		'Launching the Pdf file.
		System.Diagnostics.Process.Start("Overlay.pdf")
	    End Sub
     End Class
End Namespace

After executing above code, we can get the output file as below:

Draw Overlay Images

I have introduced one solution to set graphic overlay for PDF document, I wish it can give you some insights. If you have problem, feedback and suggestions, please do not hesitate to put them on E-iceblue Forum, we will give prompt reply.

Spire.PDF for .NET is a PDF application which is designed to perform processing tasks on managing PDF files. It is completely written in C# but also support VB.NET.

Wednesday, 14 December 2011 06:45

How to Draw Special Shapes in PDF

Written by support iceblue

Spire.PDF can help us draw different shapes in PDF document. We can use Spire.PDF to draw rectangles in PDF, draw circles in PDF, draw arcs in PDF and draw ellipses in PDF. Besides these shapes, we can also use Spire.PDF to draw some other special shapes such as Spiral and five-pointed stars.

Draw Five-Pointed Star in PDF

By using Spire.PDF, we can draw different stars in PDF document. The sample below is showing how to draw 6 types of different five-pointed star.

[C#]
        private static void DrawStar(PdfPageBase page)
        {
            PointF[] points = new PointF[5];
            for (int i = 0; i < points.Length; i++)
            {
                float x = (float)Math.Cos(i * 2 * Math.PI / 5);
                float y = (float)Math.Sin(i * 2 * Math.PI / 5);
                points[i] = new PointF(x, y);
            }
            PdfPath path = new PdfPath();
            path.AddLine(points[2], points[0]);
            path.AddLine(points[0], points[3]);
            path.AddLine(points[3], points[1]);
            path.AddLine(points[1], points[4]);
            path.AddLine(points[4], points[2]);

            //save graphics state
            PdfGraphicsState state = page.Canvas.Save();
            PdfPen pen = new PdfPen(Color.DeepSkyBlue, 0.02f);
            PdfBrush brush1 = new PdfSolidBrush(Color.CadetBlue);

            page.Canvas.ScaleTransform(50f, 50f);
            page.Canvas.TranslateTransform(5f, 1.2f);
            page.Canvas.DrawPath(pen, path);

            page.Canvas.TranslateTransform(2f, 0f);
            path.FillMode = PdfFillMode.Alternate;
            page.Canvas.DrawPath(pen, brush1, path);

            page.Canvas.TranslateTransform(2f, 0f);
            path.FillMode = PdfFillMode.Winding;
            page.Canvas.DrawPath(pen, brush1, path);

            PdfLinearGradientBrush brush2
                = new PdfLinearGradientBrush(new PointF(-2, 0), new PointF(2, 0), Color.Red, Color.Blue);
            page.Canvas.TranslateTransform(-4f, 2f);
            path.FillMode = PdfFillMode.Alternate;
            page.Canvas.DrawPath(pen, brush2, path);

            PdfRadialGradientBrush brush3
                = new PdfRadialGradientBrush(new PointF(0f, 0f), 0f, new PointF(0f, 0f), 1f, Color.Red, Color.Blue);
            page.Canvas.TranslateTransform(2f, 0f);
            path.FillMode = PdfFillMode.Winding;
            page.Canvas.DrawPath(pen, brush3, path);

            PdfTilingBrush brush4 = new PdfTilingBrush(new RectangleF(0, 0, 4f, 4f));
            brush4.Graphics.DrawRectangle(brush2, 0, 0, 4f, 4f);

            page.Canvas.TranslateTransform(2f, 0f);
            path.FillMode = PdfFillMode.Winding;
            page.Canvas.DrawPath(pen, brush4, path);

            //restor graphics
            page.Canvas.Restore(state);
        }
[VB.NET]
        Private Shared Sub DrawPath(ByVal page As PdfPageBase)
            Dim points(4) As PointF
            For i As Integer = 0 To points.Length - 1
                Dim x As Single = CSng(Math.Cos(i * 2 * Math.PI / 5))
                Dim y As Single = CSng(Math.Sin(i * 2 * Math.PI / 5))
                points(i) = New PointF(x, y)
            Next i
            Dim path As New PdfPath()
            path.AddLine(points(2), points(0))
            path.AddLine(points(0), points(3))
            path.AddLine(points(3), points(1))
            path.AddLine(points(1), points(4))
            path.AddLine(points(4), points(2))

            'save graphics state
            Dim state As PdfGraphicsState = page.Canvas.Save()
            Dim pen As New PdfPen(Color.DeepSkyBlue, 0.02F)
            Dim brush1 As PdfBrush = New PdfSolidBrush(Color.CadetBlue)

            page.Canvas.ScaleTransform(50.0F, 50.0F)
            page.Canvas.TranslateTransform(5.0F, 1.2F)
            page.Canvas.DrawPath(pen, path)

            page.Canvas.TranslateTransform(2.0F, 0.0F)
            path.FillMode = PdfFillMode.Alternate
            page.Canvas.DrawPath(pen, brush1, path)

            page.Canvas.TranslateTransform(2.0F, 0.0F)
            path.FillMode = PdfFillMode.Winding
            page.Canvas.DrawPath(pen, brush1, path)

            Dim brush2 As New PdfLinearGradientBrush(New PointF(-2, 0), New PointF(2, 0), Color.Red, Color.Blue)
            page.Canvas.TranslateTransform(-4.0F, 2.0F)
            path.FillMode = PdfFillMode.Alternate
            page.Canvas.DrawPath(pen, brush2, path)

            Dim brush3 As New PdfRadialGradientBrush(New PointF(0.0F, 0.0F), _
                0.0F, New PointF(0.0F, 0.0F), 1.0F, Color.Red, Color.Blue)
            page.Canvas.TranslateTransform(2.0F, 0.0F)
            path.FillMode = PdfFillMode.Winding
            page.Canvas.DrawPath(pen, brush3, path)

            Dim brush4 As New PdfTilingBrush(New RectangleF(0, 0, 4.0F, 4.0F))
            brush4.Graphics.DrawRectangle(brush2, 0, 0, 4.0F, 4.0F)

            page.Canvas.TranslateTransform(2.0F, 0.0F)
            path.FillMode = PdfFillMode.Winding
            page.Canvas.DrawPath(pen, brush4, path)

            'restor graphics
            page.Canvas.Restore(state)
        End Sub

Effective Screenshot:

Draw Special Shapes in PDF

Draw Spiral in PDF

A spiral is a curve which emanates from a central point, getting progressively farther away as it revolves around the point. Spire.PDF can also help us easily draw Spiral in PDF and we can design at will.

[C#]
        private static void DrawSpiral(PdfPageBase page)
        {
            //save graphics state
            PdfGraphicsState state = page.Canvas.Save();

            //Draw shap - spiro
            PdfPen pen = PdfPens.DeepSkyBlue;

            int nPoints = 1000;
            double r1 = 30;
            double r2 = 25;
            double p = 35;
            double x1 = r1 + r2 - p;
            double y1 = 0;
            double x2 = 0;
            double y2 = 0;

            page.Canvas.TranslateTransform(100, 100);

            for (int i = 0; i < nPoints; i++)
            {
                double t = i * Math.PI / 90;
                x2 = (r1 + r2) * Math.Cos(t) - p * Math.Cos((r1 + r2) * t / r2);
                y2 = (r1 + r2) * Math.Sin(t) - p * Math.Sin((r1 + r2) * t / r2);
                page.Canvas.DrawLine(pen, (float)x1, (float)y1, (float)x2, (float)y2);
                x1 = x2;
                y1 = y2;
            }

            //restor graphics
            page.Canvas.Restore(state);
        }
[VB.NET]
        Private Shared Sub DrawSpiral(ByVal page As PdfPageBase)
            'save graphics state
            Dim state As PdfGraphicsState = page.Canvas.Save()

            'Draw shap - spiro
            Dim pen As PdfPen = PdfPens.DeepSkyBlue

            Dim nPoints As Integer = 1000
            Dim r1 As Double = 30
            Dim r2 As Double = 25
            Dim p As Double = 35
            Dim x1 As Double = r1 + r2 - p
            Dim y1 As Double = 0
            Dim x2 As Double = 0
            Dim y2 As Double = 0

            page.Canvas.TranslateTransform(100, 100)

            For i As Integer = 0 To nPoints - 1
                Dim t As Double = i * Math.PI / 90
                x2 = (r1 + r2) * Math.Cos(t) - p * Math.Cos((r1 + r2) * t / r2)
                y2 = (r1 + r2) * Math.Sin(t) - p * Math.Sin((r1 + r2) * t / r2)
                page.Canvas.DrawLine(pen, CSng(x1), CSng(y1), CSng(x2), CSng(y2))
                x1 = x2
                y1 = y2
            Next i

            'restor graphics
            page.Canvas.Restore(state)
        End Sub

Effective Screenshot:

Draw Special Shapes in PDF

Thursday, 07 July 2011 05:38

Draw Ellipses in PDF in C#, VB.NET

Written by support iceblue

In geometry, an ellipse is a plane curve that results from the intersection of a cone by a plane in a way that produces a closed curve. Circles are special cases of ellipses, obtained when the cutting plane is orthogonal to the cone's axis. An ellipse is also the locus of all points of the plane whose distances to two fixed points add to the same constant. In this section, I will introduce a solution to draw ellipses in PDF document and set ellipses size, color and position via Spire.PDF for .NET in C#, VB.NET. First let us see the ellipses I draw in PDF document as below picture:

Draw Ellipses in PDF

For Spire.PDF for .NET users, we can easily draw ellipses and set ellipses size and position by this method: Spire.Pdf.PdfPageBase.Canvas.DrawEllipse(PdfPen pen, PdfBrush brush, float x, float y, float width, float height); There are six parameters passed in this method. The first parameter is a class in Spire.PDF: Spire.Pdf.Graphics.PdfPen which is responsible for drawing ellipses. When drawing PDF ellipses, we also can set the outline color and outline thickness by classes provided by Microsoft: System.Drawing.Color and System.Single. The second parameter is a class Spire.Pdf.Graphics.PdfBrush, when we define this class, we can set the ellipse color. The third and fourth parameters are the distance between the left and top margin with ellipse, which certainly decide the position of ellipse we draw. The last two parameters are ellipse width and height. To say in other word, the last two parameters can determine ellipse size.

Here we can download download Spire.PDF for .NET. After installing it on system, we can begin our task as below code:

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

namespace PDFellipses
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a pdf document.
            PdfDocument doc = new PdfDocument();
            // Create one page
            PdfPageBase page = doc.Pages.Add();
            //save graphics state
            PdfGraphicsState state = page.Canvas.Save();
            //draw ellipses in PDF and set ellipse size and position
            PdfPen pen = new PdfPen(Color.Red, 1f);
            PdfBrush brush = new PdfSolidBrush(Color.Yellow);
            PdfPen pen1 = new PdfPen(Color.DeepPink, 1f);
            PdfBrush brush1 = new PdfSolidBrush(Color.DeepSkyBlue);
            PdfPen pen2 = new PdfPen(Color.Cornsilk, 1f);
            PdfBrush brush2 = new PdfSolidBrush(Color.Red);
            PdfPen pen3 = new PdfPen(Color.SpringGreen, 1f);
            PdfBrush brush3 = new PdfSolidBrush(Color.SeaGreen);
            PdfPen pen4 = new PdfPen(Color.YellowGreen, 1f);
            PdfBrush brush4 = new PdfSolidBrush(Color.Orange);
            page.Canvas.DrawEllipse(pen, brush, 50, 70, 20, 60);
            page.Canvas.DrawEllipse(pen1, brush1, 100, 100, 20, 70);
            page.Canvas.DrawEllipse(pen2, brush2, 150, 150, 20, 70);
            page.Canvas.DrawEllipse(pen3, brush3, 200, 100, 20, 70);
            page.Canvas.DrawEllipse(pen4, brush4, 250, 70, 20, 60);
            //restor graphics
            page.Canvas.Restore(state);
            doc.SaveToFile("Ellipse.pdf");
            System.Diagnostics.Process.Start("Ellipse.pdf");
        }
    }
}
[VB.NET]
Imports System.Drawing
Imports Spire.Pdf
Imports Spire.Pdf.Graphics

Namespace PDFellipses
	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()
			'save graphics state
			Dim state As PdfGraphicsState = page.Canvas.Save()
			'draw ellipses in PDF and set ellipse size and position
			Dim pen As New PdfPen(Color.Red, 1F)
			Dim brush As PdfBrush = New PdfSolidBrush(Color.Yellow)
			Dim pen1 As New PdfPen(Color.DeepPink, 1F)
			Dim brush1 As PdfBrush = New PdfSolidBrush(Color.DeepSkyBlue)
			Dim pen2 As New PdfPen(Color.Cornsilk, 1F)
			Dim brush2 As PdfBrush = New PdfSolidBrush(Color.Red)
			Dim pen3 As New PdfPen(Color.SpringGreen, 1F)
			Dim brush3 As PdfBrush = New PdfSolidBrush(Color.SeaGreen)
			Dim pen4 As New PdfPen(Color.YellowGreen, 1F)
			Dim brush4 As PdfBrush = New PdfSolidBrush(Color.Orange)
			page.Canvas.DrawEllipse(pen, brush, 50, 70, 20, 60)
			page.Canvas.DrawEllipse(pen1, brush1, 100, 100, 20, 70)
			page.Canvas.DrawEllipse(pen2, brush2, 150, 150, 20, 70)
			page.Canvas.DrawEllipse(pen3, brush3, 200, 100, 20, 70)
			page.Canvas.DrawEllipse(pen4, brush4, 250, 70, 20, 60)
			'restor graphics
			page.Canvas.Restore(state)
			doc.SaveToFile("Ellipse.pdf")
			System.Diagnostics.Process.Start("Ellipse.pdf")
		End Sub
	End Class
End Namespace

Spire.PDF for .NET is a professional PDF library that enables users draw different kinds of shapes in PDF document such as rectangles, arcs, circles, ellipse, five-pointed star etc.

Wednesday, 06 July 2011 03:48

Draw PDF Arc in C#, VB.NET

Written by support iceblue

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.

Wednesday, 06 July 2011 03:26

Draw Circles in PDF in C#, VB.NET

Written by support iceblue

Circles can be defined as the curves traced out by points that move so that its distance from a given point is constant. They are also look as special ellipses in which the two foci are coincident and the eccentricity is “0”. Whatever they are, they are indispensable in PDF document. This section will introduce a solution to draw circles and set their size and position in PDF file via a .NET PDF component Spire.PDF for .NET in C#, VB.NET.

When we draw circles in PDF, we only need to call one method in Spire.PDF: Spire.Pdf.PdfPageBase.Canvas.DrawPie(PdfPen pen, float x, float y, float width, float height, float startAngle, float sweepAngle); Here there are seven parameters in this method. The first one is the class Spire.Pdf.Graphics.PdfPen which can define the color and the outline of the circle. If we change this parameter to be another class Spire.Pdf.Graphics.PdfBrush, we can easily fill the circle with a certain color. The second and third parameters determine the exact distance between the PDF margin and the circle. "float x" decides the distance of left margin with circle, while "float y" means the distance between the top margin with the circle. By setting the fourth and fifth parameters, we can decide the circle width and height. The last two parameters are the start angle and sweep angle when drawing circles. Now please view the circles in PDF as below picture:

Draw Circles in PDF

Here we can quickly download Spire.PDF for .NET . After adding Spire.Pdf dll in the download Bin folder, we can draw circles in PDF file via Spire.PDF by below code.

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

namespace pdf_circles
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a pdf document.
            PdfDocument doc = new PdfDocument();
            // Create one page
            PdfPageBase page = doc.Pages.Add();
            //save graphics state
            PdfGraphicsState state = page.Canvas.Save();
            PdfPen pen = new PdfPen(Color.Red, 1f);
            PdfPen pen1 = new PdfPen(Color.GreenYellow, 2f);
            PdfBrush brush = new PdfSolidBrush(Color.DeepSkyBlue);
            page.Canvas.DrawPie(pen, 30, 30, 80, 90, 360, 360);
            page.Canvas.DrawPie(brush, 150, 30, 100, 90, 360, 360);
            page.Canvas.DrawPie(pen1,290, 30, 70, 90, 360, 360);
            //restor graphics
            page.Canvas.Restore(state);
            doc.SaveToFile("Circles.pdf");
            System.Diagnostics.Process.Start("Circles.pdf");
        }
    }
}
[VB.NET]
Imports System.Drawing
Imports Spire.Pdf
Imports Spire.Pdf.Graphics

Namespace pdf_circles
	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()
			'save graphics state
			Dim state As PdfGraphicsState = page.Canvas.Save()
			Dim pen As New PdfPen(Color.Red, 1F)
			Dim pen1 As New PdfPen(Color.GreenYellow, 2F)
			Dim brush As PdfBrush = New PdfSolidBrush(Color.DeepSkyBlue)
			page.Canvas.DrawPie(pen, 30, 30, 80, 90, 360, _
				360)
			page.Canvas.DrawPie(brush, 150, 30, 100, 90, 360, _
				360)
			page.Canvas.DrawPie(pen1, 290, 30, 70, 90, 360, _
				360)
			'restor graphics
			page.Canvas.Restore(state)
			doc.SaveToFile("Circles.pdf")
			System.Diagnostics.Process.Start("Circles.pdf")
		End Sub
	End Class
End Namespace

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

Tuesday, 05 July 2011 06:11

Draw Rectangles in PDF in C#, VB.NET

Written by support iceblue

In Euclidean plane geometry, a rectangle is any quadrilateral with four right angles. The term "oblong" is occasionally used to refer to a non-square rectangle. A rectangle with vertices ABCD would be denoted as ABCD. It’s simple for people to draw rectangles in paper. While how about drawing rectangles in PDF document? This section will show you the exact answer. This section will introduce a solution to draw rectangles and set the size and position of rectangles in PDF via a .NET PDF component Spire.PDF for .NET with C#, VB.NET.

In Spire.PDF, there are two classes: Spire.Pdf.Graphics.PdfPen and Spire.Pdf.Granphics.PdfBrush. By using the first class, we can set the color and decide the outline of the PDF rectangle. While the second class can quickly help us fill the rectangles with a color we want. Now let us see this method: Spire.Pdf.PdfPageBase.Canvas.DrawRectangle(PdfPen pen, RectangleF rectangle); There are two parameters passed. One is the PdfPen which I referred above. The other represents the location and size of a rectangle. By calling this method, we can draw rectangles and set their size and position very quickly. Now let us view the rectangles as below picture:

Draw Rectangles in PDF

Here we can download Spire.PDF for .NET and install it on system. After adding Spire.Pdf dll, we can draw rectangle in our PDF document as below code:

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

namespace PDF_rectangles
{
    class Program
    {
        static void Main(string[] args)
        {   
            //create a PDF 
            PdfDocument pdfDoc = new PdfDocument();
            PdfPageBase page = pdfDoc.Pages.Add();
            //save graphics state
            PdfGraphicsState state = page.Canvas.Save();
            //draw rectangles
            PdfPen pen = new PdfPen(Color.ForestGreen, 0.1f);
            PdfPen pen1 = new PdfPen(Color.Red, 3f);
            PdfBrush brush = new PdfSolidBrush(Color.Orange);
            page.Canvas.DrawRectangle(pen, new Rectangle(new Point(2, 7), new Size(120, 120)));
            page.Canvas.DrawRectangle(pen1, new Rectangle(new Point(350, 7), new Size(160, 120)));
            page.Canvas.DrawRectangle(brush, new RectangleF(new Point(158, 7), new SizeF(160, 120)));
            //restor graphics
            page.Canvas.Restore(state);
            pdfDoc.SaveToFile("Rectangles.pdf");
            System.Diagnostics.Process.Start("Rectangles.pdf");
        }
    }
}
[VB.NET]
Imports System.Drawing
Imports Spire.Pdf
Imports Spire.Pdf.Graphics

Namespace PDF_rectangles
	Class Program
		Private Shared Sub Main(args As String())
			'create a PDF 
			Dim pdfDoc As New PdfDocument()
			Dim page As PdfPageBase = pdfDoc.Pages.Add()
			'save graphics state
			Dim state As PdfGraphicsState = page.Canvas.Save()
			'draw rectangles
			Dim pen As New PdfPen(Color.ForestGreen, 0.1F)
			Dim pen1 As New PdfPen(Color.Red, 3F)
			Dim brush As PdfBrush = New PdfSolidBrush(Color.Orange)
			page.Canvas.DrawRectangle(pen, New Rectangle(New Point(2, 7), New Size(120, 120)))
			page.Canvas.DrawRectangle(pen1, New Rectangle(New Point(350, 7), New Size(160, 120)))
			page.Canvas.DrawRectangle(brush, New RectangleF(New Point(158, 7), New SizeF(160, 120)))
			'restor graphics
			page.Canvas.Restore(state)
			pdfDoc.SaveToFile("Rectangles.pdf")
			System.Diagnostics.Process.Start("Rectangles.pdf")
		End Sub
	End Class
End Namespace

Spire.PDF for .NET is a .NET PDF component that can draw different kinds of shapes in PDF document such as Circles, Arcs. Ellipse and Five-pointed Star.