This example demonstrates how to draw shapes on a PDF document.
Graphical shapes allows us to represent diagrams and data
Clicking generate button will generate a pdf file with shapes like circle , ellipse , polygon , arc , ellipse with gradient , pie shape and rectangle
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using OfficeComponent.Pdf;
using OfficeComponent.Pdf.Graphics;
namespace OfficeComponent.Samples
{
class ShapesExample : ExampleBase
{
public ShapesExample(string commonDataPath, string outputDir)
: base(commonDataPath, outputDir)
{
}
public ShapesExample(string commonDataPath, string outputDir, string xmlFile) : base(commonDataPath, outputDir, xmlFile)
{
}
public override string Execute()
{
// Create a new instance of PdfDocument class.
PdfDocument document = new PdfDocument();
Color lightGreen = Color.FromArgb(0xff, 0x76, 0xfe, 0x96);
Color lightBlue = Color.FromArgb(0xff, 0x8f, 0xb5, 0xfe);
int i;
// Create a new page.
PdfPage page = document.Pages.Add();
// Obtain PdfGraphics object.
PdfGraphics g = page.Graphics;
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold);
#region Polygon
PdfPen pen = new PdfPen(Color.Black);
pen.Width = 3;
const int pointNum = 6;
PointF[] points = new PointF[pointNum];
const double f = 360.0 / pointNum * Math.PI / 180.0;
const double r = 100;
PointF center = new PointF(140, 140);
for (i = 0; i < pointNum; ++i)
{
PointF p = new PointF();
double theta = i * f;
p.Y = (float)(Math.Sin(theta) * r + center.Y);
p.X = (float)(Math.Cos(theta) * r + center.X);
points[i] = p;
}
PdfSolidBrush brush = new PdfSolidBrush(lightBlue);
pen.Color = lightGreen;
pen.Width = 10;
pen.LineJoin = PdfLineJoin.Round;
g.DrawString("Polygon", font, PdfBrushes.DarkBlue, new PointF(50, 0));
g.DrawPolygon(pen, brush, points);
#endregion
#region Rectangle
RectangleF rect = new RectangleF(310, 50, 200, 100);
brush.Color = lightBlue;
pen.Color = lightGreen;
g.DrawString("Simple Rectangle", font, PdfBrushes.DarkBlue, new PointF(310, 0));
g.DrawRectangle(pen, brush, rect);
#endregion
#region Pie
rect = new RectangleF(200, 50, 200, 200);
brush.Color = lightBlue;
pen.Color = lightGreen;
pen.Width = 10;
rect.Location = new PointF(20, 280);
pen.LineJoin = PdfLineJoin.Round;
g.DrawString("Pie shape", font, PdfBrushes.DarkBlue, new PointF(50, 250));
g.DrawPie(pen, brush, rect, 180 + 25, 60 + 25);
g.DrawPie(pen, brush, rect, 360 - 60 + 25, 60 + 25);
g.DrawPie(pen, brush, rect, 60 + 25, 60 + 25);
#endregion
#region Arc
g.DrawString("Arcs", font, PdfBrushes.DarkBlue, new PointF(330, 250));
pen = new PdfPen(Color.Black);
pen.Width = 11;
pen.LineCap = PdfLineCap.Round;
pen.Color = lightBlue;
rect = new RectangleF(310, 280, 200, 200);
g.DrawArc(pen, rect, 0, 90);
pen.Color = lightGreen;
rect.X -= 10;
g.DrawArc(pen, rect, 90, 90);
pen.Color = lightBlue;
rect.Y -= 10;
g.DrawArc(pen, rect, 180, 90);
pen.Color = lightGreen;
rect.X += 10;
g.DrawArc(pen, rect, 270, 90);
#endregion
#region ellipse
// Draw a simple ellipse.
rect = new RectangleF(80, 520, 150, 200);
PdfLinearGradientBrush lgBrush = new PdfLinearGradientBrush(rect, lightBlue, Color.White, 90);
pen.Color = lightGreen;
pen.Width = 10.0f;
g.DrawString("Ellipse with Gradient", font, PdfBrushes.DarkBlue, new PointF(50, 490));
g.DrawEllipse(lgBrush, rect);
#endregion
#region Transaparency
page = document.Pages.Add();
// Obtain PdfGraphics object.
g = page.Graphics;
g.DrawString("Transparent Rectangles", font, PdfBrushes.DarkBlue, new PointF(230, 470));
rect = new RectangleF(250, 500, 100, 100);
pen = new PdfPen(Color.Black);
PdfBrush gBrush = new PdfSolidBrush(lightGreen);
g.DrawRectangle(pen, gBrush, rect);
gBrush = new PdfSolidBrush(lightBlue);
rect.X += 20;
rect.Y += 20;
g.SetTransparency(0.1f);
g.DrawRectangle(pen, gBrush, rect);
rect.X += 20;
rect.Y += 20;
gBrush = new PdfLinearGradientBrush(rect, Color.DarkGreen, Color.Brown, PdfLinearGradientMode.BackwardDiagonal);
g.SetTransparency(0.5f);
g.DrawRectangle(pen, gBrush, rect);
rect.X += 20;
rect.Y += 20;
gBrush = new PdfSolidBrush(Color.Gray);
g.SetTransparency(0.25f);
g.DrawRectangle(pen, gBrush, rect);
rect.X += 20;
rect.Y += 20;
gBrush = new PdfSolidBrush(Color.Red);
g.SetTransparency(0.1f);
g.DrawRectangle(pen, gBrush, rect);
#endregion
// Save and close the document.
var outputPath = Path.Combine(OutputDir, this.GetType().Name + "_" + Guid.NewGuid().ToString() + ".pdf");
document.Save(outputPath);
document.Close(true);
return outputPath;
}
}
}
Imports System.IO
Imports System.Text
Imports OfficeComponent.Pdf
Imports OfficeComponent.Pdf.Graphics
Namespace OfficeComponent.Samples
Friend Class ShapesExample
Inherits ExampleBase
Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String)
MyBase.New(commonDataPath, outputDir)
End Sub
Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String, ByVal xmlFile As String)
MyBase.New(commonDataPath, outputDir, xmlFile)
End Sub
Public Overrides Function Execute() As String
' Create a new instance of PdfDocument class.
Dim document As New PdfDocument()
Dim lightGreen As Color = Color.FromArgb(&Hff, &H76, &Hfe, &H96)
Dim lightBlue As Color = Color.FromArgb(&Hff, &H8f, &Hb5, &Hfe)
Dim i As Integer
' Create a new page.
Dim page As PdfPage = document.Pages.Add()
' Obtain PdfGraphics object.
Dim g As PdfGraphics = page.Graphics
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold)
' #Region "Polygon"
Dim pen As New PdfPen(Color.Black)
pen.Width = 3
Const pointNum As Integer = 6
Dim points(pointNum - 1) As PointF
Const f As Double = 360.0 / pointNum * Math.PI / 180.0
Const r As Double = 100
Dim center As New PointF(140, 140)
For i = 0 To pointNum - 1
Dim p As New PointF()
Dim theta As Double = i * f
p.Y = CSng(Math.Sin(theta) * r + center.Y)
p.X = CSng(Math.Cos(theta) * r + center.X)
points(i) = p
Next i
Dim brush As New PdfSolidBrush(lightBlue)
pen.Color = lightGreen
pen.Width = 10
pen.LineJoin = PdfLineJoin.Round
g.DrawString("Polygon", font, PdfBrushes.DarkBlue, New PointF(50, 0))
g.DrawPolygon(pen, brush, points)
' #End Region
' #Region "Rectangle"
Dim rect As New RectangleF(310, 50, 200, 100)
brush.Color = lightBlue
pen.Color = lightGreen
g.DrawString("Simple Rectangle", font, PdfBrushes.DarkBlue, New PointF(310, 0))
g.DrawRectangle(pen, brush, rect)
' #End Region
' #Region " Pie"
rect = New RectangleF(200, 50, 200, 200)
brush.Color = lightBlue
pen.Color = lightGreen
pen.Width = 10
rect.Location = New PointF(20, 280)
pen.LineJoin = PdfLineJoin.Round
g.DrawString("Pie shape", font, PdfBrushes.DarkBlue, New PointF(50, 250))
g.DrawPie(pen, brush, rect, 180 + 25, 60 + 25)
g.DrawPie(pen, brush, rect, 360 - 60 + 25, 60 + 25)
g.DrawPie(pen, brush, rect, 60 + 25, 60 + 25)
' #End Region
' #Region "Arc"
g.DrawString("Arcs", font, PdfBrushes.DarkBlue, New PointF(330, 250))
pen = New PdfPen(Color.Black)
pen.Width = 11
pen.LineCap = PdfLineCap.Round
pen.Color = lightBlue
rect = New RectangleF(310, 280, 200, 200)
g.DrawArc(pen, rect, 0, 90)
pen.Color = lightGreen
rect.X -= 10
g.DrawArc(pen, rect, 90, 90)
pen.Color = lightBlue
rect.Y -= 10
g.DrawArc(pen, rect, 180, 90)
pen.Color = lightGreen
rect.X += 10
g.DrawArc(pen, rect, 270, 90)
' #End Region
' #Region "ellipse"
' Draw a simple ellipse.
rect = New RectangleF(80, 520, 150, 200)
Dim lgBrush As New PdfLinearGradientBrush(rect, lightBlue, Color.White, 90)
pen.Color = lightGreen
pen.Width = 10.0F
g.DrawString("Ellipse with Gradient", font, PdfBrushes.DarkBlue, New PointF(50, 490))
g.DrawEllipse(lgBrush, rect)
' #End Region
' #Region "Transaparency"
page = document.Pages.Add()
' Obtain PdfGraphics object.
g = page.Graphics
g.DrawString("Transparent Rectangles", font, PdfBrushes.DarkBlue, New PointF(230, 470))
rect = New RectangleF(250, 500, 100, 100)
pen = New PdfPen(Color.Black)
Dim gBrush As PdfBrush = New PdfSolidBrush(lightGreen)
g.DrawRectangle(pen, gBrush, rect)
gBrush = New PdfSolidBrush(lightBlue)
rect.X += 20
rect.Y += 20
g.SetTransparency(0.1F)
g.DrawRectangle(pen, gBrush, rect)
rect.X += 20
rect.Y += 20
gBrush = New PdfLinearGradientBrush(rect, Color.DarkGreen, Color.Brown, PdfLinearGradientMode.BackwardDiagonal)
g.SetTransparency(0.5F)
g.DrawRectangle(pen, gBrush, rect)
rect.X += 20
rect.Y += 20
gBrush = New PdfSolidBrush(Color.Gray)
g.SetTransparency(0.25F)
g.DrawRectangle(pen, gBrush, rect)
rect.X += 20
rect.Y += 20
gBrush = New PdfSolidBrush(Color.Red)
g.SetTransparency(0.1F)
g.DrawRectangle(pen, gBrush, rect)
' #End Region
' Save and close the document.
Dim outputPath = Path.Combine(OutputDir, Me.GetType().Name & "_" & Guid.NewGuid().ToString() & ".pdf")
document.Save(outputPath)
document.Close(True)
Return outputPath
End Function
End Class
End Namespace